From 2c2c8e294d34beb8e9afe02314a9358da5805bc9 Mon Sep 17 00:00:00 2001 From: Laura Barcziova Date: Thu, 14 Nov 2024 15:03:47 +0100 Subject: [PATCH 1/2] Store number of found defects for OSH scans Fixes #2625 --- ...add_issues_added_count_for_oshscanmodel.py | 29 +++++++++++++++++++ packit_service/models.py | 6 ++++ .../worker/handlers/open_scan_hub.py | 2 ++ tests/unit/test_open_scan_hub.py | 1 + 4 files changed, 38 insertions(+) create mode 100644 alembic/versions/f69687c314c5_add_issues_added_count_for_oshscanmodel.py diff --git a/alembic/versions/f69687c314c5_add_issues_added_count_for_oshscanmodel.py b/alembic/versions/f69687c314c5_add_issues_added_count_for_oshscanmodel.py new file mode 100644 index 000000000..48770a109 --- /dev/null +++ b/alembic/versions/f69687c314c5_add_issues_added_count_for_oshscanmodel.py @@ -0,0 +1,29 @@ +"""Add issues_added_count for OSHScanModel + +Revision ID: f69687c314c5 +Revises: 4387d6ab90e9 +Create Date: 2024-11-14 14:02:08.691590 + +""" + +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "f69687c314c5" +down_revision = "4387d6ab90e9" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("scans", sa.Column("issues_added_count", sa.Integer(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("scans", "issues_added_count") + # ### end Alembic commands ### diff --git a/packit_service/models.py b/packit_service/models.py index 6d63126b6..020c936da 100644 --- a/packit_service/models.py +++ b/packit_service/models.py @@ -4064,6 +4064,7 @@ class OSHScanModel(Base): task_id = Column(Integer, unique=True) # open scan hub id status = Column(Enum(OSHScanStatus)) url = Column(String) + issues_added_count = Column(Integer) issues_added_url = Column(String) issues_fixed_url = Column(String) scan_results_url = Column(String) @@ -4115,6 +4116,11 @@ def set_scan_results_url(self, scan_results_url: str) -> None: self.scan_results_url = scan_results_url session.add(self) + def set_issues_added_count(self, issues_added_count: int) -> None: + with sa_session_transaction(commit=True) as session: + self.issues_added_count = issues_added_count + session.add(self) + @classmethod def get_by_task_id(cls, task_id: int) -> Optional["OSHScanModel"]: with sa_session_transaction() as session: diff --git a/packit_service/worker/handlers/open_scan_hub.py b/packit_service/worker/handlers/open_scan_hub.py index 4a02dd7cb..a9fcc3cc0 100644 --- a/packit_service/worker/handlers/open_scan_hub.py +++ b/packit_service/worker/handlers/open_scan_hub.py @@ -169,8 +169,10 @@ def run(self) -> TaskResults: f"{base_description} {number_of_new_findings} new findings identified." ) external_links.update({"Added issues": self.get_issues_added_url()}) + self.event.scan.set_issues_added_count(number_of_new_findings) else: description = f"{base_description} No new findings identified." + self.event.scan.set_issues_added_count(number_of_new_findings) self.event.scan.set_status(OSHScanStatus.succeeded) self.event.scan.set_issues_added_url(self.event.issues_added_url) diff --git a/tests/unit/test_open_scan_hub.py b/tests/unit/test_open_scan_hub.py index 5b9b9ef02..cb2160c77 100644 --- a/tests/unit/test_open_scan_hub.py +++ b/tests/unit/test_open_scan_hub.py @@ -266,6 +266,7 @@ def test_handle_scan_task_finished( flexmock(scan_mock).should_receive("set_status").with_args( "succeeded", ).once() + flexmock(scan_mock).should_receive("set_issues_added_count").with_args(2).once() flexmock(OpenScanHubTaskFinishedHandler).should_receive( "get_number_of_new_findings_identified" ).and_return(2) From 9dc1ba0f09329ed2ee0b1408520b043021f9d7ab Mon Sep 17 00:00:00 2001 From: Laura Barcziova Date: Fri, 15 Nov 2024 10:30:08 +0100 Subject: [PATCH 2/2] Expose number of found defects in OSH via API --- packit_service/service/api/osh_scans.py | 1 + tests_openshift/conftest.py | 2 ++ tests_openshift/service/test_api.py | 1 + 3 files changed, 4 insertions(+) diff --git a/packit_service/service/api/osh_scans.py b/packit_service/service/api/osh_scans.py index 178771afc..d70a7c30b 100644 --- a/packit_service/service/api/osh_scans.py +++ b/packit_service/service/api/osh_scans.py @@ -63,6 +63,7 @@ def get_scan_info(scan: OSHScanModel) -> dict: "task_id": scan.task_id, "status": scan.status, "url": scan.url, + "issues_added_count": scan.issues_added_count, "issues_added_url": scan.issues_added_url, "issues_fixed_url": scan.issues_fixed_url, "scan_results_url": scan.scan_results_url, diff --git a/tests_openshift/conftest.py b/tests_openshift/conftest.py index 37dec33a2..d90c58f03 100644 --- a/tests_openshift/conftest.py +++ b/tests_openshift/conftest.py @@ -168,6 +168,7 @@ class SampleValues: # scan task_id = 123 scan_url = "https://scan-url" + issues_added_count = 3 issues_added_url = "https://issues-added-url" issues_fixed_url = "https://issues-fixed-url" scan_results_url = "https://scan-results-url" @@ -2503,4 +2504,5 @@ def a_scan(a_copr_build_for_pr): scan.issues_added_url = SampleValues.issues_added_url scan.issues_fixed_url = SampleValues.issues_fixed_url scan.scan_results_url = SampleValues.scan_results_url + scan.issues_added_count = SampleValues.issues_added_count yield scan diff --git a/tests_openshift/service/test_api.py b/tests_openshift/service/test_api.py index 571424c1b..e972ced56 100644 --- a/tests_openshift/service/test_api.py +++ b/tests_openshift/service/test_api.py @@ -979,6 +979,7 @@ def test_scan_info( assert response_dict["repo_namespace"] == SampleValues.repo_namespace assert response_dict["repo_name"] == SampleValues.repo_name assert response_dict["project_url"] == SampleValues.project_url + assert response_dict["issues_added_count"] == SampleValues.issues_added_count def test_scans_list(