😇

Pull Requestのマージ先が「Develop」以外になっている場合コメントする

に公開

ベースブランチをmainブランチに設定したままマージしてしまうミスが多発したため、
developブランチ以外が設定されている場合コメント + メンションで通知する
GitHub Actionsを開発しました。

環境

Github上の設定

まずはGitHub Actionsに権限を与える。

SettingsActions
Workflow permissionsをRead and write permissionsを選択。
Allow GitHub Actions to create and approve pull requestに✅を入れる。

GitHub Actionsのコードを作成

Actions → set up a workflow yourself → 
ファイル名:pull_request_check.yml

[リポジトリ名]/.github/workflows/の中にファイルを新規作成でも可能です。

以下コードです。

name: プルリクエストのベースブランチを確認

on:
  pull_request:
    types:
      - opened

jobs:
  check_base_branch:
    runs-on: ubuntu-latest

    steps:
      - name: リポジトリをチェックアウト
        uses: actions/checkout@v2

      - name: ベースブランチを確認
        run: |
          if [[ "${{ github.actor }}" == "github-actions[bot]" ]]; then
            echo "このプルリクエストは GitHub Actions によって作成されましたので、スキップします。"
            exit 0
          elif [[ "${{ github.base_ref }}" != "develop" ]]; then
            echo "このプルリクエストのベースブランチは develop ブランチではありません。ベースブランチを develop に更新してください。"
            COMMENT_BODY="◤◢◤◢  ベースブランチが develop になっていません!! @${{ github.actor }} ◤◢◤◢"
            COMMENT_PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT_BODY" '.body = $body')
            curl -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -d "$COMMENT_PAYLOAD" "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
            exit 0
          fi

if [[ "${{ github.actor }}" == "github-actions[bot]" ]]; then
の部分でPull requestの作成者がGithub Actionsであった場合スキップするように
しています。
適時自分の環境に合わせてお使いください。

Discussion