Skip to content

Commit

Permalink
fix(gitlab): Add backwards compatible mergability check (#3277)
Browse files Browse the repository at this point in the history
* Add backwards compatibility for merability check in Gitlab client

* Cleanup
  • Loading branch information
jukie authored Mar 31, 2023
1 parent ebc06c1 commit 8adb001
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 58 deletions.
29 changes: 22 additions & 7 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,14 @@ func (g *GitlabClient) PullIsMergeable(repo models.Repo, pull models.PullRequest

isPipelineSkipped := mr.HeadPipeline.Status == "skipped"
allowSkippedPipeline := project.AllowMergeOnSkippedPipeline && isPipelineSkipped
if (mr.DetailedMergeStatus == "mergeable" || mr.DetailedMergeStatus == "ci_still_running") &&

ok, err := g.SupportsDetailedMergeStatus()
if err != nil {
return false, err
}

if ((ok && (mr.DetailedMergeStatus == "mergeable" || mr.DetailedMergeStatus == "ci_still_running")) ||
(!ok && mr.MergeStatus == "can_be_merged")) &&
mr.ApprovalsBeforeMerge <= 0 &&
mr.BlockingDiscussionsResolved &&
!mr.WorkInProgress &&
Expand All @@ -249,6 +256,19 @@ func (g *GitlabClient) PullIsMergeable(repo models.Repo, pull models.PullRequest
return false, nil
}

func (g *GitlabClient) SupportsDetailedMergeStatus() (bool, error) {
v, err := g.GetVersion()
if err != nil {
return false, err
}

cons, err := version.NewConstraint(">= 15.6")
if err != nil {
return false, err
}
return cons.Check(v), nil
}

// UpdateStatus updates the build status of a commit.
func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string, url string) error {
gitlabState := gitlab.Pending
Expand Down Expand Up @@ -352,12 +372,7 @@ func (g *GitlabClient) DiscardReviews(repo models.Repo, pull models.PullRequest)

// GetVersion returns the version of the Gitlab server this client is using.
func (g *GitlabClient) GetVersion() (*version.Version, error) {
req, err := g.Client.NewRequest("GET", "/version", nil, nil)
if err != nil {
return nil, err
}
versionResp := new(gitlab.Version)
_, err = g.Client.Do(req, versionResp)
versionResp, _, err := g.Client.Version.GetVersion()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8adb001

Please sign in to comment.