diff --git a/.changeset/catalog-search.md b/.changeset/catalog-search.md new file mode 100644 index 00000000..f05f1ac9 --- /dev/null +++ b/.changeset/catalog-search.md @@ -0,0 +1,6 @@ +--- +'gcb-core': minor +--- + +Add repo-scoped search actions: `search:issues` (`issues: read`) and `search:prs` +(`pull_requests: read`). The broker still injects `--repo`, and rejects `repo:` / `org:` / `user:` qualifiers in the query, so a search cannot escape the allowlist. diff --git a/README.md b/README.md index 62f2771a..e4412255 100644 --- a/README.md +++ b/README.md @@ -832,6 +832,8 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `label create` | `issues: write` | | `label edit` | `issues: write` | | `label delete` | `issues: write` | +| `search issues` | `issues: read` | +| `search prs` | `pull_requests: 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 42a4417c..f442c40d 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -757,6 +757,57 @@ const LABEL_DELETE: GhAction = { appUser: true, }; +// GitHub: GET /search/issues (issues) +const SEARCH_ISSUES: GhAction = { + name: 'search:issues', + command: ['search', 'issues'], + positionals: [{ name: 'query', type: 'string' }], + flags: [ + { name: 'assignee', type: 'string' }, + { name: 'author', type: 'string' }, + { name: 'json', type: 'string' }, + { name: 'jq', type: 'string' }, + { name: 'label', type: 'string', repeatable: true }, + { name: 'limit', type: 'number' }, + { name: 'match', type: 'string', repeatable: true }, + { name: 'order', type: 'string' }, + { name: 'sort', type: 'string' }, + { name: 'state', type: 'string' }, + { name: 'template', type: 'string' }, + ], + permissions: { issues: 'read' }, + timeoutSeconds: 120, + readOnly: true, + appUser: true, +}; + +// GitHub: GET /search/issues (pull requests) +const SEARCH_PRS: GhAction = { + name: 'search:prs', + command: ['search', 'prs'], + positionals: [{ name: 'query', type: 'string' }], + flags: [ + { name: 'assignee', type: 'string' }, + { name: 'author', type: 'string' }, + { name: 'base', type: 'string' }, + { name: 'draft', type: 'boolean' }, + { name: 'head', type: 'string' }, + { name: 'json', type: 'string' }, + { name: 'jq', type: 'string' }, + { name: 'label', type: 'string', repeatable: true }, + { name: 'limit', type: 'number' }, + { name: 'match', type: 'string', repeatable: true }, + { name: 'order', type: 'string' }, + { name: 'sort', type: 'string' }, + { name: 'state', type: 'string' }, + { name: 'template', type: 'string' }, + ], + permissions: { pull_requests: '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, @@ -803,6 +854,8 @@ export const GH_ACTIONS: Readonly> = Object.freeze({ 'label:create': LABEL_CREATE, 'label:edit': LABEL_EDIT, 'label:delete': LABEL_DELETE, + 'search:issues': SEARCH_ISSUES, + 'search:prs': SEARCH_PRS, }); /** The built-in action names, in a stable order. */ @@ -1000,6 +1053,15 @@ export function buildGhArgv( } } + if ( + action.command[0] === 'search' && + typeof values['query'] === 'string' && + /(?:^|[\s(])-?(?:repo|org|user):/iu.test(values['query']) + ) { + throw new ActionError( + 'search query cannot contain repo:, org: or user:; the broker scopes it to --repo', + ); + } if (typeof values['title'] === 'string') { if (values['title'].trim() === '') { throw new ActionError('the title must not be empty'); diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index 40a58df4..2ba9b119 100644 --- a/test/unit/actions.test.ts +++ b/test/unit/actions.test.ts @@ -57,6 +57,8 @@ describe('gh action catalog', () => { 'label:create', 'label:edit', 'label:delete', + 'search:issues', + 'search:prs', ]); assert.equal('pr:merge' in GH_ACTIONS, false); assert.equal(getGhAction('pr:create').command.join(' '), 'pr create'); @@ -117,6 +119,8 @@ describe('gh action catalog', () => { assert.deepEqual(getGhAction('label:create').permissions, { issues: 'write' }); assert.deepEqual(getGhAction('label:edit').permissions, { issues: 'write' }); assert.deepEqual(getGhAction('label:delete').permissions, { issues: 'write' }); + assert.deepEqual(getGhAction('search:issues').permissions, { issues: 'read' }); + assert.deepEqual(getGhAction('search:prs').permissions, { pull_requests: 'read' }); assert.equal(getGhAction('pr:checks').timeoutSeconds, 300); }); @@ -147,6 +151,8 @@ describe('gh action catalog', () => { 'workflow:list', 'workflow:view', 'label:list', + 'search:issues', + 'search:prs', ]) { assert.equal(getGhAction(name).readOnly, true, name); assert.equal(getGhAction(name).appUser, true, name); @@ -254,6 +260,46 @@ describe('buildGhArgv', () => { /needs --yes/u, ); + assert.deepEqual( + buildGhArgv('search:issues', { query: 'bug', label: ['one', 'two'], limit: 5 }, 'acme/widget'), + [ + 'search', + 'issues', + '--label', + 'one', + '--label', + 'two', + '--limit', + '5', + '--repo', + 'acme/widget', + '--', + 'bug', + ], + ); + for (const query of [ + 'repo:other/repo bug', + 'org:other bug', + 'user:other bug', + '-repo:other/repo', + 'bug org:other', + '(repo:other/repo)', + '(org:other)', + '(user:other)', + ]) { + assert.throws( + () => buildGhArgv('search:issues', { query }, 'acme/widget'), + /cannot contain repo:, org: or user:/u, + query, + ); + } + assert.doesNotThrow(() => buildGhArgv('search:issues', { query: 'is:issue label:bug' }, 'acme/widget')); + + assert.deepEqual( + buildGhArgv('search:prs', { query: 'fix', draft: true }, 'acme/widget'), + ['search', 'prs', '--draft', '--repo', 'acme/widget', '--', 'fix'], + ); + assert.deepEqual( buildGhArgv('issue:comment-delete', { commentId: 123 }, 'acme/widget'), ['api', '--method', 'DELETE', 'repos/acme/widget/issues/comments/123'],