|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const workspaceRoot = process.cwd(); |
| 5 | +const labelsPath = path.join(workspaceRoot, "config", "labels.json"); |
| 6 | +const deleteLabelsPath = path.join(workspaceRoot, "config", "delete-labels.json"); |
| 7 | + |
| 8 | +function assert(condition, message) { |
| 9 | + if (!condition) { |
| 10 | + throw new Error(message); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function normalizeColor(color) { |
| 15 | + return color.replace(/^#/, "").toLowerCase(); |
| 16 | +} |
| 17 | + |
| 18 | +function normalizeDescription(description) { |
| 19 | + return description ?? ""; |
| 20 | +} |
| 21 | + |
| 22 | +function normalizeName(name) { |
| 23 | + return name.trim().toLowerCase(); |
| 24 | +} |
| 25 | + |
| 26 | +async function readJson(filePath) { |
| 27 | + const contents = await fs.readFile(filePath, "utf8"); |
| 28 | + return JSON.parse(contents); |
| 29 | +} |
| 30 | + |
| 31 | +async function writeJson(filePath, value) { |
| 32 | + await fs.writeFile(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); |
| 33 | +} |
| 34 | + |
| 35 | +function validateDeleteLabels(deleteLabels) { |
| 36 | + assert(Array.isArray(deleteLabels), "config/delete-labels.json must contain an array."); |
| 37 | + |
| 38 | + const seen = new Set(); |
| 39 | + |
| 40 | + return new Set( |
| 41 | + deleteLabels.map((entry, index) => { |
| 42 | + assert(typeof entry === "string" && entry.trim(), `Delete label at index ${index} must be a non-empty string.`); |
| 43 | + |
| 44 | + const name = normalizeName(entry); |
| 45 | + assert(!seen.has(name), `Duplicate delete label detected: "${entry}".`); |
| 46 | + seen.add(name); |
| 47 | + return name; |
| 48 | + }), |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +async function githubRequest(token, method, apiPath) { |
| 53 | + const response = await fetch(`https://api.github.com${apiPath}`, { |
| 54 | + method, |
| 55 | + headers: { |
| 56 | + Accept: "application/vnd.github+json", |
| 57 | + Authorization: `Bearer ${token}`, |
| 58 | + "User-Agent": "label-sync-config", |
| 59 | + "X-GitHub-Api-Version": "2022-11-28", |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + const message = await response.text(); |
| 65 | + throw new Error(`${method} ${apiPath} failed with ${response.status}: ${message}`); |
| 66 | + } |
| 67 | + |
| 68 | + return response.json(); |
| 69 | +} |
| 70 | + |
| 71 | +async function getAllLabels(token, repo) { |
| 72 | + const labels = []; |
| 73 | + let page = 1; |
| 74 | + |
| 75 | + while (true) { |
| 76 | + const batch = await githubRequest(token, "GET", `/repos/${repo}/labels?per_page=100&page=${page}`); |
| 77 | + labels.push(...batch); |
| 78 | + |
| 79 | + if (batch.length < 100) { |
| 80 | + return labels; |
| 81 | + } |
| 82 | + |
| 83 | + page += 1; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function toManagedLabels(labels, deleteLabels) { |
| 88 | + return labels |
| 89 | + .filter((label) => !deleteLabels.has(normalizeName(label.name))) |
| 90 | + .map((label) => ({ |
| 91 | + name: label.name.trim(), |
| 92 | + color: normalizeColor(label.color), |
| 93 | + description: normalizeDescription(label.description), |
| 94 | + })) |
| 95 | + .sort((left, right) => left.name.localeCompare(right.name)); |
| 96 | +} |
| 97 | + |
| 98 | +async function main() { |
| 99 | + const token = process.env.CONFIG_LABEL_SYNC_TOKEN ?? process.env.GITHUB_TOKEN; |
| 100 | + assert(token, "CONFIG_LABEL_SYNC_TOKEN or GITHUB_TOKEN is required."); |
| 101 | + |
| 102 | + const repository = process.env.SOURCE_REPOSITORY ?? process.env.GITHUB_REPOSITORY; |
| 103 | + assert(repository, "SOURCE_REPOSITORY or GITHUB_REPOSITORY is required."); |
| 104 | + |
| 105 | + const deleteLabels = validateDeleteLabels(await readJson(deleteLabelsPath)); |
| 106 | + const repositoryLabels = await getAllLabels(token, repository); |
| 107 | + const managedLabels = toManagedLabels(repositoryLabels, deleteLabels); |
| 108 | + |
| 109 | + await writeJson(labelsPath, managedLabels); |
| 110 | + |
| 111 | + console.log( |
| 112 | + `Synced ${managedLabels.length} managed labels from ${repository} into config/labels.json after excluding ${deleteLabels.size} auto-delete labels.`, |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +main().catch((error) => { |
| 117 | + console.error(error.message); |
| 118 | + process.exitCode = 1; |
| 119 | +}); |
0 commit comments