Skip to content

Commit 8c6541f

Browse files
authored
fix: Change GitLab UpdateStatus Function to get Pipeline from Commit rather than the Merge Request (#5033)
Signed-off-by: X-Guardian <[email protected]>
1 parent d45f07b commit 8c6541f

File tree

2 files changed

+263
-53
lines changed

2 files changed

+263
-53
lines changed

server/events/vcs/gitlab_client.go

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ func (g *GitlabClient) SupportsDetailedMergeStatus(logger logging.SimpleLogging)
395395

396396
// UpdateStatus updates the build status of a commit.
397397
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)
399398
gitlabState := gitlab.Pending
400399
switch state {
401400
case models.PendingCommitStatus:
@@ -406,43 +405,47 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
406405
gitlabState = gitlab.Success
407406
}
408407

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+
}
413416

414417
retries := 1
415418
delay := 2 * time.Second
416-
var mr *gitlab.MergeRequest
419+
var commit *gitlab.Commit
420+
var resp *gitlab.Response
417421
var err error
418422

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
420424
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+
}
422429
if err != nil {
423430
return err
424431
}
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)
430436
break
431437
}
432438
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)
435440
time.Sleep(delay)
436441
} 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)
441445
}
442446
}
443447

444448
var (
445-
resp *gitlab.Response
446449
maxAttempts = 10
447450
retryer = &backoff.Backoff{
448451
Jitter: true,
@@ -455,19 +458,11 @@ func (g *GitlabClient) UpdateStatus(logger logging.SimpleLogging, repo models.Re
455458
"attempt", i+1,
456459
"max_attempts", maxAttempts,
457460
"repo", repo.FullName,
458-
"commit", pull.HeadCommit,
461+
"commit", commit.ShortID,
459462
"state", state.String(),
460463
)
461464

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)
471466

472467
if resp != nil {
473468
logger.Debug("POST /projects/%s/statuses/%s returned: %d", repo.FullName, pull.HeadCommit, resp.StatusCode)

0 commit comments

Comments
 (0)