|
15 | 15 | import subprocess |
16 | 16 | import sys |
17 | 17 | import tempfile |
| 18 | +import zipfile |
18 | 19 | from dataclasses import dataclass |
19 | 20 |
|
20 | 21 | CLEANUP_MARKER = "Auto-checked by weekly fuzzer issue cleanup" |
@@ -90,6 +91,57 @@ def extract_run_id(artifact_url: str) -> str | None: |
90 | 91 | return match.group(1) if match else None |
91 | 92 |
|
92 | 93 |
|
| 94 | +def extract_artifact_id(artifact_url: str) -> str | None: |
| 95 | + """Extract the artifact ID from an artifact URL like .../artifacts/67890.""" |
| 96 | + match = re.search(r"artifacts/(\d+)", artifact_url) |
| 97 | + return match.group(1) if match else None |
| 98 | + |
| 99 | + |
| 100 | +def download_artifact( |
| 101 | + repo: str, run_id: str, artifact_id: str | None, artifact_name: str, dest: str |
| 102 | +) -> str | None: |
| 103 | + """Download the crash artifact into dest. Returns an error message on failure. |
| 104 | +
|
| 105 | + Download by artifact ID when the URL has one. A re-run of the fuzz workflow uploads a second |
| 106 | + artifact with the same name, and `gh run download --name` then gets the artifact of the |
| 107 | + latest attempt, which does not contain the crash file of the issue. |
| 108 | + """ |
| 109 | + if not artifact_id: |
| 110 | + result = run( |
| 111 | + [ |
| 112 | + "gh", |
| 113 | + "run", |
| 114 | + "download", |
| 115 | + run_id, |
| 116 | + "--name", |
| 117 | + artifact_name, |
| 118 | + "--repo", |
| 119 | + repo, |
| 120 | + "--dir", |
| 121 | + dest, |
| 122 | + ], |
| 123 | + capture_output=True, |
| 124 | + text=True, |
| 125 | + ) |
| 126 | + return result.stderr.strip() if result.returncode != 0 else None |
| 127 | + |
| 128 | + zip_path = os.path.join(dest, "artifact.zip") |
| 129 | + with open(zip_path, "wb") as zip_file: |
| 130 | + result = run( |
| 131 | + ["gh", "api", f"repos/{repo}/actions/artifacts/{artifact_id}/zip"], |
| 132 | + stdout=zip_file, |
| 133 | + stderr=subprocess.PIPE, |
| 134 | + text=True, |
| 135 | + ) |
| 136 | + if result.returncode != 0: |
| 137 | + return result.stderr.strip() |
| 138 | + |
| 139 | + with zipfile.ZipFile(zip_path) as archive: |
| 140 | + archive.extractall(dest) |
| 141 | + os.remove(zip_path) |
| 142 | + return None |
| 143 | + |
| 144 | + |
93 | 145 | def has_cleanup_comment(repo: str, issue_number: int) -> bool: |
94 | 146 | """Check if the issue already has an 'Artifact Unavailable' cleanup comment.""" |
95 | 147 | result = run( |
@@ -281,26 +333,16 @@ def main() -> None: |
281 | 333 |
|
282 | 334 | # Download artifact into a temp directory |
283 | 335 | with tempfile.TemporaryDirectory() as tmpdir: |
284 | | - artifact_name = f"{args.target}-crash-artifacts" |
285 | | - dl_result = run( |
286 | | - [ |
287 | | - "gh", |
288 | | - "run", |
289 | | - "download", |
290 | | - run_id, |
291 | | - "--name", |
292 | | - artifact_name, |
293 | | - "--repo", |
294 | | - args.repo, |
295 | | - "--dir", |
296 | | - tmpdir, |
297 | | - ], |
298 | | - capture_output=True, |
299 | | - text=True, |
| 336 | + dl_error = download_artifact( |
| 337 | + args.repo, |
| 338 | + run_id, |
| 339 | + extract_artifact_id(issue.artifact_url), |
| 340 | + f"{args.target}-crash-artifacts", |
| 341 | + tmpdir, |
300 | 342 | ) |
301 | 343 |
|
302 | | - if dl_result.returncode != 0: |
303 | | - print(f" Artifact download failed: {dl_result.stderr.strip()}") |
| 344 | + if dl_error is not None: |
| 345 | + print(f" Artifact download failed: {dl_error}") |
304 | 346 | if not has_cleanup_comment(args.repo, issue.number): |
305 | 347 | close_artifact_unavailable(args.repo, issue.number, args.dry_run) |
306 | 348 | else: |
|
0 commit comments