Skip to content

Commit

Permalink
fix: Update GitLab Pipeline Type Detection to use Head Pipeline Prope…
Browse files Browse the repository at this point in the history
…rty (runatlantis#3887)

* Fix GitLab Mulitple Pipelines

* Add logger to tests and fix test ref

* Add retry to GetMergeRequest

* Update retries

---------

Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
2 people authored and ijames-gc committed Feb 13, 2024
1 parent 6f38eff commit 2c52a15
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
39 changes: 29 additions & 10 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,38 @@ func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, s
gitlabState = gitlab.Success
}

mr, err := g.GetMergeRequest(pull.BaseRepo.FullName, pull.Num)
if err != nil {
return err
}
// refTarget is set to current branch if no pipeline is assigned to the commit,
// otherwise it is set to the pipeline created by the merge_request_event rule
// refTarget is set to the head pipeline of the MR if it exists, or else it is set to the head branch
// of the MR. This is needed because the commit status is only shown in the MR if the pipeline is
// assigned to an MR reference.
// Try to get the MR details a couple of times in case the pipeline is not yet assigned to the MR
refTarget := pull.HeadBranch
if mr.Pipeline != nil {
switch mr.Pipeline.Source {
case "merge_request_event":
refTarget = fmt.Sprintf("refs/merge-requests/%d/head", pull.Num)

retries := 1
delay := 2 * time.Second
var mr *gitlab.MergeRequest
var err error

for i := 0; i <= retries; i++ {
mr, err = g.GetMergeRequest(pull.BaseRepo.FullName, pull.Num)
if err != nil {
return err
}
if mr.HeadPipeline != nil {
g.logger.Debug("Head pipeline found for merge request %d, source '%s'. refTarget '%s'",
pull.Num, mr.HeadPipeline.Source, mr.HeadPipeline.Ref)
refTarget = mr.HeadPipeline.Ref
break
}
if i != retries {
g.logger.Debug("Head pipeline not found for merge request %d, source '%s'. Retrying in %s",
pull.Num, mr.HeadPipeline.Source, delay)
time.Sleep(delay)
} else {
g.logger.Debug("Head pipeline not found for merge request %d, source '%s'.",
pull.Num, mr.HeadPipeline.Source)
}
}

_, resp, err := g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, &gitlab.SetCommitStatusOptions{
State: gitlabState,
Context: gitlab.String(src),
Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {

body, err := io.ReadAll(r.Body)
Ok(t, err)
exp := fmt.Sprintf(`{"state":"%s","ref":"test","context":"src","target_url":"https://google.com","description":"description"}`, c.expState)
exp := fmt.Sprintf(`{"state":"%s","ref":"patch-1-merger","context":"src","target_url":"https://google.com","description":"description"}`, c.expState)
Equals(t, exp, string(body))
defer r.Body.Close() // nolint: errcheck
w.Write([]byte("{}")) // nolint: errcheck
Expand Down

0 comments on commit 2c52a15

Please sign in to comment.