Skip to content

Commit 807b72c

Browse files
authored
Reinstate sonar tests and improve error messages (#545)
* Improve error message for sonar command
1 parent 4300ab1 commit 807b72c

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

cmd/kosli/attestSonar_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,19 @@ func (suite *AttestSonarCommandTestSuite) TestAttestSonarCmd() {
159159
wantError: true,
160160
name: "if outdated task given (i.e. we try to get results for an older scan that SonarCloud has deleted), we get an error",
161161
cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-working-dir testdata/sonar/sonarcloud/.scannerwork-old %s", suite.defaultKosliArguments),
162-
golden: "Error: analysis not found on https://sonarcloud.io. Snapshot may have been deleted by SonarQube\n",
162+
golden: "Error: No activity found for task 'AZERk4uWpzGpahwkB9ac' on https://sonarcloud.io. \nSonarQube may be experiencing problems, please check https://status.sonarqube.com/ and try again later. \nOtherwise if you are attesting an older scan, the snapshot may have been deleted by SonarQube.\n",
163163
},
164164
{
165165
wantError: true,
166166
name: "if incorrect revision given (or the scan for the given revision has been deleted by SonarCloud)",
167167
cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-project-key cyber-dojo_differ --sonar-revision b4d1053f2aac18c9fb4b9a289a8289199c932e12 %s", suite.defaultKosliArguments),
168-
golden: "Error: analysis for revision b4d1053f2aac18c9fb4b9a289a8289199c932e12 of project cyber-dojo_differ not found. Check the revision is correct. Snapshot may also have been deleted by SonarQube\n",
168+
golden: "Error: analysis for revision b4d1053f2aac18c9fb4b9a289a8289199c932e12 of project cyber-dojo_differ not found. Check the revision is correct. \nThe scan may still be being processed by SonarQube, try again later.\n Otherwise if you are attesting an older scan, the snapshot may also have been deleted by SonarQube\n",
169169
},
170170
{
171171
wantError: true,
172172
name: "if incorrect project key given, we get an error message from Sonar",
173173
cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-project-key cyber-dojo-differ --sonar-revision 38f3dc8b63abb632ac94a12b3f818b49f8047fa1 %s", suite.defaultKosliArguments),
174-
golden: "Error: sonar error: Component key 'cyber-dojo-differ' not found\n",
174+
golden: "Error: SonarQube error: Component key 'cyber-dojo-differ' not found\n",
175175
},
176176
}
177177

