LoginSignup
2
0

GitHub Actions PR 作成時にラベルを自動付与する

Last updated at Posted at 2024-04-04

概要

前回ラベルをファイルで管理するようにしたのでそれを上手く利用したい。
ブランチ名、差分ファイルに応じてラベルを自動付与したい。

Gitシリーズ記事まとめ

利用ツール

ファイルパスやブランチ名に応じてラベルを設定するツールがあります。

実装

下記、3つのファイルを用意します。

  • .github/labeler-branch.yaml
  • .github/labeler-files.yaml
  • .github/workflows/assign-label.yaml

.github/labeler-branch.yaml

.github/labeler-branch.yaml
feature:
  - head-branch: ['^feat']
bug:
  - head-branch: ['^fix']
chore:
  - head-branch: ['^chore']
refactor:
  - head-branch: ['^refactor']
document:
  - head-branch: ['^doc']

例えば feat から始まるブランチ名だと feature ラベルが付与されます。

.github/labeler-files.yaml

.github/labeler-files.yaml
npm:
  - changed-files:
    - any-glob-to-any-file: ['package.json', 'package-lock.json']
composer:
  - changed-files:
    - any-glob-to-any-file: ['composer.json', 'composer.lock']

例えば package.jsonpackage-lock.json に変更がある場合は npm ラベルが付与されます。

.github/workflows/assign-label.yaml

.github/workflows/assign-label.yaml
name: Assign Label
on:
  pull_request:
    types: [opened, synchronize]
jobs:
  assign-label:
    permissions:
      contents: read
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
	  - uses: actions/labeler@v5
        if: ${{ github.event.action == 'opened' }}
        with:
          configuration-path: .github/labeler-branch.yaml
      - uses: actions/labeler@v5
        with:
          configuration-path: .github/labeler-files.yaml

補足

プルリクエスト作成時のみブランチ名に応じてラベルを付与します。
差分ファイルは追加されたコミットで増える可能性があるので、opened, synchronize両方で実行しています。

業務改善度: ★★★☆☆

ブランチの命名ルールを正しく運用できていればラベルが自動付与されます。
ラベルの張り忘れ防止に期待できます。

また、特定のファイルが変更された時は注意が必要などの決まりごとがある場合などルールを追加しやすいので結構便利に使えそうなワークフローです。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0