Skip to content

Commit da2c524

Browse files
authored
chore: introduce generic test result metadata (#483)
1 parent 68b3b5c commit da2c524

File tree

13 files changed

+155
-176
lines changed

13 files changed

+155
-176
lines changed

internal/presenters/funcs.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ func getSarifTemplateFuncMap() template.FuncMap {
263263
fnMap["buildLocationFromIssue"] = sarif.BuildLocation
264264
fnMap["buildFixesFromIssue"] = sarif.BuildFixesFromIssue
265265
fnMap["formatIssueMessage"] = sarif.FormatIssueMessage
266-
fnMap["getManifestPath"] = getManifestPathFromTestResult
267266
return fnMap
268267
}
269268

@@ -409,22 +408,3 @@ func getFindingTypesFromTestResult(testResults testapi.TestResult) []testapi.Fin
409408

410409
return findingTypesList
411410
}
412-
413-
// getManifestPathFromTestResult extracts the manifest file path from test result
414-
func getManifestPathFromTestResult(testResults testapi.TestResult) string {
415-
// Get the test subject
416-
testSubject := testResults.GetTestSubject()
417-
418-
// Try to extract as DepGraphSubject
419-
depGraph, err := testSubject.AsDepGraphSubject()
420-
if err != nil {
421-
return "package.json" // Default fallback
422-
}
423-
424-
// Get the first path from locator
425-
if len(depGraph.Locator.Paths) > 0 {
426-
return depGraph.Locator.Paths[0]
427-
}
428-
429-
return "package.json" // Default fallback
430-
}

internal/presenters/presenter_ufm_test.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func normalizeAutomationID(run map[string]interface{}) {
5656
if id, ok := automationDetails["id"].(string); ok {
5757
parts := strings.Split(id, "/")
5858
if len(parts) >= 2 {
59-
automationDetails["id"] = strings.Join(parts[:2], "/") + "/*"
59+
automationDetails["id"] = strings.Join(parts[:len(parts)-1], "/") + "/*"
6060
}
6161
}
6262
}
@@ -203,46 +203,10 @@ func normalizeResultURIs(run map[string]interface{}) {
203203
if !ok {
204204
continue
205205
}
206-
normalizeLocationURIs(result)
207206
normalizeFixURIs(result)
208207
}
209208
}
210209

