|
| 1 | +import argparse |
| 2 | +import io |
| 3 | +import pathlib |
| 4 | +import requests |
| 5 | +import shutil |
| 6 | +import tempfile |
| 7 | +import zipfile |
| 8 | + |
| 9 | + |
| 10 | +def __main__() -> None: |
| 11 | + parser = argparse.ArgumentParser() |
| 12 | + parser.add_argument("--owner", default="testcontainers") |
| 13 | + parser.add_argument("--repo", default="testcontainers-python") |
| 14 | + parser.add_argument("--run", help="GitHub Action run id") |
| 15 | + parser.add_argument("--pr", help="GitHub PR number") |
| 16 | + parser.add_argument("--branch", default="main") |
| 17 | + parser.add_argument("--token", help="GitHub autentication token") |
| 18 | + args = parser.parse_args() |
| 19 | + |
| 20 | + # Get an access token. |
| 21 | + if args.token: |
| 22 | + token = args.token |
| 23 | + elif (path := pathlib.Path(".github-token")).is_file(): |
| 24 | + token = path.read_text().strip() |
| 25 | + else: |
| 26 | + token = input("we need a GitHub access token to fetch the requirements; please visit " |
| 27 | + "https://github.com/settings/tokens/new, create a token with `public_repo` " |
| 28 | + "scope, and paste it here: ").strip() |
| 29 | + cache = input("do you want to cache the token in a `.github-token` file [Ny]? ") |
| 30 | + if cache.lower().startswith("y"): |
| 31 | + path.write_text(token) |
| 32 | + |
| 33 | + headers = { |
| 34 | + "Authorization": f"Bearer {token}", |
| 35 | + } |
| 36 | + base_url = f"https://api.github.com/repos/{args.owner}/{args.repo}" |
| 37 | + |
| 38 | + if args.run: # Run id was specified. |
| 39 | + run = args.run |
| 40 | + elif args.pr: # PR was specified, let's get the most recent run id. |
| 41 | + print(f"fetching most recent commit for PR #{args.pr}") |
| 42 | + response = requests.get(f"{base_url}/pulls/{args.pr}", headers=headers) |
| 43 | + response.raise_for_status() |
| 44 | + response = response.json() |
| 45 | + head_sha = response["head"]["sha"] |
| 46 | + else: # Nothing was specified, let's get the most recent run id on the main branch. |
| 47 | + print(f"fetching most recent commit for branch `{args.branch}`") |
| 48 | + response = requests.get(f"{base_url}/branches/{args.branch}", headers=headers) |
| 49 | + response.raise_for_status() |
| 50 | + response = response.json() |
| 51 | + head_sha = response["commit"]["sha"] |
| 52 | + |
| 53 | + # List all completed runs and find the one that generated the requirements. |
| 54 | + response = requests.get(f"{base_url}/actions/runs", headers=headers, params={ |
| 55 | + "head_sha": head_sha, |
| 56 | + "status": "success", |
| 57 | + }) |
| 58 | + response.raise_for_status() |
| 59 | + response = response.json() |
| 60 | + |
| 61 | + # Get the requirements run. |
| 62 | + runs = [run for run in response["workflow_runs"] if |
| 63 | + run["path"].endswith("requirements.yml")] |
| 64 | + if len(runs) != 1: |
| 65 | + raise RuntimeError(f"could not identify unique workflow run: {runs}") |
| 66 | + run = runs[0]["id"] |
| 67 | + |
| 68 | + # Get all the artifacts. |
| 69 | + print(f"fetching artifacts for run {run} ...") |
| 70 | + url = f"{base_url}/actions/runs/{run}/artifacts" |
| 71 | + response = requests.get(url, headers=headers) |
| 72 | + response.raise_for_status() |
| 73 | + response = response.json() |
| 74 | + artifacts = response["artifacts"] |
| 75 | + print(f"discovered {len(artifacts)} artifacts") |
| 76 | + |
| 77 | + # Get the content for each artifact and save it. |
| 78 | + for artifact in artifacts: |
| 79 | + name: str = artifact["name"] |
| 80 | + name = name.removeprefix("requirements-") |
| 81 | + print(f"fetching artifact {name} ...") |
| 82 | + response = requests.get(artifact["archive_download_url"], headers=headers) |
| 83 | + response.raise_for_status() |
| 84 | + with zipfile.ZipFile(io.BytesIO(response.content)) as zip, \ |
| 85 | + tempfile.TemporaryDirectory() as tempdir: |
| 86 | + zip.extract("requirements.txt", tempdir) |
| 87 | + shutil.move(pathlib.Path(tempdir) / "requirements.txt", |
| 88 | + pathlib.Path("requirements") / name) |
| 89 | + |
| 90 | + print("done") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + __main__() |
0 commit comments