feat(webhook): is_repo_enabled reads .pr-review.json:enabled
This commit is contained in:
+28
-1
@@ -34,14 +34,16 @@ Env:
|
||||
(optional) request-body cap, default 10 MiB
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import urllib.parse
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
from ai_review import review_pr
|
||||
from ai_review import gitea_get, review_pr
|
||||
|
||||
try:
|
||||
import feedback_harvest # optional — absent in CI-step pod, present in
|
||||
@@ -109,6 +111,31 @@ def _labels_have_ai_review(labels) -> bool:
|
||||
return _labels_have(labels, AI_REVIEW_LABEL)
|
||||
|
||||
|
||||
def is_repo_enabled(api: str, repo: str, ref: str, token: str) -> bool:
|
||||
"""True iff `.pr-review.json` on `ref` has `"enabled": true`.
|
||||
|
||||
Reads from the given ref (typically the PR's base ref). False on any
|
||||
failure: 404, parse error, missing file, missing `enabled`, wrong type.
|
||||
The bool-coerce of `.get("enabled") is True` rejects the common
|
||||
gotchas (`null`, `1`, `"yes"`, missing field all yield False).
|
||||
"""
|
||||
code, raw = gitea_get(
|
||||
api, repo,
|
||||
"contents/.pr-review.json?ref=" + urllib.parse.quote(ref, safe=""),
|
||||
token,
|
||||
)
|
||||
if code != 200:
|
||||
return False
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
content_b64 = data.get("content", "").replace("\n", "")
|
||||
decoded = base64.b64decode(content_b64).decode("utf-8", errors="replace")
|
||||
cfg = json.loads(decoded)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
return False
|
||||
return isinstance(cfg, dict) and cfg.get("enabled") is True
|
||||
|
||||
|
||||
def _verify_signature(raw_body: bytes, headers) -> bool:
|
||||
if not WEBHOOK_SECRET:
|
||||
return False # refuse to run without a configured secret
|
||||
|
||||
Reference in New Issue
Block a user