Skip to content

Commit

Permalink
Fix sonarqube index on filename violations (#162)
Browse files Browse the repository at this point in the history
* docs: autogenerated update for docs/snippets/woke.md: `1fe097`

GitHub Action run 1, commit: cognitivegears@1fe097d

* Fix #161 SonarQube output format

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
cognitivegears and github-actions[bot] authored Nov 18, 2021
1 parent ee781d3 commit 8fb42f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/printer/sonarqube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/printer/sonarqube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pkg/printer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8fb42f5

Please sign in to comment.