[GitHub Actions] Pull requestのラベル検査

[GitHub Actions] Pull requestのラベル検査

2023-05-11 hit count image

GitHub ActionsのPR Labels Checkerを使ってPull requestにラベルが設定されたかどうかを確認する方法について説明します。

概要

GitHubPull requestを管理する時、ラベル(Label)機能を使います。また、Release DrafterのようにPull requestのラベルを基準にして動作するGitHub Actionsも存在します。

このブログポストではGitHub ActionsPR Labels Checkerを使ってPull requestのラベルがうまく設定されたかどうかチェックする方法について説明します。

ブログシリーズ

このブログはシリーズで作成しております。下記のリンクを参考して他のブログポストも確認してみてください。

PR Labels Checker

PR Labels CheckerPull requestのラベルが適用されたかチェックするGitHub Actionsです。

PR Labels Checkerを使うため.github/workflows/labels-checker.ymlファイルを作って次のように修正します。

name: Labels Checker

on:
  push:
    branches:
      - main
  pull_request:
    types:
      - opened
      - labeled
      - unlabeled
      - synchronize

jobs:
  check_labels:
    runs-on: ubuntu-latest
    steps:
      - uses: danielchabr/[email protected]
        with:
          hasSome: breaking change,feature,bug,style,refactor,test,chore,docs,ci,dependencies
          githubToken: $

このようにGitHub Actionsを生成したら、Pull requestbreaking change, feature, bug, style, refactor, test, chore, docs, ci, dependencies中で1つのラベルが設定されないとエラーが発生するようになります。

この例題で設定したラベルはRelease DrafterRelease noteを自動で作成するため使ったラベルです。

Release Drafter

私はGitHubRelease noteを自動化するためRelease Drafterを使ってます。Release Drafterについては詳しい内容は下記のリンクを参考してください。

私はRelease Drafterautolabelerがラベルをうまく設定したか確認するため下記のようにGitHub Actionsを使ってます。

name: Release Drafter

on:
  push:
    branches:
      - main
  pull_request:
    types:
      - opened
      - reopened
      - synchronize

permissions:
  contents: read

jobs:
  update_release_draft:
    permissions:
      contents: write
      pull-requests: write
      checks: write
    runs-on: ubuntu-latest
    steps:
      - uses: release-drafter/[email protected]
        env:
          GITHUB_TOKEN: $
      - uses: danielchabr/[email protected]
        with:
          hasSome: breaking change,feature,bug,style,refactor,test,chore,docs,ci,dependencies
          githubToken: $

このようにGitHub Actionsを構成するとPull requestが生成される時、Release Drafterによってラベルが設定され、その後PR Labels Checkerがラベルがうまく設定されたか確認するようになります。Release Drafterautolabelerがレベルをうまく設定してくれますが、念のためもう一度チェックするように構成してます。

完了

これでGitHub ActionsGitHub Actionsを使ってPull requestのラベルがうまく設定されたか確認する方法について見てみました。Pull requestのラベルを利用するGitHub Actionsを使ってる場合、ラベルを使ってプロジェクトを管理してる場合、PR Labels Checkersの導入をおすすめします。

私のブログが役に立ちましたか?下にコメントを残してください。それは私にとって大きな大きな力になります!

アプリ広報

今見てるブログを作成たDekuが開発したアプリを使ってみてください。
Dekuが開発したアプリはFlutterで開発されています。

興味がある方はアプリをダウンロードしてアプリを使ってくれると本当に助かります。

Posts