[GitHub Actions] Assignees 自動設定

2024-02-03 hit count image

GitHub Actionsを使ってPull requestのAssigneesにPull requestを生成した人を自動で設定する方法について方法について説明します。

share

概要

GitHubPull requestを作成する際、次のようにAssigneesを手動でPull requestを作成した人を設定していました。

GitHub Actions - Set assignees

Pull requestAssigneesは結局Pull requestを作成した人なので、Pull requestを作成するたびにAssigneesPull requestを作成した人を自動で設定したいと思いました。

今回のブログポストでは、GitHub Actionsを使ってPull requestAssigneesPull requestを作成した人を自動で設定する方法について説明します。

GitHub Actions 作成

それではPull requestAssigneesPull requestを作成した人を自動で設定するGitHub Actionsを作ってみましょう。.github/workflows/set_assignees.ymlファイルを作成し、次のように修正します。

name: Set assigness

on:
  pull_request:
    types:
      - opened

jobs:
  set-assignees:
    name: Set assignees
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
      - name: Set assignees
        run: |
          OWNER="${{ github.repository_owner }}"
          REPOSITORY="${{ github.repository }}"
          TOKEN="${{ inputs.token }}"
          PULL_REQUEST_NUMBER="${{ github.event.pull_request.number }}"

          ASSIGNEES=$(curl -s \
            "https://api.github.com/repos/$OWNER/$REPOSITORY/issues/$PULL_REQUEST_NUMBER" | \
            jq --raw-output '.assignees // [] | .[].login')

          if [ -z "$ASSIGNEES" ]; then
            ASSIGNEE=${{ github.actor }}

            curl -X POST \
              -H "Content-Type: application/json" \
              -H "Authorization: token $TOKEN" \
              -d "{ \"assignees\": \"${ASSIGNEE}\" }" \
              https://api.github.com/repos/$REPOSITORY/issues/$PULL_REQUEST_NUMBER/assignees
          fi

GitHubが提供するAPIを使ってPull requestAssigneesPull requestを作成した人(github.actor)を設定するようにしました。

Dependabot 用 GitHub Actions

私は個人プロジェクトでDependabotを使ってます。Dependabotが作ったPull requestにプロジェクトOwnerAssigneesに設定したかったです。

このためにGitHub Actionsを次のように修正しました。

name: Set assigness

on:
  pull_request:
    types:
      - opened

jobs:
  set-assignees:
    name: Set assignees
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
      - name: Set assignees
        run: |
          OWNER="${{ github.repository_owner }}"
          REPOSITORY="${{ github.repository }}"
          TOKEN="${{ inputs.token }}"
          PULL_REQUEST_NUMBER="${{ github.event.pull_request.number }}"

          ASSIGNEES=$(curl -s \
            "https://api.github.com/repos/$OWNER/$REPOSITORY/issues/$PULL_REQUEST_NUMBER" | \
            jq --raw-output '.assignees // [] | .[].login')

          if [ -z "$ASSIGNEES" ]; then
            ASSIGNEE=${{ github.actor }}
            BRANCH_NAME=${{ github.event.pull_request.head.ref }}

            if [[ "${BRANCH_NAME}" == "dependabot/"* ]]; then
              ASSIGNEE=$OWNER
            fi

            curl -X POST \
              -H "Content-Type: application/json" \
              -H "Authorization: token $TOKEN" \
              -d "{ \"assignees\": \"${ASSIGNEE}\" }" \
              https://api.github.com/repos/$REPOSITORY/issues/$PULL_REQUEST_NUMBER/assignees
          fi

追加した内容は次のようにbranch名がdependabot/で始まる場合、Assigneesgithub.repository_ownerを設定するようにしました。

BRANCH_NAME=${{ github.event.pull_request.head.ref }}

if [[ "${BRANCH_NAME}" == "dependabot/"* ]]; then
  ASSIGNEE=$OWNER
fi

しかし、このGitHub ActionsDependabotPull requestで正常に動作しませんでした。セキュリティーの理由で、DependabotPull requestではどのsecretsも使用できないし、Readonlyの動作しかできないためエラーが発生しました。

これを修正するためにはonpull_requestではなくpull_request_targetを使用するように修正する必要があります。

name: Set assigness

on:
  pull_request_target:
    types:
      - opened

actions/github-script

GitHubが提供するactions/github-scriptを使うともっと読みやすいコードを作成することができます。

actions/github-scriptを使うと次のように修正することができます。

name: 'Set assignees'
description: 'Set assignees automatically to the pull request.'

inputs:
  token:
    description: 'GitHub token'
    required: true

runs:
  using: 'composite'
  steps:
    - name: Set assignees
      uses: actions/github-script@v6
      with:
        github-token: ${{ inputs.token }}
        script: |
          const { owner, repo } = context.repo;
          const prNumber = context.payload.pull_request.number;

          const response = await github.request(`GET /repos/${owner}/${repo}/issues/${prNumber}`);
          const assignees = response.data.assignees.map(assignee => assignee.login);

          if (assignees.length === 0) {
            let assignee = context.actor;
            const branchName = context.payload.pull_request.head.ref;

            if (branchName.startsWith('dependabot/')) {
              assignee = owner;
            }

            await github.rest.issues.addAssignees({
              owner: owner,
              repo: repo,
              issue_number: prNumber,
              assignees: [assignee]
            });
          }

完了

これでPull requestAssigneesPull requestを作成した人を自動で設定するGitHub Actionsを作成しました。Pull requestを作成するたびにAssigneesPull requestを作成した人を手動で設定してる場合、このGitHub Actionsを使用して自動化してみてください。

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

アプリ広報

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

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



SHARE
Twitter Facebook RSS