Skip to content

Commit

Permalink
Parse OpenScanHub URL correctly from stdout (#2477)
Browse files Browse the repository at this point in the history
Parse OpenScanHub URL correctly from stdout

Stdout may include lot of info e.g. about Kerberos ticket processing before the actual dict response from the osh-client, which should be in the last line of the output.

Reviewed-by: Siteshwar Vashisht
Reviewed-by: Laura Barcziová
Reviewed-by: Maja Massarini
Reviewed-by: Nikola Forró
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jul 29, 2024
2 parents 7540cb0 + d8278d2 commit e7e19ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import json
import logging
import re
import tempfile
from datetime import datetime, timezone
from os import getenv
Expand Down Expand Up @@ -564,17 +565,20 @@ def handle_scan(self):
logger.debug("Something went wrong, skipping the reporting.")
return

logger.info(f"Scan submitted successfully, output: {output}")
logger.info("Scan submitted successfully.")

response = json.loads(output)
if not (url := response.get("url")):
response_dict = self.parse_dict_from_output(output)

logger.debug(f"Parsed dict from output: {response_dict} ")

if not (url := response_dict.get("url")):
logger.debug("It was not possible to get the URL from the response.")
return

self.copr_build_helper._report(
state=BaseCommitStatus.success,
description=(
"Scan in OpenScanHub submitted succesfully. Check the URL for more details."
"Scan in OpenScanHub submitted successfully. Check the URL for more details."
),
url=url,
check_names=["osh-diff-scan:fedora-rawhide-x86_64"],
Expand All @@ -587,6 +591,17 @@ def handle_scan(self):
),
)

@staticmethod
def parse_dict_from_output(output: str) -> dict:
json_pattern = r"\{.*?\}"
matches = re.findall(json_pattern, output, re.DOTALL)

if not matches:
return {}

json_str = matches[-1]
return json.loads(json_str)

def find_base_build_job(self) -> Optional[JobConfig]:
"""
Find the job in the config that can provide the base build for the scan
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_handle_scan():
).and_return([base_copr_model])

flexmock(PackitAPI).should_receive("run_osh_build").once().and_return(
'{"url": "scan-url"}'
'some\nmultiline\noutput\n{"id": 123}\nand\nmore\n{"url": "scan-url"}\n'
)

flexmock(CoprBuildJobHelper).should_receive("_report")
Expand Down

0 comments on commit e7e19ce

Please sign in to comment.