@@ -461,22 +461,34 @@ function waitForPRMerge(repoDir, repo, headBranch, baseBranch, dryRun, options =
461461 }
462462
463463 try {
464- // Find the PR number for this head branch
464+ // Find the PR number for this head branch. Right after gh pr create,
465+ // list/search APIs can lag for a few seconds.
465466 const prQuery = `gh pr list --repo ${ slug } --head ${ headBranch } --base ${ baseBranch } --state open --json number --jq '.[0].number'` ;
467+ const prDiscoveryTimeoutMs = options . prDiscoveryTimeoutMs || 2 * 60 * 1000 ;
468+ const prDiscoveryPollIntervalMs = options . prDiscoveryPollIntervalMs || 5000 ;
469+ const prDiscoveryStart = Date . now ( ) ;
466470 let prNumber ;
467- try {
468- prNumber = parseInt ( runQuiet ( prQuery , repoDir ) , 10 ) ;
469- } catch ( err ) {
470- if ( required ) {
471- throw new Error ( `No open PR found for ${ headBranch } -> ${ baseBranch } ` ) ;
471+
472+ while ( ( Date . now ( ) - prDiscoveryStart ) < prDiscoveryTimeoutMs ) {
473+ try {
474+ const rawNumber = String ( runQuiet ( prQuery , repoDir ) || '' ) . trim ( ) ;
475+ prNumber = parseInt ( rawNumber , 10 ) ;
476+ } catch ( err ) {
477+ prNumber = NaN ;
472478 }
473- console . log ( `No open PR found for ${ headBranch } -> ${ baseBranch } ` ) ;
474- return { status : 'skip' , reason : 'no-pr' } ;
479+
480+ if ( prNumber ) {
481+ break ;
482+ }
483+
484+ const waitedSeconds = Math . floor ( ( Date . now ( ) - prDiscoveryStart ) / 1000 ) ;
485+ console . log ( ` Waiting for PR to appear for ${ headBranch } -> ${ baseBranch } (${ waitedSeconds } s elapsed)...` ) ;
486+ sleepMs ( prDiscoveryPollIntervalMs ) ;
475487 }
476488
477489 if ( ! prNumber ) {
478490 if ( required ) {
479- throw new Error ( `No open PR found for ${ headBranch } -> ${ baseBranch } ` ) ;
491+ throw new Error ( `No open PR found for ${ headBranch } -> ${ baseBranch } after waiting ${ Math . floor ( prDiscoveryTimeoutMs / 1000 ) } s ` ) ;
480492 }
481493 console . log ( `No open PR found for ${ headBranch } -> ${ baseBranch } ` ) ;
482494 return { status : 'skip' , reason : 'no-pr' } ;
0 commit comments