GitHub で複数の PR 文章テンプレート機能を実現する

GitHub で複数の PR 文章テンプレート機能を実現する

GitHub には Pull Request テンプレートの機能があります。

https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/creating-a-pull-request-template-for-your-repository

GitLab にも同様の機能がありますが、GitLab では複数のテンプレートを登録することが可能です。Merge Request 作成時にそのなかから選択することができます。

https://docs.gitlab.com/user/project/description_templates/

一方で GitHub では登録できるテンプレートは1つのみです。しかしこれでは作成する PR の内容によって異なるテンプレートを使いたい場合などには不便です。すこし工夫をすれば GitHub でも擬似的に複数のテンプレートの機能を実現できますのでご紹介です。


.github/pull_request_template.md は Pull Request 作成時に自動的に読み込まれるテンプレートです。このファイルの中でテンプレートのリストを表示しリンクを押してもらうことで、別途用意してあるテンプレートファイルを適用するという仕組みです。

ディレクトリ構成 #

└ .github/
  ├ pull_request_template.md
  └ PULL_REQUEST_TEMPLATE/
    ├ feature-template.md
    ├ bugfix-template.md
    └ hotfix-template.md

ファイル例 #

.github/pull_request_template.md

Switch to the `Preview` tab, then select the template from the following list:

- [feature template](?template=feature-template.md&expand=1)
- [bugfix template](?template=bugfix-template.md&expand=1)
- [hotfix template](?template=hotfix-template.md&expand=1&labels=HOTFIX)

.github/PULL_REQUEST_TEMPLATE/feature-template.md

# Feature Request

.github/PULL_REQUEST_TEMPLATE/bugfix-template.md

# Bugfix Request

.github/PULL_REQUEST_TEMPLATE/hotfix-template.md

# Hotfix Request