@@ -261,7 +261,7 @@ func (suite *AttestSonarQubeCommandTestSuite) TestAttestSonarQubeCmd() {
261261
wantError: true,
262262
name: "if incorrect project key given, we get an error message from Sonar",
263263
cmd: fmt.Sprintf("attest sonar --name cli.foo --commit HEAD --origin-url http://www.example.com --sonar-server-url http://localhost:9000 --sonar-project-key test99 --sonar-revision 38f3dc8b63abb632ac94a12b3f818b49f8047fa1 %s", suite.defaultKosliArguments),
264-
golden: "Error: sonar error: Component key 'test99' not found\n",
264+
golden: "Error: SonarQube error: Component key 'test99' not found\n",
265265
},
266266
{
267267
wantError: true,
@@ -282,6 +282,6 @@ func (suite *AttestSonarQubeCommandTestSuite) TestAttestSonarQubeCmd() {
282282
// In order for 'go test' to run this suite, we need to create
283283
// a normal test function and pass our suite to suite.Run
284284
func TestAttestSonarCommandTestSuite(t *testing.T) {
285-
//suite.Run(t, new(AttestSonarCommandTestSuite)) // Commented out since they currently fail Tore Hagen 2025.09.30
285+
suite.Run(t, new(AttestSonarCommandTestSuite))
286286
suite.Run(t, new(AttestSonarQubeCommandTestSuite))
287287
}

internal/sonar/sonar.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type Conditions struct {
8080

8181
// These are the structs for the response from the ceTaskURL
8282
type TaskResponse struct {
83-
Task Task `json:"task"`
83+
Task Task `json:"task"`
84+
Errors []Error `json:"errors,omitempty"`
8485
}
8586
type Task struct {
8687
TaskID string `json:"id"`
@@ -232,14 +233,13 @@ func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *Sona
232233
if err != nil {
233234
return "", err
234235
}
235-
236236
err = json.NewDecoder(taskResponse.Body).Decode(taskResponseData)
237237
if err != nil {
238238
return "", fmt.Errorf("please check your API token is correct and you have the correct permissions in SonarQube")
239239
}
240-
// If the CETaskURL from the report-task.txt file gives a 404, the CE task does not exist.
241-
if taskResponseData.Task.Status == "" {
242-
return "", fmt.Errorf("analysis not found on %s. Snapshot may have been deleted by SonarQube", sonarResults.ServerUrl)
240+
// If the CETaskURL from the report-task.txt file gives a 404, the CE task does not exist, or SonarQube is down.
241+
if taskResponseData.Errors != nil {
242+
return "", fmt.Errorf("%s on %s. \nSonarQube may be experiencing problems, please check https://status.sonarqube.com/ and try again later. \nOtherwise if you are attesting an older scan, the snapshot may have been deleted by SonarQube.", taskResponseData.Errors[0].Msg, sonarResults.ServerUrl)
243243
}
244244

245245
if taskResponseData.Task.Status == "PENDING" || taskResponseData.Task.Status == "IN_PROGRESS" {
@@ -281,7 +281,7 @@ func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *Sona
281281
// This should only happen if the task is pending - either because the project is large and the scan takes a long time
282282
// to process, or because SonarQube is experiencing delays for some reason.
283283
if analysisId == "" {
284-
return "", fmt.Errorf("analysis ID not found on %s. The scan results are not yet available, likely due to: \n1. Your project being particularly large and the scan taking time to process, or \n2. SonarQube is experiencing delays in processing scans. \nTry rerunning the command with the --max-wait flag.", sonarResults.ServerUrl)
284+
return "", fmt.Errorf("analysis ID not found on %s. The scan results are not yet available, likely due to: \n1. Your project being particularly large and the scan taking time to process, or \n2. SonarQube experiencing delays in processing scans. \nTry rerunning the command with the --max-wait flag.", sonarResults.ServerUrl)
285285
}
286286

287287
if project.Url == "" {
@@ -329,11 +329,11 @@ func GetProjectAnalysisFromRevision(httpClient *http.Client, sonarResults *Sonar
329329
}
330330

331331
if projectAnalysesData.Errors != nil {
332-
return "", fmt.Errorf("sonar error: %s", projectAnalysesData.Errors[0].Msg)
332+
return "", fmt.Errorf("SonarQube error: %s", projectAnalysesData.Errors[0].Msg)
333333
}
334334

335335
if sonarResults.AnalaysedAt == "" {
336-
return "", fmt.Errorf("analysis for revision %s of project %s not found. Check the revision is correct. Snapshot may also have been deleted by SonarQube", revision, project.Key)
336+
return "", fmt.Errorf("analysis for revision %s of project %s not found. Check the revision is correct. \nThe scan may still be being processed by SonarQube, try again later.\n Otherwise if you are attesting an older scan, the snapshot may also have been deleted by SonarQube", revision, project.Key)
337337
}
338338
projectAnalysesResponse.Body.Close()
339339

@@ -367,6 +367,10 @@ func GetProjectAnalysisFromAnalysisID(httpClient *http.Client, sonarResults *Son
367367
}
368368
}
369369

370+
if projectAnalysesData.Errors != nil {
371+
return fmt.Errorf("SonarQube error: %s", projectAnalysesData.Errors[0].Msg)
372+
}
373+
370374
if sonarResults.AnalaysedAt == "" {
371375
return fmt.Errorf("analysis with ID %s not found on %s. Snapshot may have been deleted by SonarQube", analysisID, sonarResults.ServerUrl)
372376
}
@@ -392,7 +396,7 @@ func GetQualityGate(httpClient *http.Client, sonarResults *SonarResults, quality
392396
if err != nil {
393397
return nil, err
394398
} else if qualityGateData.Errors != nil {
395-
return nil, fmt.Errorf("sonar error: %s", qualityGateData.Errors[0].Msg) //We should never reach this point, since incorrect/outdated task/analysis IDs etc. should already have raised errors
399+
return nil, fmt.Errorf("SonarQube error: %s", qualityGateData.Errors[0].Msg) //We should never reach this point, since incorrect/outdated task/analysis IDs etc. should already have raised errors
396400
} else {
397401
qualityGate.Status = qualityGateData.ProjectStatus.Status
398402
// The server expects an array of conditions if the Quality Gate exists, so if there are no conditions, we need to send an empty array

0 commit comments

Comments
 (0)