Templating Pull Requests & Merge Requests
How do you communicate with your team about proposed code changes?
One of the most critical roles we play as developers is to clearly communicate with our team. When a change or addition is proposed to a codebase as a developer, I want the team members who review the code to understand what I was trying to achieve with that work. As a code reviewer, I want those who propose code changes to explain their changes and let me know me how to test the code.
Both GitHub or GitLab offer the ability to enable “templates” for proposed code changes. These templates are boilerplate text or other content that will automatically show up in the change request form when project collaborators issue a pull request or a merge request against a code repository.
Let’s talk about each service and how to work with them:
Creating Multiple GitHub Pull Request Templates for a Repository
Follow the below steps in order to create multiple pull request templates in GitHub:
- In your repository’s root, create a directory named
PULL_REQUEST_TEMPLATE
. Do not name that directory anything else or put it in a different location, otherwise, GitHub will not recognize any of your templates - Place your template markdown files within
PULL_REQUEST_TEMPLATE
directory. Name these files as per your convenience e.g. Bug.md, Feature.md, Spike.md, Chore.md, etc. - Commit these changes and push it to your default branch.
- While issuing a new pull request, add query string parameter
template=name_of_template_file.md
in the URL. This will auto populate the content of the template in description of the pull request as shown below.
https://github.com/apurvajain/ToDoList/compare/test?template=bug.md&expand=1
The process of manually adding query string parameter in GitHub is cumbersome 😫.
For more information, see GitHub’s documentation about URL structure, namely around query parameters (the
?
in their URLs) for issues and pull requests.
Creating Multiple GitLab Pull Request Templates for a Repository
Creating multiple pull request templates in GitLab is pretty much straightforward:
- In your repository’s root, create a directory named
.gitlab
- Inside of that
.gitlab
directory, create a directory namedmerge_request_templates
. - Inside your
.gitlab/merge_request_templates
directory, add one or many template files, but they must be markdown files. - Commit these changes and push it to your default branch.
- That’s it! 😎
When a contributor issues a new merge request, they’ll see a dropdown that encourages them to “choose a template” and pick which template suits their merge request.
Generic Pull/Merge Request Templates for Chore, Spike, Feature & Bug:
Following are the ready to use templates that you can easily import in your repositories and customise as needed.
Communicate better with Templates
By creating templates for pull and merge requests, we can help encourage contributors to communicate more effectively with their teams by noting what’s new or changed, give testing instructions to reviewers, or even help to ensure that code changes have been thoroughly vetted.
If you liked this post, please share, comment and give few 👏 😊