|
| 1 | +const CLOSING_REFERENCE_REGEX = |
| 2 | + /\b(?:fix(?:es|ed)?|close(?:s|d)?|resolve(?:s|d)?)\s*:?\s*((?:#\d+)(?:\s*(?:,|and)\s*#\d+)*)/gi; |
| 3 | + |
| 4 | +function resolveExecutionContext(context) { |
| 5 | + const isDryRun = /^true$/i.test(process.env.DRY_RUN || ""); |
| 6 | + const prNumber = Number(process.env.PR_NUMBER) || context.payload.pull_request?.number; |
| 7 | + return { prNumber, isDryRun, owner: context.repo.owner, repo: context.repo.repo }; |
| 8 | +} |
| 9 | + |
| 10 | +function validatePrNumber(prNumber) { |
| 11 | + if (!prNumber) { |
| 12 | + throw new Error("PR number could not be determined."); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function isBotAuthor(login = "") { |
| 17 | + return /\[bot\]$/i.test(login) || /dependabot/i.test(login); |
| 18 | +} |
| 19 | + |
| 20 | +function parseIssueNumbers(prBody) { |
| 21 | + const numbers = new Set(); |
| 22 | + let match; |
| 23 | + while ((match = CLOSING_REFERENCE_REGEX.exec(prBody)) !== null) { |
| 24 | + const text = match[1] || ""; |
| 25 | + for (const n of text.matchAll(/#(\d+)/g)) { |
| 26 | + numbers.add(Number(n[1])); |
| 27 | + } |
| 28 | + } |
| 29 | + return [...numbers]; |
| 30 | +} |
| 31 | + |
| 32 | +function extractLabels(labelData) { |
| 33 | + const result = []; |
| 34 | + for (const item of labelData) { |
| 35 | + const name = typeof item === "string" ? item : item?.name; |
| 36 | + if (name?.trim()) { |
| 37 | + result.push(name.trim()); |
| 38 | + } |
| 39 | + } |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +async function fetchPrData(github, context, prNumber) { |
| 44 | + if (context.payload.pull_request) { |
| 45 | + return context.payload.pull_request; |
| 46 | + } |
| 47 | + const { data } = await github.rest.pulls.get({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + pull_number: prNumber, |
| 51 | + }); |
| 52 | + return data; |
| 53 | +} |
| 54 | + |
| 55 | +function checkSkipConditions(prAuthor, linkedIssues) { |
| 56 | + if (isBotAuthor(prAuthor)) { |
| 57 | + return { skip: true, reason: `Skipping bot-authored PR from ${prAuthor}.` }; |
| 58 | + } |
| 59 | + if (!linkedIssues.length) { |
| 60 | + return { skip: true, reason: "No linked issue references found in PR body." }; |
| 61 | + } |
| 62 | + return { skip: false }; |
| 63 | +} |
| 64 | + |
| 65 | +async function fetchIssueLabels(github, owner, repo, issueNumber) { |
| 66 | + try { |
| 67 | + const { data } = await github.rest.issues.get({ owner, repo, issue_number: issueNumber }); |
| 68 | + if (data.pull_request) { |
| 69 | + console.log(`[sync] Skipping #${issueNumber}: is a PR reference.`); |
| 70 | + return []; |
| 71 | + } |
| 72 | + const labels = extractLabels(data.labels || []); |
| 73 | + console.log(`[sync] Issue #${issueNumber} labels: ${labels.length ? labels.join(", ") : "(none)"}`); |
| 74 | + return labels; |
| 75 | + } catch (err) { |
| 76 | + if (err?.status === 404) { |
| 77 | + console.log(`[sync] Issue #${issueNumber} not found. Skipping.`); |
| 78 | + return []; |
| 79 | + } |
| 80 | + throw err; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function computeDelta(existingLabels, issueLabels) { |
| 85 | + const existing = new Set(existingLabels); |
| 86 | + return issueLabels.filter((l) => !existing.has(l)); |
| 87 | +} |
| 88 | + |
| 89 | +function logResults(prNum, toAdd, existing) { |
| 90 | + console.log(`[sync] Processing PR #${prNum}.`); |
| 91 | + console.log(`[sync] Existing: ${existing.size ? [...existing].join(", ") : "(none)"}`); |
| 92 | + console.log(`[sync] To add: ${toAdd.length ? toAdd.join(", ") : "(none)"}`); |
| 93 | +} |
| 94 | + |
| 95 | +async function syncLabels({ github, context }) { |
| 96 | + const { prNumber, isDryRun, owner, repo } = resolveExecutionContext(context); |
| 97 | + validatePrNumber(prNumber); |
| 98 | + |
| 99 | + console.log(`[sync] Processing PR #${prNumber} in ${owner}/${repo} (dry_run=${isDryRun}).`); |
| 100 | + |
| 101 | + const prData = await fetchPrData(github, context, prNumber); |
| 102 | + const linkedIssues = parseIssueNumbers(prData?.body || ""); |
| 103 | + |
| 104 | + const skip = checkSkipConditions(prData?.user?.login || "", linkedIssues); |
| 105 | + if (skip.skip) { |
| 106 | + console.log(`[sync] ${skip.reason}`); |
| 107 | + return { labels: [] }; |
| 108 | + } |
| 109 | + |
| 110 | + console.log(`[sync] Linked issues: ${linkedIssues.map((n) => `#${n}`).join(", ")}`); |
| 111 | + |
| 112 | + const allLabels = []; |
| 113 | + for (const num of linkedIssues) { |
| 114 | + const labels = await fetchIssueLabels(github, owner, repo, num); |
| 115 | + allLabels.push(...labels); |
| 116 | + } |
| 117 | + |
| 118 | + if (!allLabels.length) { |
| 119 | + console.log("[sync] No labels on linked issues."); |
| 120 | + return { labels: [] }; |
| 121 | + } |
| 122 | + |
| 123 | + const existing = extractLabels(prData?.labels || []); |
| 124 | + const toAdd = computeDelta(existing, allLabels); |
| 125 | + logResults(prNumber, toAdd, new Set(existing)); |
| 126 | + |
| 127 | + if (!toAdd.length) { |
| 128 | + console.log("[sync] PR already has all labels."); |
| 129 | + return { labels: [] }; |
| 130 | + } |
| 131 | + |
| 132 | + if (isDryRun) { |
| 133 | + console.log(`[sync] DRY_RUN: would add ${toAdd.join(", ")}`); |
| 134 | + return { labels: toAdd }; |
| 135 | + } |
| 136 | + |
| 137 | + await github.rest.issues.addLabels({ owner, repo, issue_number: prNumber, labels: toAdd }); |
| 138 | + console.log(`[sync] Added labels: ${toAdd.join(", ")}`); |
| 139 | + |
| 140 | + return { labels: toAdd }; |
| 141 | +} |
| 142 | + |
| 143 | +module.exports = syncLabels; |
0 commit comments