ci/github-script: add commander CLI interface

This makes it easier to add additional features.
This commit is contained in:
Wolfgang Walther
2025-07-13 16:24:24 +02:00
parent 6f6c625026
commit aaaabe0cb7
4 changed files with 58 additions and 37 deletions

View File

@@ -10,4 +10,4 @@ To run any of the scripts locally:
## Labeler
Run `./run.js OWNER REPO`, where OWNER is your username or "NixOS" and REPO the name of your fork or "nixpkgs".
Run `./run.js labels OWNER REPO`, where OWNER is your username or "NixOS" and REPO the name of your fork or "nixpkgs".

View File

@@ -1,5 +1,5 @@
{
"name": "labels",
"name": "github-script",
"lockfileVersion": 3,
"requires": true,
"packages": {
@@ -7,7 +7,8 @@
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
"bottleneck": "2.19.5"
"bottleneck": "2.19.5",
"commander": "14.0.0"
}
},
"node_modules/@actions/artifact": {
@@ -950,6 +951,15 @@
"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",
"resolved": "https://registry.npmjs.org/compress-commons/-/compress-commons-6.0.2.tgz",

View File

@@ -4,6 +4,7 @@
"dependencies": {
"@actions/artifact": "2.3.2",
"@actions/github": "6.0.1",
"bottleneck": "2.19.5"
"bottleneck": "2.19.5",
"commander": "14.0.0"
}
}

View File

@@ -3,43 +3,53 @@ import { execSync } from 'node:child_process'
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'
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
async function run(action, owner, repo) {
const token = execSync('gh auth token', { encoding: 'utf-8' }).trim()
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)
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,
await action({
github: getOctokit(token),
context: {
payload: {},
repo: {
owner,
repo,
},
},
},
core: {
getInput() {
return token
core: {
getInput() {
return token
},
error: console.error,
info: console.log,
notice: console.log,
setFailed(msg) {
console.error(msg)
process.exitCode = 1
},
},
error: console.error,
info: console.log,
notice: console.log,
setFailed(msg) {
console.error(msg)
process.exitCode = 1
},
},
dry: true,
})
} finally {
rmSync(tmp, { recursive: true })
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()