lib/checks/check-review-ready.py

104 lines · 2928 bytes

  1"""Decides whether a PR is ready for maintainer review.
  2
  3Conditions: 2+ approvals from external contributors AND all CI checks passing.
  4
  5Reads:
  6  pr-meta/reviews.json       — array of {user, state} from GitHub API
  7  pr-meta/check-runs.json    — array of {status, conclusion} from GitHub API
  8  pr-meta/already-notified.txt — "true" if notification comment already exists
  9
 10Writes:
 11  pr-meta/action.txt         — "notify" or "skip"
 12"""
 13
 14import json
 15import logging
 16import os
 17import sys
 18
 19sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 20import utils
 21
 22utils.setup_logging()
 23logger = logging.getLogger(__name__)
 24
 25WORK_DIR = "pr-meta"
 26MAINTAINER = "Lissy93"
 27REQUIRED_APPROVALS = 2
 28PASSING_CONCLUSIONS = {"success", "skipped", "neutral"}
 29
 30
 31def read_json(filename):
 32    """Load a JSON file from the work directory, or empty list on error."""
 33    try:
 34        with open(os.path.join(WORK_DIR, filename)) as f:
 35            return json.load(f)
 36    except Exception:
 37        return []
 38
 39
 40def count_external_approvals(reviews):
 41    """Count unique non-maintainer users who approved."""
 42    approvers = {
 43        r["user"]
 44        for r in reviews
 45        if r.get("state") == "APPROVED"
 46        and r.get("user", "").lower() != MAINTAINER.lower()
 47    }
 48    return len(approvers)
 49
 50
 51def all_checks_passing(check_runs):
 52    """Return True if every check run completed successfully."""
 53    if not check_runs:
 54        return False
 55    return all(
 56        cr.get("status") == "completed"
 57        and cr.get("conclusion") in PASSING_CONCLUSIONS
 58        for cr in check_runs
 59    )
 60
 61
 62def already_notified():
 63    """Return True if the notification comment already exists on the PR."""
 64    try:
 65        with open(os.path.join(WORK_DIR, "already-notified.txt")) as f:
 66            return f.read().strip().lower() == "true"
 67    except Exception:
 68        return False
 69
 70
 71def write_action(action):
 72    os.makedirs(WORK_DIR, exist_ok=True)
 73    with open(os.path.join(WORK_DIR, "action.txt"), "w") as f:
 74        f.write(action)
 75
 76
 77def main():
 78    logger.info("Checking whether the PR is ready for maintainer review")
 79    if already_notified():
 80        logger.info("Maintainer already notified, action=skip")
 81        write_action("skip")
 82        return
 83
 84    reviews = read_json("reviews.json")
 85    approvals = count_external_approvals(reviews)
 86    logger.info("%d external approval(s), need %d", approvals, REQUIRED_APPROVALS)
 87
 88    if approvals < REQUIRED_APPROVALS:
 89        logger.info("Not enough approvals, action=skip")
 90        write_action("skip")
 91        return
 92
 93    check_runs = read_json("check-runs.json")
 94    if not all_checks_passing(check_runs):
 95        logger.info("Not all %d check run(s) passing, action=skip", len(check_runs))
 96        write_action("skip")
 97        return
 98
 99    logger.info("Approvals met and all checks passing, action=notify")
100    write_action("notify")
101
102
103if __name__ == "__main__":
104    main()