From f76f59efedfdaf1a81f87be00971aa2b4ba9aab6 Mon Sep 17 00:00:00 2001 From: Oleksandr Andrieiev Date: Mon, 16 Sep 2024 19:10:18 +0300 Subject: [PATCH] fix(report): Parse data from sarif as URI, instead of treating it as string --- tests/test_code_report.py | 42 ++++++++++++++++++++++ universum/modules/code_report_collector.py | 12 +++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/tests/test_code_report.py b/tests/test_code_report.py index c960558c..34f1a8e0 100644 --- a/tests/test_code_report.py +++ b/tests/test_code_report.py @@ -169,6 +169,46 @@ def finalize(self) -> str: } """ +sarif_report_uri = """ +{ + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "Checkstyle", + "semanticVersion": "8.43", + "version": "8.43" + } + }, + "results": [ + { + "level": "warning", + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "file:///my_path/my_file" + }, + "region": { + "startColumn": 1, + "startLine": 1 + } + } + } + ], + "message": { + "text": "Error!" + }, + "ruleId": "testRule" + } + ] + } + ] +} +""" + config_uncrustify = """ code_width = 120 input_tab_size = 2 @@ -189,6 +229,7 @@ def finalize(self) -> str: [[sarif_report_minimal], True], [[sarif_report], False], [[sarif_report_split_uri], False], + [[sarif_report_uri], False], [[json_report_minimal, sarif_report_minimal], True], [[json_report, sarif_report], False], [[json_report_minimal, sarif_report], False], @@ -199,6 +240,7 @@ def finalize(self) -> str: 'sarif_no_issues', 'sarif_issues_found', 'sarif_split_uri_issues_found', + 'sarif_uri', 'both_tested_no_issues', 'both_tested_issues_in_both', 'both_tested_issues_in_sarif', diff --git a/universum/modules/code_report_collector.py b/universum/modules/code_report_collector.py index 2919b2cc..e2ebd49f 100644 --- a/universum/modules/code_report_collector.py +++ b/universum/modules/code_report_collector.py @@ -65,17 +65,17 @@ def _process_one_sarif_issue(self, issue, root_uri_base_paths, who) -> None: if location_data.get('address'): continue # binary artifact can't be processed raise ValueError("Unexpected lack of artifactLocation tag") + uri = artifact_data.get('uri') + if not uri: + raise ValueError("Unexpected lack of uri tag") + path = urllib.parse.unquote(urllib.parse.urlparse(uri).path) if artifact_data.get('uriBaseId'): - uri = artifact_data.get('uri') - if not uri: - raise ValueError("Unexpected lack of uri tag") + # means path is relative, need to make absolute uri_base_id = artifact_data.get('uriBaseId', '') root_base_path = root_uri_base_paths.get(uri_base_id, '') if uri_base_id and not root_base_path: raise ValueError(f"Unexpected lack of 'originalUriBaseIds' value for {uri_base_id}") - path = str(Path(root_base_path, urllib.parse.unquote(uri))) - else: - path = urllib.parse.unquote(artifact_data.get('uri', '')) + path = str(Path(root_base_path) / path) region_data = location_data.get('region') if not region_data: continue # TODO: cover this case as comment to the file as a whole