From a28589ec74e8a5430c041374cf4f351d7f1d6889 Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Tue, 16 Sep 2025 16:34:16 +0300 Subject: [PATCH] feat: add trigger loader guard --- src/ahriman/core/triggers/trigger.py | 12 ++++++++++++ src/ahriman/core/triggers/trigger_loader.py | 5 +++-- tests/ahriman/core/triggers/test_trigger.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ahriman/core/triggers/trigger.py b/src/ahriman/core/triggers/trigger.py index 8e525d30..d4272d44 100644 --- a/src/ahriman/core/triggers/trigger.py +++ b/src/ahriman/core/triggers/trigger.py @@ -36,6 +36,7 @@ class Trigger(LazyLogging): CONFIGURATION_SCHEMA(ConfigurationSchema): (class attribute) configuration schema template CONFIGURATION_SCHEMA_FALLBACK(str | None): (class attribute) optional fallback option for defining configuration schema type used + REQUIRES_REPOSITORY(bool): (class attribute) either trigger requires loaded repository or not configuration(Configuration): configuration instance repository_id(RepositoryId): repository unique identifier @@ -59,6 +60,7 @@ class Trigger(LazyLogging): CONFIGURATION_SCHEMA: ClassVar[ConfigurationSchema] = {} CONFIGURATION_SCHEMA_FALLBACK: ClassVar[str | None] = None + REQUIRES_REPOSITORY: ClassVar[bool] = True def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None: """ @@ -79,6 +81,16 @@ class Trigger(LazyLogging): """ return self.repository_id.architecture + @property + def is_allowed_to_run(self) -> bool: + """ + whether trigger allowed to run or not + + Returns: + bool: ``True`` in case if trigger allowed to run and ``False`` otherwise + """ + return not (self.REQUIRES_REPOSITORY and self.repository_id.is_empty) + @classmethod def configuration_schema(cls, configuration: Configuration | None) -> ConfigurationSchema: """ diff --git a/src/ahriman/core/triggers/trigger_loader.py b/src/ahriman/core/triggers/trigger_loader.py index 6aac9051..d4a4db2c 100644 --- a/src/ahriman/core/triggers/trigger_loader.py +++ b/src/ahriman/core/triggers/trigger_loader.py @@ -77,8 +77,9 @@ class TriggerLoader(LazyLogging): """ instance = cls() instance.triggers = [ - instance.load_trigger(trigger, repository_id, configuration) - for trigger in instance.selected_triggers(configuration) + trigger + for trigger_name in instance.selected_triggers(configuration) + if (trigger := instance.load_trigger(trigger_name, repository_id, configuration)).is_allowed_to_run ] return instance diff --git a/tests/ahriman/core/triggers/test_trigger.py b/tests/ahriman/core/triggers/test_trigger.py index 9923d968..f4804d90 100644 --- a/tests/ahriman/core/triggers/test_trigger.py +++ b/tests/ahriman/core/triggers/test_trigger.py @@ -3,6 +3,7 @@ from unittest.mock import MagicMock from ahriman.core.configuration import Configuration from ahriman.core.report import ReportTrigger from ahriman.core.triggers import Trigger +from ahriman.models.repository_id import RepositoryId from ahriman.models.result import Result @@ -13,6 +14,19 @@ def test_architecture(trigger: Trigger) -> None: assert trigger.architecture == trigger.repository_id.architecture +def test_is_allowed_to_run(trigger: Trigger) -> None: + """ + must return flag correctly + """ + assert trigger.is_allowed_to_run + + trigger.repository_id = RepositoryId("", "") + assert not trigger.is_allowed_to_run + + trigger.REQUIRES_REPOSITORY = False + assert trigger.is_allowed_to_run + + def test_configuration_schema(configuration: Configuration) -> None: """ must return used configuration schema