diff --git a/.changeset/catalog-update-branch.md b/.changeset/catalog-update-branch.md new file mode 100644 index 00000000..ce0106e9 --- /dev/null +++ b/.changeset/catalog-update-branch.md @@ -0,0 +1,12 @@ +--- +'gcb-core': minor +--- + +Add `pr:update-branch` to the action catalog. It takes an optional `--rebase`, uses +`pull_requests: write` and `contents: write`, and sets `appUser`, so bare `--as-user` works through +the app broker. `appUser` follows `pr:edit`: updating a branch is an author operation, while +close/reopen/ready/lock stay host-account only. Bare `--as-user` needs a user token with +`contents: write`; the recommended `Contents: Read` token is enough for PR editing but not for +updating a branch. The default app route and `--as-user ` do not depend on that token. For a +PR whose head is a fork, the app installation token is scoped to the base repository and cannot +write the head fork; use `--as-user ` for that case. diff --git a/README.md b/README.md index 0075d67d..291f3b90 100644 --- a/README.md +++ b/README.md @@ -492,8 +492,9 @@ than promised here. `userTokens` is consulted for opening, replying, editing or and for an issue creation or resolve when no `user.refresh` exists. Resolving a review thread needs **Contents: Read and write** (`resolveReviewThread` is refused without it), which would also let that token push and merge; if that trade is not acceptable, resolve through `authorize`, whose token is the person's own. -`user.refresh` wins over `userTokens` when it exists. A plain `gcb pr` stays on the app's -installation token. Dropping the field does not change the default; it only makes bare `--as-user` +`pr:update-branch --as-user` also needs **Contents: Read and write** for the same reason; without it, +use the default app route or `--as-user `. `user.refresh` wins over `userTokens` when it +exists. A plain `gcb pr` stays on the app's installation token. Dropping the field does not change the default; it only makes bare `--as-user` fail with `no authorized user token`. #### Authorizing as a person instead @@ -770,6 +771,7 @@ actions. Grant them on the App, then approve any changed permissions on each ins | `pr ready` | `pull_requests: write` | | `pr lock` | `pull_requests: write` | | `pr unlock` | `pull_requests: write` | +| `pr update-branch` | `pull_requests: write`, `contents: write` | | `pr checks` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr status` | `pull_requests: read`, `checks: read`, `statuses: read` | | `pr comment` | `pull_requests: write` | diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index 1f92ea78..c777ea9e 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -209,6 +209,18 @@ const PR_READY: GhAction = { timeoutSeconds: 60, }; +// GitHub GraphQL: +// https://docs.github.com/en/graphql/reference/pulls#mutation-updatepullrequestbranch +const PR_UPDATE_BRANCH: GhAction = { + name: 'pr:update-branch', + command: ['pr', 'update-branch'], + positionals: [{ name: 'number', type: 'number', required: true }], + flags: [{ name: 'rebase', type: 'boolean' }], + permissions: { pull_requests: 'write', contents: 'write' }, + timeoutSeconds: 120, + appUser: true, +}; + // GitHub GraphQL: // https://docs.github.com/en/graphql/reference/issues#mutation-locklockable // https://docs.github.com/en/graphql/reference/issues#mutation-unlocklockable @@ -525,6 +537,7 @@ export const GH_ACTIONS: Readonly> = Object.freeze({ 'pr:ready': PR_READY, 'pr:lock': PR_LOCK, 'pr:unlock': PR_UNLOCK, + 'pr:update-branch': PR_UPDATE_BRANCH, 'pr:checks': PR_CHECKS, 'pr:status': PR_STATUS, 'pr:comment': PR_COMMENT, diff --git a/test/unit/actions.test.ts b/test/unit/actions.test.ts index c2e69102..30469b55 100644 --- a/test/unit/actions.test.ts +++ b/test/unit/actions.test.ts @@ -23,6 +23,7 @@ describe('gh action catalog', () => { 'pr:ready', 'pr:lock', 'pr:unlock', + 'pr:update-branch', 'pr:checks', 'pr:status', 'pr:comment', @@ -94,15 +95,21 @@ describe('gh action catalog', () => { assert.equal(getGhAction('pr:ready').appUser, undefined); assert.equal(getGhAction('pr:lock').appUser, undefined); assert.equal(getGhAction('pr:unlock').appUser, undefined); + assert.equal(getGhAction('pr:update-branch').appUser, true); 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('pr:update-branch').permissions, { + pull_requests: 'write', + contents: '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], + ['pr:update-branch', 120], ['run:cancel', 60], ['run:rerun', 120], ] as const) { @@ -270,6 +277,15 @@ describe('buildGhArgv', () => { ]); assert.throws(() => buildGhArgv('pr:lock', {}, 'acme/widget'), /needs number/u); assert.throws(() => buildGhArgv('pr:unlock', {}, 'acme/widget'), /needs number/u); + assert.deepEqual(buildGhArgv('pr:update-branch', { number: 7, rebase: true }, 'acme/widget'), [ + 'pr', + 'update-branch', + '7', + '--rebase', + '--repo', + 'acme/widget', + ]); + assert.throws(() => buildGhArgv('pr:update-branch', {}, 'acme/widget'), /needs number/u); assert.deepEqual(buildGhArgv('run:cancel', { runId: 42, force: true }, 'acme/widget'), [ 'run', 'cancel',