mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-30 05:59:39 +02:00
feat(sonar): create report for issue count (#2691)
* create json report * archive report * add test case * generate report * correct report name
This commit is contained in:
parent
f9c0092f2e
commit
0fcbfa8da7
@ -222,6 +222,24 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
|
||||
return err
|
||||
}
|
||||
log.Entry().Debugf("Influx values: %v", influx.sonarqube_data.fields)
|
||||
err = SonarUtils.WriteReport(SonarUtils.ReportData{
|
||||
ServerURL: taskReport.ServerURL,
|
||||
ProjectKey: taskReport.ProjectKey,
|
||||
TaskID: taskReport.TaskID,
|
||||
ChangeID: config.ChangeID,
|
||||
BranchName: config.BranchName,
|
||||
Organization: config.Organization,
|
||||
NumberOfIssues: SonarUtils.Issues{
|
||||
Blocker: influx.sonarqube_data.fields.blocker_issues,
|
||||
Critical: influx.sonarqube_data.fields.critical_issues,
|
||||
Major: influx.sonarqube_data.fields.major_issues,
|
||||
Minor: influx.sonarqube_data.fields.minor_issues,
|
||||
Info: influx.sonarqube_data.fields.info_issues,
|
||||
},
|
||||
}, sonar.workingDir, ioutil.WriteFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
38
pkg/sonar/report.go
Normal file
38
pkg/sonar/report.go
Normal file
@ -0,0 +1,38 @@
|
||||
package sonar
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const reportFileName = "sonarscan.json"
|
||||
|
||||
//ReportData is representing the data of the step report JSON
|
||||
type ReportData struct {
|
||||
ServerURL string `json:"serverUrl"`
|
||||
ProjectKey string `json:"projectKey"`
|
||||
TaskID string `json:"taskId"`
|
||||
ChangeID string `json:"changeID,omitempty"`
|
||||
BranchName string `json:"branchName,omitempty"`
|
||||
Organization string `json:"organization,omitempty"`
|
||||
NumberOfIssues Issues `json:"numberOfIssues"`
|
||||
}
|
||||
|
||||
// Issues ...
|
||||
type Issues struct {
|
||||
Blocker int `json:"blocker"`
|
||||
Critical int `json:"critical"`
|
||||
Major int `json:"major"`
|
||||
Minor int `json:"minor"`
|
||||
Info int `json:"info"`
|
||||
}
|
||||
|
||||
// WriteReport ...
|
||||
func WriteReport(data ReportData, reportPath string, writeToFile func(f string, d []byte, p os.FileMode) error) error {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeToFile(filepath.Join(reportPath, reportFileName), jsonData, 0644)
|
||||
}
|
40
pkg/sonar/report_test.go
Normal file
40
pkg/sonar/report_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package sonar
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
var fileContent string
|
||||
var fileName string
|
||||
|
||||
func writeToFileMock(f string, d []byte, p os.FileMode) error {
|
||||
fileContent = string(d)
|
||||
fileName = f
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestWriteReport(t *testing.T) {
|
||||
// init
|
||||
const expected = `{"serverUrl":"https://sonarcloud.io","projectKey":"Piper-Validation/Golang","taskId":"mock.Anything","numberOfIssues":{"blocker":0,"critical":1,"major":2,"minor":3,"info":4}}`
|
||||
testData := ReportData{
|
||||
ServerURL: "https://sonarcloud.io",
|
||||
ProjectKey: "Piper-Validation/Golang",
|
||||
TaskID: mock.Anything,
|
||||
NumberOfIssues: Issues{
|
||||
Critical: 1,
|
||||
Major: 2,
|
||||
Minor: 3,
|
||||
Info: 4,
|
||||
},
|
||||
}
|
||||
// test
|
||||
err := WriteReport(testData, "", writeToFileMock)
|
||||
// assert
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, fileContent)
|
||||
assert.Equal(t, reportFileName, fileName)
|
||||
}
|
@ -68,6 +68,7 @@ void call(Map parameters = [:]) {
|
||||
influxWrapper(script){
|
||||
piperExecuteBin.credentialWrapper(config, credentialInfo){
|
||||
sh "${piperGoPath} ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
||||
archiveArtifacts artifacts: "sonarscan.json", allowEmptyArchive: true
|
||||
}
|
||||
jenkinsUtils.handleStepResults(STEP_NAME, false, false)
|
||||
script.commonPipelineEnvironment.readFromDisk(script)
|
||||
|
Loading…
x
Reference in New Issue
Block a user