export async function mapLabelsToAttributes()

in github-projects/map-labels/mapLabelsToAttributes.ts [36:111]


export async function mapLabelsToAttributes(args: MapLabelsToAttributesArgs) {
  const { issueNumber: issueNumbers, projectNumber, owner, repo, mapping, all, dryRun, githubToken } = args;

  const hasFilter = issueNumbers.length > 0;
  // If we're requesting all issues, we should list ~1000 issues to max it out
  // if we have a filter, we will also want to search for those issues, so max it out
  // if we're running with either of these args, we should be fine with the 50 most recent
  const issueCount = hasFilter || all ? 1000 : 50;

  const octokit = new Octokit({
    auth: githubToken.trim(),
  });

  if (dryRun) {
    console.log('⚠️ Running in dry-run mode. No changes will be made.');
  }

  console.log(`Loading label mapping file ${mapping}`);
  const labelsToFields = loadMapping(mapping);

  console.log(`Requesting project ${owner}/${projectNumber} and its issues...`);
  const projectAndFields = await gqlGetProject(octokit, { projectNumber, owner });

  const issuesInProject = await gqlGetIssuesForProject(
    octokit,
    { projectNumber, findIssueNumbers: issueNumbers, owner },
    {
      issueCount,
    },
  );

  const targetIssues = repo ? filterIssuesByRepo(issuesInProject, repo) : issuesInProject;
  if (!targetIssues.length) {
    console.error(`Could not find any update target(s) issues for params:`, {
      projectNumber,
      issueNumbers,
      repo,
      owner,
    });
    throw new Error('No target issues found');
  } else {
    console.log(`Found ${targetIssues.length} target issue(s) for update`);
  }

  const updateResults = {
    success: [] as IssueNode[],
    failure: [] as IssueNode[],
    skipped: [] as IssueNode[],
    projectUrl: projectAndFields.url,
  };
  for (const issueNode of targetIssues) {
    console.log(`Updating issue target: ${issueNode.content.url}...`);
    try {
      const updatedFields = await adjustSingleItemLabels(octokit, {
        issueNode,
        owner,
        projectNumber,
        projectId: projectAndFields.id,
        mapping: labelsToFields,
        dryRun,
      });
      if (updatedFields.length) {
        console.log(`Updated fields: ${updatedFields.join(', ')}`);
        updateResults.success.push(issueNode);
      } else {
        console.log('No fields updated');
        updateResults.skipped.push(issueNode);
      }
    } catch (error) {
      console.error('Error updating issue', error);
      updateResults.failure.push(issueNode);
    }
  }

  return updateResults;
}