|
| 1 | +import requests |
| 2 | +import time |
| 3 | + |
| 4 | + |
| 5 | +class Auth: |
| 6 | + def __init__(self, token, org, repo) -> None: |
| 7 | + self._token = token |
| 8 | + self.org = org |
| 9 | + self.repo = repo |
| 10 | + |
| 11 | + def get_authenticated_header(self): |
| 12 | + return { |
| 13 | + "Authorization": f"token {self._token}", |
| 14 | + "Accept": "application/vnd.github.v3+json", |
| 15 | + } |
| 16 | + |
| 17 | + |
| 18 | +class Workflow: |
| 19 | + def __init__(self, auth: Auth): |
| 20 | + self._auth = auth |
| 21 | + |
| 22 | + def _get_dispatch_url(self, workflow_id: str): |
| 23 | + return f"https://api.github.com/repos/{self._auth.org}/{self._auth.repo}/actions/workflows/{workflow_id}/dispatches" |
| 24 | + |
| 25 | + def _get_runs_url(self, workflow_id: str): |
| 26 | + return f"https://api.github.com/repos/{self._auth.org}/{self._auth.repo}/actions/workflows/{workflow_id}/runs" |
| 27 | + |
| 28 | + def _get_run_url(self, run_id: str): |
| 29 | + return f"https://api.github.com/repos/{self._auth.org}/{self._auth.repo}/actions/runs/{run_id}" |
| 30 | + |
| 31 | + def _dispatch_workflow(self, ref: str, workflow_id: str): |
| 32 | + url = self._get_dispatch_url(workflow_id) |
| 33 | + |
| 34 | + data = {"ref": ref} |
| 35 | + |
| 36 | + response = requests.post( |
| 37 | + url, |
| 38 | + headers=self._auth.get_authenticated_header(), |
| 39 | + json=data, |
| 40 | + ) |
| 41 | + |
| 42 | + if response.status_code == 204: |
| 43 | + print( |
| 44 | + f" Successfully started workflow with ID: {workflow_id} on the repository: {self._auth.repo}." |
| 45 | + ) |
| 46 | + else: |
| 47 | + print( |
| 48 | + f"Failed to start workflow with ID: {workflow_id} on the repository: {self._auth.repo}." |
| 49 | + ) |
| 50 | + print(response.text) |
| 51 | + response.raise_for_status() |
| 52 | + |
| 53 | + def _get_latest_run_id(self, workflow_id: str): |
| 54 | + url = self._get_runs_url(workflow_id) |
| 55 | + |
| 56 | + response = requests.get(url, headers=self._auth.get_authenticated_header()) |
| 57 | + |
| 58 | + if response.status_code == 200: |
| 59 | + runs = response.json() |
| 60 | + run_id = runs["workflow_runs"][0]["id"] |
| 61 | + print( |
| 62 | + f"Workflow run retrieved. Details:\n - Owner: {self._auth.org}\n - Repository: {self._auth.repo}\n - Workflow ID: {workflow_id}\n - Run ID: {run_id}" |
| 63 | + ) |
| 64 | + return run_id |
| 65 | + else: |
| 66 | + print("Failed to retrieve workflow run ID.") |
| 67 | + print(response.text) |
| 68 | + response.raise_for_status() |
| 69 | + |
| 70 | + def _wait_for_completion(self, run_id: str, interval: int = 30): |
| 71 | + while True: |
| 72 | + response = requests.get( |
| 73 | + self._get_run_url(run_id), |
| 74 | + headers=self._auth.get_authenticated_header(), |
| 75 | + ) |
| 76 | + if response.status_code == 200: |
| 77 | + run = response.json() |
| 78 | + print(f"Current status of workflow run ID {run_id}: {run['status']}") |
| 79 | + if run["status"] == "completed": |
| 80 | + conclusion = run["conclusion"] |
| 81 | + print( |
| 82 | + f"Workflow run with ID {run_id} has completed with conclusion: {conclusion}" |
| 83 | + ) |
| 84 | + return run["status"] |
| 85 | + else: |
| 86 | + time.sleep(interval) |
| 87 | + else: |
| 88 | + print("Failed to retrieve workflow run status.") |
| 89 | + print(response.text) |
| 90 | + response.raise_for_status() |
| 91 | + |
| 92 | + def invoke(self, ref: str, workflow_id: str): |
| 93 | + self._dispatch_workflow(ref, workflow_id) |
| 94 | + time.sleep(10) |
| 95 | + run_id = self._get_latest_run_id(workflow_id) |
| 96 | + return self._wait_for_completion(run_id) |
0 commit comments