1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-11-06 09:09:19 +02:00

Add Coverage Info to sonarscan.json (#3262)

* Add coverage metrics to report + influx

* Write unit tests

* Add integration test for Sonar Measures Component Service
This commit is contained in:
Marc Bormeth
2021-12-08 09:02:12 +01:00
committed by GitHub
parent d0762f5db9
commit b213af1089
9 changed files with 296 additions and 9 deletions

View File

@@ -231,6 +231,16 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
if err != nil {
return err
}
componentService := SonarUtils.NewMeasuresComponentService(taskReport.ServerURL, config.Token, taskReport.ProjectKey, config.Organization, apiClient)
cov, err := componentService.GetCoverage()
if err != nil {
return err // No wrap, description already added one level below
}
influx.sonarqube_data.fields.coverage = cov.Coverage
influx.sonarqube_data.fields.branch_coverage = cov.BranchCoverage
influx.sonarqube_data.fields.line_coverage = cov.LineCoverage
log.Entry().Debugf("Influx values: %v", influx.sonarqube_data.fields)
err = SonarUtils.WriteReport(SonarUtils.ReportData{
ServerURL: taskReport.ServerURL,
@@ -246,6 +256,7 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
Minor: influx.sonarqube_data.fields.minor_issues,
Info: influx.sonarqube_data.fields.info_issues,
},
Coverage: *cov,
}, sonar.workingDir, ioutil.WriteFile)
if err != nil {
return err

View File

@@ -62,6 +62,9 @@ type sonarExecuteScanInflux struct {
major_issues int
minor_issues int
info_issues int
coverage float32
branch_coverage float32
line_coverage float32
}
tags struct {
}
@@ -81,6 +84,9 @@ func (i *sonarExecuteScanInflux) persist(path, resourceName string) {
{valType: config.InfluxField, measurement: "sonarqube_data", name: "major_issues", value: i.sonarqube_data.fields.major_issues},
{valType: config.InfluxField, measurement: "sonarqube_data", name: "minor_issues", value: i.sonarqube_data.fields.minor_issues},
{valType: config.InfluxField, measurement: "sonarqube_data", name: "info_issues", value: i.sonarqube_data.fields.info_issues},
{valType: config.InfluxField, measurement: "sonarqube_data", name: "coverage", value: i.sonarqube_data.fields.coverage},
{valType: config.InfluxField, measurement: "sonarqube_data", name: "branch_coverage", value: i.sonarqube_data.fields.branch_coverage},
{valType: config.InfluxField, measurement: "sonarqube_data", name: "line_coverage", value: i.sonarqube_data.fields.line_coverage},
}
errCount := 0
@@ -526,7 +532,7 @@ func sonarExecuteScanMetadata() config.StepData {
Type: "influx",
Parameters: []map[string]interface{}{
{"Name": "step_data"}, {"fields": []map[string]string{{"name": "sonar"}}},
{"Name": "sonarqube_data"}, {"fields": []map[string]string{{"name": "blocker_issues"}, {"name": "critical_issues"}, {"name": "major_issues"}, {"name": "minor_issues"}, {"name": "info_issues"}}},
{"Name": "sonarqube_data"}, {"fields": []map[string]string{{"name": "blocker_issues"}, {"name": "critical_issues"}, {"name": "major_issues"}, {"name": "minor_issues"}, {"name": "info_issues"}, {"name": "coverage"}, {"name": "branch_coverage"}, {"name": "line_coverage"}}},
},
},
},

View File

@@ -108,6 +108,38 @@ ceTaskId=AXERR2JBbm9IiM5TEST
ceTaskUrl=` + sonarServerURL + `/api/ce/task?id=AXERR2JBbm9IiMTEST
`
const measuresComponentResponse = `
{
"component": {
"key": "com.sap.piper.test",
"name": "com.sap.piper.test",
"qualifier": "TRK",
"measures": [
{
"metric": "line_coverage",
"value": "80.4",
"bestValue": false
},
{
"metric": "branch_coverage",
"value": "81.0",
"bestValue": false
},
{
"metric": "coverage",
"value": "80.7",
"bestValue": false
},
{
"metric": "extra_valie",
"value": "42.7",
"bestValue": false
}
]
}
}
`
func TestRunSonar(t *testing.T) {
mockRunner := mock.ExecMockRunner{}
mockDownloadClient := mockDownloader{shouldFail: false}
@@ -119,6 +151,7 @@ func TestRunSonar(t *testing.T) {
// add response handler
httpmock.RegisterResponder(http.MethodGet, sonarServerURL+"/api/"+SonarUtils.EndpointCeTask+"", httpmock.NewStringResponder(http.StatusOK, `{ "task": { "componentId": "AXERR2JBbm9IiM5TEST", "status": "SUCCESS" }}`))
httpmock.RegisterResponder(http.MethodGet, sonarServerURL+"/api/"+SonarUtils.EndpointIssuesSearch+"", httpmock.NewStringResponder(http.StatusOK, `{ "total": 0 }`))
httpmock.RegisterResponder(http.MethodGet, sonarServerURL+"/api/"+SonarUtils.EndpointMeasuresComponent+"", httpmock.NewStringResponder(http.StatusOK, measuresComponentResponse))
t.Run("default", func(t *testing.T) {
// init