Skip to content

Commit

Permalink
Adjust URL reporting for OpenScanHub
Browse files Browse the repository at this point in the history
Report only the HTML URL for added issues and only if there are any.
Context: packit/packit#2371 (comment)
  • Loading branch information
lbarcziova committed Nov 7, 2024
1 parent 200ed20 commit c9817b0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
69 changes: 62 additions & 7 deletions packit_service/worker/handlers/open_scan_hub.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import json
import logging
from typing import Union

import requests
from packit.config import (
JobType,
)
Expand Down Expand Up @@ -99,17 +100,71 @@ class OpenScanHubTaskFinishedHandler(
event: OpenScanHubTaskFinishedEvent
task_name = TaskName.openscanhub_task_finished

@staticmethod
def check_new_findings_identified(url: str) -> bool:
"""
Downloads a JSON file from the specified URL and
checks if the 'defects' array is empty.
Parameters:
url (str): The URL of the JSON file.
Returns:
bool: True if the 'defects' array is not empty or we were not
able to get it, False otherwise.
"""
logger.info(
f"About to check if there were any new findings identified by the scan in {url}."
)
try:
with requests.get(url, timeout=10) as response:
response.raise_for_status()
data = response.json()

defects = data.get("defects", [])
return len(defects) > 0

except requests.exceptions.RequestException:
logger.error("Error occurred while trying to download the JSON file.")
return True
except json.JSONDecodeError:
logger.error("The response is not a valid JSON.")
return True

@staticmethod
def get_issues_added_url(
task_id: int,
openscanhub_url: str = "https://openscanhub.fedoraproject.org",
file_format: str = "html",
) -> str:
"""
Constructs the URL for the added issues in the specified
format for the given OpenScanHub task.
Parameters:
task_id (int): The ID of the task.
openscanhub_url (str)
file_format (str): The format of the added issues file ('html' or 'json').
Returns:
str: The full URL to access the added issues in the specified format.
"""

return f"{openscanhub_url}/task/{task_id}/log/added.{file_format}"

def run(self) -> TaskResults:
self.check_scan_and_build()

if self.event.status == OpenScanHubTaskFinishedEvent.Status.success:
state = BaseCommitStatus.success
description = "Scan in OpenScanHub is finished. Check the URL for more details."
external_links = {
"Added issues": self.event.issues_added_url,
"Fixed issues": self.event.issues_fixed_url,
"Scan results": self.event.scan_results_url,
}

if self.check_new_findings_identified(self.event.issues_added_url):
description = "Scan in OpenScanHub is finished. Check the URL for more details."
external_links = {"Added issues": self.get_issues_added_url(self.event.task_id)}
else:
description = "Scan in OpenScanHub is finished. No new findings identified."
external_links = {}

self.event.scan.set_status(OSHScanStatus.succeeded)
self.event.scan.set_issues_added_url(self.event.issues_added_url)
self.event.scan.set_issues_fixed_url(self.event.issues_fixed_url)
Expand Down
15 changes: 5 additions & 10 deletions tests/unit/test_open_scan_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
OpenScanHubTaskFinishedEvent,
OpenScanHubTaskStartedEvent,
)
from packit_service.worker.handlers import OpenScanHubTaskFinishedHandler
from packit_service.worker.handlers.copr import OpenScanHubHelper
from packit_service.worker.helpers import open_scan_hub
from packit_service.worker.helpers.build import CoprBuildJobHelper
Expand Down Expand Up @@ -257,17 +258,11 @@ def test_handle_scan_task_finished(
flexmock(scan_mock).should_receive("set_status").with_args(
"succeeded",
).once()
flexmock(OpenScanHubTaskFinishedHandler).should_receive(
"check_new_findings_identified"
).and_return(True)
links_to_external_services = {
"Added issues": (
"http://openscanhub.fedoraproject.org/task/15649/log/added.js" "?format=raw"
),
"Fixed issues": (
"http://openscanhub.fedoraproject.org/task/15649/log/fixed.js" "?format=raw"
),
"Scan results": (
"http://openscanhub.fedoraproject.org/task/15649/log/gvisor-tap-vsock-"
"0.7.5-1.20241007054606793155.pr405.23.g829aafd6/scan-results.js?format=raw"
),
"Added issues": ("https://openscanhub.fedoraproject.org/task/15649/log/added.html"),
}
elif scan_status == OpenScanHubTaskFinishedEvent.Status.cancel:
state = BaseCommitStatus.neutral
Expand Down

0 comments on commit c9817b0

Please sign in to comment.