GitHub Action
Label requires reviews
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.
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:
runs-on: ubuntu-latest
steps:
- name: Require-reviewers
uses: travelperk/[email protected]
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 earlier versions https://github.com/travelperk/label-requires-reviews-action/tree/1.3.0#readme.
To make this check mandatory you need to specify it on the Branch protection rule
section of the repository settings like the example:
According to this configuration, the master
branch is protected by the option Required approving reviews
set to 1
. That means that any Pull Request that wants to merge code into master 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.
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.