[GitHub Actions] Set Reviewers Automatically

2024-02-23 hit count image

Let's see how to set reviewers automatically in Pull request using GitHub Actions.

Outline

When I create a Pull request on GitHub, I manually set the Reviewers as follows.

GitHub Actions - Set reviewers

In this blog post, I will introduce how to set Reviewers automatically in Pull request using GitHub Actions.

GitHub Actions

To set the reviewers automatically in the Pull request, create the .github/workflows/set_reviewers.yml file and modify it as follows.

name: Set reviewers

on:
  pull_request:

jobs:
  set-reviewers:
    name: Set reviewers
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
      - name: Set reviewers
        run: |
          actor="$"
          reviewers=("USER_1" "USER_2" "USER_3")

          reviewers=($(printf "%s\n" "${reviewers[@]}" | grep -v "$actor" | shuf -n 2))
          reviewers=$(printf '%s\n' "${reviewers[@]}" | jq -R . | jq -s .)

          curl -X POST \
            -H "Content-Type: application/json" \
            -H "Authorization: token $" \
            -d "{ \"reviewers\": $reviewers }" \
            "https://api.github.com/repos/$/pulls/$/requested_reviewers"

This GitHub Actions is designed to randomly set 2 people as Reviewers by excluding the person who created the Pull request from USER_1, USER_2, and USER_3.

actor="$"
reviewers=("USER_1" "USER_2" "USER_3")

reviewers=($(printf "%s\n" "${reviewers[@]}" | grep -v "$actor" | shuf -n 2))

The reviewers of the Pull request cannot be set to the person who created the Pull request by default.

When using this GitHub Actions, you can set reviewers randomly every time the Pull request is created and reduce the time to select reviewers.

GitHub Actions for Dependabot

The previous GitHub Actions does not work with the Pull request that Dependabot creates. To make it work, you need to modify the pull_request part to pull_request_target. Open the .github/workflows/set_reviewers.yml file and modify it as follows.

name: Set reviewers

on:
  pull_request_target:

jobs:
  set-reviewers:
    name: Set reviewers
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
      - name: Set reviewers
        run: |
          actor="$"
          reviewers=("USER_1" "USER_2" "USER_3")

          reviewers=($(printf "%s\n" "${reviewers[@]}" | grep -v "$actor" | shuf -n 2))
          reviewers=$(printf '%s\n' "${reviewers[@]}" | jq -R . | jq -s .)

          curl -X POST \
            -H "Content-Type: application/json" \
            -H "Authorization: token $" \
            -d "{ \"reviewers\": $reviewers }" \
            "https://api.github.com/repos/$/pulls/$/requested_reviewers"

You can use any secrets in the Pull request that Dependabot creates, and the Pull request created by Dependabot will run with read-only permissions, so an error occurs.

To fix this, you need to modify the pull_request to pull_request_target in the on condition.

actions/github-script

If you use actions/github-script provided by GitHub, you can write more readable code.

To use actions/github-script, you can modify it as follows.

name: Set reviewers

on:
  pull_request_target:

jobs:
  set-reviewers:
    name: Set reviewers
    runs-on: ubuntu-latest
    timeout-minutes: 1
    steps:
      - name: Set reviewers
        uses: actions/github-script@v5
        with:
          github-token: $
          script: |
            const reviewers = ["USER_1", "USER_2", "USER_3"];
            const actor = context.payload.pull_request.user.login;
            const { owner, repo } = context.repo;
            const prNumber = context.payload.pull_request.number;

            const { data: assignedReviewers } = await github.rest.pulls.listRequestedReviewers({
              owner,
              repo,
              pull_number: prNumber,
            });

            if (assignedReviewers.length === 0) {
              const filteredReviewers = reviewers.filter(reviewer => reviewer !== actor);
              const selectedReviewers = filteredReviewers.sort(() => Math.random() - 0.5).slice(0, 2);

              const prNumber = context.payload.pull_request.number;
              await github.rest.pulls.requestReviewers({
                owner,
                repo,
                pull_number: prNumber,
                reviewers: selectedReviewers
              });
            }

Completed

Done! We’ve seen how to set reviewers automatically in Pull request using GitHub Actions. By using this GitHub Actions, you can reduce the time to set reviewers and prevent focusing on specific users for review requests.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts