|
| 1 | +// Checks if the given login belongs to a bot account (ends with [bot] or contains "dependabot"). |
| 2 | +function isBotLogin(login = "") { |
| 3 | + return /\[bot\]$/i.test(login) || /dependabot/i.test(login); |
| 4 | +} |
| 5 | + |
| 6 | +// Extracts issue numbers from PR body closing keywords (Fixes #123, Closes #456, Resolves #789, etc.). |
| 7 | +function extractLinkedIssueNumbers(prBody = "") { |
| 8 | + const closingReferenceRegex = |
| 9 | + /\b(?:fix(?:es|ed)?|close(?:s|d)?|resolve(?:s|d)?)\s*:?\s*((?:#\d+)(?:\s*(?:,|and)\s*#\d+)*)/gi; |
| 10 | + const numbers = new Set(); |
| 11 | + let referenceMatch; |
| 12 | + |
| 13 | + while ((referenceMatch = closingReferenceRegex.exec(prBody)) !== null) { |
| 14 | + const referencesText = referenceMatch[1] || ""; |
| 15 | + const issueMatches = referencesText.matchAll(/#(\d+)/g); |
| 16 | + |
| 17 | + for (const issueMatch of issueMatches) { |
| 18 | + numbers.add(Number(issueMatch[1])); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return [...numbers]; |
| 23 | +} |
| 24 | + |
| 25 | +// Normalizes label objects/strings to an array of trimmed label names. |
| 26 | +function normalizeLabelNames(labels = []) { |
| 27 | + const names = []; |
| 28 | + for (const label of labels) { |
| 29 | + if (typeof label === "string" && label.trim()) { |
| 30 | + names.push(label.trim()); |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (label && typeof label.name === "string" && label.name.trim()) { |
| 35 | + names.push(label.name.trim()); |
| 36 | + } |
| 37 | + } |
| 38 | + return names; |
| 39 | +} |
| 40 | + |
| 41 | +// Fetches PR data from the payload or GitHub API if not present in payload. |
| 42 | +async function getPullRequestData({ github, context, prNumber }) { |
| 43 | + if (context.payload.pull_request) { |
| 44 | + return context.payload.pull_request; |
| 45 | + } |
| 46 | + |
| 47 | + const response = await github.rest.pulls.get({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + pull_number: prNumber, |
| 51 | + }); |
| 52 | + |
| 53 | + return response.data; |
| 54 | +} |
| 55 | + |
| 56 | +// Resolves PR number from environment or context, and determines if dry-run mode is enabled. |
| 57 | +function resolveExecutionContext(context) { |
| 58 | + const isDryRun = /^true$/i.test(process.env.DRY_RUN || ""); |
| 59 | + const prNumber = Number(process.env.PR_NUMBER) || context.payload.pull_request?.number; |
| 60 | + return { prNumber, isDryRun, owner: context.repo.owner, repo: context.repo.repo }; |
| 61 | +} |
| 62 | + |
| 63 | +// Determines if the PR should be skipped (e.g., bot-authored PRs, no linked issues). |
| 64 | +function shouldSkipPR(prData, linkedIssueNumbers) { |
| 65 | + const prAuthor = prData?.user?.login || ""; |
| 66 | + |
| 67 | + if (isBotLogin(prAuthor)) { |
| 68 | + return { skip: true, reason: `Skipping bot-authored PR from ${prAuthor}.` }; |
| 69 | + } |
| 70 | + |
| 71 | + if (!linkedIssueNumbers.length) { |
| 72 | + return { skip: true, reason: "No linked issue references found in PR body." }; |
| 73 | + } |
| 74 | + |
| 75 | + return { skip: false }; |
| 76 | +} |
| 77 | + |
| 78 | +// Collects labels from all linked issues, handling 404s and PR references. |
| 79 | +async function collectLabelsFromLinkedIssues({ github, owner, repo, linkedIssueNumbers }) { |
| 80 | + const labelsFromIssues = new Set(); |
| 81 | + |
| 82 | + for (const issueNumber of linkedIssueNumbers) { |
| 83 | + try { |
| 84 | + const issueResponse = await github.rest.issues.get({ |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + issue_number: issueNumber, |
| 88 | + }); |
| 89 | + |
| 90 | + if (issueResponse?.data?.pull_request) { |
| 91 | + console.log(`[sync-issue-labels] Skipping #${issueNumber}: reference points to a pull request.`); |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + const issueLabelNames = normalizeLabelNames(issueResponse?.data?.labels || []); |
| 96 | + console.log( |
| 97 | + `[sync-issue-labels] Issue #${issueNumber} labels: ${ |
| 98 | + issueLabelNames.length ? issueLabelNames.join(", ") : "(none)" |
| 99 | + }` |
| 100 | + ); |
| 101 | + |
| 102 | + for (const label of issueLabelNames) { |
| 103 | + labelsFromIssues.add(label); |
| 104 | + } |
| 105 | + } catch (error) { |
| 106 | + if (error?.status === 404) { |
| 107 | + console.log(`[sync-issue-labels] Linked issue #${issueNumber} not found. Skipping.`); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + throw error; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return labelsFromIssues; |
| 116 | +} |
| 117 | + |
| 118 | +// Computes which labels should be added to the PR (missing from PR but present in issues). |
| 119 | +function computeLabelsToAdd(prData, labelsFromIssues) { |
| 120 | + const prLabelNames = new Set(normalizeLabelNames(prData?.labels || [])); |
| 121 | + return [...labelsFromIssues].filter((label) => !prLabelNames.has(label)); |
| 122 | +} |
| 123 | + |
| 124 | +// Adds labels to the pull request via GitHub API. |
| 125 | +async function addLabelsToPullRequest({ github, owner, repo, prNumber, labelsToAdd }) { |
| 126 | + await github.rest.issues.addLabels({ |
| 127 | + owner, |
| 128 | + repo, |
| 129 | + issue_number: prNumber, |
| 130 | + labels: labelsToAdd, |
| 131 | + }); |
| 132 | + |
| 133 | + console.log(`[sync-issue-labels] Added labels to PR #${prNumber}: ${labelsToAdd.join(", ")}`); |
| 134 | +} |
| 135 | + |
| 136 | +// Main entry point: syncs labels from linked issues to the PR. |
| 137 | +module.exports = async ({ github, context }) => { |
| 138 | + const { prNumber, isDryRun, owner, repo } = resolveExecutionContext(context); |
| 139 | + |
| 140 | + if (!prNumber) { |
| 141 | + throw new Error("PR number could not be determined."); |
| 142 | + } |
| 143 | + |
| 144 | + console.log( |
| 145 | + `[sync-issue-labels] Processing PR #${prNumber} in ${owner}/${repo} (dry_run=${isDryRun}).` |
| 146 | + ); |
| 147 | + |
| 148 | + let prData; |
| 149 | + try { |
| 150 | + prData = await getPullRequestData({ github, context, prNumber }); |
| 151 | + } catch (error) { |
| 152 | + throw new Error(`[sync-issue-labels] Failed to fetch PR #${prNumber}: ${error?.message || error}`); |
| 153 | + } |
| 154 | + |
| 155 | + const linkedIssueNumbers = extractLinkedIssueNumbers(prData?.body || ""); |
| 156 | + const skipResult = shouldSkipPR(prData, linkedIssueNumbers); |
| 157 | + |
| 158 | + if (skipResult.skip) { |
| 159 | + console.log(`[sync-issue-labels] ${skipResult.reason}`); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + console.log( |
| 164 | + `[sync-issue-labels] Linked issues detected: ${linkedIssueNumbers.map((n) => `#${n}`).join(", ")}` |
| 165 | + ); |
| 166 | + |
| 167 | + const labelsFromIssues = await collectLabelsFromLinkedIssues({ |
| 168 | + github, |
| 169 | + owner, |
| 170 | + repo, |
| 171 | + linkedIssueNumbers, |
| 172 | + }); |
| 173 | + |
| 174 | + if (!labelsFromIssues.size) { |
| 175 | + console.log("[sync-issue-labels] No labels found on linked issues. Nothing to sync."); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + const labelsToAdd = computeLabelsToAdd(prData, labelsFromIssues); |
| 180 | + const prLabelNames = new Set(normalizeLabelNames(prData?.labels || [])); |
| 181 | + |
| 182 | + console.log( |
| 183 | + `[sync-issue-labels] Existing PR labels: ${ |
| 184 | + prLabelNames.size ? [...prLabelNames].join(", ") : "(none)" |
| 185 | + }` |
| 186 | + ); |
| 187 | + console.log( |
| 188 | + `[sync-issue-labels] Labels to add: ${labelsToAdd.length ? labelsToAdd.join(", ") : "(none)"}` |
| 189 | + ); |
| 190 | + |
| 191 | + if (!labelsToAdd.length) { |
| 192 | + console.log("[sync-issue-labels] PR already contains all labels from linked issues."); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + if (isDryRun) { |
| 197 | + console.log(`[sync-issue-labels] DRY_RUN enabled; would add labels: ${labelsToAdd.join(", ")}`); |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + try { |
| 202 | + await addLabelsToPullRequest({ github, owner, repo, prNumber, labelsToAdd }); |
| 203 | + } catch (error) { |
| 204 | + throw new Error( |
| 205 | + `[sync-issue-labels] Failed to add labels to PR #${prNumber}: ${error?.message || error}` |
| 206 | + ); |
| 207 | + } |
| 208 | +}; |
0 commit comments