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:
Simon Heather
2023-11-04 02:22:12 +00:00
committed by GitHub
parent 1dcf234a6b
commit 3056701c6b
2 changed files with 30 additions and 11 deletions

View File

@@ -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),

View File

@@ -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