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

fix: GitLab - use "approvals_left" instead of "approvals_before_merge" when checking if pull is mergeable #5049

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (g *GitlabClient) PullIsApproved(logger logging.SimpleLogging, repo models.

// PullIsMergeable returns true if the merge request can be merged.
// In GitLab, there isn't a single field that tells us if the pull request is
// mergeable so for now we check the merge_status and approvals_before_merge
// mergeable so for now we check the merge_status and approvals_left
// fields.
// In order to check if the repo required these, we'd need to make another API
// call to get the repo settings.
Expand Down Expand Up @@ -360,14 +360,22 @@ func (g *GitlabClient) PullIsMergeable(logger logging.SimpleLogging, repo models
logger.Debug("Merge status: '%s'", mr.MergeStatus) //nolint:staticcheck // Need to reference deprecated field for backwards compatibility
}

approval, resp, err := g.Client.MergeRequestApprovals.GetConfiguration(repo.FullName, pull.Num)
if resp != nil {
logger.Debug("GET /projects/%d/merge_requests/%d/approvals returned: %d", mr.ProjectID, mr.IID, resp.StatusCode)
}
if err != nil {
return false, err
}

if ((supportsDetailedMergeStatus &&
(mr.DetailedMergeStatus == "mergeable" ||
mr.DetailedMergeStatus == "ci_still_running" ||
mr.DetailedMergeStatus == "ci_must_pass" ||
mr.DetailedMergeStatus == "need_rebase")) ||
(!supportsDetailedMergeStatus &&
mr.MergeStatus == "can_be_merged")) && //nolint:staticcheck // Need to reference deprecated field for backwards compatibility
mr.ApprovalsBeforeMerge <= 0 &&
approval.ApprovalsLeft == 0 &&
mr.BlockingDiscussionsResolved &&
!mr.WorkInProgress &&
(allowSkippedPipeline || !isPipelineSkipped) {
Expand Down
4 changes: 4 additions & 0 deletions server/events/vcs/gitlab_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,10 @@ func TestGitlabClient_PullIsMergeable(t *testing.T) {
case fmt.Sprintf("/api/v4/projects/runatlantis%%2Fatlantis/merge_requests/%v", defaultMr):
w.WriteHeader(http.StatusOK)
w.Write(pipelineSuccess) // nolint: errcheck
case fmt.Sprintf("/api/v4/projects/runatlantis%%2Fatlantis/merge_requests/%v/approvals", c.mrID):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you enhance the tests for this? It needs a test where approvals_left is 0 and another where approvals_left is greater than 0.

w.WriteHeader(http.StatusOK)
response := fmt.Sprintf(`{"id":%v,"name":"%s","rule_type":"regular","report_type":null,"eligible_approvers":[{"id":5,"name":"John Doe","username":"jdoe","state":"active","avatar_url":"https://www.gravatar.com/avatar/0?s=80&d=identicon","web_url":"http://localhost/jdoe"},{"id":50,"name":"Group Member 1","username":"group_member_1","state":"active","avatar_url":"https://www.gravatar.com/avatar/0?s=80&d=identicon","web_url":"http://localhost/group_member_1"}],"approvals_required":0,"users":[{"id":5,"name":"John Doe","username":"jdoe","state":"active","avatar_url":"https://www.gravatar.com/avatar/0?s=80&d=identicon","web_url":"http://localhost/jdoe"}],"contains_hidden_groups":false}`, c.mrID, c.statusName)
w.Write([]byte(response)) // nolint: errcheck
case fmt.Sprintf("/api/v4/projects/runatlantis%%2Fatlantis/merge_requests/%v", noHeadPipelineMR):
w.WriteHeader(http.StatusOK)
w.Write(headPipelineNotAvailable) // nolint: errcheck
Expand Down
Loading