Even though Merge Request Approvals are not part of the Free GitLab tier, the functionality is still available on the Merge Request user interface and data is available in the API, so as a workaround you can create a GitLab CI job that implements checking the number of approvals and fails if insufficient.
First, ensure that in your project's Merge Request settings, Pipelines must succeed
is enabled.
Then create a new job in .gitlab-ci.yml
that will fail when there are insufficient merge request approvals:
code_approved:
rules:
- if: $CI_MERGE_REQUEST_ID
script:
- apt-get update
- apt-get install -y jq curl
- >
curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/approvals"
| jq -e '.approved_by[0]'
apt-get ...
: Install the curl
and jq
binaries.
curl --header "JOB-TOKEN: $CI_JOB_TOKEN"
: authenticates the call.
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/approvals"
GitLab API endpoint returning approval data for the current merge request. Note that on GitLab Free, a lot of data returned by this resource is missing or misleading, but approved_by
is properly populated.
You can also visit this API URL in a browser window to preview, e.g. https://gitlab.com/api/v4/projects/blaisekal%2Fmr-approvals/merge_requests/1/approvals
| jq -e '.approved_by[0]'
pipes the output to jq
, attempts to fetch the first approved_by
item from the list. -e
ensures that when it cannot find that item, the command will return with an error exit status, which causes the CI job to fail, which causes the option to merge to be blocked. If you require two approvals, use jq
filter '.approved_by[1]'
.
Demo: https://gitlab.com/blaisekal/mr-approvals/-/merge_requests
The biggest disadvantages of this workaround are that you need to run a job to determine approved status, you have to re-run the job manually if the number of approvals changes, and a malicious member of your team could remove the CI job before merging.