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
12 changes: 12 additions & 0 deletions .changeset/catalog-update-branch.md
Original file line number Diff line number Diff line change
@@ -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 <login>` 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 <login>` for that case.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <login>`. `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
Expand Down Expand Up @@ -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` |
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -525,6 +537,7 @@ export const GH_ACTIONS: Readonly<Record<string, GhAction>> = 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,
Expand Down
16 changes: 16 additions & 0 deletions test/unit/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('gh action catalog', () => {
'pr:ready',
'pr:lock',
'pr:unlock',
'pr:update-branch',
'pr:checks',
'pr:status',
'pr:comment',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down
Loading