From 377ca9ed6e5bf0973154c7ff7b402f65521622ce Mon Sep 17 00:00:00 2001 From: yuriy-tereshchuk-sap Date: Fri, 29 Aug 2025 13:15:39 +0200 Subject: [PATCH] fix(sonar): Fix null value for errors entry in sonarscan report (#5459) Co-authored-by: Yuriy.Tereshchuk Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> --- cmd/sonarExecuteScan.go | 2 +- cmd/sonarExecuteScan_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/sonarExecuteScan.go b/cmd/sonarExecuteScan.go index 307f4e09b..1eaedbb9f 100644 --- a/cmd/sonarExecuteScan.go +++ b/cmd/sonarExecuteScan.go @@ -252,7 +252,7 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne } // fetch number of issues by severity issueService := SonarUtils.NewIssuesService(serverUrl, config.Token, taskReport.ProjectKey, config.Organization, config.BranchName, config.ChangeID, apiClient) - var categories []SonarUtils.Severity + var categories = make([]SonarUtils.Severity, 0) influx.sonarqube_data.fields.blocker_issues, err = issueService.GetNumberOfBlockerIssues(&categories) if err != nil { return err diff --git a/cmd/sonarExecuteScan_test.go b/cmd/sonarExecuteScan_test.go index 982c187a5..8327b6134 100644 --- a/cmd/sonarExecuteScan_test.go +++ b/cmd/sonarExecuteScan_test.go @@ -4,6 +4,7 @@ package cmd import ( + "encoding/json" "fmt" "net/http" "os" @@ -180,8 +181,15 @@ func TestRunSonar(t *testing.T) { defer os.Setenv("SONAR_SCANNER_OPTS", "") // test err := runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &mock.FilesMock{}, &sonarExecuteScanInflux{}) - // assert assert.NoError(t, err) + // load sonarscan report file + reportFile, err := os.ReadFile(filepath.Join(tmpFolder, "sonarscan.json")) + assert.NoError(t, err) + var reportData SonarUtils.ReportData + err = json.Unmarshal(reportFile, &reportData) + assert.NoError(t, err) + // assert + assert.NotNil(t, reportData.Errors) assert.Contains(t, sonar.options, "-Dsonar.projectVersion=1") assert.Contains(t, sonar.options, "-Dsonar.organization=SAP") assert.Contains(t, sonar.environment, "SONAR_HOST_URL="+sonarServerURL)