Skip to content

Commit 045c546

Browse files
committed
Patch create_dispatch
1 parent 161fc8d commit 045c546

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/libkernelbot/launchers/github.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import zlib
1212
from typing import Awaitable, Callable, Optional
1313

14+
import github
1415
import requests
1516
from github import Github, UnknownObjectException
17+
from github.GithubObject import NotSet, Opt
1618
from github.Workflow import Workflow
1719
from github.WorkflowRun import WorkflowRun
1820

@@ -177,6 +179,35 @@ class GitHubArtifact:
177179
_WORKFLOW_FILE_CACHE: dict[str, Workflow] = {}
178180

179181

182+
def patched_create_dispatch(
183+
workflow: Workflow,
184+
ref: github.Branch.Branch | github.Tag.Tag | github.Commit.Commit | str,
185+
inputs: Opt[dict] = NotSet,
186+
) -> bool:
187+
"""
188+
:calls: `POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches <https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event>`_
189+
"""
190+
assert (
191+
isinstance(ref, github.Branch.Branch)
192+
or isinstance(ref, github.Tag.Tag)
193+
or isinstance(ref, github.Commit.Commit)
194+
or isinstance(ref, str)
195+
), ref
196+
assert inputs is NotSet or isinstance(inputs, dict), inputs
197+
if isinstance(ref, github.Branch.Branch):
198+
ref = ref.name
199+
elif isinstance(ref, github.Commit.Commit):
200+
ref = ref.sha
201+
elif isinstance(ref, github.Tag.Tag):
202+
ref = ref.name
203+
if inputs is NotSet:
204+
inputs = {}
205+
status, _, _ = workflow._requester.requestJson(
206+
"POST", f"{workflow.url}/dispatches", input={"ref": ref, "inputs": inputs}
207+
)
208+
return status == 200
209+
210+
180211
class GitHubRun:
181212
def __init__(self, repo: str, token: str, branch: str, workflow_file: str):
182213
gh = Github(token)
@@ -261,8 +292,8 @@ async def trigger(self, inputs: dict) -> bool:
261292
pprint.pformat(inputs_with_run_id),
262293
)
263294
success = await asyncio.to_thread(
264-
workflow.create_dispatch, self.branch, inputs=inputs_with_run_id
265-
)
295+
patched_create_dispatch, workflow, self.branch, inputs=inputs_with_run_id
296+
) # noqa: E501
266297

267298
if success:
268299
wait_seconds = 10

0 commit comments

Comments
 (0)