From a465ec2070dcdf577bf9e2e96360fcd29b60149b Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Thu, 24 Jul 2025 18:03:09 +0200 Subject: [PATCH] Add a command to trigger a repository dispatch event Fixes #459 --- taskboot/cli.py | 34 ++++++++++++++++++++++++++++------ taskboot/github.py | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/taskboot/cli.py b/taskboot/cli.py index c49b56c..4fcdb71 100644 --- a/taskboot/cli.py +++ b/taskboot/cli.py @@ -16,6 +16,7 @@ from taskboot.cargo import cargo_publish from taskboot.git import git_push from taskboot.github import github_release +from taskboot.github import github_repository_dispatch from taskboot.push import heroku_release from taskboot.push import push_artifacts from taskboot.pypi import publish_pypi @@ -301,26 +302,26 @@ def main() -> None: git_push_cmd.set_defaults(func=git_push) # Deploy as a github release - github_release_cmd = commands.add_parser( + github_repository_dispatch_cmd = commands.add_parser( "github-release", help="Create a GitHub release and publish assets" ) - github_release_cmd.add_argument( + github_repository_dispatch_cmd.add_argument( "repository", type=str, help="Github repository name to use (example: mozilla/task-boot)", ) - github_release_cmd.add_argument( + github_repository_dispatch_cmd.add_argument( "version", type=str, help="Release version tag to create or update on github", ) - github_release_cmd.add_argument( + github_repository_dispatch_cmd.add_argument( "--task-id", type=str, default=os.environ.get("TASK_ID"), help="Taskcluster task group to analyse", ) - group = github_release_cmd.add_mutually_exclusive_group() + group = github_repository_dispatch_cmd.add_mutually_exclusive_group() group.add_argument( "--local-asset", nargs="+", @@ -333,7 +334,28 @@ def main() -> None: type=str, help="Asset to upload on the release, retrieved from previously created artifacts. Format is asset-name:path/to/artifact", ) - github_release_cmd.set_defaults(func=github_release) + github_repository_dispatch_cmd.set_defaults(func=github_release) + + # Trigger a repository dispatch event + github_repository_dispatch_cmd = commands.add_parser( + "github-repository-dispatch", help="Trigger a repository dispatch event" + ) + github_repository_dispatch_cmd.add_argument( + "repository", + type=str, + help="Github repository name to use (example: mozilla/task-boot)", + ) + github_repository_dispatch_cmd.add_argument( + "event_type", + type=str, + help="Custom webhook event name", + ) + github_repository_dispatch_cmd.add_argument( + "client_payload", + type=str, + help="JSON payload with extra information about the webhook event that the action or workflow may use", + ) + github_repository_dispatch_cmd.set_defaults(func=github_repository_dispatch) # Publish on crates.io cargo_publish_cmd = commands.add_parser( diff --git a/taskboot/github.py b/taskboot/github.py index 2be07ac..a9b5cb6 100644 --- a/taskboot/github.py +++ b/taskboot/github.py @@ -136,3 +136,26 @@ def github_release(target: Target, args: argparse.Namespace) -> None: release.upload_asset(name=asset_name, path=artifact_path, label=asset_name) logger.info(f"Release available as {release.html_url}") + + +def github_repository_dispatch(target: Target, args: argparse.Namespace) -> None: + """ + Push all artifacts from dependent tasks + """ + assert args.task_id is not None, "Missing task id" + + # Load config from file/secret + config = Configuration(args) + assert config.has_git_auth(), "Missing Github authentication" + + # Setup GitHub API client and load repository + github = Github(config.git["token"]) + try: + repository = github.get_repo(args.repository) + logger.info(f"Loaded Github repository {repository.full_name} #{repository.id}") + except UnknownObjectException: + raise Exception(f"Repository {args.repository} is not available") + + repository.create_repository_dispatch(args.event_type, args.client_payload) + + logger.info("Repository dispatch triggered")