|
| 1 | +import githubLabelSync from 'github-label-sync' |
| 2 | +import { readFile } from 'fs/promises' |
| 3 | +import path from 'path' |
| 4 | +import url from 'url' |
| 5 | + |
| 6 | +const GH_TOKEN = process.env.GITHUB_TOKEN |
| 7 | +if (!GH_TOKEN) { |
| 8 | + console.log('Error: Please set the GITHUB_TOKEN environment variable') |
| 9 | + process.exit(1) |
| 10 | +} |
| 11 | + |
| 12 | +const REPO_ROOT = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '../../'); |
| 13 | + |
| 14 | +(async () => { |
| 15 | + let repos = process.argv.splice(2) |
| 16 | + // Apply to all repos if no repos are specified. |
| 17 | + if (repos.length == 0) { |
| 18 | + repos = JSON.parse(await readFile(path.join(REPO_ROOT, ".github/workflows/config.json"))) |
| 19 | + .map(item => item.target) |
| 20 | + } |
| 21 | + |
| 22 | + let labels = JSON.parse(await readFile(path.join(REPO_ROOT, "templates/.github/issue-labels.json"))) |
| 23 | + |
| 24 | + for (const repo of repos) { |
| 25 | + try { |
| 26 | + let diff = await githubLabelSync({ |
| 27 | + accessToken: GH_TOKEN, |
| 28 | + repo: repo, |
| 29 | + allowAddedLabels: true, |
| 30 | + labels: labels |
| 31 | + }) |
| 32 | + console.log(`updating ${repo}`) |
| 33 | + for (var change of diff) { |
| 34 | + switch (change.type) { |
| 35 | + case 'missing': |
| 36 | + console.log(` added ${change.expected.name}`) |
| 37 | + break; |
| 38 | + case 'changed': |
| 39 | + if (change.actual.name !== change.expected.name) { |
| 40 | + console.log(` renamed ${change.actual.name} to ${change.expected.name}`) |
| 41 | + } else if (change.actual.description !== change.expected.description) { |
| 42 | + console.log(` changed ${change.actual.name} description to: ${change.expected.description}`) |
| 43 | + } else if (change.actual.color !== change.expected.color) { |
| 44 | + console.log(` recolored ${change.actual.name}`) |
| 45 | + } else { |
| 46 | + console.log(` updated ${change.actual.name}`) |
| 47 | + } |
| 48 | + break; |
| 49 | + case 'added': |
| 50 | + console.log(` removed ${change.actual.name}`) |
| 51 | + break; |
| 52 | + default: |
| 53 | + console.log(` unknown change ${diff}`) |
| 54 | + } |
| 55 | + } |
| 56 | + } catch (e) { |
| 57 | + console.log(`failed to update labels for ${repo}: ${e}`) |
| 58 | + console.dir(e, {depth: 4, colors: true}) |
| 59 | + } |
| 60 | + } |
| 61 | +})() |
0 commit comments