Using CI/CD to automatically merge pull requests

Hello,

I was wondering if there is currently a way to use GitLab’s CI/CD to automatically merge pull requests based on a number of conditions (target/source branch, tag, etc) on the SaaS (Gitlab.com) version? While I appreciate it might go against the GitLab flow, we might have a use case for such an implementation.

If you wish to do that, there’s no technical reason why you can’t. You would need to write something that calls the GitLab API.

I’m not sure whether it’s possible to merge directly from the API but you could certainly use a bot.

Thank you for replying.

Is it possible to run an API call in CI/CD jobs on GitLab.com via something like cUrl or maybe there is an easier way? I can only find examples for various images that use pre-existing libraries, nothing custom.

Yes, the script part of your CI jobs is just a Bash script, so you can do anything that you would normally do in Bash. If you need a tool like curl, you just need to make sure that it is installed in the Docker image for that job.

2 Likes

Here is a snippit with how I do it:

script:
    - apk add jq curl
    - curl --request GET --url "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/repository/commits/$CI_COMMIT_BEFORE_SHA/merge_requests" --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN"

You will want to create a personal access token here and add it to your CI/CD variables here.

1 Like

I managed to build a pipeline in two flavours: bash & js, but I think I am going to stick with the Node image since most of my team is familiar with JS so there may be more hands on deck to help with maintenance.

Thanks to both of you guys, you rock!

1 Like