diff --git a/runner_manager/models/runner.py b/runner_manager/models/runner.py index d301caf4..dad6f4bd 100644 --- a/runner_manager/models/runner.py +++ b/runner_manager/models/runner.py @@ -83,7 +83,8 @@ def find_from_webhook(cls, webhook: WorkflowJobEvent) -> "Runner | None": Returns: Runner: A runner object """ - + if webhook.workflow_job.runner_id is None: + return None try: runner: Runner | None = cls.find( cls.id == webhook.workflow_job.runner_id diff --git a/runner_manager/models/runner_group.py b/runner_manager/models/runner_group.py index 390f4317..b8d117d7 100644 --- a/runner_manager/models/runner_group.py +++ b/runner_manager/models/runner_group.py @@ -271,7 +271,7 @@ def save( return super().save(pipeline=pipeline) @classmethod - def find_from_webhook(cls, webhook: WorkflowJobEvent) -> "RunnerGroup": + def find_from_webhook(cls, webhook: WorkflowJobEvent) -> "RunnerGroup | None": """Find the runner group from a webhook instance. Args: @@ -280,6 +280,8 @@ def find_from_webhook(cls, webhook: WorkflowJobEvent) -> "RunnerGroup": Returns: RunnerGroup: Runner group instance. """ + if webhook.workflow_job.runner_group_id is None: + return None try: group: RunnerGroup | None = cls.find( (cls.id == webhook.workflow_job.runner_group_id) diff --git a/tests/unit/models/test_runner.py b/tests/unit/models/test_runner.py index 05d453a6..e775ef16 100644 --- a/tests/unit/models/test_runner.py +++ b/tests/unit/models/test_runner.py @@ -52,6 +52,8 @@ def test_find_from_webhook(runner: Runner, webhook: WorkflowJobCompleted): assert Runner.find_from_webhook(webhook) == runner runner.delete(runner.pk) assert Runner.find_from_webhook(webhook) is None + webhook.workflow_job.runner_id = None + assert Runner.find_from_webhook(webhook) is None def test_update_from_github(runner: Runner, github: GitHub): diff --git a/tests/unit/models/test_runner_group.py b/tests/unit/models/test_runner_group.py index 78bcc4a9..360dbb21 100644 --- a/tests/unit/models/test_runner_group.py +++ b/tests/unit/models/test_runner_group.py @@ -94,6 +94,9 @@ def test_find_from_webhook(runner_group: RunnerGroup, webhook: WorkflowJobComple assert RunnerGroup.find_from_webhook(webhook) == runner_group runner_group.delete(runner_group.pk) assert RunnerGroup.find_from_webhook(webhook) is None + webhook.workflow_job.runner_group_name = None + webhook.workflow_job.runner_group_id = None + assert RunnerGroup.find_from_webhook(webhook) is None def test_runner_group_delete_method(runner_group: RunnerGroup, github: GitHub):