mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-08-03 15:08:49 +00:00
fix: Update GitLab Pipeline Type Detection to use Head Pipeline Property (#3887)
* Fix GitLab Mulitple Pipelines * Add logger to tests and fix test ref * Add retry to GetMergeRequest * Update retries --------- Co-authored-by: PePe Amengual <jose.amengual@gmail.com>
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user