|
| 1 | +#!/usr/bin/python |
| 2 | +# Dismiss open code scanning alerts whose analysis category is not produced by |
| 3 | +# the current scan matrix. |
| 4 | +# |
| 5 | +# GitHub only auto-closes an alert when a newer analysis is uploaded to the |
| 6 | +# *same* category. When an image is retired from the scan matrix, its |
| 7 | +# categories stop receiving uploads and their alerts stay open forever. This |
| 8 | +# script dismisses those orphaned alerts. |
| 9 | +# |
| 10 | +# The set of expected categories is derived from the same versions.py that |
| 11 | +# generates the scan matrix, so retiring an image automatically retires its |
| 12 | +# alerts on the next scheduled run. |
| 13 | +# |
| 14 | +# Pass --dry-run to only print what would be dismissed. |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import urllib.request |
| 20 | + |
| 21 | +sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "matrix")) |
| 22 | +import versions |
| 23 | + |
| 24 | +DRY_RUN = "--dry-run" in sys.argv |
| 25 | +REPO = os.environ.get("GITHUB_REPOSITORY", "pulumi/pulumi-docker-containers") |
| 26 | +TOKEN = os.environ["GH_TOKEN"] |
| 27 | +ARCHS = ["amd64", "arm64"] |
| 28 | + |
| 29 | +DISMISS_COMMENT = ( |
| 30 | + "This alert belongs to an analysis category that is not produced by the " |
| 31 | + "Snyk scan workflow (retired image or renamed category), so it can never " |
| 32 | + "be closed automatically. Current results are tracked under the per-image " |
| 33 | + "categories." |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +def expected_categories(): |
| 38 | + expected = set() |
| 39 | + for suffix in ["", "-nonroot"]: |
| 40 | + for arch in ARCHS: |
| 41 | + expected.add(f"pulumi{suffix}-{arch}") |
| 42 | + for arch in ARCHS: |
| 43 | + expected.add(f"pulumi-provider-build-environment-{arch}") |
| 44 | + for base_os in ["debian", "ubi"]: |
| 45 | + for arch in ARCHS: |
| 46 | + expected.add(f"pulumi-base-{base_os}-{arch}") |
| 47 | + for sdk in versions.unversioned: |
| 48 | + for arch in ARCHS: |
| 49 | + expected.add(f"pulumi-{sdk}-debian-{arch}") |
| 50 | + for sdk, info in versions.versioned.items(): |
| 51 | + for version in [info["default"]] + info["additional"]: |
| 52 | + for arch in ARCHS: |
| 53 | + expected.add(f"pulumi-{sdk}-{version}-debian-{arch}") |
| 54 | + for sdk in ["nodejs", "python", "dotnet", "go"]: |
| 55 | + expected.add(f"pulumi-{sdk}-ubi") |
| 56 | + return expected |
| 57 | + |
| 58 | + |
| 59 | +def api(path, method="GET", body=None): |
| 60 | + request = urllib.request.Request( |
| 61 | + f"https://api.github.com{path}", |
| 62 | + method=method, |
| 63 | + data=json.dumps(body).encode() if body is not None else None, |
| 64 | + headers={ |
| 65 | + "Authorization": f"Bearer {TOKEN}", |
| 66 | + "Accept": "application/vnd.github+json", |
| 67 | + "X-GitHub-Api-Version": "2022-11-28", |
| 68 | + }, |
| 69 | + ) |
| 70 | + with urllib.request.urlopen(request) as response: |
| 71 | + return json.load(response) |
| 72 | + |
| 73 | + |
| 74 | +def open_alerts(): |
| 75 | + alerts = [] |
| 76 | + page = 1 |
| 77 | + while True: |
| 78 | + batch = api(f"/repos/{REPO}/code-scanning/alerts?state=open&per_page=100&page={page}") |
| 79 | + alerts.extend(batch) |
| 80 | + if len(batch) < 100: |
| 81 | + return alerts |
| 82 | + page += 1 |
| 83 | + |
| 84 | + |
| 85 | +expected = expected_categories() |
| 86 | +stale = [ |
| 87 | + alert |
| 88 | + for alert in open_alerts() |
| 89 | + if alert.get("most_recent_instance", {}).get("category") not in expected |
| 90 | +] |
| 91 | +print(f"Found {len(stale)} open alerts in stale categories.") |
| 92 | + |
| 93 | +for alert in stale: |
| 94 | + category = alert["most_recent_instance"]["category"] |
| 95 | + label = f"alert #{alert['number']} ({alert['rule']['id']}) in category '{category}'" |
| 96 | + if DRY_RUN: |
| 97 | + print(f"Would dismiss {label}") |
| 98 | + continue |
| 99 | + api( |
| 100 | + f"/repos/{REPO}/code-scanning/alerts/{alert['number']}", |
| 101 | + method="PATCH", |
| 102 | + body={ |
| 103 | + "state": "dismissed", |
| 104 | + "dismissed_reason": "won't fix", |
| 105 | + "dismissed_comment": DISMISS_COMMENT, |
| 106 | + }, |
| 107 | + ) |
| 108 | + print(f"Dismissed {label}") |
0 commit comments