Skip to content

Commit

Permalink
fix(events): refactor Koji events
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Focko <[email protected]>
  • Loading branch information
mfocko committed Jan 6, 2025
1 parent c0c227d commit 8e6af25
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 121 deletions.
3 changes: 2 additions & 1 deletion packit_service/worker/allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packit_service/worker/checker/bodhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packit_service/worker/checker/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packit_service/worker/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 2 additions & 0 deletions packit_service/worker/events/koji/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
102 changes: 102 additions & 0 deletions packit_service/worker/events/koji/abstract.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import logging
from typing import Optional, Union

Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -222,7 +132,7 @@ def from_event_dict(cls, event: dict):
)


class KojiTaskEvent(AbstractKojiEvent):
class Task(KojiEvent):
"""
Used for scratch builds.
"""
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion packit_service/worker/handlers/bodhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packit_service/worker/handlers/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packit_service/worker/handlers/koji.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packit_service/worker/handlers/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packit_service/worker/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packit_service/worker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_listen_to_fedmsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 8e6af25

Please sign in to comment.