@@ -211,7 +211,7 @@ func GetElasticArtifactURL(artifact string, version string, operativeSystem stri
211
211
212
212
// GetObjectURLFromBucket extracts the media URL for the desired artifact from the
213
213
// Google Cloud Storage bucket used by the CI to push snapshots
214
- func GetObjectURLFromBucket (bucket string , object string , maxtimeout time.Duration ) (string , error ) {
214
+ func GetObjectURLFromBucket (bucket string , prefix string , object string , maxtimeout time.Duration ) (string , error ) {
215
215
exp := GetExponentialBackOff (maxtimeout )
216
216
217
217
retryCount := 1
@@ -222,14 +222,15 @@ func GetObjectURLFromBucket(bucket string, object string, maxtimeout time.Durati
222
222
223
223
storageAPI := func () error {
224
224
r := curl.HTTPRequest {
225
- URL : fmt .Sprintf ("https://storage.googleapis.com/storage/v1/b/%s/o?prefix=pull-requests%s " , bucket , pageTokenQueryParam ),
225
+ URL : fmt .Sprintf ("https://storage.googleapis.com/storage/v1/b/%s/o?prefix=%s%s " , bucket , prefix , pageTokenQueryParam ),
226
226
}
227
227
228
228
response , err := curl .Get (r )
229
229
if err != nil {
230
230
log .WithFields (log.Fields {
231
231
"bucket" : bucket ,
232
232
"elapsedTime" : exp .GetElapsedTime (),
233
+ "prefix" : prefix ,
233
234
"error" : err ,
234
235
"object" : object ,
235
236
"retry" : retryCount ,
@@ -243,6 +244,7 @@ func GetObjectURLFromBucket(bucket string, object string, maxtimeout time.Durati
243
244
log .WithFields (log.Fields {
244
245
"bucket" : bucket ,
245
246
"elapsedTime" : exp .GetElapsedTime (),
247
+ "prefix" : prefix ,
246
248
"object" : object ,
247
249
"retries" : retryCount ,
248
250
"url" : r .URL ,
@@ -252,6 +254,7 @@ func GetObjectURLFromBucket(bucket string, object string, maxtimeout time.Durati
252
254
if err != nil {
253
255
log .WithFields (log.Fields {
254
256
"bucket" : bucket ,
257
+ "prefix" : prefix ,
255
258
"object" : object ,
256
259
}).Warn ("Could not parse the response body for the object" )
257
260
@@ -260,71 +263,58 @@ func GetObjectURLFromBucket(bucket string, object string, maxtimeout time.Durati
260
263
return err
261
264
}
262
265
263
- items := jsonParsed .Path ("items" ).Children ()
264
-
265
- log .WithFields (log.Fields {
266
- "bucket" : bucket ,
267
- "elapsedTime" : exp .GetElapsedTime (),
268
- "objects" : len (items ),
269
- "retries" : retryCount ,
270
- }).Debug ("Objects found" )
271
-
272
- for _ , item := range items {
273
- itemID := item .Path ("id" ).Data ().(string )
274
- objectPath := bucket + "/" + object + "/"
275
- if strings .HasPrefix (itemID , objectPath ) {
276
- mediaLink = item .Path ("mediaLink" ).Data ().(string )
277
-
278
- log .WithFields (log.Fields {
279
- "bucket" : bucket ,
280
- "elapsedTime" : exp .GetElapsedTime (),
281
- "medialink" : mediaLink ,
282
- "object" : object ,
283
- "retries" : retryCount ,
284
- }).Debug ("Media link found for the object" )
285
- return nil
286
- }
287
-
266
+ mediaLink , err = processBucketSearchPage (jsonParsed , currentPage , bucket , prefix , object )
267
+ if err != nil {
268
+ log .WithFields (log.Fields {
269
+ "currentPage" : currentPage ,
270
+ "bucket" : bucket ,
271
+ "prefix" : prefix ,
272
+ "object" : object ,
273
+ }).Warn (err .Error ())
274
+ } else if mediaLink != "" {
288
275
log .WithFields (log.Fields {
289
276
"bucket" : bucket ,
290
277
"elapsedTime" : exp .GetElapsedTime (),
278
+ "prefix" : prefix ,
279
+ "medialink" : mediaLink ,
291
280
"object" : object ,
292
- "itemID" : itemID ,
293
281
"retries" : retryCount ,
294
- }).Trace ("Media link not found" )
282
+ }).Debug ("Media link found for the object" )
283
+ return nil
295
284
}
296
285
297
- if jsonParsed .Path ("nextPageToken" ) == nil {
286
+ pageTokenQueryParam = getBucketSearchNextPageParam (jsonParsed )
287
+ if pageTokenQueryParam == "" {
298
288
log .WithFields (log.Fields {
299
289
"currentPage" : currentPage ,
300
290
"bucket" : bucket ,
291
+ "prefix" : prefix ,
301
292
"object" : object ,
302
293
}).Warn ("Reached the end of the pages and the object was not found" )
303
294
304
295
return nil
305
296
}
306
297
307
- nextPageToken := jsonParsed .Path ("nextPageToken" ).Data ().(string )
308
- pageTokenQueryParam = "&pageToken=" + nextPageToken
309
298
currentPage ++
310
299
311
300
log .WithFields (log.Fields {
312
301
"currentPage" : currentPage ,
313
302
"bucket" : bucket ,
314
303
"elapsedTime" : exp .GetElapsedTime (),
304
+ "prefix" : prefix ,
315
305
"object" : object ,
316
306
"retries" : retryCount ,
317
307
}).Warn ("Object not found in current page. Continuing" )
318
308
319
- return fmt .Errorf ("The %s object could not be found in the current page (%d) the %s bucket" , object , currentPage , bucket )
309
+ return fmt .Errorf ("The %s object could not be found in the current page (%d) the %s bucket and %s prefix " , object , currentPage , bucket , prefix )
320
310
}
321
311
322
312
err := backoff .Retry (storageAPI , exp )
323
313
if err != nil {
324
314
return "" , err
325
315
}
326
316
if mediaLink == "" {
327
- return "" , fmt .Errorf ("Reached the end of the pages and the %s object was not found for the %s bucket" , object , bucket )
317
+ return "" , fmt .Errorf ("Reached the end of the pages and the %s object was not found for the %s bucket and %s prefix " , object , bucket , prefix )
328
318
}
329
319
330
320
return mediaLink , nil
@@ -406,6 +396,40 @@ func DownloadFile(url string) (string, error) {
406
396
return filepath , nil
407
397
}
408
398
399
+ func getBucketSearchNextPageParam (jsonParsed * gabs.Container ) string {
400
+ token := jsonParsed .Path ("nextPageToken" )
401
+ if token == nil {
402
+ return ""
403
+ }
404
+
405
+ nextPageToken := token .Data ().(string )
406
+ return "&pageToken=" + nextPageToken
407
+ }
408
+
409
+ func processBucketSearchPage (jsonParsed * gabs.Container , currentPage int , bucket string , prefix string , object string ) (string , error ) {
410
+ items := jsonParsed .Path ("items" ).Children ()
411
+
412
+ log .WithFields (log.Fields {
413
+ "bucket" : bucket ,
414
+ "prefix" : prefix ,
415
+ "objects" : len (items ),
416
+ "object" : object ,
417
+ }).Debug ("Objects found" )
418
+
419
+ for _ , item := range items {
420
+ itemID := item .Path ("id" ).Data ().(string )
421
+ objectPath := bucket + "/" + prefix + "/" + object + "/"
422
+ if strings .HasPrefix (itemID , objectPath ) {
423
+ mediaLink := item .Path ("mediaLink" ).Data ().(string )
424
+
425
+ log .Infof ("medialink: %s" , mediaLink )
426
+ return mediaLink , nil
427
+ }
428
+ }
429
+
430
+ return "" , fmt .Errorf ("The %s object could not be found in the current page (%d) in the %s bucket and %s prefix" , object , currentPage , bucket , prefix )
431
+ }
432
+
409
433
//nolint:unused
410
434
func randomStringWithCharset (length int , charset string ) string {
411
435
b := make ([]byte , length )
0 commit comments