0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

github actionsでWIPラベルがついているプルリクエストのマージを禁止する

Posted at

githubでWIPラベルをつけてまだマージしたくないプルリクエストを管理していたのだけど、うっかりマージしないよう制限したい、ということでgithub actionsでチェックするようにしてみた。

※Draft Pull Requestもあるけど、こちらは優先度を下げる意味で使ってるので別にしたい。Draft Pull Requestは見た目の差があまりないので(ラベルの方が見やすい)もう少し差をつけて欲しいところ。

yamlファイル

github actionsの細かい説明は省略して、 gitに.github/workflows/check-wip-label.ymlファイルを追加してプッシュする。

check-wip-label.yml
name: check-wip-label
on:
  pull_request:
    types:
      - opened
      - reopened
      - ready_for_review
      - labeled
      - unlabeled
jobs:
  check-wip-label:
    runs-on: ubuntu-latest
    if: |
      contains(github.event.pull_request.labels.*.name, 'WIP')
    steps:
      - name: fail
        run: |
          exit 1

細かい指定の説明

pull_requestの箇所で、どのタイミングで動作するかを指定できる。
デフォルトだとラベルをつけた場合・外した場合には動作しないので、labeledunlabeledの追加が必要。他、reopenedready_for_reviewも適当に追加。

  pull_request:
    types:
      - opened
      - reopened
      - ready_for_review
      - labeled
      - unlabeled

参考:
https://docs.github.com/ja/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request

ラベルの取得はgithub.event.pull_request.labels.*.nameでできる。
ラベルに指定したラベルが含まれているかcontainsで判定して、以下の処理を実行するか決定する。
ラベル名はWIPを使っているが、これは自由に修正可能。

    if: |
      contains(github.event.pull_request.labels.*.name, 'WIP')

ラベルがついていた場合はエラー終了する。
0以外のエラーコードを返せばエラー終了と判定してくれる。

        run: |
          exit 1

リポジトリの設定

github actionsを追加しただけでは実行されても何も起こらないので、github側で設定を変更する。

Settings/Rules から New rulesetをクリックしてNew branch rulesetを追加する。

スクリーンショット 2025-09-24 19.24.33.png

以下のように設定する。

  • Enforcement status: Active
  • Target branches: master(またはmain)、developなどマージを制限するブランチ名を追加
  • Require a pull request before merging: チェック追加
  • Require status checks to pass: チェック追加

スクリーンショット 2025-09-24 19.36.26.png

さらに、Require status checks to passAdd Checksで作成したアクションを追加する(例ではcheck-wip-label

スクリーンショット 2025-09-24 19.32.14.png

最後にCreateをクリックして作成する。

ラベルの設定

ラベルはプルリク画面で新規に作成できる。
画面右側のLabelsをクリックして、Edit LabelsからWIPと入力しラベルを追加する。

スクリーンショット 2025-09-24 19.42.55.png

動作確認

プルリクの作成時にWIPラベルをつけると、以下のようにチェックでエラーになりMergeボタンが押せなくなる。
WIPラベルを消すとMergeできるようになる。

スクリーンショット 2025-09-24 19.44.20.png

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?