From 0944cc383ba2e44be75b1c6a85affe0c3b51e424 Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 05:11:18 +0000 Subject: [PATCH 1/7] feat: add pr lock/unlock actions --- .changeset/catalog-pr-lock.md | 7 +++++++ README.md | 2 ++ packages/core/src/actions.ts | 21 +++++++++++++++++++++ test/unit/actions.test.ts | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 .changeset/catalog-pr-lock.md diff --git a/.changeset/catalog-pr-lock.md b/.changeset/catalog-pr-lock.md new file mode 100644 index 00000000..5f8b0bdb --- /dev/null +++ b/.changeset/catalog-pr-lock.md @@ -0,0 +1,7 @@ +--- +'gcb-core': minor +--- + +Add `pr:lock` and `pr:unlock` to the action catalog. Lock takes an optional `--reason`; both use +`pull_requests: write`. The permission was verified with a token limited to `pull_requests: write`, +and an `issues: write` token was refused. diff --git a/README.md b/README.md index bf2589da..19579f68 100644 --- a/README.md +++ b/README.md @@ -767,6 +767,8 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `pr close` | `pull_requests: write` | | `pr reopen` | `pull_requests: write` | | `pr ready` | `pull_requests: write` | +| `pr lock` | `pull_requests: write` | +| `pr unlock` | `pull_requests: write` | | `pr checks` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr status` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr comment` | `pull_requests: write` | diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index 31ddf221..fd4d6d2e 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -209,6 +209,25 @@ const PR_READY: GhAction = { timeoutSeconds: 60, }; +// GitHub GraphQL: lockLockable and unlockLockable +const PR_LOCK: GhAction = { + name: 'pr:lock', + command: ['pr', 'lock'], + positionals: [{ name: 'number', type: 'number', required: true }], + flags: [{ name: 'reason', type: 'string' }], + permissions: { pull_requests: 'write' }, + timeoutSeconds: 60, +}; + +const PR_UNLOCK: GhAction = { + name: 'pr:unlock', + command: ['pr', 'unlock'], + positionals: [{ name: 'number', type: 'number', required: true }], + flags: [], + permissions: { pull_requests: 'write' }, + timeoutSeconds: 60, +}; + // GitHub: GET /repos/{owner}/{repo}/pulls/{number} then its check-runs and commit statuses const PR_CHECKS: GhAction = { name: 'pr:checks', @@ -498,6 +517,8 @@ export const GH_ACTIONS: Readonly> = Object.freeze({ 'pr:close': PR_CLOSE, 'pr:reopen': PR_REOPEN, 'pr:ready': PR_READY, + 'pr:lock': PR_LOCK, + 'pr:unlock': PR_UNLOCK, 'pr:checks': PR_CHECKS, 'pr:status': PR_STATUS, 'pr:comment': PR_COMMENT, diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index 22fffabc..627e5f3c 100644 --- a/test/unit/actions.test.ts +++ b/test/unit/actions.test.ts @@ -21,6 +21,8 @@ describe('gh action catalog', () => { 'pr:close', 'pr:reopen', 'pr:ready', + 'pr:lock', + 'pr:unlock', 'pr:checks', 'pr:status', 'pr:comment', @@ -90,11 +92,17 @@ describe('gh action catalog', () => { assert.equal(getGhAction(name).appUser, true, name); } assert.equal(getGhAction('pr:ready').appUser, undefined); + assert.equal(getGhAction('pr:lock').appUser, undefined); + assert.equal(getGhAction('pr:unlock').appUser, undefined); assert.deepEqual(getGhAction('pr:ready').permissions, { pull_requests: 'write' }); + assert.deepEqual(getGhAction('pr:lock').permissions, { pull_requests: 'write' }); + assert.deepEqual(getGhAction('pr:unlock').permissions, { pull_requests: 'write' }); assert.deepEqual(getGhAction('run:cancel').permissions, { actions: 'write' }); assert.deepEqual(getGhAction('run:rerun').permissions, { actions: 'write' }); for (const [name, seconds] of [ ['pr:ready', 60], + ['pr:lock', 60], + ['pr:unlock', 60], ['run:cancel', 60], ['run:rerun', 120], ] as const) { @@ -244,6 +252,22 @@ describe('buildGhArgv', () => { '--repo', 'acme/widget', ]); + assert.deepEqual(buildGhArgv('pr:lock', { number: 7, reason: 'spam' }, 'acme/widget'), [ + 'pr', + 'lock', + '7', + '--reason', + 'spam', + '--repo', + 'acme/widget', + ]); + assert.deepEqual(buildGhArgv('pr:unlock', { number: 7 }, 'acme/widget'), [ + 'pr', + 'unlock', + '7', + '--repo', + 'acme/widget', + ]); assert.deepEqual(buildGhArgv('run:cancel', { runId: 42, force: true }, 'acme/widget'), [ 'run', 'cancel', From 4e80b7cdd067c28d41fcad478ba8dcc78a5dc46a Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 05:34:30 +0000 Subject: [PATCH 2/7] test: pin README permissions to the catalog --- test/unit/readme-actions.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/unit/readme-actions.test.ts diff --git a/test/unit/readme-actions.test.ts b/test/unit/readme-actions.test.ts new file mode 100644 index 00000000..e469b490 --- /dev/null +++ b/test/unit/readme-actions.test.ts @@ -0,0 +1,27 @@ +/** + * The README permission table is user-facing documentation; the catalog is the source. Keep the + * two from drifting without generating the README at build time. + */ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { describe, it } from 'node:test'; + +import { GH_ACTIONS } from '../../packages/core/src/actions.ts'; + +const README = readFileSync(new URL('../../README.md', import.meta.url), 'utf8'); + +describe('README action permissions', () => { + it('lists every catalog action with its declared permissions', () => { + for (const [name, action] of Object.entries(GH_ACTIONS)) { + const label = name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); + const row = README.split('\n').find((line) => line.startsWith(`| \`${label}\``)); + assert.ok(row, `${name} is missing from the README table`); + for (const [permission, level] of Object.entries(action.permissions)) { + assert.ok( + row.includes(`\`${permission}: ${level}\``), + `${name} is missing ${permission}: ${level} in the README table`, + ); + } + } + }); +}); From 0d5974bfb011b4eb876c12315e51ba002c326203 Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 05:41:01 +0000 Subject: [PATCH 3/7] test: compare README permissions as sets --- test/unit/actions.test.ts | 2 ++ test/unit/readme-actions.test.ts | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index 627e5f3c..c2e69102 100644 --- a/test/unit/actions.test.ts +++ b/test/unit/actions.test.ts @@ -268,6 +268,8 @@ describe('buildGhArgv', () => { '--repo', 'acme/widget', ]); + assert.throws(() => buildGhArgv('pr:lock', {}, 'acme/widget'), /needs number/u); + assert.throws(() => buildGhArgv('pr:unlock', {}, 'acme/widget'), /needs number/u); assert.deepEqual(buildGhArgv('run:cancel', { runId: 42, force: true }, 'acme/widget'), [ 'run', 'cancel', diff --git a/test/unit/readme-actions.test.ts b/test/unit/readme-actions.test.ts index e469b490..bc1d407c 100644 --- a/test/unit/readme-actions.test.ts +++ b/test/unit/readme-actions.test.ts @@ -16,12 +16,17 @@ describe('README action permissions', () => { const label = name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); const row = README.split('\n').find((line) => line.startsWith(`| \`${label}\``)); assert.ok(row, `${name} is missing from the README table`); - for (const [permission, level] of Object.entries(action.permissions)) { - assert.ok( - row.includes(`\`${permission}: ${level}\``), - `${name} is missing ${permission}: ${level} in the README table`, - ); - } + const documented = [...row.matchAll(/`([a-z_]+): (read|write)`/gu)].map( + (match) => `${match[1]}: ${match[2]}`, + ); + const declared = Object.entries(action.permissions).map( + ([permission, level]) => `${permission}: ${level}`, + ); + assert.deepEqual( + documented.sort(), + declared.sort(), + `${name} documents a different permission set than the catalog`, + ); } }); }); From 584c2a0cda28addc158bacad198320c989d8a69a Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 06:05:45 +0000 Subject: [PATCH 4/7] docs: generate the action permission table --- README.md | 4 ++- package.json | 1 + scripts/action-permissions.mjs | 44 ++++++++++++++++++++++++++++++++ test/unit/readme-actions.test.ts | 32 +++++++---------------- 4 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 scripts/action-permissions.mjs diff --git a/README.md b/README.md index 19579f68..d30e6b5d 100644 --- a/README.md +++ b/README.md @@ -757,6 +757,7 @@ local build; CI and production pass a published version instead, e.g. The action catalog is the source for these requirements; `gcb gh --help` prints the same actions. Grant them on the App, then approve any changed permissions on each installation. + | Action | App permissions | |---|---| | `pr view` | `pull_requests: read`, `contents: read`, `checks: read`, `statuses: read` | @@ -773,7 +774,7 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `pr status` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr comment` | `pull_requests: write` | | `pr review` | `pull_requests: write` | -| `pr review --id` (`pr:review-by-id`) | `pull_requests: read` | +| `pr review --id` | `pull_requests: read` | | `issue create` | `issues: write` | | `issue edit` | `issues: write` | | `issue comment` | `issues: write` | @@ -789,6 +790,7 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `run list` | `actions: read` | | `run cancel` | `actions: write` | | `run rerun` | `actions: write` | + `pr status` with `--repo` reports the app identity's relevant pull requests and review requests with their checks; gh's current-branch section is absent because the broker has no branch. diff --git a/package.json b/package.json index 1b90ce04..6d40864e 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "test:unit": "node --conditions=development --disable-warning=ExperimentalWarning --test test/unit/*.test.ts", "test:e2e": "yarn build && node --conditions=development --disable-warning=ExperimentalWarning --test test/e2e/*.test.ts", "check": "yarn typecheck && yarn test", + "docs:actions": "node scripts/action-permissions.mjs", "pack:check": "yarn build && node scripts/pack-check.mjs", "prepublishOnly": "yarn check", "version": "changeset version && yarn install --mode=update-lockfile", diff --git a/scripts/action-permissions.mjs b/scripts/action-permissions.mjs new file mode 100644 index 00000000..0c8fb24f --- /dev/null +++ b/scripts/action-permissions.mjs @@ -0,0 +1,44 @@ +#!/usr/bin/env node +/** + * Generate the README's action permission table from the catalog. + * + * Run with no arguments to rewrite the marked block; run with --check to fail when it is stale. + */ +import { readFileSync, writeFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; + +import { GH_ACTIONS } from '../packages/core/src/actions.ts'; + +const README = fileURLToPath(new URL('../README.md', import.meta.url)); +const START = ''; +const END = ''; + +const label = (name, action) => + name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); + +function render() { + const lines = ['| Action | App permissions |', '|---|---|']; + for (const [name, action] of Object.entries(GH_ACTIONS)) { + const permissions = Object.entries(action.permissions) + .map(([permission, level]) => `\`${permission}: ${level}\``) + .join(', '); + lines.push(`| \`${label(name, action)}\` | ${permissions} |`); + } + return lines.join('\n'); +} + +const readme = readFileSync(README, 'utf8'); +const start = readme.indexOf(START); +const end = readme.indexOf(END); +if (start < 0 || end < start) { + throw new Error(`README is missing ${START} ... ${END}`); +} +const expected = `${START}\n${render()}\n${END}`; +const actual = readme.slice(start, end + END.length); +if (process.argv.includes('--check')) { + if (actual !== expected) { + throw new Error('README action permission table is stale; run `yarn docs:actions`'); + } +} else { + writeFileSync(README, readme.slice(0, start) + expected + readme.slice(end + END.length)); +} diff --git a/test/unit/readme-actions.test.ts b/test/unit/readme-actions.test.ts index bc1d407c..4837caae 100644 --- a/test/unit/readme-actions.test.ts +++ b/test/unit/readme-actions.test.ts @@ -1,32 +1,18 @@ /** - * The README permission table is user-facing documentation; the catalog is the source. Keep the - * two from drifting without generating the README at build time. + * The README permission table is generated from the catalog; --check fails when it is stale. */ import assert from 'node:assert/strict'; -import { readFileSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; import { describe, it } from 'node:test'; -import { GH_ACTIONS } from '../../packages/core/src/actions.ts'; - -const README = readFileSync(new URL('../../README.md', import.meta.url), 'utf8'); +const script = fileURLToPath(new URL('../../scripts/action-permissions.mjs', import.meta.url)); +const root = fileURLToPath(new URL('../..', import.meta.url)); describe('README action permissions', () => { - it('lists every catalog action with its declared permissions', () => { - for (const [name, action] of Object.entries(GH_ACTIONS)) { - const label = name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); - const row = README.split('\n').find((line) => line.startsWith(`| \`${label}\``)); - assert.ok(row, `${name} is missing from the README table`); - const documented = [...row.matchAll(/`([a-z_]+): (read|write)`/gu)].map( - (match) => `${match[1]}: ${match[2]}`, - ); - const declared = Object.entries(action.permissions).map( - ([permission, level]) => `${permission}: ${level}`, - ); - assert.deepEqual( - documented.sort(), - declared.sort(), - `${name} documents a different permission set than the catalog`, - ); - } + it('matches the generated catalog table', () => { + assert.doesNotThrow(() => + execFileSync(process.execPath, [script, '--check'], { cwd: root, stdio: 'pipe' }), + ); }); }); From db1c0a802f14a305754cc70106a091f428bff04d Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 06:16:56 +0000 Subject: [PATCH 5/7] fix: preserve README style when regenerating --- DEVELOPMENT.md | 1 + README.md | 2 +- scripts/action-permissions.mjs | 34 +++++++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index a2dc0d89..a6efb1f3 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -59,6 +59,7 @@ Two consequences: ```sh corepack yarn build # tsc per package + scripts/postbuild.mjs corepack yarn typecheck # tsc, no emit +corepack yarn docs:actions # regenerate the README action permission table (or --check) ``` Output goes to `packages/*/lib/`. `scripts/postbuild.mjs` exists for one reason: the two diff --git a/README.md b/README.md index d30e6b5d..0075d67d 100644 --- a/README.md +++ b/README.md @@ -774,7 +774,7 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `pr status` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr comment` | `pull_requests: write` | | `pr review` | `pull_requests: write` | -| `pr review --id` | `pull_requests: read` | +| `pr review --id` (`pr:review-by-id`) | `pull_requests: read` | | `issue create` | `issues: write` | | `issue edit` | `issues: write` | | `issue comment` | `issues: write` | diff --git a/scripts/action-permissions.mjs b/scripts/action-permissions.mjs index 0c8fb24f..1abea9a2 100644 --- a/scripts/action-permissions.mjs +++ b/scripts/action-permissions.mjs @@ -4,7 +4,7 @@ * * Run with no arguments to rewrite the marked block; run with --check to fail when it is stale. */ -import { readFileSync, writeFileSync } from 'node:fs'; +import { readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { GH_ACTIONS } from '../packages/core/src/actions.ts'; @@ -13,16 +13,24 @@ const README = fileURLToPath(new URL('../README.md', import.meta.url)); const START = ''; const END = ''; -const label = (name, action) => - name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); +/** The label README uses for one action. */ +function label(name, action) { + return name === 'pr:review-by-id' ? 'pr review --id' : action.command.join(' '); +} + +/** The catalog-id suffix for the one action whose label differs from it. */ +function annotation(name) { + return name === 'pr:review-by-id' ? ' (`pr:review-by-id`)' : ''; +} +/** Render the table rows from the catalog. */ function render() { const lines = ['| Action | App permissions |', '|---|---|']; for (const [name, action] of Object.entries(GH_ACTIONS)) { const permissions = Object.entries(action.permissions) .map(([permission, level]) => `\`${permission}: ${level}\``) .join(', '); - lines.push(`| \`${label(name, action)}\` | ${permissions} |`); + lines.push(`| \`${label(name, action)}\`${annotation(name)} | ${permissions} |`); } return lines.join('\n'); } @@ -33,12 +41,24 @@ const end = readme.indexOf(END); if (start < 0 || end < start) { throw new Error(`README is missing ${START} ... ${END}`); } -const expected = `${START}\n${render()}\n${END}`; +const eol = readme.includes('\r\n') ? '\r\n' : '\n'; +const expected = `${START}${eol}${render().replaceAll('\n', eol)}${eol}${END}`; const actual = readme.slice(start, end + END.length); if (process.argv.includes('--check')) { - if (actual !== expected) { + if (actual.replaceAll('\r\n', '\n') !== expected.replaceAll('\r\n', '\n')) { throw new Error('README action permission table is stale; run `yarn docs:actions`'); } } else { - writeFileSync(README, readme.slice(0, start) + expected + readme.slice(end + END.length)); + const temporary = `${README}.${process.pid}.tmp`; + try { + writeFileSync(temporary, readme.slice(0, start) + expected + readme.slice(end + END.length)); + renameSync(temporary, README); + } catch (error) { + try { + unlinkSync(temporary); + } catch { + // The temporary file was never created; the original README is untouched. + } + throw error; + } } From c6f0d086473cca9b7de7821dcb5c492ec4c2b92b Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 06:27:24 +0000 Subject: [PATCH 6/7] docs: link the mutation references --- packages/core/src/actions.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index fd4d6d2e..6098d7d6 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -209,7 +209,9 @@ const PR_READY: GhAction = { timeoutSeconds: 60, }; -// GitHub GraphQL: lockLockable and unlockLockable +// GitHub GraphQL: +// https://docs.github.com/en/graphql/reference/mutations#locklockable +// https://docs.github.com/en/graphql/reference/mutations#unlocklockable const PR_LOCK: GhAction = { name: 'pr:lock', command: ['pr', 'lock'], @@ -402,7 +404,9 @@ const ISSUE_LIST: GhAction = { timeoutSeconds: 120, }; -// GitHub GraphQL: lockLockable and unlockLockable +// GitHub GraphQL: +// https://docs.github.com/en/graphql/reference/mutations#locklockable +// https://docs.github.com/en/graphql/reference/mutations#unlocklockable const ISSUE_LOCK: GhAction = { name: 'issue:lock', command: ['issue', 'lock'], @@ -423,7 +427,9 @@ const ISSUE_UNLOCK: GhAction = { appUser: true, }; -// GitHub GraphQL: pinIssue and unpinIssue +// GitHub GraphQL: +// https://docs.github.com/en/graphql/reference/mutations#pinissue +// https://docs.github.com/en/graphql/reference/mutations#unpinissue const ISSUE_PIN: GhAction = { name: 'issue:pin', command: ['issue', 'pin'], From 225e581e0a109d0ad0dd74cc986734b30696d785 Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 5 Nov 2026 06:36:59 +0000 Subject: [PATCH 7/7] docs: point mutations at the issues reference --- packages/core/src/actions.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index 6098d7d6..1f92ea78 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -210,8 +210,8 @@ const PR_READY: GhAction = { }; // GitHub GraphQL: -// https://docs.github.com/en/graphql/reference/mutations#locklockable -// https://docs.github.com/en/graphql/reference/mutations#unlocklockable +// https://docs.github.com/en/graphql/reference/issues#mutation-locklockable +// https://docs.github.com/en/graphql/reference/issues#mutation-unlocklockable const PR_LOCK: GhAction = { name: 'pr:lock', command: ['pr', 'lock'], @@ -405,8 +405,8 @@ const ISSUE_LIST: GhAction = { }; // GitHub GraphQL: -// https://docs.github.com/en/graphql/reference/mutations#locklockable -// https://docs.github.com/en/graphql/reference/mutations#unlocklockable +// https://docs.github.com/en/graphql/reference/issues#mutation-locklockable +// https://docs.github.com/en/graphql/reference/issues#mutation-unlocklockable const ISSUE_LOCK: GhAction = { name: 'issue:lock', command: ['issue', 'lock'], @@ -428,8 +428,8 @@ const ISSUE_UNLOCK: GhAction = { }; // GitHub GraphQL: -// https://docs.github.com/en/graphql/reference/mutations#pinissue -// https://docs.github.com/en/graphql/reference/mutations#unpinissue +// https://docs.github.com/en/graphql/reference/issues#mutation-pinissue +// https://docs.github.com/en/graphql/reference/issues#mutation-unpinissue const ISSUE_PIN: GhAction = { name: 'issue:pin', command: ['issue', 'pin'],