Skip to content

travelperk/label-requires-reviews-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Label requires reviews action Maintainability Rating build

This is a Github Action to modify the required minimum number of approving reviews on a Pull Request depending on the set of labels applied to it.

Usage

Create workflow

Create a workflow (eg: .github/workflows/label-reviews.yml see Creating a Workflow file) to utilize this action with content:

# This workflow will set a number or reviewers depending on the labels
name: Label Reviews
# Trigger the workflow on pull requests
on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - labeled
      - unlabeled
  pull_request_review:
    types:
      - submitted
      - edited
      - dismissed
jobs:
  require-reviewers:
    # Optional: skip check if no relevant label is present
    # This needs to be kept in sync with the labels being checked
    if: ${{ contains(github.event.pull_request.labels.*.name, 'typescript') || contains(github.event.pull_request.labels.*.name, 'migration') }}
    runs-on: ubuntu-latest
    steps:
      - name: Require-reviewers
        uses: travelperk/label-requires-reviews-action@1.3.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          rules_yaml: | # define which PR labels require how many aprroving reviewers
            typescript: 2
            migration: 5

rules_yaml is a (yaml-formatted multi-line) string of pairs label: # of approving reviewers. With the example configuration above, this check will fail on a Pull Request that has the typescript label until two or more reviewers have approved it. If instead the Pull Request has the migration label it will require five, in case both labels are present it will also require five.

rules_yaml also supports an array of objects format, as well as being defined in an external file (but then the workflow also needs a checkout step), see documentation of versions earlier than 1.3.0.

Enforce the requirement

To make this check mandatory you need to specify it on the Branch protection rule section of the repository settings like the example:

Marking the action as required

According to this configuration, the main branch is protected by the option Required approving reviews set to 1. That means that any Pull Request that wants to merge code into main would have to be approved by at least one reviewer.

By checking Require status checks to pass before merging and require-reviewers anytime the Pull Request gets a new review this action will fire and the Pull Request is labeled with one of the labels that require more than one approving review blocking the possibility of merging until this label required number of approving reviews is reached.

Saving tip

Since Github Workflow jobs can have conditionals, and in the workflow you can directly access some action metadata.

You can avoid checking out the code and running this action if you know the issue does not contain any of the labels that will trigger it, that will set the action as skipped and will never run.

The drawback is that the list of labels will be duplicated, but you can save a lot of actions time.