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
6 changes: 6 additions & 0 deletions .changeset/catalog-release-workflow.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
<!-- action-permissions:end -->

`pr status` with `--repo` reports the app identity's relevant pull requests and review requests
Expand Down
92 changes: 90 additions & 2 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial<Record<GhPermission, PermissionLevel>>>;
Expand Down Expand Up @@ -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' },
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{ 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<Record<string, GhAction>> = Object.freeze({
'pr:view': PR_VIEW,
Expand Down Expand Up @@ -598,6 +670,10 @@ export const GH_ACTIONS: Readonly<Record<string, GhAction>> = 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. */
Expand Down Expand Up @@ -667,6 +743,7 @@ export function buildGhArgv(
}

let argv: string[];
const trailingPositionals: string[] = [];
if (action.api !== undefined) {
const substitutions: Record<string, string> = {
owner,
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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() === '') {
Expand All @@ -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;
}

Expand Down
64 changes: 64 additions & 0 deletions test/unit/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
Loading