Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sonarqube:chore - merge entities/sonarqube with services/sonarqube pkg #947

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
}
62 changes: 30 additions & 32 deletions internal/services/sonarqube/sonarqube.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,39 @@ 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
vuln := sq.analysis.AnalysisVulnerabilities[index].Vulnerability

issue := sq.formatReportStruct(&vulnerability)
issue := sq.formatReportStruct(&vuln)

report.Issues = append(report.Issues, *issue)
}

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",
}
}