GitHub ラベルをテンプレート化する:gh CLI で一括作成・再利用
# デフォルトラベルを全削除 gh label list --json name | jq -r '.[].name' | while IFS= read -r label; do gh label delete "$label" --yes done # カスタムラベルを一括作成 bash labels.sh
GitHub はリポジトリ作成時に bug や enhancement などのデフォルトラベルを 9 種類用意しています。しかし実際のチーム開発では命名規則がバラバラになりやすく、使われないラベルが残りがちです。この記事では gh CLI を使ってデフォルトラベルを削除し、チーム独自のラベルセットをシェルスクリプトとして管理・再利用する方法を解説します。
GitHub のデフォルトラベル
新規リポジトリには以下の 9 種類が自動で作成されます。
| ラベル名 | 用途 |
|---|---|
| bug | バグ報告 |
| documentation | ドキュメント |
| duplicate | 重複 |
| enhancement | 機能改善 |
| good first issue | 初心者向け |
| help wanted | 協力者募集 |
| invalid | 無効 |
| question | 質問 |
| wontfix | 対応しない |
これらは汎用的すぎて「どれを付けるか」が曖昧になりやすいです。チームの運用に合った命名規則に統一したい場合は、一度削除してカスタムセットに差し替えるのが確実です。
デフォルトラベルを一括削除する
gh label list --json name | jq -r '.[].name' | while IFS= read -r label; do gh label delete "$label" --yes done
gh label list --json name で全ラベル名を JSON 取得し、jq でラベル名だけ抜き出して gh label delete に渡します。--yes は確認プロンプトをスキップします。xargs ではなく while read を使うことで、スペースを含むラベル名でも安全に処理できます。
カスタムラベルのセット設計
プレフィックスでカテゴリを明示する設計が管理しやすいです。
| カテゴリ | ラベル例 | 用途 |
|---|---|---|
kind: |
kind: bug kind: feature kind: docs kind: chore |
変更の種類 |
priority: |
priority: high priority: medium priority: low |
優先度 |
status: |
status: in-progress status: blocked status: needs-review |
進捗状態 |
labels.sh でテンプレート化する
カスタムラベルをシェルスクリプトにまとめておくと、新しいリポジトリに何度でも流用できます。
#!/bin/bash # labels.sh set -e # kind gh label create "kind: bug" --color "d73a4a" --description "バグ・不具合" gh label create "kind: feature" --color "0075ca" --description "新機能・機能改善" gh label create "kind: docs" --color "0052cc" --description "ドキュメント" gh label create "kind: chore" --color "e4e669" --description "依存関係・設定変更" gh label create "kind: refactor" --color "fbca04" --description "リファクタリング" # priority gh label create "priority: high" --color "b60205" --description "優先度:高" gh label create "priority: medium" --color "e99695" --description "優先度:中" gh label create "priority: low" --color "f9d0c4" --description "優先度:低" # status gh label create "status: in-progress" --color "0e8a16" --description "作業中" gh label create "status: blocked" --color "ee0701" --description "ブロック中" gh label create "status: needs-review" --color "c2e0c6" --description "レビュー待ち"
bash labels.sh
gh label create の主なオプションは以下のとおりです。
| オプション | 説明 |
|---|---|
--color |
6 桁の16進カラーコード(# なし) |
--description |
ラベルの説明文 |
--force |
同名ラベルが存在する場合に上書き |
別リポジトリへコピーする
既存リポジトリのラベルをそのまま別リポジトリへ移植するには、JSON でエクスポートしてスクリプトを生成する方法が便利です。
# コピー元リポジトリのラベルを取得してそのまま別リポジトリへ作成 gh label list --json name,color,description --repo owner/source-repo \ | jq -r '.[] | "gh label create \"\(.name)\" --color \"\(.color)\" --description \"\(.description)\""' \ | bash
--repo オプションでリポジトリを指定するため、ローカルに clone していなくても実行できます。
まとめ
| やりたいこと | コマンド |
|---|---|
| ラベルを一覧表示 | gh label list |
| ラベルを作成 | gh label create "名前" --color "色" --description "説明" |
| ラベルを削除 | gh label delete "名前" --yes |
| 全ラベルを一括削除 | gh label list --json name | jq -r '.[].name' | while ... gh label delete |
| 別リポジトリへコピー | gh label list --json ... --repo owner/repo | jq ... | bash |
デフォルトラベルを削除して labels.sh に自分たちの運用ルールを定義しておくと、新しいリポジトリを作るたびに bash labels.sh だけで同じラベルセットを再現できます。
GitHub の PR テンプレートについては「GitHub PR・ISSUEテンプレートの作り方:チームのレビュー効率を上げる設定まとめ」も参照してください。


