Using templates in your messages

Using templates in your messages

Julien Danjou

As Mergify merges more and more pull requests for its users, new behaviors emerge, and new ideas come up.

We’re building a tool for developers, and while we started by scratching our own itch, we’re now way beyond that territory — which makes it even more exciting if you ask us.

One of the most recent needs our users needed was the ability to inject pull request data into Mergify rules.

Why is that? It turns out that this enables unusual uses cases, such as the ones described below.

Dynamic Messages

Mergify provides an action to comment on pull requests. This is quite handy, but so far, the message string was static and not able to use any context.

This has now changed, and you can now write rules such as:

- name: ping author on conflicts
  conditions:
    - conflict
  actions:
    comment:
      message: This pull request is now in conflict, you need to update it @{{author}}.

That rule posts a comment as soon as a pull request is in conflict, notifying the pull request author that they need to intervene!

You can now use those templates in the message for any action.

Dynamic Commit Messages

You expect your commit messages to be static, well-written in your git history, or in the little text box that GitHub provides when you’re about to press that big green merge button.

That’s nice, but you could do better. What if you could write commit messages based on the pull request content and attributes?

Here come the dynamic commit messages, powered by templates. You can now write the final commit message for your pull request in the pull request body. Use the markdown header Commit Message and write down the title of the commit message and its content.

The following example uses the pull request title as the first line of the pull request while thanking the author and listing the CI that passed before the PR was merged:

You can see the final result in the commit history:

You can use any pull request attribute in those messages, as described in our documentation. The template engine behind is Jinja2.