diff --git a/.changeset/catalog-release-workflow.md b/.changeset/catalog-release-workflow.md new file mode 100644 index 00000000..8a89bc26 --- /dev/null +++ b/.changeset/catalog-release-workflow.md @@ -0,0 +1,6 @@ +--- +'gcb-core': minor +--- + +Add read-only release and workflow actions to the catalog: `release:list`, `release:view`, +`workflow:list`, and `workflow:view`, with their GitHub permissions, flags and tests. diff --git a/README.md b/README.md index 993df0a7..a96e4528 100644 --- a/README.md +++ b/README.md @@ -819,6 +819,10 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `run watch` | `actions: read`, `checks: read` | | `run cancel` | `actions: write` | | `run rerun` | `actions: write` | +| `release list` | `contents: read` | +| `release view` | `contents: read` | +| `workflow list` | `actions: read` | +| `workflow view` | `actions: read`, `contents: read` | `pr status` with `--repo` reports the app identity's relevant pull requests and review requests diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index 120b5afd..d9b2d6c8 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -63,6 +63,8 @@ export interface GhAction { readonly bodyRequiredWith?: readonly string[]; /** Flag pairs that may not be present together. */ readonly conflicts?: readonly (readonly [string, string])[]; + /** When the first flag is present, the second must be present too. */ + readonly requires?: readonly (readonly [string, string])[]; /** This action only reads; publicRead may run it on a verified public repository with the route's public-read token. */ readonly readOnly?: boolean; readonly permissions: Readonly>>; @@ -564,6 +566,76 @@ const RUN_LIST: GhAction = { appUser: true, }; +// GitHub: GET /repos/{owner}/{repo}/releases +const RELEASE_LIST: GhAction = { + name: 'release:list', + command: ['release', 'list'], + positionals: [], + flags: [ + { name: 'exclude-drafts', type: 'boolean' }, + { name: 'exclude-pre-releases', type: 'boolean' }, + { name: 'json', type: 'string' }, + { name: 'jq', type: 'string' }, + { name: 'limit', type: 'number' }, + { name: 'order', type: 'string' }, + { name: 'template', type: 'string' }, + ], + permissions: { contents: 'read' }, + timeoutSeconds: 120, + readOnly: true, + appUser: true, +}; + +// GitHub: GET /repos/{owner}/{repo}/releases/tags/{tag} +const RELEASE_VIEW: GhAction = { + name: 'release:view', + command: ['release', 'view'], + positionals: [{ name: 'tag', type: 'string' }], + flags: [ + { name: 'json', type: 'string' }, + { name: 'jq', type: 'string' }, + { name: 'template', type: 'string' }, + ], + permissions: { contents: 'read' }, + timeoutSeconds: 120, + readOnly: true, + appUser: true, +}; + +// GitHub: GET /repos/{owner}/{repo}/actions/workflows +const WORKFLOW_LIST: GhAction = { + name: 'workflow:list', + command: ['workflow', 'list'], + positionals: [], + flags: [ + { name: 'all', type: 'boolean' }, + { name: 'json', type: 'string' }, + { name: 'jq', type: 'string' }, + { name: 'limit', type: 'number' }, + { name: 'template', type: 'string' }, + ], + permissions: { actions: 'read' }, + timeoutSeconds: 120, + readOnly: true, + appUser: true, +}; + +// GitHub: GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} (and contents for --yaml/--ref) +const WORKFLOW_VIEW: GhAction = { + name: 'workflow:view', + command: ['workflow', 'view'], + positionals: [{ name: 'workflow', type: 'string', required: true }], + flags: [ + { name: 'ref', type: 'string' }, + { name: 'yaml', type: 'boolean' }, + ], + requires: [['ref', 'yaml']], + permissions: { actions: 'read', contents: 'read' }, + timeoutSeconds: 120, + readOnly: true, + appUser: true, +}; + /** Every built-in action, keyed by its action name. */ export const GH_ACTIONS: Readonly> = Object.freeze({ 'pr:view': PR_VIEW, @@ -598,6 +670,10 @@ export const GH_ACTIONS: Readonly> = Object.freeze({ 'run:watch': RUN_WATCH, 'run:cancel': RUN_CANCEL, 'run:rerun': RUN_RERUN, + 'release:list': RELEASE_LIST, + 'release:view': RELEASE_VIEW, + 'workflow:list': WORKFLOW_LIST, + 'workflow:view': WORKFLOW_VIEW, }); /** The built-in action names, in a stable order. */ @@ -667,6 +743,7 @@ export function buildGhArgv( } let argv: string[]; + const trailingPositionals: string[] = []; if (action.api !== undefined) { const substitutions: Record = { owner, @@ -700,7 +777,7 @@ export function buildGhArgv( if (positional.type === 'number') { argv.push(String(asNumber(value, positional.name))); } else { - argv.push(asString(value, positional.name)); + trailingPositionals.push(asString(value, positional.name)); } } } @@ -777,6 +854,11 @@ export function buildGhArgv( throw new ActionError(`${name} does not allow --${left} with --${right}`); } } + for (const [left, right] of action.requires ?? []) { + if (present.has(left) && !present.has(right)) { + throw new ActionError(`${name} needs --${right} with --${left}`); + } + } if (typeof values['title'] === 'string') { if (values['title'].trim() === '') { @@ -790,7 +872,13 @@ export function buildGhArgv( throw new ActionError(`the body must be at most ${MAX_BODY_CHARS} characters`); } - if (action.api === undefined) argv.push('--repo', repo); + if (action.api === undefined) { + argv.push('--repo', repo); + if (trailingPositionals.length > 0) { + // Keep caller-controlled strings after -- so a tag or workflow name cannot become a flag. + argv.push('--', ...trailingPositionals); + } + } return argv; } diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index f691df3e..96da7c01 100644 --- a/test/unit/actions.test.ts +++ b/test/unit/actions.test.ts @@ -45,6 +45,10 @@ describe('gh action catalog', () => { 'run:watch', 'run:cancel', 'run:rerun', + 'release:list', + 'release:view', + 'workflow:list', + 'workflow:view', ]); assert.equal('pr:merge' in GH_ACTIONS, false); assert.equal(getGhAction('pr:create').command.join(' '), 'pr create'); @@ -86,6 +90,10 @@ describe('gh action catalog', () => { }); assert.deepEqual(getGhAction('issue:view').permissions, { issues: 'read' }); assert.deepEqual(getGhAction('issue:list').permissions, { issues: 'read' }); + assert.deepEqual(getGhAction('release:list').permissions, { contents: 'read' }); + assert.deepEqual(getGhAction('release:view').permissions, { contents: 'read' }); + assert.deepEqual(getGhAction('workflow:list').permissions, { actions: 'read' }); + assert.deepEqual(getGhAction('workflow:view').permissions, { actions: 'read', contents: 'read' }); assert.equal(getGhAction('pr:checks').timeoutSeconds, 300); }); @@ -111,6 +119,10 @@ describe('gh action catalog', () => { 'run:view', 'run:list', 'run:watch', + 'release:list', + 'release:view', + 'workflow:list', + 'workflow:view', ]) { assert.equal(getGhAction(name).readOnly, true, name); assert.equal(getGhAction(name).appUser, true, name); @@ -170,6 +182,58 @@ describe('gh action catalog', () => { }); describe('buildGhArgv', () => { + it('builds release and workflow read argv', () => { + assert.deepEqual(buildGhArgv('release:list', { limit: 5, json: 'tagName' }, 'acme/widget'), [ + 'release', + 'list', + '--json', + 'tagName', + '--limit', + '5', + '--repo', + 'acme/widget', + ]); + assert.deepEqual(buildGhArgv('workflow:view', { workflow: 'ci.yml', yaml: true }, 'acme/widget'), [ + 'workflow', + 'view', + '--yaml', + '--repo', + 'acme/widget', + '--', + 'ci.yml', + ]); + assert.deepEqual( + buildGhArgv('workflow:view', { workflow: 'ci.yml', ref: 'main', yaml: true }, 'acme/widget'), + ['workflow', 'view', '--ref', 'main', '--yaml', '--repo', 'acme/widget', '--', 'ci.yml'], + ); + assert.deepEqual(buildGhArgv('workflow:list', { all: true, limit: 10 }, 'acme/widget'), [ + 'workflow', + 'list', + '--all', + '--limit', + '10', + '--repo', + 'acme/widget', + ]); + assert.deepEqual(buildGhArgv('release:view', { tag: 'v1.0.0', json: 'tagName' }, 'acme/widget'), [ + 'release', + 'view', + '--json', + 'tagName', + '--repo', + 'acme/widget', + '--', + 'v1.0.0', + ]); + const injected = buildGhArgv('release:view', { tag: '--web' }, 'acme/widget'); + assert.deepEqual(injected.slice(-2), ['--', '--web']); + assert.throws(() => buildGhArgv('workflow:view', {}, 'acme/widget'), /needs workflow/u); + assert.throws( + () => buildGhArgv('workflow:view', { workflow: 'ci.yml', ref: 'main' }, 'acme/widget'), + /needs --yaml with --ref/u, + ); + }); + it('rebuilds the canonical argv and injects the repository', () => { assert.deepEqual( buildGhArgv(