Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit aaca811

Browse files
committed
feat: add scripts for syncing issue labels
We're going to use a github action to trigger this process _normally_, but I want these scripts to live somewhere.
1 parent 6ec60e6 commit aaca811

6 files changed

Lines changed: 1745 additions & 1 deletion

File tree

scripts/sync-labels/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

scripts/sync-labels/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Issue Label Syncer
2+
3+
Manually sync issue labels.
4+
5+
## Install
6+
7+
Before usage, you'll need to install the dependencies.
8+
9+
```go
10+
$ npm install
11+
```
12+
13+
## Usage
14+
15+
To sync issue labels to all repos listed in `.config/workflows/config.json`, run:
16+
17+
```bash
18+
$ node index.js
19+
```
20+
21+
You can also sync labels to a specific repo:
22+
23+
```bash
24+
$ node index.js my-org/my-repo
25+
```

scripts/sync-labels/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)