|
| 1 | +import requests |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +GITHUB_TOKEN = os.getenv("IREE_TOKEN") |
| 6 | + |
| 7 | +OWNER = "iree-org" |
| 8 | +REPO = "iree" |
| 9 | + |
| 10 | +API_URL = f"https://api.github.com/repos/{OWNER}/{REPO}/actions/workflows/pkgci.yml/runs" |
| 11 | + |
| 12 | +# Function to get the latest workflow run ID for pkgci.yml |
| 13 | +def get_latest_pkgci_workflow_run(): |
| 14 | + headers = {"Authorization": f"token {GITHUB_TOKEN}"} |
| 15 | + params = {"per_page": 1} |
| 16 | + response = requests.get(API_URL, headers=headers, params=params) |
| 17 | + |
| 18 | + if response.status_code == 200: |
| 19 | + data = response.json() |
| 20 | + if data["total_count"] > 0: |
| 21 | + latest_run = data["workflow_runs"][0] |
| 22 | + return latest_run["id"] |
| 23 | + else: |
| 24 | + print("No workflow runs found for pkgci.yml.") |
| 25 | + return None |
| 26 | + else: |
| 27 | + print(f"Error fetching workflow runs: {response.status_code}") |
| 28 | + return None |
| 29 | + |
| 30 | +# Function to get the artifacts of a specific workflow run |
| 31 | +def get_artifacts(workflow_run_id): |
| 32 | + artifacts_url = f"https://api.github.com/repos/{OWNER}/{REPO}/actions/runs/{workflow_run_id}/artifacts" |
| 33 | + headers = {"Authorization": f"token {GITHUB_TOKEN}"} |
| 34 | + response = requests.get(artifacts_url, headers=headers) |
| 35 | + |
| 36 | + if response.status_code == 200: |
| 37 | + artifacts = response.json()["artifacts"] |
| 38 | + if artifacts: |
| 39 | + print(f"Artifacts for pkgci.yml workflow run {workflow_run_id}:") |
| 40 | + for artifact in artifacts: |
| 41 | + print(f"- {artifact['name']} (Size: {artifact['size_in_bytes']} bytes)") |
| 42 | + download_artifact(artifact['archive_download_url'], artifact['name']) |
| 43 | + else: |
| 44 | + print("No artifacts found for the pkgci.yml workflow run.") |
| 45 | + else: |
| 46 | + print(f"Error fetching artifacts: {response.status_code}") |
| 47 | + |
| 48 | +# Function to download an artifact |
| 49 | +def download_artifact(download_url, artifact_name): |
| 50 | + headers = {"Authorization": f"token {GITHUB_TOKEN}"} |
| 51 | + response = requests.get(download_url, headers=headers, stream=True) |
| 52 | + |
| 53 | + if response.status_code == 200: |
| 54 | + file_name = f"{artifact_name}.tar.gz" |
| 55 | + with open(file_name, "wb") as f: |
| 56 | + for chunk in response.iter_content(chunk_size=8192): |
| 57 | + if chunk: |
| 58 | + f.write(chunk) |
| 59 | + print(f"Artifact '{artifact_name}' downloaded successfully as '{file_name}'.") |
| 60 | + else: |
| 61 | + print(f"Error downloading artifact '{artifact_name}': {response.status_code}") |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + workflow_run_id = get_latest_pkgci_workflow_run() |
| 65 | + if workflow_run_id: |
| 66 | + get_artifacts(workflow_run_id) |
0 commit comments