Code Merge With Mergify

Code Merge With Mergify

ยท

6 min read

1. What is Mergify

002.PNG

Mergify is a GitHub Application that operates on your GitHub repositories to automate them. You need to install Mergify on your GitHub Account to have it working. Mergify is a pull request automation service, triggering actions when a pull request matches defined criteria.

2. Challenges faces with Github

c9c2f8b6c76b456e3e063e8ce667bf42-2891128-1628620707495-16x9-1-480x320-center-middle.jpg

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. You can easily modify codes on every projects and also solve issue in production line straightly. But there are lots of challenges that should faced by every developer and contributors, there two main challenges that is out dated issues and merge conflicts while merges the code on a large code base. To avoid this merge conflicts problem we have to create a new branch on every new commit. This solution some how effective for small changes but in case of multiple commits it is very hard to maintain the all branches according to commits so comes in to picture mergify. We can easily manage and maintain the code while marging.

3. Why mergify

0001.PNG

4. How to use

Mergify is a github application so it will operate github repositories that is selected by the user. Now you can easily signup and it ask you login with your github repository.

00001.PNG

After create your account select the specific repository where you want to install mergify

00002.PNG

After then the nice mergify UI present on your screen , but you need to configure the rules. Select the configure rule option over here.

00003.PNG

A demo syntax will present via mergify UI, you need to install the yaml configuration on a specific repo where you select in previous step. Just make a yaml file in the repo and copy the following code over here in github.


pull_request_rules:
  - name: Automatic merge on approval
    conditions:
      - "#approved-reviews-by>=1"
    actions:
      merge:
        method: merge

00004.PNG

Here the key consist of set of pullrequest rules

Now the question is that what the rules contains? The rules basicaslly contains three elements

  • Name of the rule : that is what the name of the rule and what it does.
  • Condition : Each condition must match what the rule that is to be apply
  • Action : For merge the the PR

Here the following rule can merge a PR then only approving the pull request code, that show in my example.

00005.PNG

00006.PNG

  • More Actions:

  • self assign rule :

You can assign people for review based on any criteria you like. A classic is to use the name of modified files to do it

pull_request_rules:
  - name: assign PRs with Python files modified to jd
    conditions:
      - files~=\.py$
      - -closed
    actions:
      assign:
        add_users:
          - jd
  • Using Labels to Backport Pull-Requests:

Copying pull requests to a maintenance branch is a common workflow. In order to elect a pull request to be backported, it's possible to use a label. With a rule such as the one below, you can trigger a backport as soon as the label is added and the pull request merged

pull_request_rules:
  - name: backport patches to stable branch
    conditions:
      - base=main
      - label=backport-to-stable
    actions:
      backport:
        branches:
          - stable
        assignees:
          - "{{ author }}"
  • Automatically close pull request touching a file:

If you wanted to automatically close pull request that touches a certain file, let's say do_not_touch.txt, you could write a rule such as

pull_request_rules:
  - name: disallow changing a file
    conditions:
      - files=do_not_touch.txt
    actions:
      close:
  • Copy a Pull Request to Another Branch:

The following rule copies a pull request from the staging branch to the prod branch as soon as the CI passes and a label ready for prod is set on the pull request

pull_request_rules:
  - name: copy pull request when CI passes
    conditions:
      - base=staging
      - check-success=test
      - label=ready for prod
    actions:
      copy:
        branches:
          - prod
  • Request for Action:

If any event that requires the author of the pull request to edit its pull request happen, you could write a rule that says something about it.

pull_request_rules:
  - name: ask to resolve conflict
    conditions:
      - conflict
    actions:
        comment:
          message: This pull request is now in conflicts. Could you fix it @{{author}}? ๐Ÿ™

The same goes if one of your check fails. It might be good to give a few hints to your contributor.


pull_request_rules:
  - name: ask to fix commit message
    conditions:
      - check-failure=Semantic Pull Request
      - -closed
    actions:
        comment:
          message: |
            Title does not follow the guidelines of [Conventional Commits](https://www.conventionalcommits.org).
            Please adjust title before merge.

When somebody that's not part of your team creates a pull requests, it might be great to give him a few hints about what to expect next. You could write him a little message.

pull_request_rules:
  - name: say hi to contributors if they are not part of the regularcontributors team
    conditions:
      - author!=@regularcontributors
    actions:
        comment:
          message: |
              Welcome to our great project!
              We're delighted to have you onboard ๐Ÿ’˜

Some continuous integration systems allow you to trigger jobs by commenting on the pull request. For example, Microsoft Azure allows that using the /AzurePipelines command. You could automate this using Mergify, and for example trigger the job after other CI jobs have passed and at least one developer has reviewed the pull request.


pull_request_rules:
  - name: run Azure CI job once ready to merge
    conditions:
      - "#approved-reviews-by>=1"
      - "check-success=ci/circleci: my_testing_job"
      - -closed
    actions:
      comments:
        message: /AzurePipelines run mypipeline
  • Warn on Conflicts:

When browsing the list of pull request, GitHub does not give any indication on which pull requests might be in conflict. Mergify allows to do this easily by adding a label.


pull_request_rules:
  - name: warn on conflicts
    conditions:
      - conflict
    actions:
      comment:
        message: "@{{author}} this pull request is now in conflict ๐Ÿ˜ฉ"
      label:
        add:
          - conflict
  - name: remove conflict label if not needed
    conditions:
      - -conflict
    actions:
      label:
        remove:
          - conflict

5. References

That's all for this blog, I hope you will learn something new. And feel free to share your thoughts and feedback, Thanks for reading.

Feel free to reach out me ๐Ÿ‘€

Twitter ๐Ÿ–ฑ

LinkedIn ๐Ÿ–ฑ

Github ๐Ÿ–ฑ

Did you find this article valuable?

Support Hashnode by becoming a sponsor. Any amount is appreciated!

ย