[GitHub Actions] Automatically Assign Assignees to PR

2024-08-30 hit count image

Let's see how to automatically add the developer who created the PR to the Assignees using GitHub Actions.

Outline

After completing the development, the developer in charge of the development creates a PR(Pull Request) on GitHub and adds himself to the Assignees. However, it is cumbersome to add yourself to the Assignees every time you create a PR.

In this blog post, I will introduce how to automatically add the developer who created the PR to the Assignees using GitHub Actions.

Create GitHub Actions

To create a GitHub Actions that automatically adds Assignees to PRs, create a .github/workflows/auto-assign-assignees.yml file and modify it as follows.

name: Assign assignees

on:
  pull_request:
    types:
      - opened

jobs:
  assign:
    name: Set assignees
    runs-on: ubuntu-latest
    timeout-minutes: 1
    if: github.event.pull_request.user.login != 'dependabot[bot]'
    steps:
      - name: Set assignees
        uses: actions/github-script@v7
        with:
          github-token: $
          script: |
            const { owner, repo } = context.repo
            const prNumber = context.payload.pull_request.number

            const response = await github.rest.issues.get({
              owner,
              repo,
              issue_number: prNumber,
            })
            const { assignees } = response.data

            if (assignees.length === 0) {
              await github.rest.issues.addAssignees({
                owner: owner,
                repo: repo,
                issue_number: prNumber,
                assignees: [context.actor]
              })
            }

Let’s take a closer look at this GitHub Actions.

...
on:
  pull_request:
    types:
      - opened
...

This GitHub Actions runs when a PR is created.

...
jobs:
  assign:
    ...
    if: github.event.pull_request.user.login != 'dependabot[bot]'
    ...

In my case, I use Dependabot on GitHub. Since Dependabot cannot be added to the Assignees of the PR created by Dependabot, I set it not to add Assignees to the PR created by Dependabot.

...
jobs:
  assign:
    ...
    steps:
      - name: Set assignees
        uses: actions/github-script@v7
        with:
          github-token: $
          script: |
            const { owner, repo } = context.repo
            const prNumber = context.payload.pull_request.number

            const response = await github.rest.issues.get({
              owner,
              repo,
              issue_number: prNumber,
            })
            const { assignees } = response.data

            if (assignees.length === 0) {
              await github.rest.issues.addAssignees({
                owner: owner,
                repo: repo,
                issue_number: prNumber,
                assignees: [context.actor]
              })
            }

I used the GitHub Actions provided by GitHub to write code that adds the developer(context.actor) who creates a PR to the Assignees by using JavaScript code.

Completed

Done! We’ve seen how to create a GitHub Actions that automatically adds the developer who created the PR to the Assignees every time a PR is created. If you set the Assignees manually, try using this GitHub Actions to set the Assignees automatically.

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