Search code examples
gitgitlabscriptinggitlab-cicicd

How to add conflict resolving scripts in Gitlab CICD so that approvals aren't lost?


In Gitlab, when the target branch updates, in case of no conflicts, Gitlab directly provides a button to rebase. If we rebase using that, then all approvals that are present on the MR is retained.

But instead, if there is conflict, we have to resolve it locally and push again. In this case, all the approvals present on MR will be lost.

In our codebase, there is a file that is updated automatically in some cases, and most of the time it causes conflicts. But using some scripting, we can easily resolve those conflicts. But resolving locally and then pushing is of no use as approvals would be removed anyway.

So, how to integrate this script into Gitlab CICD, so that, when there is a conflict in that particular file, we are able to resolve conflict in Gitlab itself without losing our approvals.


Solution

  • You can keep approvals by unchecking the MR approval setting for Remove all approvals when commits are added to the source branch.

    As you have noted:

    Approvals aren’t removed when a merge request is rebased from the UI However, approvals are reset if the target branch is changed.

    So, with this setting you can choose to retain all approvals even after changes are made. However, beyond the above note for rebases made from the UI, there is no way to distinguish between changes to resolve conflicts and other kinds of changes, or make exceptions for specific files.


    If you do want to try to make an exception in the approval removal behavior for just that file, one possible workaround might be to exclusively rely on Code Owner approval rules, which can be configured to only remove approvals of users if their owned files are changed.

    What that might look like is a CODEOWNERS file that makes all files except the automatically-updated file "owned" by (all) members of your project who participate in reviews. The effect of this is that, because the automatically-updated file is not owned, when a change is pushed that only modifies that file, none of the Code Owner approvals will be removed if the "Remove approvals by Code Owners if their files changed" setting is configured.