| @@ -1,6 +1,7 @@ |
| 1 | 1 | """Checks project health: URL reachability, GitHub repo stars, activity, and author match.""" |
| 2 | 2 | |
| 3 | 3 | import json |
| 4 | import logging |
| 4 | 5 | import os |
| 5 | 6 | import re |
| 6 | 7 | import sys |
| @@ -9,6 +10,12 @@ from datetime import datetime, timedelta, timezone |
| 9 | 10 | import requests |
| 10 | 11 | import yaml |
| 11 | 12 | |
| 13 | logging.basicConfig( |
| 14 | level=logging.INFO, |
| 15 | format="%(levelname)s [%(filename)s] %(message)s", |
| 16 | stream=sys.stderr, |
| 17 | ) |
| 18 | |
| 12 | 19 | PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 | 20 | DATA_PATH = os.path.join(PROJECT_ROOT, "awesome-privacy.yml") |
| 14 | 21 | DIFF_PATH = "/tmp/pr-diff.json" |
| @@ -112,8 +119,11 @@ def check_url(url): |
| 112 | 119 | headers={"User-Agent": USER_AGENT}, stream=True, |
| 113 | 120 | ) |
| 114 | 121 | resp.close() |
| 122 | if resp.status_code >= 400: |
| 123 | logging.warning("URL check failed for %s (HTTP %d)", url, resp.status_code) |
| 115 | 124 | return resp.status_code < 400 |
| 116 | | except Exception: |
| 125 | except Exception as exc: |
| 126 | logging.warning("URL check error for %s: %s", url, exc) |
| 117 | 127 | return True |
| 118 | 128 | |
| 119 | 129 | |
| @@ -133,23 +143,37 @@ def parse_github_field(value): |
| 133 | 143 | return None, None |
| 134 | 144 | |
| 135 | 145 | |
| 136 | | def fetch_repo(owner, repo, token): |
| 137 | | """Fetch GitHub repo metadata, returning None on any error.""" |
| 146 | def _gh_get(path, token, params=None, label=""): |
| 147 | """GET a GitHub API endpoint. Returns parsed JSON on 200, None otherwise.""" |
| 148 | headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT} |
| 149 | if token: |
| 150 | headers["Authorization"] = f"token {token}" |
| 138 | 151 | try: |
| 139 | | headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT} |
| 140 | | if token: |
| 141 | | headers["Authorization"] = f"token {token}" |
| 142 | 152 | resp = requests.get( |
| 143 | | f"https://api.github.com/repos/{owner}/{repo}", |
| 144 | | headers=headers, timeout=TIMEOUT, |
| 153 | f"https://api.github.com{path}", |
| 154 | headers=headers, timeout=TIMEOUT, params=params, |
| 145 | 155 | ) |
| 156 | remaining = resp.headers.get("X-RateLimit-Remaining") |
| 157 | if remaining is not None: |
| 158 | try: |
| 159 | if int(remaining) < 100: |
| 160 | logging.warning("[%s] GitHub rate limit low: %s/%s remaining", |
| 161 | label, remaining, resp.headers.get("X-RateLimit-Limit")) |
| 162 | except ValueError: |
| 163 | pass |
| 146 | 164 | if resp.status_code == 200: |
| 147 | 165 | return resp.json() |
| 148 | | except Exception: |
| 149 | | pass |
| 166 | logging.warning("[%s] HTTP %d from %s", label, resp.status_code, path) |
| 167 | except Exception as exc: |
| 168 | logging.warning("[%s] request error for %s: %s", label, path, exc) |
| 150 | 169 | return None |
| 151 | 170 | |
| 152 | 171 | |
| 172 | def fetch_repo(owner, repo, token): |
| 173 | """Fetch GitHub repo metadata, returning None on any error.""" |
| 174 | return _gh_get(f"/repos/{owner}/{repo}", token, label="repos") |
| 175 | |
| 176 | |
| 153 | 177 | def load_yaml_data(): |
| 154 | 178 | """Load the head YAML, returning None on any error.""" |
| 155 | 179 | try: |
| @@ -224,65 +248,42 @@ def _commit_has_bot(commit, bot_set): |
| 224 | 248 | |
| 225 | 249 | def check_ai_commits(owner, repo, token): |
| 226 | 250 | """Return AI_CODE_MSG if recent commits contain significant AI bot activity.""" |
| 227 | | try: |
| 228 | | headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT} |
| 229 | | if token: |
| 230 | | headers["Authorization"] = f"token {token}" |
| 231 | | resp = requests.get( |
| 232 | | f"https://api.github.com/repos/{owner}/{repo}/commits", |
| 233 | | headers=headers, timeout=TIMEOUT, params={"per_page": 100}, |
| 234 | | ) |
| 235 | | if resp.status_code != 200: |
| 236 | | return None |
| 237 | | commits = resp.json() |
| 238 | | if not commits: |
| 239 | | return None |
| 240 | | bot_set = {a.lower() for a in AI_BOT_AUTHORS} |
| 241 | | count = sum(1 for c in commits if _commit_has_bot(c, bot_set)) |
| 242 | | if count / len(commits) >= AI_COMMIT_RATIO: |
| 243 | | return AI_CODE_MSG |
| 244 | | except Exception: |
| 245 | | pass |
| 251 | commits = _gh_get(f"/repos/{owner}/{repo}/commits", token, |
| 252 | params={"per_page": 100}, label="commits") |
| 253 | if not commits: |
| 254 | return None |
| 255 | bot_set = {a.lower() for a in AI_BOT_AUTHORS} |
| 256 | count = sum(1 for c in commits if _commit_has_bot(c, bot_set)) |
| 257 | if count / len(commits) >= AI_COMMIT_RATIO: |
| 258 | return AI_CODE_MSG |
| 246 | 259 | return None |
| 247 | 260 | |
| 248 | 261 | |
| 249 | 262 | def check_security_alerts(owner, repo, token): |
| 250 | 263 | """Return SECURITY_MSG if the repo has open critical/high Dependabot alerts.""" |
| 251 | | try: |
| 252 | | headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT} |
| 253 | | if token: |
| 254 | | headers["Authorization"] = f"token {token}" |
| 255 | | resp = requests.get( |
| 256 | | f"https://api.github.com/repos/{owner}/{repo}/dependabot/alerts", |
| 257 | | headers=headers, timeout=TIMEOUT, |
| 258 | | params={"state": "open", "severity": "critical,high", "per_page": 1}, |
| 259 | | ) |
| 260 | | if resp.status_code == 200 and resp.json(): |
| 261 | | return SECURITY_MSG |
| 262 | | except Exception: |
| 263 | | pass |
| 264 | alerts = _gh_get(f"/repos/{owner}/{repo}/dependabot/alerts", token, |
| 265 | params={"state": "open", "severity": "critical,high", "per_page": 1}, |
| 266 | label="dependabot") |
| 267 | if alerts: |
| 268 | return SECURITY_MSG |
| 264 | 269 | return None |
| 265 | 270 | |
| 266 | 271 | |
| 267 | 272 | def check_spam_prs(pr_user, token): |
| 268 | 273 | """Return list of spam findings based on recent PR activity.""" |
| 269 | 274 | if not pr_user or not token: |
| 275 | logging.warning("check_spam_prs skipped: %s", "no PR_USER" if not pr_user else "no GITHUB_TOKEN") |
| 270 | 276 | return [] |
| 271 | 277 | try: |
| 272 | 278 | since = (datetime.now(timezone.utc) - timedelta(days=SPAM_WINDOW_DAYS)).strftime("%Y-%m-%d") |
| 273 | | headers = { |
| 274 | | "Accept": "application/vnd.github.v3+json", |
| 275 | | "User-Agent": USER_AGENT, |
| 276 | | "Authorization": f"token {token}", |
| 277 | | } |
| 278 | | resp = requests.get( |
| 279 | | "https://api.github.com/search/issues", |
| 280 | | headers=headers, timeout=TIMEOUT, |
| 281 | | params={"q": f"type:pr author:{pr_user} created:>={since}"}, |
| 282 | | ) |
| 283 | | if resp.status_code != 200: |
| 279 | data = _gh_get("/search/issues", token, |
| 280 | params={"q": f"type:pr author:{pr_user} created:>={since}", "per_page": 100}, |
| 281 | label="spam") |
| 282 | if not data: |
| 284 | 283 | return [] |
| 285 | | items = resp.json().get("items", []) |
| 284 | items = data.get("items", []) |
| 285 | logging.info("check_spam_prs: %d total PRs for %s (fetched %d)", |
| 286 | data.get("total_count", 0), pr_user, len(items)) |
| 286 | 287 | this_repo = os.environ.get("GITHUB_REPOSITORY", "Lissy93/awesome-privacy").lower() |
| 287 | 288 | awesome_count = 0 |
| 288 | 289 | distinct_repos = set() |
| @@ -296,13 +297,16 @@ def check_spam_prs(pr_user, token): |
| 296 | 297 | repo_name = repo_url.rstrip("/").split("/")[-1].lower() |
| 297 | 298 | if repo_name.startswith("awesome-"): |
| 298 | 299 | awesome_count += 1 |
| 300 | logging.info("check_spam_prs: %d awesome-* PRs, %d distinct repos", |
| 301 | awesome_count, len(distinct_repos)) |
| 299 | 302 | findings = [] |
| 300 | 303 | if awesome_count >= SPAM_AWESOME_THRESHOLD: |
| 301 | 304 | findings.append(SPAM_AWESOME_MSG) |
| 302 | 305 | if len(distinct_repos) >= SPAM_DISTINCT_REPO_THRESHOLD: |
| 303 | 306 | findings.append(SPAM_MANY_MSG) |
| 304 | 307 | return findings |
| 305 | | except Exception: |
| 308 | except Exception as exc: |
| 309 | logging.warning("check_spam_prs error for %s: %s", pr_user, exc) |
| 306 | 310 | return [] |
| 307 | 311 | |
| 308 | 312 | |
| @@ -310,18 +314,13 @@ def check_new_account(pr_user, token): |
| 310 | 314 | """Return NEW_ACCOUNT_MSG if the PR author's GitHub account is very new.""" |
| 311 | 315 | if not pr_user or not token: |
| 312 | 316 | return None |
| 317 | data = _gh_get(f"/users/{pr_user}", token, label="user") |
| 318 | if not data: |
| 319 | return None |
| 320 | created = data.get("created_at") |
| 321 | if not created: |
| 322 | return None |
| 313 | 323 | try: |
| 314 | | headers = {"Accept": "application/vnd.github.v3+json", "User-Agent": USER_AGENT} |
| 315 | | headers["Authorization"] = f"token {token}" |
| 316 | | resp = requests.get( |
| 317 | | f"https://api.github.com/users/{pr_user}", |
| 318 | | headers=headers, timeout=TIMEOUT, |
| 319 | | ) |
| 320 | | if resp.status_code != 200: |
| 321 | | return None |
| 322 | | created = resp.json().get("created_at") |
| 323 | | if not created: |
| 324 | | return None |
| 325 | 324 | created_dt = datetime.fromisoformat(created.replace("Z", "+00:00")) |
| 326 | 325 | if (datetime.now(timezone.utc) - created_dt).days < NEW_ACCOUNT_DAYS: |
| 327 | 326 | return NEW_ACCOUNT_MSG |
| @@ -439,8 +438,13 @@ def check_repo_signals(diff, pr_user, token, pr_body=""): |
| 439 | 438 | def main(): |
| 440 | 439 | findings = [] |
| 441 | 440 | try: |
| 441 | pr_user = os.environ.get("PR_USER", "") |
| 442 | token = os.environ.get("GITHUB_TOKEN", "") |
| 443 | logging.info("PR_USER=%s, GITHUB_TOKEN=%s", pr_user or "(empty)", "present" if token else "MISSING") |
| 444 | |
| 442 | 445 | diff = load_diff(DIFF_PATH) |
| 443 | 446 | if not diff: |
| 447 | logging.info("No diff file at %s, nothing to check", DIFF_PATH) |
| 444 | 448 | with open(FINDINGS_PATH, "w") as f: |
| 445 | 449 | json.dump(findings, f) |
| 446 | 450 | sys.exit(0) |
| @@ -450,9 +454,7 @@ def main(): |
| 450 | 454 | if finding: |
| 451 | 455 | findings.append(finding) |
| 452 | 456 | |
| 453 | | pr_user = os.environ.get("PR_USER", "") |
| 454 | 457 | pr_body = os.environ.get("PR_BODY", "") |
| 455 | | token = os.environ.get("GITHUB_TOKEN", "") |
| 456 | 458 | |
| 457 | 459 | findings.extend(check_spam_prs(pr_user, token)) |
| 458 | 460 | |
| @@ -465,9 +467,10 @@ def main(): |
| 465 | 467 | findings.append(finding) |
| 466 | 468 | |
| 467 | 469 | findings.extend(check_repo_signals(diff, pr_user, token, pr_body)) |
| 468 | | except Exception: |
| 469 | | pass |
| 470 | except Exception as exc: |
| 471 | logging.error("Unhandled error in main: %s", exc, exc_info=True) |
| 470 | 472 | |
| 473 | logging.info("Writing %d finding(s) to %s", len(findings), FINDINGS_PATH) |
| 471 | 474 | with open(FINDINGS_PATH, "w") as f: |
| 472 | 475 | json.dump(findings, f) |
| 473 | 476 | sys.exit(0) |