Skip to content

Commit

Permalink
build: trigger_action.py is more helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 16, 2024
1 parent ee19480 commit bc4cfa0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ cheats: ## Create some useful snippets for releasing.
python igor.py cheats | tee cheats.txt

relbranch: #: Create the branch for releasing (see howto.txt).
git switch -c nedbat/release-$$(date +%Y%m%d)
git switch -c nedbat/release-$$(date +%Y%m%d-%H%M%S)

relcommit1: #: Commit the first release changes (see howto.txt).
git commit -am "docs: prep for $$(python setup.py --version)"
Expand All @@ -206,9 +206,11 @@ kit: ## Make the source distribution.

pypi_upload: ## Upload the built distributions to PyPI.
python ci/trigger_action.py $(REPO_OWNER) publish-pypi
@echo "Use that^ URL to approve the upload"

test_upload: ## Upload the distributions to PyPI's testing server.
python ci/trigger_action.py $(REPO_OWNER) publish-testpypi
@echo "Use that^ URL to approve the upload"

kit_local:
# pip.conf looks like this:
Expand Down
46 changes: 36 additions & 10 deletions ci/trigger_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
"""Trigger a repository_dispatch GitHub action."""

import sys
import time

from session import get_session

repo_owner, event_type = sys.argv[1:]

# The GitHub URL makes no mention of which workflow to use. It's found based on
# the event_type, which matches the types in the workflow:
#
Expand All @@ -18,12 +17,39 @@
# - build-kits
#

url = f"https://api.github.com/repos/{repo_owner}/dispatches"
data = {"event_type": event_type}

resp = get_session().post(url, json=data)
if resp.status_code // 100 == 2:
print("Success")
else:
print(f"Status: {resp.status_code}")
print(resp.text)
def latest_action_run(repo_owner, event):
"""
Get the newest action run for a certain kind of event.
"""
resp = get_session().get(
f"https://api.github.com/repos/{repo_owner}/actions/runs?event={event}"
)
resp.raise_for_status()
return resp.json()["workflow_runs"][0]


def dispatch_action(repo_owner, event_type):
"""
Trigger an action with a particular dispatch event_type.
Wait until it starts, and print the URL to it.
"""
latest_id = latest_action_run(repo_owner, "repository_dispatch")["id"]

url = f"https://api.github.com/repos/{repo_owner}/dispatches"
data = {"event_type": event_type}

resp = get_session().post(url, json=data)
resp.raise_for_status()
print(f"Success: {resp.status_code}")
while True:
run = latest_action_run(repo_owner, "repository_dispatch")
if run["id"] != latest_id:
break
print(".", end=" ", flush=True)
time.sleep(0.5)
print(run["html_url"])


if __name__ == "__main__":
dispatch_action(*sys.argv[1:])

0 comments on commit bc4cfa0

Please sign in to comment.