@@ -36,10 +36,6 @@ const (
3636 gitGetProjectAttribute = "evergreen.command.git_get_project"
3737
3838 generatedTokenKey = "EVERGREEN_GENERATED_GITHUB_TOKEN"
39-
40- // githubMergeQueueInvalidRefError is the error message returned by Git when it fails to find
41- // a GitHub merge queue reference.
42- githubMergeQueueInvalidRefError = "couldn't find remote ref gh-readonly-queue"
4339)
4440
4541var (
@@ -74,6 +70,8 @@ type gitFetchProject struct {
7470
7571 CommitterEmail string `mapstructure:"committer_email"`
7672
73+ refNotFound bool
74+
7775 base
7876}
7977
@@ -360,6 +358,22 @@ func (c *gitFetchProject) fetchSource(ctx context.Context, logger client.LoggerP
360358 attempt := 0
361359 return c .retryFetch (ctx , logger , comm , conf , true , opts , func (opts cloneOpts ) error {
362360 attempt ++
361+ // On the second attempt, check if the merge queue ref was deleted before retrying.
362+ if attempt == 2 && conf .Task .Requester == evergreen .GithubMergeRequester && conf .GithubMergeData .HeadBranch != "" {
363+ ref := "heads/" + conf .GithubMergeData .HeadBranch
364+ appToken , tokenErr := comm .CreateInstallationTokenForClone (ctx , conf .TaskData (), opts .owner , opts .repo )
365+ if tokenErr == nil && appToken != "" {
366+ exists , checkErr := thirdparty .MergeQueueRefExists (ctx , opts .owner , opts .repo , ref , appToken )
367+ if checkErr != nil {
368+ return errors .Wrap (checkErr , "checking if merge queue ref exists" )
369+ }
370+ if ! exists {
371+ c .refNotFound = true
372+ return errors .New ("the GitHub merge SHA is not available most likely because the merge completed or was aborted" )
373+ }
374+ }
375+ }
376+
363377 gitCommands , err := c .buildSourceCloneCommand (conf , opts )
364378 if err != nil {
365379 return err
@@ -389,9 +403,10 @@ func (c *gitFetchProject) fetchSource(ctx context.Context, logger client.LoggerP
389403
390404 if err = fetchSourceCmd .Run (ctx ); err != nil {
391405 span .SetAttributes (attribute .String (cloneErrorAttribute , err .Error ()))
406+ return err
392407 }
393408
394- return err
409+ return nil
395410 })
396411}
397412
@@ -410,6 +425,9 @@ func (c *gitFetchProject) retryFetch(ctx context.Context, logger client.LoggerPr
410425 return utility .Retry (
411426 ctx ,
412427 func () (bool , error ) {
428+ if isSource {
429+ c .refNotFound = false
430+ }
413431 if attemptNum > 2 {
414432 opts .useVerbose = true // use verbose for the last 2 attempts
415433 logger .Task ().Error (message.Fields {
@@ -423,7 +441,7 @@ func (c *gitFetchProject) retryFetch(ctx context.Context, logger client.LoggerPr
423441 if isSource && attemptNum == 1 {
424442 logger .Task ().Warning ("git source clone failed with cached merge SHA; re-requesting merge SHA from GitHub" )
425443 }
426- if strings . Contains ( err . Error (), githubMergeQueueInvalidRefError ) {
444+ if isSource && c . refNotFound {
427445 if markErr := comm .MarkMergeQueueGitRefNotFound (ctx , conf .TaskData ()); markErr != nil {
428446 logger .Task ().Warningf ("Failed to mark git ref not found: %s" , markErr )
429447 }
0 commit comments