44// Environment:
55// GITHUB_REPOSITORY, GITHUB_EVENT_PATH, and others
66// See https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
7- // GITHUB_TOKEN: GitHub authorization token.
8- // Must have write permissions for `actions`, `contents`, `pull_requests`.
7+ // GITHUB_WRITE_TOKEN: GitHub authorization token for creating a branch.
8+ // Must have `contents:write` permissions.
9+ // GITHUB_PR_TOKEN: GitHub authorization token.
10+ // Must have write permissions for `actions` and `pull_requests`.
911
1012import fs from 'fs' ;
1113
@@ -19,7 +21,8 @@ import { getOctokit } from './lib/dependencies';
1921type EnvironmentVariableName =
2022 'GITHUB_REPOSITORY' |
2123 'GITHUB_EVENT_PATH' |
22- 'GITHUB_TOKEN' ;
24+ 'GITHUB_WRITE_TOKEN' |
25+ 'GITHUB_PR_TOKEN' ;
2326
2427/**
2528 * Partial contents of the event payload, for a release event.
@@ -59,7 +62,7 @@ function getEnv(variable: EnvironmentVariableName): string {
5962 */
6063async function ensureBranch ( owner : string , repo : string , branchName : string , tagName : string ) : Promise < void > {
6164 const ref = `heads/${ branchName } ` ;
62- const { git } = getOctokit ( ) . rest ;
65+ const { git } = getOctokit ( getEnv ( 'GITHUB_WRITE_TOKEN' ) ) . rest ;
6366 const { data : tagRef } = await git . getRef ( {
6467 owner, repo, ref : `tags/${ tagName } ` ,
6568 } ) ;
@@ -77,11 +80,14 @@ async function ensureBranch(owner: string, repo: string, branchName: string, tag
7780 await git . updateRef ( {
7881 owner, repo, ref, sha,
7982 } ) ;
83+ } else {
84+ console . log ( `Branch ${ owner } /${ repo } /${ ref } is already up-to-date.` ) ;
8085 }
8186 } catch ( ex ) {
8287 if ( ! ( ex instanceof RequestError ) || ex . status !== 404 ) {
8388 throw ex ;
8489 }
90+ console . log ( `Creating new branch ${ owner } /${ repo } /${ ref } at ${ sha } ` ) ;
8591 // Branch does not exist; create it.
8692 await git . createRef ( {
8793 // Only this API takes a `refs/` prefix; get & update omit it.
@@ -100,14 +106,14 @@ async function ensureBranch(owner: string, repo: string, branchName: string, tag
100106async function findExisting ( owner : string , repo : string , branch : string ) {
101107 const fullRepo = `${ owner } /${ repo } ` ;
102108 const query = `type:pr is:open repo:${ fullRepo } base:${ base } head:${ branch } sort:updated` ;
103- const result = await getOctokit ( ) . rest . search . issuesAndPullRequests ( { q : query } ) ;
109+ const result = await getOctokit ( getEnv ( 'GITHUB_WRITE_TOKEN' ) ) . rest . search . issuesAndPullRequests ( { q : query } ) ;
104110
105111 for ( const item of result . data . items ) {
106112 // Must be an open item, and that item must be a pull request.
107113 if ( item . state !== 'open' || ! item . pull_request ) {
108114 continue ;
109115 }
110- const { data : pr } = await getOctokit ( ) . rest . pulls . get ( {
116+ const { data : pr } = await getOctokit ( getEnv ( 'GITHUB_PR_TOKEN' ) ) . rest . pulls . get ( {
111117 owner, repo, pull_number : item . number ,
112118 } ) ;
113119
@@ -169,7 +175,7 @@ async function findExisting(owner: string, repo: string, branch: string) {
169175 console . log ( `Creating new PR on ${ owner } /${ repo } : ${ base } <- ${ branchName } ` ) ;
170176 await ensureBranch ( owner , repo , branchName , tagName ) ;
171177 const title = `Merge release ${ tagName } back into ${ base } ` ;
172- const { data : item } = await getOctokit ( ) . rest . pulls . create ( {
178+ const { data : item } = await getOctokit ( getEnv ( 'GITHUB_PR_TOKEN' ) ) . rest . pulls . create ( {
173179 owner, repo, title, head : branchName , base, maintainer_can_modify : true ,
174180 } ) ;
175181
0 commit comments