|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// helpers/labels.js |
| 4 | +// |
| 5 | +// Label creation, determination, and synchronization. |
| 6 | +// |
| 7 | +// Key design decisions: |
| 8 | +// - ensureLabel() silently handles 422 (race condition / concurrent run) |
| 9 | +// - determineLabel() uses a 4-stage pipeline gated by maintainer approval |
| 10 | +// - syncLabel() adds the correct label FIRST, then removes stale ones |
| 11 | +// (crash-safe: PR never has zero queue labels) |
| 12 | + |
| 13 | +const { QUEUE_LABELS, ALL_QUEUE_LABEL_NAMES } = require('./constants'); |
| 14 | +const { countApprovals } = require('./permissions'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Ensure a single label exists in the repo. |
| 18 | + * Silently handles 422 (label already exists). |
| 19 | + * |
| 20 | + * Note: checks existence only. If a label already exists with the wrong |
| 21 | + * colour or description, it will not be corrected in Phase 1. |
| 22 | + */ |
| 23 | +async function ensureLabel(github, owner, repo, label, dryRun) { |
| 24 | + try { |
| 25 | + await github.rest.issues.getLabel({ owner, repo, name: label.name }); |
| 26 | + console.log(` Label "${label.name}" already exists. Skipping creation.`); |
| 27 | + } catch (error) { |
| 28 | + if (error.status === 404) { |
| 29 | + if (dryRun) { |
| 30 | + console.log(` [DRY RUN] Would create label "${label.name}" (${label.color}).`); |
| 31 | + return; |
| 32 | + } |
| 33 | + try { |
| 34 | + await github.rest.issues.createLabel({ |
| 35 | + owner, |
| 36 | + repo, |
| 37 | + name: label.name, |
| 38 | + color: label.color, |
| 39 | + description: label.description, |
| 40 | + }); |
| 41 | + console.log(` Created label "${label.name}" (#${label.color}).`); |
| 42 | + } catch (createError) { |
| 43 | + // 422 = label already exists (race condition or concurrent run) |
| 44 | + if (createError.status === 422) { |
| 45 | + console.log(` Label "${label.name}" already exists (422). Skipping.`); |
| 46 | + } else { |
| 47 | + throw createError; |
| 48 | + } |
| 49 | + } |
| 50 | + } else { |
| 51 | + throw error; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Determine the correct queue label for a PR based on approval counts. |
| 58 | + * |
| 59 | + * Phase 1 logic (4-stage pipeline): |
| 60 | + * maintainerApproval >= 1 → ready-to-merge (CODEOWNERS satisfied) |
| 61 | + * writeApproval >= 1 → queue:maintainers (committer approved, needs maintainer) |
| 62 | + * anyApproval >= 1 → queue:committers (has any approval, needs committer) |
| 63 | + * else → queue:junior-committer (no approvals yet) |
| 64 | + * |
| 65 | + * @param {{ maintainerApproval: number, writeApproval: number, softApproval: number, anyApproval: number }} approvals |
| 66 | + * @returns {object} The correct QUEUE_LABELS entry |
| 67 | + */ |
| 68 | +function determineLabel(approvals) { |
| 69 | + if (approvals.maintainerApproval >= 1) { |
| 70 | + return QUEUE_LABELS.MERGE; |
| 71 | + } |
| 72 | + if (approvals.writeApproval >= 1) { |
| 73 | + return QUEUE_LABELS.MAINTAINERS; |
| 74 | + } |
| 75 | + if (approvals.anyApproval >= 1) { |
| 76 | + return QUEUE_LABELS.COMMITTERS; |
| 77 | + } |
| 78 | + return QUEUE_LABELS.JUNIOR; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Sync the queue label on a single PR. |
| 83 | + * |
| 84 | + * Order of operations (non-negotiable): |
| 85 | + * 1. Compute stale queue labels on the PR |
| 86 | + * 2. Skip only if correct label is already present AND no stale labels exist |
| 87 | + * 3. ADD the correct label first |
| 88 | + * 4. THEN remove any stale queue labels |
| 89 | + * |
| 90 | + * This ensures a PR never has zero queue labels, even if the process |
| 91 | + * crashes mid-run. Stale labels are always cleaned up, even when the |
| 92 | + * correct label was already present. |
| 93 | + * |
| 94 | + * @param {object} github - Octokit instance |
| 95 | + * @param {string} owner - Repository owner |
| 96 | + * @param {string} repo - Repository name |
| 97 | + * @param {object} pr - Pull request object from the list API |
| 98 | + * @param {boolean} dryRun - If true, log without making changes |
| 99 | + * @returns {boolean} true if the label was changed, false if already correct |
| 100 | + */ |
| 101 | +async function syncLabel(github, owner, repo, pr, dryRun) { |
| 102 | + const prNumber = pr.number; |
| 103 | + const currentLabels = (pr.labels || []).map((l) => l.name); |
| 104 | + |
| 105 | + // Count approvals and determine the correct label |
| 106 | + const approvals = await countApprovals(github, owner, repo, prNumber); |
| 107 | + const correctLabel = determineLabel(approvals); |
| 108 | + |
| 109 | + console.log( |
| 110 | + ` PR #${prNumber}: maintainerApproval=${approvals.maintainerApproval}, ` + |
| 111 | + `writeApproval=${approvals.writeApproval}, ` + |
| 112 | + `softApproval=${approvals.softApproval}, anyApproval=${approvals.anyApproval} ` + |
| 113 | + `→ ${correctLabel.name}` |
| 114 | + ); |
| 115 | + |
| 116 | + // Determine which stale queue labels to remove |
| 117 | + const staleLabels = currentLabels.filter( |
| 118 | + (name) => ALL_QUEUE_LABEL_NAMES.includes(name) && name !== correctLabel.name |
| 119 | + ); |
| 120 | + |
| 121 | + // Check if the correct label is already present AND there are no stale labels to remove |
| 122 | + if (currentLabels.includes(correctLabel.name) && staleLabels.length === 0) { |
| 123 | + console.log(` ✓ Already has "${correctLabel.name}". No change needed.`); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + if (dryRun) { |
| 128 | + console.log(` [DRY RUN] Would add "${correctLabel.name}".`); |
| 129 | + if (staleLabels.length > 0) { |
| 130 | + console.log(` [DRY RUN] Would remove: ${staleLabels.join(', ')}.`); |
| 131 | + } |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + // Step 1: ADD the correct label FIRST (crash-safe: PR always has at least one label) |
| 136 | + await github.rest.issues.addLabels({ |
| 137 | + owner, |
| 138 | + repo, |
| 139 | + issue_number: prNumber, |
| 140 | + labels: [correctLabel.name], |
| 141 | + }); |
| 142 | + console.log(` + Added "${correctLabel.name}".`); |
| 143 | + |
| 144 | + // Step 2: THEN remove stale queue labels one by one |
| 145 | + for (const stale of staleLabels) { |
| 146 | + try { |
| 147 | + await github.rest.issues.removeLabel({ |
| 148 | + owner, |
| 149 | + repo, |
| 150 | + issue_number: prNumber, |
| 151 | + name: stale, |
| 152 | + }); |
| 153 | + console.log(` - Removed "${stale}".`); |
| 154 | + } catch (error) { |
| 155 | + // 404 = label was already removed (race condition or manual action) |
| 156 | + if (error.status === 404) { |
| 157 | + console.log(` - Label "${stale}" already gone (404). Skipping.`); |
| 158 | + } else { |
| 159 | + const message = error instanceof Error ? error.message : String(error); |
| 160 | + console.error(` ✗ Failed to remove "${stale}": ${message}`); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return true; |
| 166 | +} |
| 167 | + |
| 168 | +module.exports = { ensureLabel, determineLabel, syncLabel }; |
0 commit comments