diff --git a/pkg/printer/sonarqube.go b/pkg/printer/sonarqube.go index 82da7477..e761ee9b 100644 --- a/pkg/printer/sonarqube.go +++ b/pkg/printer/sonarqube.go @@ -78,6 +78,13 @@ func (p *SonarQube) Print(fs *result.FileResults) error { StartColumn: res.GetStartPosition().Column, EndColumn: res.GetEndPosition().Column}}} + // start column and end column are both 1 for file results, all other findings + // should be at least 1 character long + if res.GetStartPosition().Column == 1 && res.GetEndPosition().Column == 1 { + // File / path results should be 0 based for sonarqube, but are 1 based instead + issue.PrimaryLocation.TextRange.StartColumn = 0 + } + var buf bytes.Buffer err := json.NewEncoder(&buf).Encode(issue) if err != nil { diff --git a/pkg/printer/sonarqube_test.go b/pkg/printer/sonarqube_test.go index bdaaa5e5..060c227b 100644 --- a/pkg/printer/sonarqube_test.go +++ b/pkg/printer/sonarqube_test.go @@ -26,6 +26,17 @@ func TestSonarQube_Print(t *testing.T) { assert.Equal(t, expected, got) } +func TestSonarQube_PrintPath(t *testing.T) { + buf := new(bytes.Buffer) + p := NewSonarQube(buf) + res := generateFilePathResult() + assert.NoError(t, p.Print(res)) + got := buf.String() + + expected := `{"engineId":"woke","ruleId":"whitelist","primaryLocation":{"message":"` + "`" + `whitelist` + "`" + ` may be insensitive, use ` + "`" + `allowlist` + "`" + ` instead","filePath":"whitelist.txt","textRange":{"startLine":1,"startColumn":0,"endColumn":1}},"type":"CODE_SMELL","severity":"MINOR"}` + "\n" + assert.Equal(t, expected, got) +} + func TestSonarQube_PrintSuccessExitMessage(t *testing.T) { buf := new(bytes.Buffer) p := NewSonarQube(buf) diff --git a/pkg/printer/util_test.go b/pkg/printer/util_test.go index 9edbde4b..402a968b 100644 --- a/pkg/printer/util_test.go +++ b/pkg/printer/util_test.go @@ -91,6 +91,34 @@ func generateThirdResults(filename string) []result.Result { } } +func generateFilePathResult() *result.FileResults { + r := result.FileResults{Filename: "whitelist.txt"} + r.Results = generatePathResults(r.Filename) + return &r +} + +func generatePathResults(filename string) []result.Result { + return []result.Result{ + result.LineResult{ + Rule: &rule.TestRule, + Finding: "whitelist", + Line: "this whitelist must change", + StartPosition: &token.Position{ + Filename: filename, + Offset: 0, + Line: 1, + Column: 1, + }, + EndPosition: &token.Position{ + Filename: filename, + Offset: 0, + Line: 1, + Column: 1, + }, + }, + } +} + func newPosition(f string, l, c int) *token.Position { return &token.Position{ Filename: f,