DEV Community

Cover image for Pull Request automation with Mergify
Karan Pratap Singh
Karan Pratap Singh

Posted on

Pull Request automation with Mergify

In this article, we'll be taking a look at Mergify which is a pull request automation service, triggering actions when a pull request matches defined criteria.

banner

Why do we need it?

As we all know, a developer's job doesn't end at opening a pull request. There so many variables such as:

  • CI Tests: Please don't fail
  • Reviewers: Who do I ask to review my PR?
  • Rebasing: Conflicts!!!
  • Merging: I hope it deploys successfully

And whether you're working at a startup or an enterprise, this becomes really tedious as we scale our teams. So why not just automate the entire thing? This is exactly what Mergify does.

Features

Mergify was able to handle all my use cases and has great features such as:

  • Automatic Merges

We can customise automatic merge behaviour and once that criteria such as CI tests passes, our PR will be merged automatically! This brings a lot of consistency to our PR workflow.

  • Reviewers Assignment

Github supports CODEOWNERS but sometimes we need additional functionality which might not just be code dependent such as validating branches, documentation, updates and much more.

  • Merge Queue

This has to be my favourite feature, I can't remember how many times I went "Okay, I need to merge this after that and this after that...wait what was it again?".

But as the new merge happens, it is possible that the pull request that was once working breaks the main branch. The stalled pull request might introduce regression or breakages into the production system.

Using a merge queue solves that problem by updating any pull request that is not up-to-date with its base branch before it is merged. The update forces the continuous integration system to retest the pull request with the new code from its base branch, catching any potential regression.

This basically eliminates manual merges and help us plan our release better by prioritising important pull requests.

Learn more about merge queues in detail here

  • Automatic Updates and Backports

This feature helps us keep our pull request up to date with their base branch. An example would be when we ship bug fixes to our release branches, this is quite common if you follow the gitflow pattern but it comes back in form of conflicts when it's time to rebase your branch with the main branch. So, backports and automatic updates are quite effective and helps us to push bug fixes in all our maintained branches.

Hands on!

Let's try do integrate a simple use case into our demo repository. We will create a pull request assignment automation.

Installation

First, we need to login into Mergify and allow access to our Github repository. This should install Mergify in our Github account.

installation

We're in! So this is the dashboard, and here we will be able to create configurations, merge queues, check usage, billing and much more.

dashboard

Config

Now, let's create our configuration file .mergify.yml.

Note: Make sure to checkout full reference here.

pull_request_rules:
  - name: assign PRs with Txt files modified to karan
    conditions:
      - files~=\.txt$
    actions:
      assign:
        add_users:
          - karanpratapsingh
Enter fullscreen mode Exit fullscreen mode

If you're not sure if your config is correct, you can always validate it on the dashboard!

editor

Action!

Let's see it in action. I'm going to add a *.txt file such as work.txt and then create a pull request. Shortly we'll see Mergify do it's thing!

result

As you can see our pull request has been assigned a user by the Mergify bot!

But wait, There's more!

This doesn't even scratch the surface, there is so much more you can do with Mergify!

actions

Conclusion

I hope this was helpful! I think Mergify is a great tool that can help you simplify your workflow and improve your SDLC. Feel free to reach out if you face any issues!

Top comments (0)