Skip to content

Commit

Permalink
sonarqube:chore - merge entities/sonarqube with services/sonarqube pkg
Browse files Browse the repository at this point in the history
This commit remove the redundant package `entities/sonarqube` and move
the schema declaration to `services/sonarqube` package, so all sonarqube
declaration and implementation is declared on the same package.

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Jan 24, 2022
1 parent 5d8b435 commit e1d0cf4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 94 deletions.
3 changes: 1 addition & 2 deletions internal/controllers/printresults/print_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"

"github.com/ZupIT/horusec/config"
sq "github.com/ZupIT/horusec/internal/entities/sonarqube"
"github.com/ZupIT/horusec/internal/enums/outputtype"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/sonarqube"
Expand All @@ -40,7 +39,7 @@ import (
var ErrOutputJSON = errors.New("{HORUSEC_CLI} error creating and/or writing to the specified file")

type SonarQubeConverter interface {
ConvertVulnerabilityToSonarQube() sq.Report
ConvertVulnerabilityToSonarQube() sonarqube.Report
}

type analysisOutputJSON struct {
Expand Down
21 changes: 0 additions & 21 deletions internal/entities/sonarqube/location.go

This file was deleted.

19 changes: 0 additions & 19 deletions internal/entities/sonarqube/report.go

This file was deleted.

22 changes: 0 additions & 22 deletions internal/entities/sonarqube/text_range.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package sonarqube

type Report struct {
Issues []Issue `json:"issues"`
}

type Issue struct {
Type string `json:"type"`
RuleID string `json:"ruleId"`
Expand All @@ -23,3 +27,16 @@ type Issue struct {
PrimaryLocation Location `json:"primaryLocation"`
SecondaryLocations []Location `json:"secondaryLocations,omitempty"`
}

type Location struct {
Message string `json:"message"`
Filepath string `json:"filePath"`
Range TextRange `json:"textRange"`
}

type TextRange struct {
StartLine int `json:"startLine"`
EndLine int `json:"endLine,omitempty"`
StartColumn int `json:"startColumn,omitempty"`
EndColumn int `json:"endColumn,omitempty"`
}
58 changes: 28 additions & 30 deletions internal/services/sonarqube/sonarqube.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@ package sonarqube
import (
"strconv"

horusecEntities "github.com/ZupIT/horusec-devkit/pkg/entities/analysis"
vulnEntity "github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
horusecSeverity "github.com/ZupIT/horusec-devkit/pkg/enums/severities"

"github.com/ZupIT/horusec/internal/entities/sonarqube"
"github.com/ZupIT/horusec-devkit/pkg/entities/analysis"
"github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
)

type SonarQube struct {
analysis *horusecEntities.Analysis
analysis *analysis.Analysis
}

func NewSonarQube(analysis *horusecEntities.Analysis) *SonarQube {
func NewSonarQube(analysiss *analysis.Analysis) *SonarQube {
return &SonarQube{
analysis: analysis,
analysis: analysiss,
}
}

func (sq *SonarQube) ConvertVulnerabilityToSonarQube() (report sonarqube.Report) {
report.Issues = []sonarqube.Issue{}
func (sq *SonarQube) ConvertVulnerabilityToSonarQube() (report Report) {
report.Issues = []Issue{}
for index := range sq.analysis.AnalysisVulnerabilities {
vulnerability := sq.analysis.AnalysisVulnerabilities[index].Vulnerability

Expand All @@ -47,11 +45,11 @@ func (sq *SonarQube) ConvertVulnerabilityToSonarQube() (report sonarqube.Report)
return report
}

func (sq *SonarQube) formatReportStruct(vulnerability *vulnEntity.Vulnerability) (issue *sonarqube.Issue) {
issue = sq.newIssue(vulnerability)
func (sq *SonarQube) formatReportStruct(vuln *vulnerability.Vulnerability) (issue *Issue) {
issue = sq.newIssue(vuln)

convertedVulnerabilityLine, _ := strconv.Atoi(vulnerability.Line)
convertedVulnerabilityColumn, _ := strconv.Atoi(vulnerability.Column)
convertedVulnerabilityLine, _ := strconv.Atoi(vuln.Line)
convertedVulnerabilityColumn, _ := strconv.Atoi(vuln.Column)

issue.PrimaryLocation.Range.StartLine = sq.shouldBeGreatherThanZero(convertedVulnerabilityLine)
issue.PrimaryLocation.Range.StartColumn = sq.shouldBeGreatherThanZero(convertedVulnerabilityColumn)
Expand All @@ -66,30 +64,30 @@ func (sq *SonarQube) shouldBeGreatherThanZero(v int) int {
return 1
}

func (sq *SonarQube) newIssue(vulnerability *vulnEntity.Vulnerability) *sonarqube.Issue {
return &sonarqube.Issue{
func (sq *SonarQube) newIssue(vuln *vulnerability.Vulnerability) *Issue {
return &Issue{
EngineID: "horusec",
Type: "VULNERABILITY",
Severity: sq.convertHorusecSeverityToSonarQube(vulnerability.Severity),
RuleID: vulnerability.SecurityTool.ToString(),
PrimaryLocation: sonarqube.Location{
Message: vulnerability.Details,
Filepath: vulnerability.File,
Severity: sq.convertHorusecSeverityToSonarQube(vuln.Severity),
RuleID: vuln.SecurityTool.ToString(),
PrimaryLocation: Location{
Message: vuln.Details,
Filepath: vuln.File,
},
}
}

func (sq *SonarQube) convertHorusecSeverityToSonarQube(severity horusecSeverity.Severity) string {
func (sq *SonarQube) convertHorusecSeverityToSonarQube(severity severities.Severity) string {
return sq.getSonarQubeSeverityMap()[severity]
}

func (sq *SonarQube) getSonarQubeSeverityMap() map[horusecSeverity.Severity]string {
return map[horusecSeverity.Severity]string{
horusecSeverity.Critical: "BLOCKER",
horusecSeverity.High: "CRITICAL",
horusecSeverity.Medium: "MAJOR",
horusecSeverity.Low: "MINOR",
horusecSeverity.Unknown: "INFO",
horusecSeverity.Info: "INFO",
func (sq *SonarQube) getSonarQubeSeverityMap() map[severities.Severity]string {
return map[severities.Severity]string{
severities.Critical: "BLOCKER",
severities.High: "CRITICAL",
severities.Medium: "MAJOR",
severities.Low: "MINOR",
severities.Unknown: "INFO",
severities.Info: "INFO",
}
}

0 comments on commit e1d0cf4

Please sign in to comment.