@@ -395,7 +395,6 @@ func (g *GitlabClient) SupportsDetailedMergeStatus(logger logging.SimpleLogging)
395
395
396
396
// UpdateStatus updates the build status of a commit.
397
397
func (g * GitlabClient ) UpdateStatus (logger logging.SimpleLogging , repo models.Repo , pull models.PullRequest , state models.CommitStatus , src string , description string , url string ) error {
398
- logger .Debug ("Updating GitLab commit status for '%s' to '%s'" , src , state )
399
398
gitlabState := gitlab .Pending
400
399
switch state {
401
400
case models .PendingCommitStatus :
@@ -406,43 +405,47 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
406
405
gitlabState = gitlab .Success
407
406
}
408
407
409
- // refTarget is only set to the head branch of the MR if HeadPipeline is not found
410
- // when HeadPipeline is found we set the pipelineID for the request instead
411
- var refTarget * string
412
- var pipelineID * int
408
+ logger .Info ("Updating GitLab commit status for '%s' to '%s'" , src , gitlabState )
409
+
410
+ setCommitStatusOptions := & gitlab.SetCommitStatusOptions {
411
+ State : gitlabState ,
412
+ Context : gitlab .Ptr (src ),
413
+ Description : gitlab .Ptr (description ),
414
+ TargetURL : & url ,
415
+ }
413
416
414
417
retries := 1
415
418
delay := 2 * time .Second
416
- var mr * gitlab.MergeRequest
419
+ var commit * gitlab.Commit
420
+ var resp * gitlab.Response
417
421
var err error
418
422
419
- // Try to get the MR details a couple of times in case the pipeline is not yet assigned to the MR
423
+ // Try a couple of times to get the pipeline ID for the commit
420
424
for i := 0 ; i <= retries ; i ++ {
421
- mr , err = g .GetMergeRequest (logger , pull .BaseRepo .FullName , pull .Num )
425
+ commit , resp , err = g .Client .Commits .GetCommit (repo .FullName , pull .HeadCommit , nil )
426
+ if resp != nil {
427
+ logger .Debug ("GET /projects/%s/repository/commits/%d: %d" , pull .BaseRepo .ID (), pull .HeadCommit , resp .StatusCode )
428
+ }
422
429
if err != nil {
423
430
return err
424
431
}
425
- if mr .HeadPipeline != nil {
426
- logger .Debug ("Head pipeline found for merge request %d, source '%s'. refTarget '%s'" ,
427
- pull .Num , mr .HeadPipeline .Source , mr .HeadPipeline .Ref )
428
- // set pipeline ID for the req once found
429
- pipelineID = gitlab .Ptr (mr .HeadPipeline .ID )
432
+ if commit .LastPipeline != nil {
433
+ logger .Info ("Pipeline found for commit %s, setting pipeline ID to %d" , pull .HeadCommit , commit .LastPipeline .ID )
434
+ // Set the pipeline ID to the last pipeline that ran for the commit
435
+ setCommitStatusOptions .PipelineID = gitlab .Ptr (commit .LastPipeline .ID )
430
436
break
431
437
}
432
438
if i != retries {
433
- logger .Debug ("Head pipeline not found for merge request %d. Retrying in %s" ,
434
- pull .Num , delay )
439
+ logger .Info ("No pipeline found for commit %s, retrying in %s" , pull .HeadCommit , delay )
435
440
time .Sleep (delay )
436
441
} else {
437
- // set the ref target here if the pipeline wasn't found
438
- refTarget = gitlab .Ptr (pull .HeadBranch )
439
- logger .Debug ("Head pipeline not found for merge request %d." ,
440
- pull .Num )
442
+ // If we've exhausted all retries, set the Ref to the branch name
443
+ logger .Info ("No pipeline found for commit %s, setting Ref to %s" , pull .HeadCommit , pull .HeadBranch )
444
+ setCommitStatusOptions .Ref = gitlab .Ptr (pull .HeadBranch )
441
445
}
442
446
}
443
447
444
448
var (
445
- resp * gitlab.Response
446
449
maxAttempts = 10
447
450
retryer = & backoff.Backoff {
448
451
Jitter : true ,
@@ -455,19 +458,11 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
455
458
"attempt" , i + 1 ,
456
459
"max_attempts" , maxAttempts ,
457
460
"repo" , repo .FullName ,
458
- "commit" , pull . HeadCommit ,
461
+ "commit" , commit . ShortID ,
459
462
"state" , state .String (),
460
463
)
461
464
462
- _ , resp , err = g .Client .Commits .SetCommitStatus (repo .FullName , pull .HeadCommit , & gitlab.SetCommitStatusOptions {
463
- State : gitlabState ,
464
- Context : gitlab .Ptr (src ),
465
- Description : gitlab .Ptr (description ),
466
- TargetURL : & url ,
467
- // only one of these should get sent in the request
468
- PipelineID : pipelineID ,
469
- Ref : refTarget ,
470
- })
465
+ _ , resp , err = g .Client .Commits .SetCommitStatus (repo .FullName , pull .HeadCommit , setCommitStatusOptions )
471
466
472
467
if resp != nil {
473
468
logger .Debug ("POST /projects/%s/statuses/%s returned: %d" , repo .FullName , pull .HeadCommit , resp .StatusCode )
0 commit comments