LoginSignup
4
1

Pull Request Labeler V5でブランチ名を元にラベルを付与 & 前バージョンからの移行方法

Last updated at Posted at 2023-12-05

🎊 Pull Request Labeler V5がリリースされました

Pull Request Labelerは、GitHubのプルリクエストに自動的にラベルを付けるためのアクションです。大規模なプロジェクト、多くのコントリビューターがいる場合、モノレポで開発している場合などに便利です。

今回、新たなバージョンとしてv5.0.0がリリースされましたが、Breaking Changesとして設定の書き方が変わったり新機能が追加されていたのでまとめました。

🔄 変更点(Breaking Changesを含む)

V5では以下の重要な変更が行われました。

  1. ベースおよびヘッドブランチの名前に基づいたラベル付け: 変更されたファイルのマッチング方法が拡張され、設定ファイルの構造も大幅に変更されました。これにより、以前のバージョンとの互換性はありません。
  2. sync-labels入力のバグ修正: この入力は正しく読み取られるようになりました。
  3. dot入力のデフォルト設定変更: デフォルトでtrueに設定され、.githubのようにドットで始まるパスがデフォルトでマッチします。
  4. Node.js 20へのアップデート: このアクションはNode.js 20で実行されるようになり、Node.js 16と20の間の重要な変更が影響します。

設定ファイルの書き方が変更され以前のバージョンの書き方との互換性がなくなったのと、ブランチ名を元にしたラベル付与機能が追加されたのが、今回のポイントです。

⚙️ 設定ファイルの書き方の例・前バージョンからの移行方法

例えば、こんなルールでラベルを設定したいと仮定します。

ディレクトリパターン

変更されたファイルのパターン 付与するラベル
src/** app
.github/** github_actions
docs/** documentation
tests/** tests

ブランチパターン

ブランチパターン ラベル
feature/* feature
fix/* fix
hotfix/* hotfix
release/* release

↩️ 以前の書き方

Pull Request LabelerはV5以前はブランチパターンを元にラベルを付与できなかったので、その部分だけTimonVS/pr-labeler-actionを代わりに利用していました。

そのためactions/labeler@v4(Pull Request Labeler V4)とTimonVS/pr-labeler-actionを使って以下のように実装していました。

actions/labeler@v4TimonVS/pr-labeler-action@v4のワークフロー

.github/workflows/pr-labeler.yml
name: 'PR Labeler'
on:
  - pull_request_target
jobs:
  label:
    permissions:
      contents: read
      pull-requests: write
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/labeler@v4
        with:
          configuration-path: .github/configs/labeler.yml

      - uses: TimonVS/pr-labeler-action@v4
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          configuration-path: .github/configs/pr-labeler-action.yml

actions/labeler@v4の設定ファイル

.github/configs/labeler.yml
app:
  - src/**
github_actions:
  - .github/workflows/**
documentation:
  - docs/**
tests:
  - tests/**

TimonVS/pr-labeler-action@v4の設定ファイル

.github/configs/pr-labeler-action.yml
feature: feature/*
fix: fix/*
hotfix: hotfix/*
release: release/*

🆕 Pull Request Labeler V5の新しい書き方

Pull Request Labeler V5では、以前のバージョンとは異なり、ブランチパターンに基づいたラベル付与機能が追加されました。これにより、Pull Request Labeler V5だけで全てのラベル付与を行うことが可能になります。

新しいバージョンでは、以下のように設定ファイルを書きます。

actions/labeler@v5のみのワークフロー

.github/workflows/pr-labeler.yml
name: 'PR Labeler'
on:
  - pull_request_target
jobs:
  label:
    permissions:
      contents: read
      pull-requests: write
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/labeler@v5
        with:
          configuration-path: .github/configs/labeler.yml

actions/labeler@v5の設定ファイル

.github/configs/labeler.yml
# Directory pattern
app:
  - changed-files:
      - any-glob-to-any-file: 'src/**'

github_actions:
  - changed-files:
      - any-glob-to-any-file: '.github/**'

documentation:
  - changed-files:
      - any-glob-to-any-file: 'docs/**'

tests:
  - changed-files:
      - any-glob-to-any-file: 'tests/**'

# Branch pattern
feature:
  - head-branch: ['^feature/']
fix:
  - head-branch: ['^fix/']
hotfix:
  - head-branch: ['^hotfix/']
release:
  - head-branch: ['^release/']

💡 他のパターンマッチング

ディレクトリ名やブランチ名の他のパターンは以下に詳しく説明されています。

このような例が示されていますね。

example-labeler.yml
# 'docs' フォルダまたはそのサブフォルダ内の変更に 'Documentation' ラベルを追加
Documentation:
- changed-files:
  - any-glob-to-any-file: docs/**

# 'docs' フォルダ内のファイル変更に 'Documentation' ラベルを追加
Documentation:
- changed-files:
  - any-glob-to-any-file: docs/*

# リポジトリ全体の .md ファイルへの変更に 'Documentation' ラベルを追加
Documentation:
- changed-files:
  - any-glob-to-any-file: '**/*.md'

# ソースディレクトリ内のsrcファイルへの変更に 'source' ラベルを追加(ただし 'src/docs' サブフォルダは除く)
source:
- all:
  - changed-files:
    - any-glob-to-any-file: 'src/**/*'
    - all-globs-to-all-files: '!src/docs/*'

# ヘッドブランチ名が `feature` で始まる、または名前に `feature` セクションがある任意のPRに 'feature' ラベルを追加
feature:
 - head-branch: ['^feature', 'feature']

 # `main` ブランチに対してオープンされたPRに 'release' ラベルを追加
release:
 - base-branch: 'main'
4
1
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
4
1