Skip to content

Commit

Permalink
Add handler for OpenScanHub task finish
Browse files Browse the repository at this point in the history
  • Loading branch information
majamassarini committed Oct 16, 2024
1 parent 1502962 commit 7cbb194
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 223 deletions.
10 changes: 10 additions & 0 deletions packit_service/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,13 @@ def from_number(number: int):
USAGE_PAST_YEAR_DATE_STR = (datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d")
USAGE_DATE_IN_THE_PAST = USAGE_CURRENT_DATE.replace(year=USAGE_CURRENT_DATE.year - 100)
USAGE_DATE_IN_THE_PAST_STR = USAGE_DATE_IN_THE_PAST.strftime("%Y-%m-%d")

OPEN_SCAN_HUB_FEATURE_DESCRIPTION = (
"This is an experimental feature. Once the scan finishes, you can see the "
"new findings in the `added.html` in `Logs`. \n\n"
":warning: You can see the list of known issues and also provide your feedback"
" [here](https://github.com/packit/packit/discussions/2371). \n\n"
"You can disable the scanning in your configuration by "
"setting `osh_diff_scan_after_copr_build` to `false`. For more information, "
f"see [docs]({DOCS_URL}/configuration#osh_diff_scan_after_copr_build)."
)
2 changes: 2 additions & 0 deletions packit_service/worker/allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ReleaseEvent,
TestingFarmResultsEvent,
CheckRerunEvent,
OpenScanHubTaskFinishEvent,
)
from packit_service.worker.events.gitlab import ReleaseGitlabEvent
from packit_service.worker.events.koji import KojiBuildEvent, KojiBuildTagEvent
Expand Down Expand Up @@ -485,6 +486,7 @@ def check_and_report(
KojiBuildTagEvent,
CheckRerunEvent,
NewHotnessUpdateEvent,
OpenScanHubTaskFinishEvent,
): self._check_unchecked_event,
(
ReleaseEvent,
Expand Down
32 changes: 28 additions & 4 deletions packit_service/worker/events/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ def from_event_dict(cls, event: dict):
branches_override=branches_override,
)

def to_event(self) -> "Event":
"""
Create an instance of Event class from the data in this class.
"""
mod = __import__("packit_service.worker.events", fromlist=[self.event_type])
kls = getattr(mod, self.event_type)
kwargs = copy.copy(self.event_dict)
# The following data should be reconstructed by the Event instance (when needed)
kwargs.pop("event_type", None)
kwargs.pop("event_id", None)
kwargs.pop("task_accepted_time", None)
kwargs.pop("build_targets_override", None)
kwargs.pop("tests_targets_override", None)
kwargs.pop("branches_override", None)
pr_id = kwargs.pop("_pr_id", None)
kwargs["pr_id"] = pr_id
return kls(**kwargs)

@property
def project(self):
if not self._project:
Expand Down Expand Up @@ -367,12 +385,17 @@ def store_packages_config(self):
self.db_project_event.set_packages_config(package_config_dict)

def get_non_serializable_attributes(self):
"""List here both non serializable attributes and attributes that
we want to skip from the dict because are not needed to re-create
the event.
"""
return [
"_db_project_object",
"_db_project_event",
"_project",
"_base_project",
"_package_config",
"_package_config_searched",
]

def get_dict(self, default_dict: Optional[Dict] = None) -> dict:
Expand Down Expand Up @@ -618,10 +641,11 @@ def get_all_build_targets_by_status(
statuses_to_filter_with=statuses_to_filter_with,
)

def get_dict(self, default_dict: Optional[Dict] = None) -> dict:
result = super().get_dict()
result.pop("_pull_request_object")
return result
def get_non_serializable_attributes(self):
return super().get_non_serializable_attributes() + [
"fail_when_config_file_missing",
"_pull_request_object",
]


class AbstractResultEvent(AbstractForgeIndependentEvent):
Expand Down
33 changes: 30 additions & 3 deletions packit_service/worker/events/openscanhub.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from typing import Optional
from logging import getLogger
from packit_service.worker.events import Event

from ogr.abstract import GitProject
from packit_service.config import ServiceConfig
from packit_service.worker.events.event import AbstractResultEvent
from packit_service.models import (
AbstractProjectObjectDbType,
ProjectEventModel,
ScanModel,
)

logger = getLogger(__name__)


class OpenScanHubTaskFinishEvent(Event):
class OpenScanHubTaskFinishEvent(AbstractResultEvent):
def __init__(
self,
task_id: int,
issues_added_url: str,
issues_fixed_url: str,
scan_results_url: str,
**kwargs,
):
super().__init__()
super().__init__(**kwargs)

self.task_id = task_id
self.issues_added_url = issues_added_url
self.issues_fixed_url = issues_fixed_url
self.scan_results_url = scan_results_url

self.scan = ScanModel.get_by_task_id(task_id)
self.build = self.scan.copr_build_target

def get_db_project_object(self) -> Optional[AbstractProjectObjectDbType]:
return self.build.get_project_event_object()

def get_db_project_event(self) -> Optional[ProjectEventModel]:
return self.build.get_project_event_model()

def get_project(self) -> GitProject:
return ServiceConfig.get_service_config().get_project(
self.db_project_object.project.project_url
)

def get_non_serializable_attributes(self):
return super().get_non_serializable_attributes() + ["build", "scan"]
4 changes: 4 additions & 0 deletions packit_service/worker/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
VMImageBuildHandler,
VMImageBuildResultHandler,
)
from packit_service.worker.handlers.open_scan_hub import (
OpenScanHubTaskFinishHandler,
)

__all__ = [
Handler.__name__,
Expand All @@ -55,4 +58,5 @@
GithubFasVerificationHandler.__name__,
VMImageBuildHandler.__name__,
VMImageBuildResultHandler.__name__,
OpenScanHubTaskFinishHandler.__name__,
]
1 change: 1 addition & 0 deletions packit_service/worker/handlers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class TaskName(str, enum.Enum):
check_onboarded_projects = "task.check_onboarded_projects"
koji_build_tag = "task.koji_build_tag"
tag_into_sidetag = "task.tag_into_sidetag"
openscanhub_task_finish = "task.openscanhub_task_finish"


class Handler(PackitAPIProtocol, Config):
Expand Down
Loading

0 comments on commit 7cbb194

Please sign in to comment.