@@ -215,102 +215,104 @@ export class Runner {
215215 return false ; // did not move state, return false
216216 }
217217
218- // Try to merge PR
219- try {
220- // skip CI if there is a dependent PR that is awaiting merge
221- const skipCI =
222- dependentsAwaitingMerge . length > 0 &&
223- this . config . mergeSettings &&
224- this . config . mergeSettings . skipBuildOnDependentsAwaitingMerge ;
225- const pullRequestId = landRequest . pullRequestId ;
226- Logger . verbose ( 'Attempting merge pull request' , {
227- namespace : 'lib:runner:moveFromAwaitingMerge' ,
228- pullRequestId,
229- landRequestId : landRequest . id ,
230- lockId,
231- } ) ;
232- await this . client . mergePullRequest ( landRequestStatus , { skipCI } ) ;
233- Logger . info ( 'Successfully merged PR' , {
234- namespace : 'lib:runner:moveFromAwaitingMerge' ,
235- landRequestId : landRequest . id ,
236- pullRequestId,
237- lockId,
238- } ) ;
218+ Logger . info ( 'Triggering merge attempt' , {
219+ namespace : 'lib:runner:moveFromAwaitingMerge' ,
220+ pullRequestId : landRequest . pullRequestId ,
221+ landRequestId : landRequest . id ,
222+ lockId,
223+ } ) ;
239224
240- const end = Date . now ( ) ;
241- const queuedDate = await this . getLandRequestQueuedDate ( landRequest . id ) ;
242- const start = queuedDate ! . getTime ( ) ;
243- eventEmitter . emit ( 'PULL_REQUEST.MERGE.SUCCESS' , {
244- landRequestId : landRequestStatus . requestId ,
245- pullRequestId : landRequest . pullRequestId ,
246- commit : landRequest . forCommit ,
247- sourceBranch : pullRequest . sourceBranch ,
248- targetBranch : pullRequest . targetBranch ,
249- duration : end - start ,
250- } ) ;
251- return landRequest . setStatus ( 'success' ) ;
252- } catch ( err ) {
253- eventEmitter . emit ( 'PULL_REQUEST.MERGE.FAIL' , {
254- landRequestId : landRequestStatus . requestId ,
255- pullRequestId : landRequest . pullRequestId ,
256- commit : landRequest . forCommit ,
257- sourceBranch : pullRequest . sourceBranch ,
258- targetBranch : pullRequest . targetBranch ,
225+ // Start merge attempt
226+ await landRequest . setStatus ( 'merging' ) ;
227+
228+ // skip CI if there is a dependent PR that is awaiting merge
229+ const skipCI =
230+ dependentsAwaitingMerge . length > 0 &&
231+ this . config . mergeSettings &&
232+ this . config . mergeSettings . skipBuildOnDependentsAwaitingMerge ;
233+
234+ this . client
235+ . mergePullRequest ( landRequestStatus , { skipCI } )
236+ . then ( async ( ) => {
237+ const end = Date . now ( ) ;
238+ const queuedDate = await this . getLandRequestQueuedDate ( landRequest . id ) ;
239+ const start = queuedDate ! . getTime ( ) ;
240+ eventEmitter . emit ( 'PULL_REQUEST.MERGE.SUCCESS' , {
241+ landRequestId : landRequestStatus . requestId ,
242+ pullRequestId : landRequest . pullRequestId ,
243+ commit : landRequest . forCommit ,
244+ sourceBranch : pullRequest . sourceBranch ,
245+ targetBranch : pullRequest . targetBranch ,
246+ duration : end - start ,
247+ } ) ;
248+ await landRequest . setStatus ( 'success' ) ;
249+ } )
250+ . catch ( async ( ) => {
251+ eventEmitter . emit ( 'PULL_REQUEST.MERGE.FAIL' , {
252+ landRequestId : landRequestStatus . requestId ,
253+ pullRequestId : landRequest . pullRequestId ,
254+ commit : landRequest . forCommit ,
255+ sourceBranch : pullRequest . sourceBranch ,
256+ targetBranch : pullRequest . targetBranch ,
257+ } ) ;
258+ await landRequest . setStatus ( 'fail' , 'Unable to merge pull request' ) ;
259259 } ) ;
260- return landRequest . setStatus ( 'fail' , 'Unable to merge pull request' ) ;
261- }
262260 } ;
263261
264262 // Next must always return early if ever doing a single state transition
265263 next = async ( ) => {
266- const runNextAgain = await withLock ( 'status-transition' , async ( lockId : Date ) => {
267- const queue = await this . queue . getQueue ( ) ;
268- Logger . info ( 'Next() called' , {
269- namespace : 'lib:runner:next' ,
270- lockId,
271- queue,
272- } ) ;
264+ const runNextAgain = await withLock (
265+ 'status-transition' ,
266+ async ( lockId : Date ) => {
267+ const queue = await this . queue . getQueue ( ) ;
268+ Logger . info ( 'Next() called' , {
269+ namespace : 'lib:runner:next' ,
270+ lockId,
271+ queue,
272+ } ) ;
273273
274- for ( const landRequestStatus of queue ) {
275- // Check for this _before_ looking at the state so that we don't have to wait until
276- const landRequest = landRequestStatus . request ;
277- const failedDeps = await landRequest . getFailedDependencies ( ) ;
278- if ( failedDeps . length !== 0 ) {
279- Logger . info ( 'LandRequest failed due to failing dependency' , {
280- namespace : 'lib:runner:next' ,
281- lockId,
282- landRequestId : landRequest . id ,
283- pullRequestId : landRequest . pullRequestId ,
284- landRequestStatus,
285- failedDeps,
286- } ) ;
287- const failedPrIds = failedDeps . map ( d => d . request . pullRequestId ) . join ( ', ' ) ;
288- const failReason = `Failed due to failed dependency builds: ${ failedPrIds } ` ;
289- await landRequest . setStatus ( 'fail' , failReason ) ;
290- await landRequest . update ( { dependsOn : null } ) ;
291- await this . client . stopLandBuild ( landRequest . buildId , lockId ) ;
292- const user = await this . client . getUser ( landRequest . triggererAaid ) ;
293- return landRequest . setStatus ( 'queued' , `Queued by ${ user . displayName || user . aaid } ` ) ;
294- }
295- if ( landRequestStatus . state === 'awaiting-merge' ) {
296- const awaitingMergeQueue = Runner . getDependentsAwaitingMerge ( queue , landRequestStatus ) ;
297- const didChangeState = await this . moveFromAwaitingMerge (
298- landRequestStatus ,
299- lockId ,
300- awaitingMergeQueue ,
301- ) ;
302- // if we moved, we need to exit early, otherwise, just keep checking the queue
303- if ( didChangeState ) return true ;
304- } else if ( landRequestStatus . state === 'queued' ) {
305- const didChangeState = await this . moveFromQueueToRunning ( landRequestStatus , lockId ) ;
306- // if the landrequest was able to move from queued to running, exit early, otherwise, keep
307- // checking the rest of the queue
308- if ( didChangeState ) return true ;
274+ for ( const landRequestStatus of queue ) {
275+ // Check for this _before_ looking at the state so that we don't have to wait until
276+ const landRequest = landRequestStatus . request ;
277+ const failedDeps = await landRequest . getFailedDependencies ( ) ;
278+ if ( failedDeps . length !== 0 ) {
279+ Logger . info ( 'LandRequest failed due to failing dependency' , {
280+ namespace : 'lib:runner:next' ,
281+ lockId,
282+ landRequestId : landRequest . id ,
283+ pullRequestId : landRequest . pullRequestId ,
284+ landRequestStatus,
285+ failedDeps,
286+ } ) ;
287+ const failedPrIds = failedDeps . map ( d => d . request . pullRequestId ) . join ( ', ' ) ;
288+ const failReason = `Failed due to failed dependency builds: ${ failedPrIds } ` ;
289+ await landRequest . setStatus ( 'fail' , failReason ) ;
290+ await landRequest . update ( { dependsOn : null } ) ;
291+ await this . client . stopLandBuild ( landRequest . buildId , lockId ) ;
292+ const user = await this . client . getUser ( landRequest . triggererAaid ) ;
293+ return landRequest . setStatus ( 'queued' , `Queued by ${ user . displayName || user . aaid } ` ) ;
294+ }
295+ if ( landRequestStatus . state === 'awaiting-merge' ) {
296+ const awaitingMergeQueue = Runner . getDependentsAwaitingMerge ( queue , landRequestStatus ) ;
297+ const didChangeState = await this . moveFromAwaitingMerge (
298+ landRequestStatus ,
299+ lockId ,
300+ awaitingMergeQueue ,
301+ ) ;
302+ // if we moved, we need to exit early, otherwise, just keep checking the queue
303+ if ( didChangeState ) return true ;
304+ } else if ( landRequestStatus . state === 'queued' ) {
305+ const didChangeState = await this . moveFromQueueToRunning ( landRequestStatus , lockId ) ;
306+ // if the landrequest was able to move from queued to running, exit early, otherwise, keep
307+ // checking the rest of the queue
308+ if ( didChangeState ) return true ;
309+ }
310+ // otherwise, we must just be running, nothing to do here
309311 }
310- // otherwise, we must just be running, nothing to do here
311- }
312- return false ;
313- } ) ;
312+ return false ;
313+ } ,
314+ false ,
315+ ) ;
314316 if ( runNextAgain ) {
315317 await this . next ( ) ;
316318 }
@@ -522,42 +524,46 @@ export class Runner {
522524 } ;
523525
524526 checkWaitingLandRequests = async ( ) => {
525- await withLock ( 'status-transition' , async ( ) => {
526- const waitingRequestStatuses = await this . queue . getStatusesForWaitingRequests ( ) ;
527+ await withLock (
528+ 'status-transition' ,
529+ async ( ) => {
530+ const waitingRequestStatuses = await this . queue . getStatusesForWaitingRequests ( ) ;
531+
532+ Logger . info ( 'Checking for waiting landrequests ready to queue' , {
533+ namespace : 'lib:runner:checkWaitingLandRequests' ,
534+ waitingRequestStatuses,
535+ } ) ;
527536
528- Logger . info ( 'Checking for waiting landrequests ready to queue' , {
529- namespace : 'lib:runner:checkWaitingLandRequests' ,
530- waitingRequestStatuses,
531- } ) ;
537+ for ( const landRequestStatus of waitingRequestStatuses ) {
538+ const landRequest = landRequestStatus . request ;
539+ const pullRequestId = landRequest . pullRequestId ;
540+ const triggererUserMode = await permissionService . getPermissionForUser (
541+ landRequest . triggererAaid ,
542+ ) ;
543+ const isAllowedToLand = await this . isAllowedToLand (
544+ pullRequestId ,
545+ triggererUserMode ,
546+ this . getQueue ,
547+ ) ;
532548
533- for ( const landRequestStatus of waitingRequestStatuses ) {
534- const landRequest = landRequestStatus . request ;
535- const pullRequestId = landRequest . pullRequestId ;
536- const triggererUserMode = await permissionService . getPermissionForUser (
537- landRequest . triggererAaid ,
538- ) ;
539- const isAllowedToLand = await this . isAllowedToLand (
540- pullRequestId ,
541- triggererUserMode ,
542- this . getQueue ,
543- ) ;
544-
545- if ( isAllowedToLand . errors . length === 0 ) {
546- if ( isAllowedToLand . existingRequest ) {
547- Logger . warn ( 'Already has existing Land build' , {
548- pullRequestId,
549- landRequestId : landRequest . id ,
550- landRequestStatus,
551- namespace : 'lib:runner:checkWaitingLandRequests' ,
552- } ) ;
553- await landRequest . setStatus ( 'aborted' , 'Already has existing Land build' ) ;
554- continue ;
549+ if ( isAllowedToLand . errors . length === 0 ) {
550+ if ( isAllowedToLand . existingRequest ) {
551+ Logger . warn ( 'Already has existing Land build' , {
552+ pullRequestId,
553+ landRequestId : landRequest . id ,
554+ landRequestStatus,
555+ namespace : 'lib:runner:checkWaitingLandRequests' ,
556+ } ) ;
557+ await landRequest . setStatus ( 'aborted' , 'Already has existing Land build' ) ;
558+ continue ;
559+ }
560+ const movedState = await this . moveFromWaitingToQueued ( landRequestStatus ) ;
561+ if ( movedState ) return this . next ( ) ;
555562 }
556- const movedState = await this . moveFromWaitingToQueued ( landRequestStatus ) ;
557- if ( movedState ) return this . next ( ) ;
558563 }
559- }
560- } ) ;
564+ } ,
565+ undefined ,
566+ ) ;
561567 } ;
562568
563569 getStatusesForLandRequests = async (
0 commit comments