From e2e23372b3c6acb7f7499b3ad5c5acc9997f34e8 Mon Sep 17 00:00:00 2001 From: Josef Andersson Date: Sat, 3 Dec 2022 10:35:22 +0100 Subject: [PATCH] Remove def.ws prefix from aggregate sarifreport This will replace the default workspace prefix from when creating the aggregate sarif report output. (def. megalinter-reports.sarif). In other words, the artifact uri will be /root/path/of/source/code instead of as of now: DEFAULT_WORKSPACE/root/path/of/source/code. As the DEFAULT_WORKSPACE might only exist during runtime, it needs to be removed. Signed-off-by: Josef Andersson --- megalinter/reporters/SarifReporter.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/megalinter/reporters/SarifReporter.py b/megalinter/reporters/SarifReporter.py index 8eeff22a688..54a418d251b 100644 --- a/megalinter/reporters/SarifReporter.py +++ b/megalinter/reporters/SarifReporter.py @@ -221,11 +221,22 @@ def fix_sarif(self, linter_sarif_obj, linter: Linter): # Update run in full list linter_sarif_obj["runs"][id_run] = run return linter_sarif_obj + + # If DEFAULT_WORKSPACE is set, don't add that to the SARIF-report prefix + def fix_default_workspace_prefix(self, artifactLocation): + default_workspace = config.get("DEFAULT_WORKSPACE", "") + if default_workspace: + if artifactLocation["uri"].startswith(default_workspace): + artifactLocation["uri"] = artifactLocation["uri"].replace(default_workspace,"",1) + return artifactLocation["uri"] # Replace startLine and endLine in region or contextRegion def fix_sarif_physical_location(self, physical_location): + for location_key in physical_location.keys(): location_item = physical_location[location_key] + if "uri" in location_item and location_key == "artifactLocation": + location_item["uri"] = self.fix_default_workspace_prefix(location_item) if "startLine" in location_item and location_item["startLine"] == 0: location_item["startLine"] = 1 if "endLine" in location_item and location_item["endLine"] == 0: