Skip to content

Commit 33f027f

Browse files
committed
Change back to max-wait instead of max-retries
1 parent c502b01 commit 33f027f

3 files changed

Lines changed: 23 additions & 22 deletions

File tree

cmd/kosli/attestSonar.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type attestSonarOptions struct {
2424
projectKey string
2525
serverURL string
2626
revision string
27-
maxRetries int
27+
maxWait int
2828
payload SonarAttestationPayload
2929
}
3030

@@ -39,9 +39,9 @@ The scan to be retrieved can be specified in two ways:
3939
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,
4040
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.
4141
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-retries^ flag to retry the command while waiting for the scan to be processed.
43-
This flag takes the maximum number of retries for the Kosli CLI to attempt to retrieve the scan results, with exponential backoff. Once the results
44-
are available they are attested to Kosli as usual.
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.
4545
4646
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
4747
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
@@ -62,7 +62,7 @@ kosli attest sonar \
6262
--api-token yourAPIToken \
6363
--org yourOrgName \
6464
65-
# report a SonarQube Server attestation about a trail using SonarQube's metadata, allowing for up to 10 retries:
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:
6666
kosli attest sonar \
6767
--name yourAttestationName \
6868
--flow yourFlowName \
@@ -71,7 +71,7 @@ kosli attest sonar \
7171
--sonar-working-dir yourSonarWorkingDirPath \
7272
--api-token yourAPIToken \
7373
--org yourOrgName \
74-
--max-retries 10
74+
--max-wait 60
7575
7676
# report a SonarQube Cloud attestation for a specific branch about a trail using key/revision:
7777
kosli attest sonar \
@@ -98,7 +98,7 @@ kosli attest sonar \
9898
--api-token yourAPIToken \
9999
--org yourOrgName \
100100
101-
# report a SonarQube Cloud attestation about a trail with an attachment using SonarQube's metadata, allowing for up to 5 retries:
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:
102102
kosli attest sonar \
103103
--name yourAttestationName \
104104
--flow yourFlowName \
@@ -108,7 +108,7 @@ kosli attest sonar \
108108
--attachment yourAttachmentPath \
109109
--api-token yourAPIToken \
110110
--org yourOrgName \
111-
--max-retries 5
111+
--max-wait 300
112112
`
113113

114114
func newAttestSonarCmd(out io.Writer) *cobra.Command {
@@ -164,7 +164,7 @@ func newAttestSonarCmd(out io.Writer) *cobra.Command {
164164
cmd.Flags().StringVar(&o.projectKey, "sonar-project-key", "", sonarProjectKeyFlag)
165165
cmd.Flags().StringVar(&o.serverURL, "sonar-server-url", "https://sonarcloud.io", sonarServerURLFlag)
166166
cmd.Flags().StringVar(&o.revision, "sonar-revision", o.commitSHA, sonarRevisionFlag)
167-
cmd.Flags().IntVar(&o.maxRetries, "max-retries", 0, sonarMaxRetriesFlag)
167+
cmd.Flags().IntVar(&o.maxWait, "max-wait", 30, sonarMaxWaitFlag)
168168

169169
err := RequireFlags(cmd, []string{"flow", "trail", "name", "sonar-api-token"})
170170
if err != nil {
@@ -182,7 +182,7 @@ func (o *attestSonarOptions) run(args []string) error {
182182
return err
183183
}
184184

185-
sc := sonar.NewSonarConfig(o.apiToken, o.workingDir, o.ceTaskURL, o.projectKey, o.serverURL, o.revision, o.maxRetries)
185+
sc := sonar.NewSonarConfig(o.apiToken, o.workingDir, o.ceTaskURL, o.projectKey, o.serverURL, o.revision, o.maxWait)
186186

187187
o.payload.SonarResults, err = sc.GetSonarResults(logger)
188188
if err != nil {

cmd/kosli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
241241
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."
242242
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."
243243
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-
sonarMaxRetriesFlag = "[optional] Allow the command to retry fetching the scan results from SonarQube, up to the maximum number of retries 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 0."
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."
245245
logicalEnvFlag = "[required] The logical environment."
246246
physicalEnvFlag = "[required] The physical environment."
247247
attestationTypeDescriptionFlag = "[optional] The attestation type description."

internal/sonar/sonar.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type SonarConfig struct {
2020
revision string
2121
projectKey string
2222
serverURL string
23-
maxRetries int
23+
maxWait int
2424
}
2525

2626
// Structs to build the JSON for our attestation payload
@@ -113,15 +113,15 @@ type Error struct {
113113
Msg string `json:"msg"`
114114
}
115115

116-
func NewSonarConfig(apiToken, workingDir, ceTaskUrl, projectKey, serverURL, revision string, maxRetries int) *SonarConfig {
116+
func NewSonarConfig(apiToken, workingDir, ceTaskUrl, projectKey, serverURL, revision string, maxWait int) *SonarConfig {
117117
return &SonarConfig{
118118
APIToken: apiToken,
119119
WorkingDir: workingDir,
120120
CETaskUrl: ceTaskUrl,
121121
revision: revision,
122122
projectKey: projectKey,
123123
serverURL: serverURL,
124-
maxRetries: maxRetries,
124+
maxWait: maxWait,
125125
}
126126
}
127127

@@ -164,7 +164,7 @@ func (sc *SonarConfig) GetSonarResults(logger *log.Logger) (*SonarResults, error
164164

165165
if analysisID == "" {
166166
//Get the analysis ID, status, project name and branch data from the ceTaskURL (ce API)
167-
analysisID, err = GetCETaskData(httpClient, project, sonarResults, sc.CETaskUrl, tokenHeader, sc.maxRetries, logger)
167+
analysisID, err = GetCETaskData(httpClient, project, sonarResults, sc.CETaskUrl, tokenHeader, sc.maxWait, logger)
168168
if err != nil {
169169
return nil, err
170170
}
@@ -215,7 +215,7 @@ func (sc *SonarConfig) readFile(project *Project, results *SonarResults) error {
215215
return nil
216216
}
217217

218-
func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *SonarResults, ceTaskURL, tokenHeader string, maxRetries int, logger *log.Logger) (string, error) {
218+
func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *SonarResults, ceTaskURL, tokenHeader string, maxWait int, logger *log.Logger) (string, error) {
219219
taskRequest, err := http.NewRequest("GET", ceTaskURL, nil)
220220
taskRequest.Header.Add("Authorization", tokenHeader)
221221
if err != nil {
@@ -227,7 +227,7 @@ func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *Sona
227227
elapsed := 0 // seconds elapsed
228228
taskResponseData := &TaskResponse{}
229229

230-
for retries < maxRetries || maxRetries == 0 {
230+
for elapsed < maxWait {
231231
taskResponse, err := httpClient.Do(taskRequest)
232232
if err != nil {
233233
return "", err
@@ -238,14 +238,15 @@ func GetCETaskData(httpClient *http.Client, project *Project, sonarResults *Sona
238238
return "", fmt.Errorf("please check your API token is correct and you have the correct permissions in SonarQube")
239239
}
240240

241-
if maxRetries == 0 {
242-
taskResponse.Body.Close()
243-
break
244-
}
245-
246241
if taskResponseData.Task.Status == "PENDING" || taskResponseData.Task.Status == "IN_PROGRESS" {
242+
// So that we don't wait longer than maxWait
243+
if (elapsed + wait) > maxWait {
244+
wait = maxWait - elapsed
245+
}
246+
247247
logger.Info("retry %d: waiting %ds for SonarQube scan to be processed...", retries+1, wait)
248248
time.Sleep(time.Duration(wait) * time.Second)
249+
249250
if elapsed > 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.
250251
elapsed += wait
251252
retries++

0 commit comments

Comments
 (0)