|
| 1 | +import path from "node:path"; |
| 2 | +import { |
| 3 | + assert, |
| 4 | + labelsExactlyMatch, |
| 5 | + normalizeColor, |
| 6 | + normalizeDescription, |
| 7 | + normalizeName, |
| 8 | + readJsonc, |
| 9 | +} from "./lib/config-utils.mjs"; |
| 10 | +import { |
| 11 | + validateDeleteLabels, |
| 12 | + validateLabels, |
| 13 | + validateProperties, |
| 14 | +} from "./lib/config-validation.mjs"; |
| 15 | + |
| 16 | +const workspaceRoot = process.cwd(); |
| 17 | +const propertiesPath = path.join(workspaceRoot, "config", "properties.jsonc"); |
| 18 | +const labelsPath = path.join(workspaceRoot, "config", "labels.jsonc"); |
| 19 | +const autoPrunedLabelsPath = path.join(workspaceRoot, "config", "auto-pruned-labels.jsonc"); |
| 20 | + |
| 21 | +const validateOnly = process.argv.includes("--validate-only"); |
| 22 | +const dryRun = validateOnly || process.env.DRY_RUN === "true"; |
| 23 | + |
| 24 | +async function githubRequest(token, method, apiPath, body) { |
| 25 | + const response = await fetch(`https://api.github.com${apiPath}`, { |
| 26 | + method, |
| 27 | + headers: { |
| 28 | + Accept: "application/vnd.github+json", |
| 29 | + Authorization: `Bearer ${token}`, |
| 30 | + "User-Agent": "reverse-config-label-sync", |
| 31 | + "X-GitHub-Api-Version": "2022-11-28", |
| 32 | + }, |
| 33 | + body: body ? JSON.stringify(body) : undefined, |
| 34 | + }); |
| 35 | + |
| 36 | + if (!response.ok) { |
| 37 | + const message = await response.text(); |
| 38 | + throw new Error(`${method} ${apiPath} failed with ${response.status}: ${message}`); |
| 39 | + } |
| 40 | + |
| 41 | + if (response.status === 204) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + return response.json(); |
| 46 | +} |
| 47 | + |
| 48 | +async function getAllLabels(token, repo) { |
| 49 | + const labels = []; |
| 50 | + let page = 1; |
| 51 | + |
| 52 | + while (true) { |
| 53 | + const batch = await githubRequest(token, "GET", `/repos/${repo}/labels?per_page=100&page=${page}`); |
| 54 | + labels.push(...batch); |
| 55 | + |
| 56 | + if (batch.length < 100) { |
| 57 | + return labels; |
| 58 | + } |
| 59 | + |
| 60 | + page += 1; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function summarizeLabelDiff(existing, desired) { |
| 65 | + if (!existing) { |
| 66 | + return "create"; |
| 67 | + } |
| 68 | + |
| 69 | + const sameColor = normalizeColor(existing.color) === desired.color; |
| 70 | + const sameDescription = normalizeDescription(existing.description) === desired.description; |
| 71 | + const sameName = existing.name === desired.name; |
| 72 | + |
| 73 | + if (sameColor && sameDescription && sameName) { |
| 74 | + return "unchanged"; |
| 75 | + } |
| 76 | + |
| 77 | + return "update"; |
| 78 | +} |
| 79 | + |
| 80 | +function isExactAutoPrunedLabel(label, deleteLabels) { |
| 81 | + return deleteLabels.some((deleteLabel) => labelsExactlyMatch(label, deleteLabel)); |
| 82 | +} |
| 83 | + |
| 84 | +async function syncSourceRepository(token, repository, desiredLabels, deleteLabels) { |
| 85 | + console.log(`Syncing ${repository} labels from config`); |
| 86 | + const existingLabels = await getAllLabels(token, repository); |
| 87 | + const existingByName = new Map(existingLabels.map((label) => [normalizeName(label.name), label])); |
| 88 | + const desiredKeys = new Set(desiredLabels.map((label) => normalizeName(label.name))); |
| 89 | + |
| 90 | + let created = 0; |
| 91 | + let updated = 0; |
| 92 | + let deletedConfigured = 0; |
| 93 | + let deletedMissing = 0; |
| 94 | + let unchanged = 0; |
| 95 | + |
| 96 | + for (const desired of desiredLabels) { |
| 97 | + const existing = existingByName.get(normalizeName(desired.name)); |
| 98 | + const action = summarizeLabelDiff(existing, desired); |
| 99 | + |
| 100 | + if (action === "unchanged") { |
| 101 | + unchanged += 1; |
| 102 | + console.log(` = ${desired.name}`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (action === "create") { |
| 107 | + created += 1; |
| 108 | + console.log(` + ${desired.name}`); |
| 109 | + |
| 110 | + if (!dryRun) { |
| 111 | + await githubRequest(token, "POST", `/repos/${repository}/labels`, desired); |
| 112 | + } |
| 113 | + |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + updated += 1; |
| 118 | + console.log(` ~ ${desired.name}`); |
| 119 | + |
| 120 | + if (!dryRun) { |
| 121 | + await githubRequest( |
| 122 | + token, |
| 123 | + "PATCH", |
| 124 | + `/repos/${repository}/labels/${encodeURIComponent(existing.name)}`, |
| 125 | + desired, |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + for (const existing of existingLabels) { |
| 131 | + const existingKey = normalizeName(existing.name); |
| 132 | + |
| 133 | + if (desiredKeys.has(existingKey) || !isExactAutoPrunedLabel(existing, deleteLabels)) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + deletedConfigured += 1; |
| 138 | + console.log(` - ${existing.name} (configured delete)`); |
| 139 | + |
| 140 | + if (!dryRun) { |
| 141 | + await githubRequest( |
| 142 | + token, |
| 143 | + "DELETE", |
| 144 | + `/repos/${repository}/labels/${encodeURIComponent(existing.name)}`, |
| 145 | + ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + for (const existing of existingLabels) { |
| 150 | + const existingKey = normalizeName(existing.name); |
| 151 | + |
| 152 | + if (desiredKeys.has(existingKey) || isExactAutoPrunedLabel(existing, deleteLabels)) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + deletedMissing += 1; |
| 157 | + console.log(` - ${existing.name} (not in config)`); |
| 158 | + |
| 159 | + if (!dryRun) { |
| 160 | + await githubRequest( |
| 161 | + token, |
| 162 | + "DELETE", |
| 163 | + `/repos/${repository}/labels/${encodeURIComponent(existing.name)}`, |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + console.log( |
| 169 | + `Summary for ${repository}: created=${created}, updated=${updated}, deletedConfigured=${deletedConfigured}, deletedMissing=${deletedMissing}, unchanged=${unchanged}`, |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +async function main() { |
| 174 | + const properties = validateProperties(await readJsonc(propertiesPath), { |
| 175 | + includeSourceRepository: true, |
| 176 | + defaultSourceRepository: process.env.GITHUB_REPOSITORY ?? "", |
| 177 | + }); |
| 178 | + const labels = validateLabels(await readJsonc(labelsPath)); |
| 179 | + const deleteLabels = validateDeleteLabels(await readJsonc(autoPrunedLabelsPath)); |
| 180 | + const repository = process.env.SOURCE_REPOSITORY ?? properties.sourceRepository; |
| 181 | + assert(repository, "SOURCE_REPOSITORY or properties.sourceRepository is required."); |
| 182 | + |
| 183 | + console.log( |
| 184 | + `Loaded ${labels.length} managed labels and ${deleteLabels.length} exact auto-pruned label specs for ${repository}.`, |
| 185 | + ); |
| 186 | + |
| 187 | + if (validateOnly) { |
| 188 | + console.log("Configuration is valid."); |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + const token = process.env.LABEL_SYNC_TOKEN; |
| 193 | + assert(token, "LABEL_SYNC_TOKEN is required unless --validate-only is used."); |
| 194 | + |
| 195 | + await syncSourceRepository(token, repository, labels, deleteLabels); |
| 196 | + |
| 197 | + if (dryRun) { |
| 198 | + console.log("Dry-run mode did not apply label changes."); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +main().catch((error) => { |
| 203 | + console.error(error.message); |
| 204 | + process.exitCode = 1; |
| 205 | +}); |
0 commit comments