211-
// normalizeLocationURIs normalizes URIs in result locations
212-
func normalizeLocationURIs(result map[string]interface{}) {
213-
locations, ok := result["locations"].([]interface{})
214-
if !ok {
215-
return
216-
}
217-
for _, locInterface := range locations {
218-
loc, ok := locInterface.(map[string]interface{})
219-
if !ok {
220-
continue
221-
}
222-
physLoc, ok := loc["physicalLocation"].(map[string]interface{})
223-
if !ok {
224-
continue
225-
}
226-
artLoc, ok := physLoc["artifactLocation"].(map[string]interface{})
227-
if !ok {
228-
continue
229-
}
230-
uri, ok := artLoc["uri"].(string)
231-
if !ok {
232-
continue
233-
}
234-
// Normalize common manifest filenames
235-
if strings.HasSuffix(uri, "package.json") || strings.HasSuffix(uri, "package-lock.json") || strings.HasSuffix(uri, "pom.xml") {
236-
// Preserve subproject path
237-
if idx := strings.LastIndex(uri, "/"); idx >= 0 {
238-
artLoc["uri"] = uri[:idx+1] + "manifest"
239-
} else {
240-
artLoc["uri"] = "manifest"
241-
}
242-
}
243-
}
244-
}
245-
246210
// normalizeFixURIs normalizes URIs in fixes
247211
func normalizeFixURIs(result map[string]interface{}) {
248212
fixes, ok := result["fixes"].([]interface{})

internal/presenters/templates/ufm.sarif.tmpl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@
4848
}
4949
},
5050
"results": [
51-
{{- $manifestPath := getManifestPath $result }}
51+
{{- $targetFile := index $result.GetMetadata "display-target-file" }}
5252
{{- range $index, $issue := $issues }}
53-
{{- $location := buildLocationFromIssue $issue }}
53+
{{- $location := buildLocationFromIssue $issue $targetFile }}
54+
{{- $physicalLoc := index $location "physicalLocation" }}
55+
{{- $region := index $physicalLoc "region" }}
56+
{{- $artifactLocation := index $physicalLoc "artifactLocation" }}
5457
{{- $fixes := buildFixesFromIssue $issue }}
5558
{{- $ignoreDetails := $issue.GetIgnoreDetails }}
5659
{
@@ -61,13 +64,12 @@
6164
},
6265
"locations": [
6366
{{- if $location }}
64-
{{- $physicalLoc := index $location "physicalLocation" }}
65-
{{- $region := index $physicalLoc "region" }}
67+
6668
{{- $logicalLocs := index $location "logicalLocations" }}
6769
{
6870
"physicalLocation": {
6971
"artifactLocation": {
70-
"uri": {{ getQuotedString $manifestPath }}
72+
"uri": {{ getQuotedString (index $artifactLocation "uri") }}
7173
},
7274
"region": {
7375
"startLine": {{ index $region "startLine" }}
@@ -97,7 +99,7 @@
9799
{{- range $changeIndex, $change := $artifactChanges }}
98100
{
99101
"artifactLocation": {
100-
"uri": {{ getQuotedString $manifestPath }}
102+
"uri": {{ getQuotedString (index $artifactLocation "uri") }}
101103
},
102104
"replacements": [
103105
{{- $replacements := index $change "replacements" }}
@@ -198,7 +200,8 @@
198200
},
199201
*/ -}}
200202
"automationDetails": {
201-
"id": "{{- getAutomationDetailsId (getValueFromConfig "project-name") (printf "%s" $findingType) }}"
203+
{{- $projectName := index $result.GetMetadata "project-name" }}
204+
"id": "{{- getAutomationDetailsId $projectName (printf "%s" $findingType) }}"
202205
}
203206
}{{if or (lt $findingTypeIndex $findingTypesSize) (lt $resultIndex (sub (len $.TestResults) 1))}},{{end}}{{- end}}
204207
{{- end}}

internal/presenters/testdata/ufm/multi_project.testresult.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
},
1010
"timeout": { "outcome": "fail", "seconds": 1200 }
1111
},
12+
"metadata": {
13+
"project-name": "tpwe",
14+
"display-target-file": "dotnet/obj/project.assets.json",
15+
"target-directory": "multi-project"
16+
},
1217
"createdAt": "2025-11-13T15:29:14.585711Z",
1318
"testSubject": {
1419
"locator": {
@@ -816,6 +821,11 @@
816821
},
817822
"timeout": { "outcome": "fail", "seconds": 1200 }
818823
},
824+
"metadata": {
825+
"project-name": "package.json",
826+
"display-target-file": "ghost/package.json",
827+
"target-directory": "multi-project"
828+
},
819829
"createdAt": "2025-11-13T15:29:14.491151Z",
820830
"testSubject": {
821831
"locator": { "paths": ["ghost/package-lock.json"], "type": "local_path" },
@@ -4375,6 +4385,11 @@
43754385
},
43764386
"timeout": { "outcome": "fail", "seconds": 1200 }
43774387
},
4388+
"metadata": {
4389+
"project-name": "golangproject",
4390+
"display-target-file": "golang/go.mod",
4391+
"target-directory": "multi-project"
4392+
},
43784393
"createdAt": "2025-11-13T15:29:14.506347Z",
43794394
"testSubject": {
43804395
"locator": { "paths": ["golang/go.mod"], "type": "local_path" },
@@ -4525,6 +4540,11 @@
45254540
},
45264541
"timeout": { "outcome": "fail", "seconds": 1200 }
45274542
},
4543+
"metadata": {
4544+
"project-name": "demo:maven-demo",
4545+
"display-target-file": "maven/pom.xml",
4546+
"target-directory": "multi-project"
4547+
},
45284548
"createdAt": "2025-11-13T15:29:14.480432Z",
45294549
"testSubject": {
45304550
"locator": { "paths": ["maven/pom.xml"], "type": "local_path" },
@@ -4572,6 +4592,11 @@
45724592
},
45734593
"timeout": { "outcome": "fail", "seconds": 1200 }
45744594
},
4595+
"metadata": {
4596+
"project-name": "not-python",
4597+
"display-target-file": "not-python/requirements.txt",
4598+
"target-directory": "multi-project"
4599+
},
45754600
"createdAt": "2025-11-13T15:29:14.440374Z",
45764601
"testSubject": {
45774602
"locator": {
@@ -6385,6 +6410,11 @@
63856410
},
63866411
"timeout": { "outcome": "fail", "seconds": 1200 }
63876412
},
6413+
"metadata": {
6414+
"project-name": "python",
6415+
"display-target-file": "python/requirements.txt",
6416+
"target-directory": "multi-project"
6417+
},
63886418
"createdAt": "2025-11-13T15:29:18.009229Z",
63896419
"testSubject": {
63906420
"locator": { "paths": ["python/requirements.txt"], "type": "local_path" },
@@ -8195,6 +8225,11 @@
81958225
},
81968226
"timeout": { "outcome": "fail", "seconds": 1200 }
81978227
},
8228+
"metadata": {
8229+
"project-name": "tsc",
8230+
"display-target-file": "tsc/package.json",
8231+
"target-directory": "multi-project"
8232+
},
81988233
"createdAt": "2025-11-13T15:29:19.594887Z",
81998234
"testSubject": {
82008235
"locator": { "paths": ["tsc/package-lock.json"], "type": "local_path" },

0 commit comments

Comments
 (0)