@@ -2,10 +2,13 @@ import core from '@actions/core';
22import github from '@actions/github' ;
33import type { PullRequestEvent , PushEvent } from '@octokit/webhooks-types' ;
44import { isString } from '@sequelize/utils' ;
5- import { execFileSync } from 'node:child_process' ;
5+ import childProcess from 'node:child_process' ;
66import fs from 'node:fs/promises' ;
77import * as path from 'node:path' ;
88import { setTimeout } from 'node:timers/promises' ;
9+ import { promisify } from 'node:util' ;
10+
11+ const execFile = promisify ( childProcess . execFile ) ;
912
1013isString . assert ( process . env . GITHUB_TOKEN , 'GITHUB_TOKEN env must be provided' ) ;
1114
@@ -32,6 +35,10 @@ const updateBranchBot = process.env.UPDATE_BRANCH_PAT
3235 * We recommend using a user PAT for this, as:
3336 * - Using the GITHUB_TOKEN will not trigger subsequent workflows.
3437 * - Using a bot PAT will cause an error if the branch update includes a workflow file.
38+ *
39+ * This will need the following permissions:
40+ * - contents (read & write)
41+ * - workflows (read & write)
3542 */
3643const updateForkPat = process . env . UPDATE_FORK_PAT || process . env . GITHUB_TOKEN ;
3744const updateForkUsername = process . env . UPDATE_FORK_USERNAME || 'x-access-token' ;
@@ -73,7 +80,7 @@ const updateExcludedAuthors = getCommaSeparatedInput('update-excluded-authors');
7380const updateRequiresSource = getEnumInput ( 'update-requires-source' , [
7481 'all' ,
7582 'fork' ,
76- 'branches ' ,
83+ 'branch ' ,
7784] as const ) ;
7885
7986interface RepositoryId {
@@ -305,22 +312,24 @@ async function updatePrBranch(repositoryId: RepositoryId, pullRequest: PullReque
305312
306313 updatedPrs . push ( pullRequest . number ) ;
307314
308- console . info ( `[PR ${ pullRequest . number } ] ✅ Updating branch.` ) ;
309-
310315 if ( dryRun ) {
311316 return ;
312317 }
313318
314319 // The "update-branch" endpoint does not allow modifying pull requests from repositories we do not own,
315320 // even if the "allow maintainers to modify" setting is enabled on the PR.
316321 if ( ! isForkPr ( pullRequest ) ) {
322+ console . info ( `[PR ${ pullRequest . number } ] ✅ Updating branch via API.` ) ;
323+
317324 // This operation cannot be done with GITHUB_TOKEN, as the GITHUB_TOKEN does not trigger subsequent workflows.
318325 return updateBranchBot . rest . pulls . updateBranch ( {
319326 ...repositoryId ,
320327 pull_number : pullRequest . number ,
321328 } ) ;
322329 }
323330
331+ console . info ( `[PR ${ pullRequest . number } ] ✅ Updating fork via git.` ) ;
332+
324333 // For fork PRs, we use git directly instead:
325334 // - Clone the repository in a new directory
326335 // - Merge the base branch into the PR branch
@@ -333,36 +342,60 @@ async function updatePrBranch(repositoryId: RepositoryId, pullRequest: PullReque
333342 const parentRepositoryUrl = `https://${ updateForkUsername } :${ updateForkPat } @github.com/${ pullRequest . baseRepository . nameWithOwner } .git` ;
334343
335344 // clone fork repository in the correct branch
336- console . log (
337- `git clone ${ forkRepositoryUrl } ${ targetDirectoryName } --branch ${ pullRequest . headRef . name } ` ,
338- ) ;
339- execFileSync (
340- 'git' ,
341- [ 'clone' , forkRepositoryUrl , targetDirectoryName , '--branch' , pullRequest . headRef . name ] ,
342- {
343- stdio : 'inherit' ,
344- } ,
345- ) ;
345+ {
346+ const { stdout, stderr } = await execFile ( 'git' , [
347+ 'clone' ,
348+ '--quiet' ,
349+ forkRepositoryUrl ,
350+ targetDirectoryName ,
351+ '--branch' ,
352+ pullRequest . headRef . name ,
353+ ] ) ;
354+
355+ stdout && console . info ( `[PR ${ pullRequest . number } ] ${ stdout } ` ) ;
356+ stderr && console . error ( `[PR ${ pullRequest . number } ] ${ stderr } ` ) ;
357+ }
346358
347359 // add parent repository as remote
348- console . log ( `git remote add parent ${ parentRepositoryUrl } ` ) ;
349- execFileSync ( 'git' , [ 'remote' , 'add' , 'parent' , parentRepositoryUrl ] , {
350- cwd : targetDirectoryPath ,
351- stdio : 'inherit' ,
352- } ) ;
360+ {
361+ const { stdout, stderr } = await execFile (
362+ 'git' ,
363+ [ 'remote' , 'add' , 'parent' , parentRepositoryUrl ] ,
364+ {
365+ cwd : targetDirectoryPath ,
366+ } ,
367+ ) ;
368+
369+ stdout && console . info ( `[PR ${ pullRequest . number } ] ${ stdout } ` ) ;
370+ stderr && console . error ( `[PR ${ pullRequest . number } ] ${ stderr } ` ) ;
371+ }
353372
354373 // merge parent branch in local branch
355- console . log ( `git pull parent ${ pullRequest . baseRef . name } --no-edit --no-rebase` ) ;
356- execFileSync ( 'git' , [ 'pull' , 'parent' , pullRequest . baseRef . name , '--no-edit' , '--no-rebase' ] , {
357- cwd : targetDirectoryPath ,
358- stdio : 'inherit' ,
359- } ) ;
360-
361- console . log ( `git push origin ${ pullRequest . headRef . name } ` ) ;
362- execFileSync ( 'git' , [ 'push' , 'origin' , pullRequest . headRef . name ] , {
363- cwd : targetDirectoryPath ,
364- stdio : 'inherit' ,
365- } ) ;
374+ {
375+ const { stdout, stderr } = await execFile (
376+ 'git' ,
377+ [ 'pull' , '--quiet' , 'parent' , pullRequest . baseRef . name , '--no-edit' , '--no-rebase' ] ,
378+ {
379+ cwd : targetDirectoryPath ,
380+ } ,
381+ ) ;
382+
383+ stdout && console . info ( `[PR ${ pullRequest . number } ] ${ stdout } ` ) ;
384+ stderr && console . error ( `[PR ${ pullRequest . number } ] ${ stderr } ` ) ;
385+ }
386+
387+ {
388+ const { stdout, stderr } = await execFile (
389+ 'git' ,
390+ [ 'push' , '--quiet' , 'origin' , pullRequest . headRef . name ] ,
391+ {
392+ cwd : targetDirectoryPath ,
393+ } ,
394+ ) ;
395+
396+ stdout && console . info ( `[PR ${ pullRequest . number } ] ${ stdout } ` ) ;
397+ stderr && console . error ( `[PR ${ pullRequest . number } ] ${ stderr } ` ) ;
398+ }
366399}
367400
368401async function handleConflict ( repositoryId : RepositoryId , pullRequest : PullRequest ) : Promise < void > {
@@ -539,7 +572,7 @@ function prMatchesSource(pullRequest: PullRequest, source: typeof updateRequires
539572 case 'fork' :
540573 return isForkPr ( pullRequest ) ;
541574
542- case 'branches ' :
575+ case 'branch ' :
543576 return ! isForkPr ( pullRequest ) ;
544577 }
545578}
0 commit comments