forked from admin/brencher
x
This commit is contained in:
@@ -51,6 +51,8 @@ class GiteaActionsTrigger(AbstractStep[GiteaActionsRun]):
|
||||
self.wd = wd
|
||||
self.poll_timeout_sec = poll_timeout_sec
|
||||
self.poll_interval_sec = poll_interval_sec
|
||||
self._last_request_key: tuple[Optional[str], tuple[tuple[str, str], ...]] | None = None
|
||||
self._last_run: GiteaActionsRun | None = None
|
||||
|
||||
def _http(self, method: str, url: str, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
data = None
|
||||
@@ -93,13 +95,31 @@ class GiteaActionsTrigger(AbstractStep[GiteaActionsRun]):
|
||||
html_url="",
|
||||
)
|
||||
|
||||
latest = self._list_recent_runs()
|
||||
resolved_inputs: Dict[str, str] = {}
|
||||
if callable(self.inputs):
|
||||
resolved_inputs = self.inputs() or {}
|
||||
elif isinstance(self.inputs, dict):
|
||||
resolved_inputs = self.inputs
|
||||
ref = self.ref() if callable(self.ref) else self.ref
|
||||
request_key = (ref, tuple(sorted(resolved_inputs.items())))
|
||||
latest = self._list_recent_runs()
|
||||
|
||||
if latest is not None and latest.status != "completed":
|
||||
logger.info("Gitea Actions run %s is already active; reusing it.", latest.run_id)
|
||||
self._last_request_key = request_key
|
||||
self._last_run = latest
|
||||
return latest
|
||||
|
||||
if (
|
||||
self._last_request_key == request_key
|
||||
and self._last_run is not None
|
||||
and latest is not None
|
||||
and latest.run_id == self._last_run.run_id
|
||||
and latest.conclusion == "success"
|
||||
):
|
||||
logger.info("Gitea Actions run %s already succeeded; reusing it.", latest.run_id)
|
||||
return latest
|
||||
|
||||
body: Dict[str, Any] = {"ref": ref}
|
||||
if resolved_inputs:
|
||||
body["inputs"] = resolved_inputs
|
||||
@@ -125,6 +145,8 @@ class GiteaActionsTrigger(AbstractStep[GiteaActionsRun]):
|
||||
if current is None or (latest is not None and current.run_id == latest.run_id):
|
||||
time.sleep(self.poll_interval_sec)
|
||||
continue
|
||||
self._last_request_key = request_key
|
||||
self._last_run = current
|
||||
return current
|
||||
raise TimeoutError(
|
||||
f"Gitea Actions did not pick up a new run within {self.poll_timeout_sec}s"
|
||||
|
||||
@@ -48,6 +48,35 @@ def test_trigger_skips_dispatch_in_dry_run() -> None:
|
||||
assert result.run_id == 0
|
||||
|
||||
|
||||
def test_trigger_reuses_active_run(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
trigger = _trigger()
|
||||
env = _environment(trigger, dry=False)
|
||||
runs = iter([
|
||||
None,
|
||||
GiteaActionsRun(42, "in_progress", None, "http://example/run/42"),
|
||||
GiteaActionsRun(42, "in_progress", None, "http://example/run/42"),
|
||||
])
|
||||
dispatches = 0
|
||||
|
||||
def fake_http(method: str, url: str, body: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
nonlocal dispatches
|
||||
if method == "POST":
|
||||
dispatches += 1
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr(trigger, "_list_recent_runs", lambda: next(runs))
|
||||
monkeypatch.setattr(trigger, "_http", fake_http)
|
||||
trigger.poll_interval_sec = 0
|
||||
trigger.poll_timeout_sec = 1
|
||||
|
||||
first = trigger.progress()
|
||||
second = trigger.progress()
|
||||
|
||||
assert first.run_id == 42
|
||||
assert second.run_id == 42
|
||||
assert dispatches == 1
|
||||
|
||||
|
||||
def test_await_skips_polling_in_dry_run() -> None:
|
||||
run = FixedRun()
|
||||
await_step = GiteaActionsAwait(
|
||||
|
||||
Reference in New Issue
Block a user