mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-07-28 23:58:19 +00:00
fix(gitlab_client): prevent duplicate pipelines in gitlab merge requests (#2745)
* fix(gitlab_client): change CommitStatus update logic to prevent duplicated pipelines in MRs * chore: refactor refTarget logic * chore: fix tests * chore: fix lint * chore: remove nested if
This commit is contained in:
committed by
GitHub
parent
b6e1c61127
commit
c800f706e4
@@ -244,12 +244,26 @@ func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, s
|
||||
case models.SuccessCommitStatus:
|
||||
gitlabState = gitlab.Success
|
||||
}
|
||||
_, _, err := g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, &gitlab.SetCommitStatusOptions{
|
||||
|
||||
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 := pull.HeadBranch
|
||||
if mr.Pipeline != nil {
|
||||
switch mr.Pipeline.Source {
|
||||
case "merge_request_event":
|
||||
refTarget = fmt.Sprintf("refs/merge-requests/%d/head", pull.Num)
|
||||
}
|
||||
}
|
||||
_, _, err = g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, &gitlab.SetCommitStatusOptions{
|
||||
State: gitlabState,
|
||||
Context: gitlab.String(src),
|
||||
Description: gitlab.String(description),
|
||||
TargetURL: &url,
|
||||
Ref: gitlab.String(pull.HeadBranch),
|
||||
Ref: gitlab.String(refTarget),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -216,6 +216,9 @@ func TestGitlabClient_UpdateStatus(t *testing.T) {
|
||||
Equals(t, exp, string(body))
|
||||
defer r.Body.Close() // nolint: errcheck
|
||||
w.Write([]byte("{}")) // nolint: errcheck
|
||||
case "/api/v4/projects/runatlantis%2Fatlantis/merge_requests/1":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(pipelineSuccess)) // nolint: errcheck
|
||||
case "/api/v4/":
|
||||
// Rate limiter requests.
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
Reference in New Issue
Block a user