Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update GitLab Pipeline Type Detection to use Head Pipeline Property #3887

Merged
merged 6 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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