From 60343006106e2f925d6ca4b0e2674899bdb1fcd7 Mon Sep 17 00:00:00 2001 From: Laura Barcziova Date: Tue, 10 Dec 2024 08:41:10 +0100 Subject: [PATCH] Consider identifiers for OpenScanHub Fixes #2653 --- packit_service/worker/checker/open_scan_hub.py | 12 ++++++++++++ packit_service/worker/events/open_scan_hub.py | 5 ++++- packit_service/worker/handlers/open_scan_hub.py | 4 ++-- packit_service/worker/helpers/open_scan_hub.py | 5 ++++- tests/unit/events/test_open_scan_hub.py | 1 + tests/unit/test_open_scan_hub.py | 13 ++++++++++--- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packit_service/worker/checker/open_scan_hub.py b/packit_service/worker/checker/open_scan_hub.py index 7e5e6c792..72f4440af 100644 --- a/packit_service/worker/checker/open_scan_hub.py +++ b/packit_service/worker/checker/open_scan_hub.py @@ -25,3 +25,15 @@ def pre_check(self) -> bool: ) return False return True + + +class IsEventForJob(Checker): + def pre_check(self) -> bool: + if self.data.identifier != self.job_config.identifier: + logger.debug( + f"Skipping reporting, identifiers don't match " + f"(identifier of the OpenScanHub job to report: {self.data.identifier}, " + f"identifier from build job config: {self.job_config.identifier}).", + ) + return False + return True diff --git a/packit_service/worker/events/open_scan_hub.py b/packit_service/worker/events/open_scan_hub.py index 85d443928..aeac43fb4 100644 --- a/packit_service/worker/events/open_scan_hub.py +++ b/packit_service/worker/events/open_scan_hub.py @@ -10,6 +10,7 @@ from packit_service.config import ServiceConfig from packit_service.models import ( AbstractProjectObjectDbType, + CoprBuildTargetModel, OSHScanModel, ProjectEventModel, ) @@ -23,6 +24,7 @@ def __init__( self, task_id: int, commit_sha: Optional[str] = None, + identifier: Optional[str] = None, **kwargs, ): super().__init__(**kwargs) @@ -31,7 +33,7 @@ def __init__( self.commit_sha = commit_sha self.scan = OSHScanModel.get_by_task_id(task_id) - self.build = None + self.build: Optional[CoprBuildTargetModel] = None if not self.scan: logger.warning( f"Scan with id {task_id} not found in the database." @@ -52,6 +54,7 @@ def __init__( # and have to be serialized to be later found in the # event metadata self.commit_sha = project_event.commit_sha if not self.commit_sha else self.commit_sha + self.identifier = identifier or self.build.identifier def get_db_project_object(self) -> Optional[AbstractProjectObjectDbType]: return self.build.get_project_event_object() diff --git a/packit_service/worker/handlers/open_scan_hub.py b/packit_service/worker/handlers/open_scan_hub.py index a9fcc3cc0..bfeb22657 100644 --- a/packit_service/worker/handlers/open_scan_hub.py +++ b/packit_service/worker/handlers/open_scan_hub.py @@ -12,7 +12,7 @@ from packit_service.models import OSHScanStatus from packit_service.service.urls import get_openscanhub_info_url from packit_service.worker.checker.abstract import Checker -from packit_service.worker.checker.open_scan_hub import RawhideX86Target +from packit_service.worker.checker.open_scan_hub import IsEventForJob, RawhideX86Target from packit_service.worker.events import ( OpenScanHubTaskFinishedEvent, OpenScanHubTaskStartedEvent, @@ -52,7 +52,7 @@ def __init__(self, **kwargs): @staticmethod def get_checkers() -> tuple[type[Checker], ...]: - return (RawhideX86Target,) + return (RawhideX86Target, IsEventForJob) def get_helper(self) -> OpenScanHubHelper: build_helper = CoprBuildJobHelper( diff --git a/packit_service/worker/helpers/open_scan_hub.py b/packit_service/worker/helpers/open_scan_hub.py index 13423c1f6..f11146051 100644 --- a/packit_service/worker/helpers/open_scan_hub.py +++ b/packit_service/worker/helpers/open_scan_hub.py @@ -151,11 +151,14 @@ def report( url: str, links_to_external_services: Optional[dict[str, str]] = None, ): + check_name = "osh-diff-scan:fedora-rawhide-x86_64" + if identifier := self.copr_build_helper.job_config.identifier: + check_name += f":{identifier}" self.copr_build_helper._report( state=state, description=description, url=url, - check_names=["osh-diff-scan:fedora-rawhide-x86_64"], + check_names=[check_name], markdown_content=OPEN_SCAN_HUB_FEATURE_DESCRIPTION, links_to_external_services=links_to_external_services, ) diff --git a/tests/unit/events/test_open_scan_hub.py b/tests/unit/events/test_open_scan_hub.py index 9736a67d1..2e25583ca 100644 --- a/tests/unit/events/test_open_scan_hub.py +++ b/tests/unit/events/test_open_scan_hub.py @@ -41,6 +41,7 @@ def scan_config_and_db(add_pull_request_event_with_sha_123456): db_build = ( flexmock( build_id="55", + identifier=None, status="success", build_submitted_time=datetime.datetime.utcnow(), target="the-target", diff --git a/tests/unit/test_open_scan_hub.py b/tests/unit/test_open_scan_hub.py index cb2160c77..8c757e750 100644 --- a/tests/unit/test_open_scan_hub.py +++ b/tests/unit/test_open_scan_hub.py @@ -61,6 +61,7 @@ def prepare_openscanhub_db_and_handler( db_build = ( flexmock( build_id="55", + identifier=None, status="success", build_submitted_time=datetime.datetime.utcnow(), target="the-target", @@ -98,12 +99,17 @@ def prepare_openscanhub_db_and_handler( @pytest.mark.parametrize( "build_models", [ - [("abcdef", [flexmock(get_srpm_build=lambda: flexmock(url="base-srpm-url"))])], + [ + ( + "abcdef", + [flexmock(identifier=None, get_srpm_build=lambda: flexmock(url="base-srpm-url"))], + ) + ], [ ("abcdef", []), ( "fedcba", - [flexmock(get_srpm_build=lambda: flexmock(url="base-srpm-url"))], + [flexmock(identifier=None, get_srpm_build=lambda: flexmock(url="base-srpm-url"))], ), ], ], @@ -137,6 +143,7 @@ def test_handle_scan(build_models): branch="main", project="commit-project", owner="user-123", + identifier=None, ), ], ) @@ -170,7 +177,7 @@ def test_handle_scan(build_models): project=project, metadata=flexmock(pr_id=12), db_project_event=flexmock(get_project_event_object=lambda: None), - job_config=flexmock(), + job_config=flexmock(identifier=None), ), ).handle_scan()