Last active
February 27, 2023 19:29
-
-
Save hspedro/c1e8c6c7d24fed6970f47a6a0644d4b1 to your computer and use it in GitHub Desktop.
GitHub Action to Count Number of Approvals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/[email protected] | |
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