Skip to content

Commit

Permalink
Add review checking script
Browse files Browse the repository at this point in the history
Simplify with workflow event

Remove condition for approval

Also run when unlabeled

Colourise output
  • Loading branch information
jfrost-mo committed Jan 18, 2024
1 parent 19fe726 commit c8b476e
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions .github/workflows/check-required-reviews.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ name: Check required reviews

on:
pull_request:
types: [labeled]
types: [labeled, unlabeled]
pull_request_review:
types: [submitted]

jobs:
check-reviews:
# Only run if passing the basic GitHub review requirements.
# TODO: Removed for testing, so uncomment.
# if: github.event.review.state == 'APPROVED'
if: contains(github.event.pull_request.labels.*.name, 'full_review')
runs-on: ubuntu-latest
steps:
- name: Set up Python
Expand All @@ -20,19 +18,41 @@ jobs:

- name: Check reviews
env:
PR_REF: github.ref
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: python
run: |
"""Check there are enough approving reviews."""
import json
import os
import re
import subprocess
import sys
ref = os.getenv("PR_REF")
print(f"Ref: {ref}")
REQUIRED_REVIEWS = 3
# Get info with gh pr -R MetOffice/CSET view $PR_NUMBER --json
# labels,reviews If full_review label exists parse with program to see
# if enough reviews. Newer reviews by the same person should
# invalidate older reviews.
repo = "${{ github.repository }}"
pr_number = "${{ github.event.pull_request.number }}"
sys.exit(0)
# Get info on PR labels and reviews.
pr_info = json.loads(
subprocess.run(
("gh", "pr", "--json", "reviews", "--repo", repo, "view", pr_number),
check=True,
capture_output=True,
timeout=60,
text=True,
).stdout
)
# Check for enough reviews. Newer reviews invalidate older ones.
approving_reviewers = set()
for review in sorted(pr_info["reviews"], key=lambda r: r["submittedAt"]):
if review["state"] == "APPROVED":
approving_reviewers.add(review["author"]["login"])
elif review["state"] == "CHANGES_REQUESTED":
approving_reviewers.discard(review["author"]["login"])
if len(approving_reviewers) < REQUIRED_REVIEWS:
print(f"\033[31m❌ Not enough reviews.\033[0m ({len(approving_reviewers)}/{REQUIRED_REVIEWS})")
sys.exit(1)
print(f"\033[32m✔ Sufficient reviews.\033[0m ({len(approving_reviewers)}/{REQUIRED_REVIEWS})")

0 comments on commit c8b476e

Please sign in to comment.