Skip to content

Open a merge request on security merge conflict

Steve Abrams requested to merge open-mr-on-conflict into master

What does this MR do and why

When the merge train fails to merge from canonical to security, it fails (example). The current process is when these failures happen, a release manager then follows https://gitlab.com/gitlab-org/release/docs/-/blob/master/general/security/how_to_sync_security_with_canonical.md to create an MR to fix the conflicts.

This MR automatically opens an MR on the security repository with the conflicts and pings the release managers so they can fix the conflicts and merge.


This project has not been updated in nearly 1.5 years, so I'd like to additionally note why I believe this is a safe change to make. Currently, when we have conflicts during the security merge, the job fails and logs the conflicts. This MR adds additional logic that occurs during that job failure. So if something does happen to go wrong with these changes, we still fail as usual, not endangering the other functionality of this project.

Related to gitlab-com/gl-infra/delivery#2728 (closed)

Local testing

Setup

  1. Pull the branch open-mr-on-conflict
  2. Update the code locally replacing @release-managers with @sabrams so I only ping myself in the test.
  3. Create a project https://gitlab.com/sabrams-public-group/merge-train-test
  4. Fork the project at https://gitlab.com/sabrams-private-group/merge-train-test
  5. Remove the fork reference (if the fork reference is kept, then any attempts to open a merge request using the git options will open an MR on the originating project)
  6. Update both projects to use master branch as the default branch.
  7. Make a commit on the first project changing a line of the README on master.
  8. Make a commit on the second project changing the same line of the README to something else on master.
  9. Run the merge train with the options below (replace the CI_PROJECT_DIR with a directory where you are ok to clone your test project):
Successful execution
~/workspace/gitlab-org/merge-train (open-mr-on-conflict ✗) CI_PROJECT_DIR='/Users/steveabrams/mt-test' SOURCE_PROJECT='sabrams-public-group/merge-train-test' TARGET_PROJECT='sabrams-private-group/merge-train-test' ./bin/merge-train security
>>> SOURCE_PROJECT: sabrams-public-group/merge-train-test
>>> SOURCE_BRANCH: master
>>> TARGET_PROJECT: sabrams-private-group/merge-train-test
>>> TARGET_BRANCH: master

>>> Updating existing clone of sabrams-public-group/merge-train-test
>>> Updating existing clone of sabrams-private-group/merge-train-test
>>> Configuring merge drivers
>>> Updating remote
>>> Resetting working directory
>>> Merging sabrams-public-group/merge-train-test@master into sabrams-private-group/merge-train-test@master
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   README.md

* Unmerged path README.md
no changes added to commit (use "git add" and/or "git commit -a")
README.md:5: leftover conflict marker
README.md:7: leftover conflict marker
README.md:9: leftover conflict marker
Switched to a new branch 'sync-master-with-sabrams-public-group/merge-train-test'
>>> Pushing changes to sabrams-private-group/merge-train-test@sync-master-with-sabrams-public-group/merge-train-test
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 10 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 655 bytes | 655.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote:
remote: View merge request for sync-master-with-sabrams-public-group/merge-train-test:
remote:   https://gitlab.com/sabrams-private-group/merge-train-test/-/merge_requests/3
remote:
To gitlab.com:sabrams-private-group/merge-train-test.git
 * [new branch]      sync-master-with-sabrams-public-group/merge-train-test -> sync-master-with-sabrams-public-group/merge-train-test
>>> sync-master-with-sabrams-public-group/merge-train-test successfully pushed
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
Deleted branch sync-master-with-sabrams-public-group/merge-train-test (was c58a8b5).
HEAD is now at bed9daf Update file README.md

We can see the merge request was opened at https://gitlab.com/sabrams-private-group/merge-train-test/-/merge_requests/1. Since this is a private project, here are some screenshots:

MR Overview MR Diffs
Screenshot_2023-04-05_at_12.04.43_PM Screenshot_2023-03-29_at_3.12.15_PM

Other scenarios

When the branch already exists on the remote
~/workspace/gitlab-org/merge-train (open-mr-on-conflict ✗) CI_PROJECT_DIR='/Users/steveabrams/mt-test' SOURCE_PROJECT='sabrams-public-group/merge-train-test' TARGET_PROJECT='sabrams-private-group/merge-train-test' ./bin/merge-train security
>>> SOURCE_PROJECT: sabrams-public-group/merge-train-test
>>> SOURCE_BRANCH: master
>>> TARGET_PROJECT: sabrams-private-group/merge-train-test
>>> TARGET_BRANCH: master

>>> Cloning sabrams-public-group/merge-train-test
>>> Cloning sabrams-private-group/merge-train-test
>>> Configuring merge drivers
>>> Adding remote
>>> Updating remote
>>> Resetting working directory
>>> Merging sabrams-public-group/merge-train-test@master into sabrams-private-group/merge-train-test@master
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   README.md

* Unmerged path README.md
no changes added to commit (use "git add" and/or "git commit -a")
README.md:5: leftover conflict marker
README.md:7: leftover conflict marker
README.md:9: leftover conflict marker
!!! sync-master-with-sabrams-public-group/merge-train-test already exists, check for an existing MR on sabrams-private-group/merge-train-test to fix conflicts or delete the branch and rerun the merge train
When pushing the branch fails

I was able to trigger this by modifying the MR description to be invalid with newline characters.

~/workspace/gitlab-org/merge-train (open-mr-on-conflict ✗) CI_PROJECT_DIR='/Users/steveabrams/mt-test' SOURCE_PROJECT='sabrams-public-group/merge-train-test' TARGET_PROJECT='sabrams-private-group/merge-train-test' ./bin/merge-train security
>>> SOURCE_PROJECT: sabrams-public-group/merge-train-test
>>> SOURCE_BRANCH: master
>>> TARGET_PROJECT: sabrams-private-group/merge-train-test
>>> TARGET_BRANCH: master

>>> Updating existing clone of sabrams-public-group/merge-train-test
>>> Updating existing clone of sabrams-private-group/merge-train-test
>>> Configuring merge drivers
>>> Updating remote
>>> Resetting working directory
>>> Merging sabrams-public-group/merge-train-test@master into sabrams-private-group/merge-train-test@master
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   README.md

* Unmerged path README.md
no changes added to commit (use "git add" and/or "git commit -a")
README.md:5: leftover conflict marker
README.md:7: leftover conflict marker
README.md:9: leftover conflict marker
Switched to a new branch 'sync-master-with-sabrams-public-group/merge-train-test'
>>> Pushing changes to sabrams-private-group/merge-train-test@sync-master-with-sabrams-public-group/merge-train-test
fatal: push options must not have new line characters
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
Deleted branch sync-master-with-sabrams-public-group/merge-train-test (was fca56ba).
HEAD is now at bed9daf Update file README.md
!!! The conflict changes could not be pushed
Edited by Steve Abrams

Merge request reports