|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Downloads GitHub Actions release artifacts.""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import io |
| 7 | +import pathlib |
| 8 | +import zipfile |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | +WORKFLOW_RUN_ARTIFACTS_URL = "https://api.github.com/repos/indygreg/python-zstandard/actions/runs/{run_id}/artifacts" |
| 13 | + |
| 14 | + |
| 15 | +def download_artifacts(token: str, run_id: str, dest: pathlib.Path): |
| 16 | + if not dest.exists(): |
| 17 | + dest.mkdir(parents=True) |
| 18 | + |
| 19 | + session = requests.session() |
| 20 | + session.headers["Authorization"] = "token %s" % token |
| 21 | + |
| 22 | + artifacts = session.get( |
| 23 | + WORKFLOW_RUN_ARTIFACTS_URL.format(run_id=run_id) |
| 24 | + ).json() |
| 25 | + |
| 26 | + for entry in artifacts["artifacts"]: |
| 27 | + download_url = entry["archive_download_url"] |
| 28 | + |
| 29 | + print("downloading %s" % download_url) |
| 30 | + zipdata = io.BytesIO(session.get(download_url).content) |
| 31 | + |
| 32 | + with zipfile.ZipFile(zipdata, "r") as zf: |
| 33 | + for name in zf.namelist(): |
| 34 | + name_path = pathlib.Path(name) |
| 35 | + dest_path = dest / name_path.name |
| 36 | + print("writing %s" % dest_path) |
| 37 | + |
| 38 | + with dest_path.open("wb") as fh: |
| 39 | + fh.write(zf.read(name)) |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + parser = argparse.ArgumentParser() |
| 44 | + parser.add_argument("token", help="GitHub token to authenticate with") |
| 45 | + parser.add_argument( |
| 46 | + "run_id", help='which GitHub Actions run download. e.g. "42"' |
| 47 | + ) |
| 48 | + parser.add_argument("dest", help="destination directory") |
| 49 | + |
| 50 | + args = parser.parse_args() |
| 51 | + |
| 52 | + download_artifacts(args.token, args.run_id, pathlib.Path(args.dest)) |
0 commit comments