|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Fail if the GitHub repo has unresolved open pull requests (REQ-REL-003). |
| 3 | +# Allowed temporary exceptions: label hold|do-not-merge AND a future review-by ISO date |
| 4 | +# in the PR body (or first few comments are not fetched; body must carry the date). |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" |
| 8 | +cd "${ROOT}" |
| 9 | + |
| 10 | +if ! command -v gh >/dev/null 2>&1; then |
| 11 | + echo "ERROR: gh CLI is required (REQ-REL-003)." >&2 |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +if ! gh auth status >/dev/null 2>&1; then |
| 16 | + echo "ERROR: gh is not authenticated — log in for nowo-tech (REQ-REL-003)." >&2 |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +if ! command -v python3 >/dev/null 2>&1; then |
| 21 | + echo "ERROR: python3 is required to evaluate open PR exceptions (REQ-REL-003)." >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +PRS_JSON="$(gh pr list --state open --limit 100 --json number,title,labels,body,url)" |
| 26 | + |
| 27 | +export PRS_JSON |
| 28 | +python3 <<'PY' |
| 29 | +import json |
| 30 | +import os |
| 31 | +import re |
| 32 | +import sys |
| 33 | +from datetime import date |
| 34 | +
|
| 35 | +prs = json.loads(os.environ.get("PRS_JSON") or "[]") |
| 36 | +if not prs: |
| 37 | + print("OK: no open pull requests (REQ-REL-003)") |
| 38 | + sys.exit(0) |
| 39 | +
|
| 40 | +HOLD_LABELS = {"hold", "do-not-merge"} |
| 41 | +# review-by: 2026-09-01 | review_by: 2026-09-01 | review-by 2026-09-01 |
| 42 | +REVIEW_BY_RE = re.compile( |
| 43 | + r"(?i)\breview[-_ ]?by\b\s*[:=]?\s*(\d{4}-\d{2}-\d{2})" |
| 44 | +) |
| 45 | +
|
| 46 | +today = date.today() |
| 47 | +unresolved = [] |
| 48 | +held_ok = [] |
| 49 | +
|
| 50 | +for pr in prs: |
| 51 | + labels = {str(l.get("name", "")).lower() for l in (pr.get("labels") or [])} |
| 52 | + body = pr.get("body") or "" |
| 53 | + m = REVIEW_BY_RE.search(body) |
| 54 | + review_by = None |
| 55 | + if m: |
| 56 | + try: |
| 57 | + review_by = date.fromisoformat(m.group(1)) |
| 58 | + except ValueError: |
| 59 | + review_by = None |
| 60 | +
|
| 61 | + has_hold = bool(labels & HOLD_LABELS) |
| 62 | + hold_valid = has_hold and review_by is not None and review_by >= today |
| 63 | +
|
| 64 | + entry = f"#{pr['number']} {pr.get('title') or ''} ({pr.get('url') or ''})" |
| 65 | + if hold_valid: |
| 66 | + held_ok.append(f"{entry} [hold until {review_by.isoformat()}]") |
| 67 | + else: |
| 68 | + reasons = [] |
| 69 | + if not has_hold: |
| 70 | + reasons.append("missing hold/do-not-merge label") |
| 71 | + if review_by is None: |
| 72 | + reasons.append("missing review-by YYYY-MM-DD in body") |
| 73 | + elif review_by < today: |
| 74 | + reasons.append(f"review-by {review_by.isoformat()} expired") |
| 75 | + unresolved.append(f"{entry} — {', '.join(reasons)}") |
| 76 | +
|
| 77 | +if held_ok: |
| 78 | + print("Allowed hold exceptions:") |
| 79 | + for line in held_ok: |
| 80 | + print(f" {line}") |
| 81 | +
|
| 82 | +if unresolved: |
| 83 | + print("ERROR: unresolved open pull requests (REQ-REL-003):", file=sys.stderr) |
| 84 | + for line in unresolved: |
| 85 | + print(f" {line}", file=sys.stderr) |
| 86 | + print( |
| 87 | + "Merge, close with reason, or label hold/do-not-merge with a future " |
| 88 | + "review-by: YYYY-MM-DD in the PR body.", |
| 89 | + file=sys.stderr, |
| 90 | + ) |
| 91 | + sys.exit(1) |
| 92 | +
|
| 93 | +print(f"OK: {len(held_ok)} open PR(s) covered by valid hold exceptions (REQ-REL-003)") |
| 94 | +sys.exit(0) |
| 95 | +PY |
0 commit comments