Search code examples
gitlabgitlab-api

How to write custom GitLab approval rules?


My team is using GitLab Premium. In the project settings, under Merge request approvals, there is an Approval rules section. In the first row, we increased the Approvals required number to 1 so that every merge request must be approved by at least one other teammate. So far so good.

I can see there is also a License-Check rule and Coverage-Check rule that can be enabled and what they do is pretty self-explanatory. There is also an Add approval rule button where you can set a name and approvers for a new rule, but you can't actually describe/choose what the actual rule should be. All the documentation I could find simply explains how to click around that UI.

How do you actually set new rules and is it possible to write own custom rules?

For example, we'd like to set a rule that only a subset of the team can approve "large" merge requests (where "large" is defined by the number of lines changed, as reported by git log). I imagine that would be something similar to License-Check or Coverage-Check, but written by us. Is that possible?


Solution

  • I don't think this is possible yet. For now, approval rules are branch-specific. The content of a merge request doesn't matter.

    Nevertheless, I think there is a solution for you: GitLab allows you to write bots that can access the GitLab API. What you'd need is an external service listening to new merge requests. Your tool would then have to find out in some way how many changes were actually made (this is not yet possible with the Merge Request API as far as I can tell). Depending on this, you can then adjust the approval rules via the API for the corresponding MR (API reference).

    Does this approach help you?

    You can also ask a question in the GitLab Community Forum, there you might get better help.