Fix BitBucket Server Importer: Changes Missing from Pull Requests
What does this MR do and why?
When importing a project from Bitbucket, the imported merge requests sometimes do not show the diffs or commits. This happens when merging with a "Squash and merge" strategy which creates a new commit. On the imported side, the commit does not exist since the branch and commit were deleted. This means that the MR cannot find the original commit SHA and therefore doesn't link the commit to the MR. This causes the diffs to also be empty.
In !131382 (merged), the fix for Bitbucket Cloud was to use the merge_commit
hash returned with the MR via the API. For Bitbucket Server, the API doesn't return any other SHAs, only the toRef
and fromRef
. The API for fetching all commits on a repo also doesn't return the original SHA.
Since we only have the toRef
SHA and nothing else, we can check if this SHA is present in the commit message since a squashed commit message looks like this:
Merge in TEST/test from test-multiple-squashed-commits to master
Squashed commit of the following:
commit 77bd7358d4c7215d5288aaf8cac21f6097e53c60
Author: maddievn <mvanniekerk@gitlab.com>
Date: Mon Sep 18 15:58:38 2023 +0200
change 6
commit c9e1050a01906cadda08830ee16fb61ebf6f8943
Author: maddievn <mvanniekerk@gitlab.com>
Date: Mon Sep 18 15:58:25 2023 +0200
change 5
If the SHA passed as the toRef
is not found in the imported repo, we can check if the SHA is present in any commit messages and use the squashed commit SHA for the MR's head.
This works for all merge strategies:
-
merge commit --no-ff
-
fast forward -ff
-
fast forward only --ff-only
-
rebase and merge --no-ff
-
rebase and fast forward --ff-only
-
squash --squash
-
squash fast forward only --squash --ff-only
Screenshots or screen recordings
Before | After |
---|---|
Commits:
Bitbucket | GitLab |
---|---|
How to set up and validate locally
- Start a bitbucket server docker instance locally:
- https://gitlab.com/gitlab-org/manage/import-and-integrate/team/-/blob/main/integrations/bitbucket_server.md#set-up-local-bitbucket-instance-from-the-official-dockerhub-image
- Note: I had to bump the amount of memory and CPU to 4 CPUs and 4GB memory otherwise the process exits. You can monitor usage by running
docker stats
- Go to the project imports page and select Bitbucket Server and follow the steps to configure
- Create a project + repo on bitbucket. Create/Enable a merge strategy for "Squash" in Settings > Merge Strategies
- Add some code and create an MR. Merge the MR with the Squash strategy & Delete the source branch.
- Checkout master:
git checkout master
- Import the project into gitlab and note that the MR doesn't have any commits or diffs.
- Checkout this branch:
git checkout 356240-fix-bitbucket-server-mr-diffs
- Import the project again and note that the MR has the commits and diffs now.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #356240 (closed)