🌟
GitHub ActionsでPull Request作成時に説明テンプレートを自動追加する
GitHub ActionsでPull Request作成時に説明テンプレートを自動追加する
Pull Request(PR)の説明欄が空白だったり、必要な情報が抜けていたりすることはありませんか?
チーム開発では特に、PRの質を一定に保つことが大切です。
本記事では、GitHub Actionsを使って、PR作成時に説明テンプレートを自動で追加する方法を解説します。
✅ やりたいこと
PR作成時に、指定したテンプレートファイルの内容をPRの本文(Description)に自動で追記する。
🧩 構成概要
-
.github/workflows/append-pr-description.yml
: GitHub Actions のワークフローファイル -
.github/pr_description_template.md
: PRの説明として追記するテンプレート
🔧 ワークフローの設定
まずは、以下のようなワークフローファイルを作成します。
.github/workflows/append-pr-description.yml
📄 name: Append PR Description Template
on:
pull_request:
types: [opened, reopened, ready_for_review]
permissions:
pull-requests: write
contents: read
jobs:
comment_on_pr:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false && github.event.action == 'opened'
steps:
- uses: actions/checkout@v4
- name: Append PR description template
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const templatePath = '.github/pr_description_template.md';
let template = '';
try {
template = fs.readFileSync(templatePath, 'utf8');
} catch (error) {
core.setFailed(`テンプレート読み込み失敗: ${error.message}`);
return;
}
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const newBody = (pr.data.body || '') + '\n\n' + template;
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
body: newBody
});
📝 テンプレートの作成
次に、PRに追加したい説明のテンプレートをMarkdownファイルとして用意します。
📄 .github/pr_description_template.md
## 📝 PRの目的
<!-- このPRが解決する問題や目的を記載してください -->
## ✅ チェックリスト
- [ ] 動作確認済み
- [ ] テストケース追加済み
- [ ] ドキュメント更新済み
🚀 実行タイミング
このWorkflowは、以下のタイミングで実行されます:
- PR作成時(opened)
- PRを再オープンしたとき(reopened)
- Draft PRを「Ready for review」に変更したとき(ready_for_review)
また、Draft状態のままでは実行されないように、以下の条件で制御しています:
if: github.event.pull_request.draft == false && github.event.action == 'opened'
✅ チェックリスト
- .github フォルダにワークフローファイルとテンプレートファイルを作成
- リモートリポジトリにpush
- 新しいPull Requestを作成
- PRのDescriptionにテンプレートが自動で追記されていることを確認
補足:既存の本文がある場合
すでにPRに本文が書かれていた場合でも、上書きされず、その下にテンプレートが追記されるようになっています。
既存の記述を損なうことなくテンプレートを追加できるため安心です。
✅ まとめ
GitHub Actionsとテンプレートファイルを組み合わせることで、PRの説明を自動で補完し、レビュー効率や運用品質を向上できます。
- PR説明の標準化ができる
- 開発者の記述漏れを防げる
- 手動の手間がなくなる
ぜひ、チーム開発に取り入れてみてください!
Discussion