From a51b3e987a40110e069038815137b6b111f99d11 Mon Sep 17 00:00:00 2001 From: Peter Spark Date: Tue, 7 Apr 2026 02:10:11 +0000 Subject: [PATCH] feat(clients): optional project_slug and gitea_repo on ingest context PHP + Node reporters read ERROR_REPORTING_* env; ingest-api tests run contract on clients/. Paperclip-Agent: 11179220-3cec-4f0f-880d-2771ff64fce8 Paperclip-Issue: BIT-1310 Paperclip-Run: 35ce1c21-9862-4c60-8b58-441d53b4b3bd --- clients/node/errorReporter.mjs | 3 + clients/php/ErrorReporter.php | 2 + ingest-api/package.json | 2 +- .../check_error_reporter_context_contract.py | 126 ++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tools/ci/check_error_reporter_context_contract.py diff --git a/clients/node/errorReporter.mjs b/clients/node/errorReporter.mjs index bea54a4..873c16f 100644 --- a/clients/node/errorReporter.mjs +++ b/clients/node/errorReporter.mjs @@ -12,6 +12,9 @@ * ERROR_REPORTING_TOKEN — ingest API key (Bearer token) * ERROR_REPORTING_ENDPOINT — base URL (default: https://errors.bitreport.pt) * ERROR_REPORTING_DISABLED — set to "1" to disable + * ERROR_REPORTING_PROJECT_NAME — display name in ingest context (optional) + * ERROR_REPORTING_PROJECT_SLUG — stable product slug (optional) + * ERROR_REPORTING_GITEA_REPO — canonical owner/repo on Gitea (optional) */ let _token = ''; diff --git a/clients/php/ErrorReporter.php b/clients/php/ErrorReporter.php index d1e4099..db6521d 100644 --- a/clients/php/ErrorReporter.php +++ b/clients/php/ErrorReporter.php @@ -14,6 +14,8 @@ declare(strict_types=1); * ERROR_REPORTING_TOKEN — ingest API key (required) * ERROR_REPORTING_ENDPOINT — base URL (default: https://errors.bitreport.pt) * ERROR_REPORTING_DISABLED — set to "1" to disable + * ERROR_REPORTING_PROJECT_SLUG — stable product slug for ERRSIG / Paperclip routing (optional) + * ERROR_REPORTING_GITEA_REPO — canonical owner/repo on Gitea, e.g. BitReport.pt/my-product (optional) */ namespace BitReport; diff --git a/ingest-api/package.json b/ingest-api/package.json index c3af015..bff0719 100644 --- a/ingest-api/package.json +++ b/ingest-api/package.json @@ -9,7 +9,7 @@ "build": "tsc", "start": "node dist/server.js", "typecheck": "tsc --noEmit", - "test": "vitest run", + "test": "vitest run && python3 ../tools/ci/check_error_reporter_context_contract.py --path ../clients", "test:watch": "vitest" }, "dependencies": { diff --git a/tools/ci/check_error_reporter_context_contract.py b/tools/ci/check_error_reporter_context_contract.py new file mode 100644 index 0000000..57c0291 --- /dev/null +++ b/tools/ci/check_error_reporter_context_contract.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +"""CI gate: shipped ingest clients must document project_slug + gitea_repo routing.""" +from __future__ import annotations + +import argparse +import fnmatch +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] + +SCAN_EXTENSIONS = {".php", ".js", ".mjs", ".cjs", ".ts", ".py"} +EXCLUDE_PATTERNS = ( + ".git/*", + "node_modules/*", + "vendor/*", + ".venv/*", + "venv/*", + "__pycache__/*", + "dist/*", + "build/*", +) + +REPORTER_MARKERS = ( + "php-error", + "js-error", + "error-monitoring", + "reporterror(", + "reportmessage(", + "sendtoingestapi", + "/v1/ingest/events", +) + +SLUG_MARKERS = ( + "project_slug", + "projectslug", + "error_monitoring_project_slug", + "error_reporting_project_slug", +) + +REPO_MARKERS = ( + "gitea_repo", + "gitearepo", + "error_monitoring_gitea_repo", + "error_reporting_gitea_repo", +) + + +def _is_excluded(rel_path: str) -> bool: + norm = rel_path.replace("\\", "/") + if "/node_modules/" in norm or norm.startswith("node_modules/"): + return True + if "/vendor/" in norm or norm.startswith("vendor/"): + return True + return any(fnmatch.fnmatch(rel_path, pattern) for pattern in EXCLUDE_PATTERNS) + + +def _is_candidate_file(rel_path: str, text_lower: str) -> bool: + if _is_excluded(rel_path): + return False + if Path(rel_path).suffix.lower() not in SCAN_EXTENSIONS: + return False + name = Path(rel_path).name.lower() + has_reporter_naming = ( + "reporter" in name + or "errorreporter" in text_lower + or "errormonitoringreporter" in text_lower + ) + if not has_reporter_naming: + return False + return any(marker in text_lower for marker in REPORTER_MARKERS) + + +def _contract_ok(text_lower: str) -> tuple[bool, bool]: + has_slug = any(marker in text_lower for marker in SLUG_MARKERS) + has_repo = any(marker in text_lower for marker in REPO_MARKERS) + return has_slug, has_repo + + +def main() -> int: + parser = argparse.ArgumentParser(description="Verify error reporter context contract in repository files.") + parser.add_argument( + "--path", + default=str(REPO_ROOT), + help="Repository root to scan.", + ) + args = parser.parse_args() + + root = Path(args.path).resolve() + if not root.is_dir(): + print(f"error: scan root does not exist: {root}") + return 2 + + failures: list[tuple[str, bool, bool]] = [] + candidates = 0 + + for file_path in root.rglob("*"): + if not file_path.is_file(): + continue + rel = file_path.relative_to(root).as_posix() + if _is_excluded(rel): + continue + try: + content = file_path.read_text(encoding="utf-8", errors="ignore") + except OSError: + continue + low = content.lower() + if not _is_candidate_file(rel, low): + continue + candidates += 1 + has_slug, has_repo = _contract_ok(low) + if not (has_slug and has_repo): + failures.append((rel, has_slug, has_repo)) + + print(f"reporter_contract_candidates={candidates}") + if not failures: + print("reporter_context_contract=ok") + return 0 + + print("reporter_context_contract=failed") + for rel, has_slug, has_repo in failures: + print(f"- {rel}: has_project_slug={int(has_slug)} has_gitea_repo={int(has_repo)}") + return 1 + + +if __name__ == "__main__": + raise SystemExit(main())