Skip to content

Instantly share code, notes, and snippets.

@hspedro
Last active February 27, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hspedro/c1e8c6c7d24fed6970f47a6a0644d4b1 to your computer and use it in GitHub Desktop.
Save hspedro/c1e8c6c7d24fed6970f47a6a0644d4b1 to your computer and use it in GitHub Desktop.
GitHub Action to Count Number of Approvals
# 1. Create a project variable named `MIN_APPROVALS_NEEDED`, must be an integer
# 2. Check approvals will use the token to fetch REST API and get the reviews for a current Pull Request
# 3. With JQ it parses all that has label "APPROVED" and count
# 4. If greater or equal, exit successfully. Otherwise, fail
#
env:
MIN_APPROVALS_NEEDED: ${{ vars.MIN_APPROVALS_NEEDED }}
jobs:
check-approvals:
if: github.event.review.state == 'approved'
runs-on: ubuntu-latest
steps:
- uses: octokit/request-action@v2.x
id: check_approvals
with:
route: GET /repos/${{ github.repository }}/pulls/${{ github.event.review.number }}/reviews
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- id: test_variables
run: |
JSON_RESPONSE='${{ steps.check_approvals.outputs.data }}'
CURRENT_APPROVALS_COUNT=$(echo $JSON_RESPONSE | jq -c '[.[] | select(.state | contains("APPROVED")) ] | length')
test $CURRENT_APPROVALS_COUNT -ge ${{ env.MIN_APPROVALS_NEEDED }} || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment