You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cmd/kosli/attestSonar.go
+18-9Lines changed: 18 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ type attestSonarOptions struct {
24
24
projectKeystring
25
25
serverURLstring
26
26
revisionstring
27
+
maxWaitint
27
28
payloadSonarAttestationPayload
28
29
}
29
30
@@ -34,19 +35,24 @@ Retrieves results for the specified scan from SonarQube Cloud or SonarQube Serve
34
35
The results are parsed to find the status of the project's quality gate which is used to determine the attestation's compliance status.
35
36
36
37
The scan to be retrieved can be specified in two ways:
37
-
1. (Default) Using metadata created by the Sonar scanner. By default this is located within a temporary .scannerwork folder in the repo base directory.
38
+
1. (Default) Using metadata created by the Sonar scanner. By default this is located within a temporary ^.scannerwork^ folder in the repo base directory.
38
39
If you have overriden the location of this folder by passing parameters to the Sonar scanner, or are running Kosli's CLI locally outside the repo's base directory,
39
-
you can provide the correct path using the --sonar-working-dir flag. This metadata is generated by a specific scan, allowing Kosli to retrieve the results of that scan.
40
+
you can provide the correct path using the ^--sonar-working-dir^ flag. This metadata is generated by a specific scan, allowing Kosli to retrieve the results of that scan.
41
+
If there are delays in the scan processing (either because the scanned project is very large, or because SonarQube is experiencing processing delays), it may happen that
42
+
the scan results are not available by the time the attest sonar command is executed. In this case you can use the ^--max-wait^ flag to retry the command while waiting for the scan to be processed.
43
+
This flag takes the maximum number of seconds to wait for the results to be available. The Kosli CLI will then attempt to retrieve the scan results until the maximum wait time is reached, with
44
+
exponential backoff between retries. Once the results are available they are attested to Kosli as usual.
45
+
40
46
2. Providing the Sonar project key and the revision of the scan (plus the SonarQube server URL if relevant). If running the Kosli CLI in some CI/CD pipeline, the revision
41
47
is defaulted to the commit SHA. If you are running the command locally, or have overriden the revision in SonarQube via parameters to the Sonar scanner, you can
42
-
provide the correct revision using the --sonar-revision flag. Kosli then finds the scan results for the specified project key and revision.
48
+
provide the correct revision using the ^--sonar-revision^ flag. Kosli then finds the scan results for the specified project key and revision.
43
49
44
50
Note that if your project is very large and you are using SonarQube Cloud's automatic analysis, it is possible for the attest sonar command to run before the SonarQube Cloud scan is completed.
45
51
In this case, we recommend using Kosli's Sonar webhook integration ( https://docs.kosli.com/integrations/sonar/ ) rather than the CLI to attest the scan results.
46
52
`+attestationBindingDesc
47
53
48
54
constattestSonarExample=`
49
-
# report a SonarQube Cloud attestation about a trail using Sonar's metadata:
55
+
# report a SonarQube Cloud attestation about a trail using SonarQube's metadata, with no retries:
50
56
kosli attest sonar \
51
57
--name yourAttestationName \
52
58
--flow yourFlowName \
@@ -56,7 +62,7 @@ kosli attest sonar \
56
62
--api-token yourAPIToken \
57
63
--org yourOrgName \
58
64
59
-
# report a SonarQube Server attestation about a trail using Sonar's metadata:
65
+
# report a SonarQube Server attestation about a trail using SonarQube's metadata, waiting for up to 60 seconds for the results to be available:
60
66
kosli attest sonar \
61
67
--name yourAttestationName \
62
68
--flow yourFlowName \
@@ -65,6 +71,7 @@ kosli attest sonar \
65
71
--sonar-working-dir yourSonarWorkingDirPath \
66
72
--api-token yourAPIToken \
67
73
--org yourOrgName \
74
+
--max-wait 60
68
75
69
76
# report a SonarQube Cloud attestation for a specific branch about a trail using key/revision:
70
77
kosli attest sonar \
@@ -91,7 +98,7 @@ kosli attest sonar \
91
98
--api-token yourAPIToken \
92
99
--org yourOrgName \
93
100
94
-
# report a SonarQube Cloud attestation about a trail with an attachment using Sonar's metadata:
101
+
# report a SonarQube Cloud attestation about a trail with an attachment using SonarQube's metadata, waiting for up to 300 seconds for the results to be available:
Copy file name to clipboardExpand all lines: cmd/kosli/root.go
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -241,6 +241,7 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
241
241
sonarProjectKeyFlag="[conditional] The project key of the SonarCloud/SonarQube project. Only required if you want to use the project key/revision to get the scan results rather than using Sonar's metadata file."
242
242
sonarServerURLFlag="[conditional] The URL of your SonarQube server. Only required if you are using SonarQube and not using SonarQube's metadata file to get scan results."
243
243
sonarRevisionFlag="[conditional] The revision of the SonarCloud/SonarQube project. Only required if you want to use the project key/revision to get the scan results rather than using Sonar's metadata file and you have overridden the default revision, or you aren't using a CI. Defaults to the value of the git commit flag."
244
+
sonarMaxWaitFlag="[optional] Allow the command to wait and retry fetching the scan results from SonarQube, up to the maximum number of seconds provided, with exponential backoff. Useful when using SonarQube's metadata file to retrieve and attest scans that take a long time to process . Defaults to 30 seconds."
244
245
logicalEnvFlag="[required] The logical environment."
245
246
physicalEnvFlag="[required] The physical environment."
246
247
attestationTypeDescriptionFlag="[optional] The attestation type description."
logger.Info("retry %d: waiting %ds for SonarQube scan to be processed...", retries+1, wait)
248
+
time.Sleep(time.Duration(wait) *time.Second)
249
+
250
+
ifelapsed>300 { // If we've waited 5 minutes, we'll wait 300 seconds (5 minutes) before checking again. This is so that we don't end up with extremely long waiting intervals with the doubling approach.
251
+
elapsed+=wait
252
+
retries++
253
+
wait+=300
254
+
} else { // Otherwise, we'll double the wait time each time
return"", fmt.Errorf("please check your API token is correct and you have the correct permissions in SonarQube")
265
+
ifelapsed!=0 {
266
+
logger.Info("Waited for %d seconds for SonarQube scan to be processed. %d retries.\n", elapsed, retries)
229
267
}
230
268
231
-
project.Name=taskResponseData.Task.ComponentName
232
-
project.Key=taskResponseData.Task.ComponentKey
233
-
sonarResults.TaskID=taskResponseData.Task.TaskID
234
-
analysisId:=taskResponseData.Task.AnalysisID
235
-
sonarResults.Status=taskResponseData.Task.Status
269
+
task:=taskResponseData.Task
270
+
271
+
project.Name=task.ComponentName
272
+
project.Key=task.ComponentKey
273
+
sonarResults.TaskID=task.TaskID
274
+
analysisId:=task.AnalysisID
275
+
sonarResults.Status=task.Status
236
276
277
+
// This should only happen if the task is pending - either because the project is large and the scan takes a long time
278
+
// to process, or because SonarQube is experiencing delays for some reason.
237
279
ifanalysisId=="" {
238
-
return"", fmt.Errorf("analysis ID not found on %s", sonarResults.ServerUrl) // This should never happen
280
+
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-retries flag.", sonarResults.ServerUrl)
0 commit comments