Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/catalog-pr-lock.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-permissions:start -->
| Action | App permissions |
|---|---|
| `pr view` | `pull_requests: read`, `contents: read`, `checks: read`, `statuses: read` |
Expand All @@ -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` |
Expand All @@ -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` |
<!-- action-permissions:end -->

`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.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand Down Expand Up @@ -498,6 +523,8 @@ export const GH_ACTIONS: Readonly<Record<string, GhAction>> = 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,
Expand Down
64 changes: 64 additions & 0 deletions scripts/action-permissions.mjs
Original file line number Diff line number Diff line change
@@ -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 = '<!-- action-permissions:start -->';
const END = '<!-- action-permissions: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;
}
}
26 changes: 26 additions & 0 deletions test/unit/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('gh action catalog', () => {
'pr:close',
'pr:reopen',
'pr:ready',
'pr:lock',
'pr:unlock',
'pr:checks',
'pr:status',
'pr:comment',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions test/unit/readme-actions.test.ts
Original file line number Diff line number Diff line change
@@ -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' }),
);
});
});
Loading