Skip to content

Commit 02dae3f

Browse files
authored
Fix merge base commit for fast-forwarded GitLab PRs (#27825)
Due to a bug in the GitLab API, the diff_refs field is populated in the response when fetching an individual merge request, but not when fetching a list of them. That field is used to populate the merge base commit SHA. While there is detection for the merge base even when not populated by the downloader, that detection is not flawless. Specifically, when a GitLab merge request has a single commit, and gets merged with the squash strategy, the base branch will be fast-forwarded instead of a separate squash or merge commit being created. The merge base detection attempts to find the last commit on the base branch that is also on the PR branch, but in the fast-forward case that is the PR's only commit. Assuming the head commit is also the merge base results in the import of a PR with 0 commits and no diff. This PR uses the individual merge request endpoint to fetch merge request data with the diff_refs field. With its data, the base merge commit can be properly set, which—by not relying on the detection mentioned above—correctly imports PRs that were "merged" by fast-forwarding the base branch. ref: https://gitlab.com/gitlab-org/gitlab/-/issues/29620
1 parent 641e816 commit 02dae3f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

services/migrations/gitlab.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,13 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
528528
perPage = g.maxPerPage
529529
}
530530

531+
view := "simple"
531532
opt := &gitlab.ListProjectMergeRequestsOptions{
532533
ListOptions: gitlab.ListOptions{
533534
PerPage: perPage,
534535
Page: page,
535536
},
537+
View: &view,
536538
}
537539

538540
allPRs := make([]*base.PullRequest, 0, perPage)
@@ -541,7 +543,13 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
541543
if err != nil {
542544
return nil, false, fmt.Errorf("error while listing merge requests: %w", err)
543545
}
544-
for _, pr := range prs {
546+
for _, simplePR := range prs {
547+
// Load merge request again by itself, as not all fields are populated in the ListProjectMergeRequests endpoint.
548+
// See https://gitlab.com/gitlab-org/gitlab/-/issues/29620
549+
pr, _, err := g.client.MergeRequests.GetMergeRequest(g.repoID, simplePR.IID, nil)
550+
if err != nil {
551+
return nil, false, fmt.Errorf("error while loading merge request: %w", err)
552+
}
545553

546554
labels := make([]*base.Label, 0, len(pr.Labels))
547555
for _, l := range pr.Labels {

0 commit comments

Comments
 (0)