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/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 bf2589da..0075d67d 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` | @@ -767,6 +768,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` | @@ -787,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/packages/core/src/actions.ts b/packages/core/src/actions.ts index 31ddf221..1f92ea78 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -209,6 +209,27 @@ const PR_READY: GhAction = { timeoutSeconds: 60, }; +// GitHub GraphQL: +// 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'], + 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', @@ -383,7 +404,9 @@ const ISSUE_LIST: GhAction = { timeoutSeconds: 120, }; -// GitHub GraphQL: lockLockable and unlockLockable +// GitHub GraphQL: +// 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'], @@ -404,7 +427,9 @@ const ISSUE_UNLOCK: GhAction = { appUser: true, }; -// GitHub GraphQL: pinIssue and unpinIssue +// GitHub GraphQL: +// 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'], @@ -498,6 +523,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/scripts/action-permissions.mjs b/scripts/action-permissions.mjs new file mode 100644 index 00000000..1abea9a2 --- /dev/null +++ b/scripts/action-permissions.mjs @@ -0,0 +1,64 @@ +#!/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, renameSync, unlinkSync, 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 = ''; + +/** 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)}\`${annotation(name)} | ${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 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.replaceAll('\r\n', '\n') !== expected.replaceAll('\r\n', '\n')) { + throw new Error('README action permission table is stale; run `yarn docs:actions`'); + } +} else { + 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; + } +} diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index 22fffabc..c2e69102 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,24 @@ 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.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 new file mode 100644 index 00000000..4837caae --- /dev/null +++ b/test/unit/readme-actions.test.ts @@ -0,0 +1,18 @@ +/** + * The README permission table is generated from the catalog; --check fails when it is stale. + */ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { describe, it } from 'node:test'; + +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('matches the generated catalog table', () => { + assert.doesNotThrow(() => + execFileSync(process.execPath, [script, '--check'], { cwd: root, stdio: 'pipe' }), + ); + }); +});