diff --git a/packit_service/worker/allowlist.py b/packit_service/worker/allowlist.py index 17c62c4df..e2e5b98aa 100644 --- a/packit_service/worker/allowlist.py +++ b/packit_service/worker/allowlist.py @@ -48,7 +48,8 @@ github, ) from packit_service.worker.events.comment import CommitCommentEvent -from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.helpers.build import CoprBuildJobHelper from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper from packit_service.worker.reporting import BaseCommitStatus diff --git a/packit_service/worker/checker/bodhi.py b/packit_service/worker/checker/bodhi.py index 06ffb9540..5dcb99670 100644 --- a/packit_service/worker/checker/bodhi.py +++ b/packit_service/worker/checker/bodhi.py @@ -16,7 +16,8 @@ IssueCommentGitlabEvent, PullRequestCommentPagureEvent, ) -from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.mixin import ( GetKojiBuildData, GetKojiBuildDataFromKojiBuildEventMixin, diff --git a/packit_service/worker/checker/distgit.py b/packit_service/worker/checker/distgit.py index c7df5462e..66845f4b5 100644 --- a/packit_service/worker/checker/distgit.py +++ b/packit_service/worker/checker/distgit.py @@ -20,7 +20,7 @@ PullRequestCommentPagureEvent, PushPagureEvent, ) -from packit_service.worker.events.koji import KojiBuildTagEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.mixin import GetProjectToSyncMixin from packit_service.worker.mixin import ( GetPagurePullRequestMixin, diff --git a/packit_service/worker/events/__init__.py b/packit_service/worker/events/__init__.py index dea797e8a..98f6b4fb5 100644 --- a/packit_service/worker/events/__init__.py +++ b/packit_service/worker/events/__init__.py @@ -82,7 +82,7 @@ from packit_service.worker.events.gitlab.release import ( Release as ReleaseGitlabEvent, ) -from packit_service.worker.events.koji import KojiTaskEvent +from packit_service.worker.events.koji.base import Task as KojiTaskEvent from packit_service.worker.events.openscanhub.task import ( Finished as OpenScanHubTaskFinishedEvent, ) diff --git a/packit_service/worker/events/koji/__init__.py b/packit_service/worker/events/koji/__init__.py new file mode 100644 index 000000000..e01ff12d5 --- /dev/null +++ b/packit_service/worker/events/koji/__init__.py @@ -0,0 +1,2 @@ +# Copyright Contributors to the Packit project. +# SPDX-License-Identifier: MIT diff --git a/packit_service/worker/events/koji/abstract.py b/packit_service/worker/events/koji/abstract.py new file mode 100644 index 000000000..be073a904 --- /dev/null +++ b/packit_service/worker/events/koji/abstract.py @@ -0,0 +1,102 @@ +# Copyright Contributors to the Packit project. +# SPDX-License-Identifier: MIT + +import logging +from typing import Optional, Union + +from packit_service.models import ( + AbstractProjectObjectDbType, + KojiBuildTargetModel, + ProjectEventModel, +) +from packit_service.worker.events.abstract import Result + +logger = logging.getLogger(__name__) + + +class KojiEvent(Result): + def __init__( + self, + task_id: int, + rpm_build_task_ids: Optional[dict[str, int]] = None, + start_time: Optional[Union[int, float, str]] = None, + completion_time: Optional[Union[int, float, str]] = None, + ): + super().__init__() + self.task_id = task_id + # dictionary with archs and IDs, e.g. {"x86_64": 123} + self.rpm_build_task_ids = rpm_build_task_ids + self.start_time: Optional[Union[int, float, str]] = start_time + self.completion_time: Optional[Union[int, float, str]] = completion_time + + # Lazy properties + self._target: Optional[str] = None + self._build_model: Optional[KojiBuildTargetModel] = None + self._build_model_searched = False + + @property + def build_model(self) -> Optional[KojiBuildTargetModel]: + if not self._build_model_searched and not self._build_model: + self._build_model = KojiBuildTargetModel.get_by_task_id( + task_id=self.task_id, + ) + self._build_model_searched = True + return self._build_model + + def get_db_project_object(self) -> Optional[AbstractProjectObjectDbType]: + return self.build_model.get_project_event_object() if self.build_model else None + + def get_db_project_event(self) -> Optional[ProjectEventModel]: + return self.build_model.get_project_event_model() if self.build_model else None + + @property + def target(self) -> Optional[str]: + if not self._target and self.build_model: + self._target = self.build_model.target + return self._target + + @staticmethod + def get_koji_rpm_build_web_url( + rpm_build_task_id: int, + koji_web_url: str = "https://koji.fedoraproject.org", + ) -> str: + """ + Constructs the web URL for the given Koji task. + You can redefine the Koji instance using the one defined in the service config. + """ + return f"{koji_web_url}/koji/taskinfo?taskID={rpm_build_task_id}" + + @staticmethod + def get_koji_build_logs_url( + rpm_build_task_id: int, + koji_logs_url: str = "https://kojipkgs.fedoraproject.org", + ) -> str: + """ + Constructs the log URL for the given Koji task. + You can redefine the Koji instance using the one defined in the service config. + """ + return ( + f"{koji_logs_url}//work/tasks/" + f"{rpm_build_task_id % 10000}/{rpm_build_task_id}/build.log" + ) + + def get_koji_build_rpm_tasks_logs_urls( + self, + koji_logs_url: str = "https://kojipkgs.fedoraproject.org", + ) -> dict[str, str]: + """ + Constructs the log URLs for all RPM subtasks of the Koji task. + """ + return { + arch: KojiEvent.get_koji_build_logs_url( + rpm_build_task_id=rpm_build_task_id, + koji_logs_url=koji_logs_url, + ) + for arch, rpm_build_task_id in self.rpm_build_task_ids.items() + } + + def get_dict(self, default_dict: Optional[dict] = None) -> dict: + result = super().get_dict() + result.pop("_build_model") + result.pop("_build_model_searched") + return result diff --git a/packit_service/worker/events/koji.py b/packit_service/worker/events/koji/base.py similarity index 72% rename from packit_service/worker/events/koji.py rename to packit_service/worker/events/koji/base.py index b7d9273f3..24229df6c 100644 --- a/packit_service/worker/events/koji.py +++ b/packit_service/worker/events/koji/base.py @@ -1,5 +1,6 @@ # Copyright Contributors to the Packit project. # SPDX-License-Identifier: MIT + import logging from typing import Optional, Union @@ -11,111 +12,20 @@ from packit_service.config import PackageConfigGetter from packit_service.constants import KojiBuildState, KojiTaskState from packit_service.models import ( - AbstractProjectObjectDbType, GitBranchModel, - KojiBuildTargetModel, - ProjectEventModel, ProjectReleaseModel, PullRequestModel, ) -from packit_service.worker.events.abstract import Result from packit_service.worker.events.event import ( use_for_job_config_trigger, ) +from packit_service.worker.events.koji.abstract import KojiEvent logger = logging.getLogger(__name__) -class AbstractKojiEvent(Result): - def __init__( - self, - task_id: int, - rpm_build_task_ids: Optional[dict[str, int]] = None, - start_time: Optional[Union[int, float, str]] = None, - completion_time: Optional[Union[int, float, str]] = None, - ): - super().__init__() - self.task_id = task_id - # dictionary with archs and IDs, e.g. {"x86_64": 123} - self.rpm_build_task_ids = rpm_build_task_ids - self.start_time: Optional[Union[int, float, str]] = start_time - self.completion_time: Optional[Union[int, float, str]] = completion_time - - # Lazy properties - self._target: Optional[str] = None - self._build_model: Optional[KojiBuildTargetModel] = None - self._build_model_searched = False - - @property - def build_model(self) -> Optional[KojiBuildTargetModel]: - if not self._build_model_searched and not self._build_model: - self._build_model = KojiBuildTargetModel.get_by_task_id( - task_id=self.task_id, - ) - self._build_model_searched = True - return self._build_model - - def get_db_project_object(self) -> Optional[AbstractProjectObjectDbType]: - return self.build_model.get_project_event_object() if self.build_model else None - - def get_db_project_event(self) -> Optional[ProjectEventModel]: - return self.build_model.get_project_event_model() if self.build_model else None - - @property - def target(self) -> Optional[str]: - if not self._target and self.build_model: - self._target = self.build_model.target - return self._target - - @staticmethod - def get_koji_rpm_build_web_url( - rpm_build_task_id: int, - koji_web_url: str = "https://koji.fedoraproject.org", - ) -> str: - """ - Constructs the web URL for the given Koji task. - You can redefine the Koji instance using the one defined in the service config. - """ - return f"{koji_web_url}/koji/taskinfo?taskID={rpm_build_task_id}" - - @staticmethod - def get_koji_build_logs_url( - rpm_build_task_id: int, - koji_logs_url: str = "https://kojipkgs.fedoraproject.org", - ) -> str: - """ - Constructs the log URL for the given Koji task. - You can redefine the Koji instance using the one defined in the service config. - """ - return ( - f"{koji_logs_url}//work/tasks/" - f"{rpm_build_task_id % 10000}/{rpm_build_task_id}/build.log" - ) - - def get_koji_build_rpm_tasks_logs_urls( - self, - koji_logs_url: str = "https://kojipkgs.fedoraproject.org", - ) -> dict[str, str]: - """ - Constructs the log URLs for all RPM subtasks of the Koji task. - """ - return { - arch: AbstractKojiEvent.get_koji_build_logs_url( - rpm_build_task_id=rpm_build_task_id, - koji_logs_url=koji_logs_url, - ) - for arch, rpm_build_task_id in self.rpm_build_task_ids.items() - } - - def get_dict(self, default_dict: Optional[dict] = None) -> dict: - result = super().get_dict() - result.pop("_build_model") - result.pop("_build_model_searched") - return result - - @use_for_job_config_trigger(trigger_type=JobConfigTriggerType.commit) -class KojiBuildEvent(AbstractKojiEvent): +class Build(KojiEvent): def __init__( self, build_id: int, @@ -199,8 +109,8 @@ def get_dict(self, default_dict: Optional[dict] = None) -> dict: return result @classmethod - def from_event_dict(cls, event: dict): - return KojiBuildEvent( + def from_event_dict(cls, event: dict) -> "Build": + return Build( build_id=event.get("build_id"), state=KojiBuildState(raw_new) if (raw_new := event.get("state")) else None, old_state=(KojiBuildState(raw_old) if (raw_old := event.get("old_state")) else None), @@ -222,7 +132,7 @@ def from_event_dict(cls, event: dict): ) -class KojiTaskEvent(AbstractKojiEvent): +class Task(KojiEvent): """ Used for scratch builds. """ @@ -294,8 +204,8 @@ def identifier(self) -> str: return self._identifier @classmethod - def from_event_dict(cls, event: dict): - return KojiTaskEvent( + def from_event_dict(cls, event: dict) -> "Task": + return Task( task_id=event.get("task_id"), state=KojiTaskState(event.get("state")) if event.get("state") else None, old_state=(KojiTaskState(event.get("old_state")) if event.get("old_state") else None), @@ -328,7 +238,7 @@ def get_dict(self, default_dict: Optional[dict] = None) -> dict: @use_for_job_config_trigger(trigger_type=JobConfigTriggerType.koji_build) -class KojiBuildTagEvent(AbstractKojiEvent): +class BuildTag(KojiEvent): _koji_helper: Optional[KojiHelper] = None def __init__( @@ -374,8 +284,8 @@ def nvr(self) -> str: return f"{self.package_name}-{self.version}-{self.release}" @classmethod - def from_event_dict(cls, event: dict) -> "KojiBuildTagEvent": - return KojiBuildTagEvent( + def from_event_dict(cls, event: dict) -> "BuildTag": + return BuildTag( build_id=event.get("build_id"), tag_id=event.get("tag_id"), tag_name=event.get("tag_name"), diff --git a/packit_service/worker/handlers/bodhi.py b/packit_service/worker/handlers/bodhi.py index c13d7f605..9086747f4 100644 --- a/packit_service/worker/handlers/bodhi.py +++ b/packit_service/worker/handlers/bodhi.py @@ -44,7 +44,7 @@ IssueCommentGitlabEvent, PullRequestCommentPagureEvent, ) -from packit_service.worker.events.koji import KojiBuildEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent from packit_service.worker.handlers.abstract import ( RetriableJobHandler, TaskName, diff --git a/packit_service/worker/handlers/distgit.py b/packit_service/worker/handlers/distgit.py index 6abfd8c90..303a34653 100644 --- a/packit_service/worker/handlers/distgit.py +++ b/packit_service/worker/handlers/distgit.py @@ -88,7 +88,7 @@ ReleaseEvent, ReleaseGitlabEvent, ) -from packit_service.worker.events.koji import KojiBuildTagEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.abstract import ( JobHandler, RetriableJobHandler, diff --git a/packit_service/worker/handlers/koji.py b/packit_service/worker/handlers/koji.py index 1d34e25d9..9a115d2f9 100644 --- a/packit_service/worker/handlers/koji.py +++ b/packit_service/worker/handlers/koji.py @@ -58,7 +58,8 @@ ReleaseEvent, ReleaseGitlabEvent, ) -from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.abstract import ( JobHandler, TaskName, diff --git a/packit_service/worker/handlers/mixin.py b/packit_service/worker/handlers/mixin.py index 6a19b96a0..f91154b99 100644 --- a/packit_service/worker/handlers/mixin.py +++ b/packit_service/worker/handlers/mixin.py @@ -30,7 +30,8 @@ from packit_service.worker.events.github.pr import ( Comment as PullRequestCommentGithubEvent, ) -from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.abstract import CeleryTask from packit_service.worker.helpers.build.copr_build import CoprBuildJobHelper from packit_service.worker.helpers.build.koji_build import KojiBuildJobHelper diff --git a/packit_service/worker/jobs.py b/packit_service/worker/jobs.py index 25c006d6f..e6ea6c7c3 100644 --- a/packit_service/worker/jobs.py +++ b/packit_service/worker/jobs.py @@ -47,7 +47,8 @@ AbstractPRCommentEvent, CommitCommentEvent, ) -from packit_service.worker.events.koji import KojiBuildTagEvent, KojiTaskEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent +from packit_service.worker.events.koji.base import Task as KojiTaskEvent from packit_service.worker.handlers import ( CoprBuildHandler, GithubAppInstallationHandler, diff --git a/packit_service/worker/parser.py b/packit_service/worker/parser.py index 0debd5350..34820a0ca 100644 --- a/packit_service/worker/parser.py +++ b/packit_service/worker/parser.py @@ -70,7 +70,8 @@ PullRequestCommentAction, ) from packit_service.worker.events.github.commit import Comment as CommitCommentGithubEvent -from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers.abstract import MAP_CHECK_PREFIX_TO_HANDLER from packit_service.worker.helpers.build import CoprBuildJobHelper, KojiBuildJobHelper from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper diff --git a/tests/conftest.py b/tests/conftest.py index 878afb60a..4800db462 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,7 +27,7 @@ PushPagureEvent, ReleaseEvent, ) -from packit_service.worker.events.koji import KojiBuildEvent +from packit_service.worker.events.koji.base import Build as KojiBuildEvent from packit_service.worker.parser import Parser from tests.spellbook import DATA_DIR, SAVED_HTTPD_REQS, load_the_message_from_file diff --git a/tests/integration/test_listen_to_fedmsg.py b/tests/integration/test_listen_to_fedmsg.py index 85d17d263..8ca6520df 100644 --- a/tests/integration/test_listen_to_fedmsg.py +++ b/tests/integration/test_listen_to_fedmsg.py @@ -47,7 +47,7 @@ get_srpm_build_info_url, ) from packit_service.worker.events import AbstractCoprBuildEvent, KojiTaskEvent -from packit_service.worker.events.koji import KojiBuildTagEvent +from packit_service.worker.events.koji.base import BuildTag as KojiBuildTagEvent from packit_service.worker.handlers import CoprBuildEndHandler from packit_service.worker.handlers.bodhi import BodhiUpdateFromSidetagHandler from packit_service.worker.handlers.distgit import DownstreamKojiBuildHandler diff --git a/tests/unit/events/test_koji.py b/tests/unit/events/test_koji.py index e96cadf82..bd92da235 100644 --- a/tests/unit/events/test_koji.py +++ b/tests/unit/events/test_koji.py @@ -8,10 +8,14 @@ from packit_service.constants import KojiBuildState, KojiTaskState from packit_service.models import KojiBuildTargetModel -from packit_service.worker.events.koji import ( - KojiBuildEvent, - KojiBuildTagEvent, - KojiTaskEvent, +from packit_service.worker.events.koji.base import ( + Build as KojiBuildEvent, +) +from packit_service.worker.events.koji.base import ( + BuildTag as KojiBuildTagEvent, +) +from packit_service.worker.events.koji.base import ( + Task as KojiTaskEvent, ) from packit_service.worker.parser import Parser diff --git a/tests/unit/test_jobs.py b/tests/unit/test_jobs.py index cd4631663..aa0c8deda 100644 --- a/tests/unit/test_jobs.py +++ b/tests/unit/test_jobs.py @@ -43,10 +43,12 @@ TestingFarmResultsEvent, VMImageBuildResultEvent, ) -from packit_service.worker.events.koji import ( - AbstractKojiEvent, - KojiBuildEvent, - KojiBuildTagEvent, +from packit_service.worker.events.koji.abstract import KojiEvent as AbstractKojiEvent +from packit_service.worker.events.koji.base import ( + Build as KojiBuildEvent, +) +from packit_service.worker.events.koji.base import ( + BuildTag as KojiBuildTagEvent, ) from packit_service.worker.handlers import ( CoprBuildEndHandler,