From 83f75fe747542e00f84ccc7ae54b5d42a41b285b Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 23 Oct 2026 11:37:01 +0200 Subject: [PATCH] Add weekly upstream drift alert --- .github/workflows/upstream-drift.yml | 237 +++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 .github/workflows/upstream-drift.yml diff --git a/.github/workflows/upstream-drift.yml b/.github/workflows/upstream-drift.yml new file mode 100644 index 00000000..0cfd855c --- /dev/null +++ b/.github/workflows/upstream-drift.yml @@ -0,0 +1,237 @@ +name: Upstream drift alert + +on: + schedule: + - cron: "17 8 * * 1" + workflow_dispatch: + +permissions: + contents: read + issues: write + +concurrency: + group: upstream-drift-alert + cancel-in-progress: false + +jobs: + report: + runs-on: ubuntu-latest + steps: + - name: Refresh upstream drift issue + env: + GH_TOKEN: ${{ github.token }} + run: | + python3 - <<'PY' + import base64 + import datetime as dt + import json + import os + import re + import subprocess + import sys + + REPOSITORY = "aafontoura/pytr" + UPSTREAM = "pytr-org/pytr" + LABEL = "upstream-drift" + TITLE = "Upstream drift: pytr-org/pytr" + CLIENT = os.environ.get("GH_API_CLI", "gh") + AUTH_TERMS = re.compile( + r"\b(?:login|auth(?:entication|orization)?|credentials?|passwords?|pin|otp|web[\s_-]*login)\b", + re.IGNORECASE, + ) + NETWORK_TERMS = re.compile( + r"\b(?:network|hosts?|hostnames?|domains?|ip addresses?|urls?|endpoints?|server address)\b", + re.IGNORECASE, + ) + URL = re.compile(r"\b(?:https?|wss?|ftp)://[^\s\"'<>]+", re.IGNORECASE) + HOST = re.compile( + r"(?|": + value = value.replace(character, "\\" + character) + return value + + def issue_body(ahead, behind, commits): + checked = dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%d %H:%M UTC") + lines = [ + "## In sync" if behind == 0 else "## Upstream drift detected", + "", + f"Compared `{UPSTREAM}:master` with `aafontoura/pytr:hardened` using the GitHub Compare API.", + f"- **Upstream commits missing from hardened:** {behind}", + f"- **Hardened-only commits:** {ahead}", + f"- **Last checked:** {checked}", + ] + if commits: + lines.extend([ + "", + "### New upstream commits", + "", + "| Date | SHA | Subject | Security-relevant | Link |", + "| --- | --- | --- | --- | --- |", + ]) + for commit in commits: + sha = commit["sha"] + if not re.fullmatch(r"[0-9a-f]{40}", sha): + raise RuntimeError(f"Invalid commit SHA from GitHub: {sha!r}") + metadata = commit.get("commit", {}) + date = metadata.get("author", {}).get("date") or metadata.get("committer", {}).get("date", "") + subject = (metadata.get("message", "").splitlines() or ["(no subject)"])[0] + reasons = security_reasons(commit) + relevance = "Yes: " + ", ".join(reasons) if reasons else "No" + link = f"https://github.com/{UPSTREAM}/commit/{sha}" + lines.append( + f"| {cell(date[:10])} | `{cell(sha[:7])}` | {cell(subject)} | {cell(relevance)} | [commit]({link}) |" + ) + else: + lines.extend(["", "No upstream commits are missing from `hardened`."]) + return "\n".join(lines) + + def main(): + if os.environ.get("GITHUB_REPOSITORY", "").lower() != REPOSITORY: + raise RuntimeError(f"This workflow may only update issues in {REPOSITORY}") + primary_pages = api_pages( + f"repos/{UPSTREAM}/compare/master...aafontoura:hardened?per_page=100", paginate=True + ) + reverse_pages = api_pages( + f"repos/{UPSTREAM}/compare/aafontoura:hardened...master?per_page=100", paginate=True + ) + if not primary_pages or not reverse_pages: + raise RuntimeError("GitHub Compare API returned no result") + primary = primary_pages[0] + ahead = int(primary["ahead_by"]) + behind = int(primary["behind_by"]) + reverse = reverse_pages[0] + if int(reverse["ahead_by"]) != behind or int(reverse["behind_by"]) != ahead: + raise RuntimeError("The directional GitHub Compare API results do not agree") + commits = [commit for page in reverse_pages for commit in page.get("commits", [])] + if len(commits) != behind: + raise RuntimeError(f"Expected {behind} upstream commits, received {len(commits)}; refusing an incomplete report") + commits.sort(key=lambda commit: ( + commit.get("commit", {}).get("author", {}).get("date") + or commit.get("commit", {}).get("committer", {}).get("date", "") + )) + + dry_run = os.environ.get("DRY_RUN", "").lower() in ("1", "true", "yes") + repo_labels = flatten_pages(api_pages(f"repos/{REPOSITORY}/labels?per_page=100", paginate=True)) + label_exists = any(label.get("name") == LABEL for label in repo_labels) + issues = [] + if label_exists: + issues = [ + issue for issue in flatten_pages(api_pages( + f"repos/{REPOSITORY}/issues?state=all&labels={LABEL}&per_page=100", paginate=True + )) + if "pull_request" not in issue + ] + issues.sort(key=lambda issue: (issue.get("state") != "open", issue["number"])) + existing = issues[0] if issues else None + body = issue_body(ahead, behind, commits) + + if dry_run: + if existing: + action = f"update issue #{existing['number']} ({'open' if behind else 'close'})" + elif behind: + action = "create the drift issue" + else: + action = "make no issue change because no drift issue exists" + print(f"DRY RUN: would {action} with label {LABEL!r}. No writes performed.\n") + print(body) + return + + if not existing and behind == 0: + return + if behind and not label_exists: + api(f"repos/{REPOSITORY}/labels", "POST", { + "name": LABEL, + "color": "1d76db", + "description": "Tracks commits missing from pytr hardened", + }) + state = "open" if behind else "closed" + if existing: + labels = [label["name"] for label in existing.get("labels", [])] + if LABEL not in labels: + labels.append(LABEL) + api(f"repos/{REPOSITORY}/issues/{existing['number']}", "PATCH", { + "title": TITLE, + "body": body, + "labels": labels, + "state": state, + }) + else: + api(f"repos/{REPOSITORY}/issues", "POST", { + "title": TITLE, + "body": body, + "labels": [LABEL], + }) + + try: + main() + except Exception as error: + print(error, file=sys.stderr) + sys.exit(1) + PY