Skip to content

Commit

Permalink
🐛 Fix panic on reading final results (QD-8925)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed May 2, 2024
1 parent 0c5e6aa commit 774bc8c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
23 changes: 15 additions & 8 deletions platform/ext_bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,29 @@ func buildReport(toolName string, annotations []bbapi.ReportAnnotation, cloudUrl
}

// buildAnnotation builds an annotation to be sent to BitBucket Code Insights
func buildAnnotation(result *sarif.Result, ruleDescription string, reportLink string) bbapi.ReportAnnotation {
bbSeverity, ok := toBitBucketSeverity[getSeverity(result)]
func buildAnnotation(r *sarif.Result, ruleDescription string, reportLink string) bbapi.ReportAnnotation {
bbSeverity, ok := toBitBucketSeverity[getSeverity(r)]
if !ok {
log.Debugf("Unknown SARIF severity: %s", getSeverity(result))
log.Debugf("Unknown SARIF severity: %s", getSeverity(r))
bbSeverity = bitBucketLow
}
location := result.Locations[0].PhysicalLocation

data := bbapi.NewReportAnnotation()
data.SetExternalId(getFingerprint(result))
data.SetExternalId(getFingerprint(r))
data.SetAnnotationType(bitBucketAnnotationType)
data.SetSummary(fmt.Sprintf("%s: %s", result.RuleId, result.Message.Text))
data.SetSummary(fmt.Sprintf("%s: %s", r.RuleId, r.Message.Text))
data.SetDetails(ruleDescription)
data.SetSeverity(bbSeverity)
data.SetLine(int32(location.Region.StartLine))
data.SetPath(location.ArtifactLocation.Uri)

if r != nil && r.Locations != nil && len(r.Locations) > 0 && r.Locations[0].PhysicalLocation != nil {
location := r.Locations[0].PhysicalLocation
if location.Region != nil {
data.SetLine(int32(location.Region.StartLine))
}
if location.ArtifactLocation != nil {
data.SetPath(location.ArtifactLocation.Uri)
}
}
data.SetLink(reportLink)
return *data
}
Expand Down
19 changes: 13 additions & 6 deletions platform/ext_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,24 @@ type Line struct {

// sarifResultToCodeClimate converts a SARIF result to a Code Climate issue.
func sarifResultToCodeClimate(r *sarif.Result) CCIssue {
loc := Location{
Path: "",
Lines: Line{
Begin: 0,
},
}

if r != nil && r.Locations != nil && len(r.Locations) > 0 {
loc.Path = r.Locations[0].PhysicalLocation.ArtifactLocation.Uri
loc.Lines.Begin = int(r.Locations[0].PhysicalLocation.Region.StartLine)
}

return CCIssue{
CheckName: r.RuleId,
Description: r.Message.Text,
Fingerprint: getFingerprint(r),
Severity: toCodeClimateSeverity[getSeverity(r)],
Location: Location{
Path: r.Locations[0].PhysicalLocation.ArtifactLocation.Uri,
Lines: Line{
Begin: int(r.Locations[0].PhysicalLocation.Region.StartLine),
},
},
Location: loc,
}
}

Expand Down
26 changes: 9 additions & 17 deletions platform/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,7 @@ const sarifFileData = `
"partialFingerprints": {
"equalIndicator/v2": "2faa123efwsfsdqwer144d723b5999101424efba41c6caf11e6da4c2d7622ae04"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "src/main/java/AppStarter.java"
},
"region": {
"startLine": 3
}
}
}
]
"locations": []
}
]
}
Expand Down Expand Up @@ -202,8 +191,8 @@ const expectedAnnotationsAsJSON = `[
"annotation_type": "CODE_SMELL",
"details": "This is a long boring description",
"external_id": "2faa123efwsfsdqwer144d723b5999101424efba41c6caf11e6da4c2d7622ae04",
"line": 3,
"path": "src/main/java/AppStarter.java",
"line": 0,
"path": "",
"severity": "LOW",
"summary": "MissingLevel: This result does not specify a level."
}
Expand All @@ -220,6 +209,9 @@ func TestSarifResultToBitBucketAnnotation(t *testing.T) {
}
expectedAnnotations := getExpectedAnnotations(t)
for i, annotation := range annotations { // doing comparison like this because only some fields are interesting
if (bbapi.ReportAnnotation{} == annotation) {
continue
}
if *annotation.Details != *expectedAnnotations[i].Details {
t.Fatalf("Mismatch in Details at index %d: got %s, want %s\n", i, *annotation.Details, *expectedAnnotations[i].Details)
}
Expand All @@ -232,10 +224,10 @@ func TestSarifResultToBitBucketAnnotation(t *testing.T) {
if *annotation.ExternalId != *expectedAnnotations[i].ExternalId {
t.Fatalf("Mismatch in ExternalId at index %d: got %s, want %s\n", i, *annotation.ExternalId, *expectedAnnotations[i].ExternalId)
}
if *annotation.Line != *expectedAnnotations[i].Line {
if annotation.Line != nil && *annotation.Line != *expectedAnnotations[i].Line {
t.Fatalf("Mismatch in Line at index %d: got %d, want %d\n", i, *annotation.Line, *expectedAnnotations[i].Line)
}
if *annotation.Path != *expectedAnnotations[i].Path {
if annotation.Path != nil && *annotation.Path != *expectedAnnotations[i].Path {
t.Fatalf("Mismatch in Path at index %d: got %s, want %s\n", i, *annotation.Path, *expectedAnnotations[i].Path)
}
if *annotation.Severity != *expectedAnnotations[i].Severity {
Expand Down Expand Up @@ -286,7 +278,7 @@ func TestSarifResultToCodeClimate(t *testing.T) {
CheckName: "MissingLevel",
Description: "This result does not specify a level.",
Fingerprint: "2faa123efwsfsdqwer144d723b5999101424efba41c6caf11e6da4c2d7622ae04",
Location: Location{Path: "src/main/java/AppStarter.java", Lines: Line{Begin: 3}},
Location: Location{Path: "", Lines: Line{Begin: 0}},
},
}

Expand Down

0 comments on commit 774bc8c

Please sign in to comment.