|
4 | 4 | """Trigger a repository_dispatch GitHub action."""
|
5 | 5 |
|
6 | 6 | import sys
|
| 7 | +import time |
7 | 8 |
|
8 | 9 | from session import get_session
|
9 | 10 |
|
10 |
| -repo_owner, event_type = sys.argv[1:] |
11 |
| - |
12 | 11 | # The GitHub URL makes no mention of which workflow to use. It's found based on
|
13 | 12 | # the event_type, which matches the types in the workflow:
|
14 | 13 | #
|
|
18 | 17 | # - build-kits
|
19 | 18 | #
|
20 | 19 |
|
21 |
| -url = f"https://api.github.com/repos/{repo_owner}/dispatches" |
22 |
| -data = {"event_type": event_type} |
23 | 20 |
|
24 |
| -resp = get_session().post(url, json=data) |
25 |
| -if resp.status_code // 100 == 2: |
26 |
| - print("Success") |
27 |
| -else: |
28 |
| - print(f"Status: {resp.status_code}") |
29 |
| - print(resp.text) |
| 21 | +def latest_action_run(repo_owner, event): |
| 22 | + """ |
| 23 | + Get the newest action run for a certain kind of event. |
| 24 | + """ |
| 25 | + resp = get_session().get( |
| 26 | + f"https://api.github.com/repos/{repo_owner}/actions/runs?event={event}" |
| 27 | + ) |
| 28 | + resp.raise_for_status() |
| 29 | + return resp.json()["workflow_runs"][0] |
| 30 | + |
| 31 | + |
| 32 | +def dispatch_action(repo_owner, event_type): |
| 33 | + """ |
| 34 | + Trigger an action with a particular dispatch event_type. |
| 35 | + Wait until it starts, and print the URL to it. |
| 36 | + """ |
| 37 | + latest_id = latest_action_run(repo_owner, "repository_dispatch")["id"] |
| 38 | + |
| 39 | + url = f"https://api.github.com/repos/{repo_owner}/dispatches" |
| 40 | + data = {"event_type": event_type} |
| 41 | + |
| 42 | + resp = get_session().post(url, json=data) |
| 43 | + resp.raise_for_status() |
| 44 | + print(f"Success: {resp.status_code}") |
| 45 | + while True: |
| 46 | + run = latest_action_run(repo_owner, "repository_dispatch") |
| 47 | + if run["id"] != latest_id: |
| 48 | + break |
| 49 | + print(".", end=" ", flush=True) |
| 50 | + time.sleep(0.5) |
| 51 | + print(run["html_url"]) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + dispatch_action(*sys.argv[1:]) |
0 commit comments