ci/github-script: add commander CLI interface

This makes it easier to add additional features.

Changed files
+58 -37
ci
+1 -1
ci/github-script/README.md
···
## Labeler
-
Run `./run.js OWNER REPO`, where OWNER is your username or "NixOS" and REPO the name of your fork or "nixpkgs".
···
## Labeler
+
Run `./run.js labels OWNER REPO`, where OWNER is your username or "NixOS" and REPO the name of your fork or "nixpkgs".
+12 -2
ci/github-script/package-lock.json
···
{
-
"name": "labels",
"lockfileVersion": 3,
"requires": true,
"packages": {
···
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
-
"bottleneck": "2.19.5"
}
},
"node_modules/@actions/artifact": {
···
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/compress-commons": {
"version": "6.0.2",
···
{
+
"name": "github-script",
"lockfileVersion": 3,
"requires": true,
"packages": {
···
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
+
"bottleneck": "2.19.5",
+
"commander": "14.0.0"
}
},
"node_modules/@actions/artifact": {
···
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
+
},
+
"node_modules/commander": {
+
"version": "14.0.0",
+
"resolved": "https://registry.npmjs.org/commander/-/commander-14.0.0.tgz",
+
"integrity": "sha512-2uM9rYjPvyq39NwLRqaiLtWHyDC1FvryJDa2ATTVims5YAS4PupsEQsDvP14FqhFr0P49CYDugi59xaxJlTXRA==",
+
"license": "MIT",
+
"engines": {
+
"node": ">=20"
+
}
},
"node_modules/compress-commons": {
"version": "6.0.2",
+2 -1
ci/github-script/package.json
···
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
-
"bottleneck": "2.19.5"
}
}
···
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
+
"bottleneck": "2.19.5",
+
"commander": "14.0.0"
}
}
+43 -33
ci/github-script/run.js
···
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { getOctokit } from '@actions/github'
-
import labels from './labels.cjs'
-
if (process.argv.length !== 4)
-
throw new Error('Call this with exactly three arguments: ./run OWNER REPO')
-
const [, , owner, repo] = process.argv
-
const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
-
const tmp = mkdtempSync(join(tmpdir(), 'labels-'))
-
try {
-
process.env.GITHUB_WORKSPACE = tmp
-
process.chdir(tmp)
-
-
await labels({
-
github: getOctokit(token),
-
context: {
-
payload: {},
-
repo: {
-
owner,
-
repo,
},
-
},
-
core: {
-
getInput() {
-
return token
-
},
-
error: console.error,
-
info: console.log,
-
notice: console.log,
-
setFailed(msg) {
-
console.error(msg)
-
process.exitCode = 1
},
-
},
-
dry: true,
-
})
-
} finally {
-
rmSync(tmp, { recursive: true })
}
···
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
+
import { program } from 'commander'
import { getOctokit } from '@actions/github'
+
async function run(action, owner, repo) {
+
const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
+
const tmp = mkdtempSync(join(tmpdir(), 'github-script-'))
+
try {
+
process.env.GITHUB_WORKSPACE = tmp
+
process.chdir(tmp)
+
await action({
+
github: getOctokit(token),
+
context: {
+
payload: {},
+
repo: {
+
owner,
+
repo,
+
},
},
+
core: {
+
getInput() {
+
return token
+
},
+
error: console.error,
+
info: console.log,
+
notice: console.log,
+
setFailed(msg) {
+
console.error(msg)
+
process.exitCode = 1
+
},
},
+
dry: true,
+
})
+
} finally {
+
rmSync(tmp, { recursive: true })
+
}
}
+
+
program
+
.command('labels')
+
.description('Manage labels on pull requests.')
+
.argument('<owner>', 'Owner of the GitHub repository to label (Example: NixOS)')
+
.argument('<repo>', 'Name of the GitHub repository to label (Example: nixpkgs)')
+
.action(async (owner, repo) => {
+
const labels = (await import('./labels.cjs')).default
+
run(labels, owner, repo)
+
})
+
+
await program.parse()