@@ -471,24 +471,63 @@ def main(argv):
471
471
found_artifacts = False
472
472
# There are possibly multiple artifacts, so iterate through all of them,
473
473
# and extract the relevant ones into a temp folder, and then summarize them all.
474
+ # Prioritize artifacts by date, older ones might be expired.
475
+ sorted_artifacts = sorted (artifacts , key = lambda art : dateutil .parser .parse (art ['created_at' ]), reverse = True )
476
+
474
477
with tempfile .TemporaryDirectory () as tmpdir :
475
- for a in artifacts :
478
+ for a in sorted_artifacts : # Iterate over sorted artifacts
476
479
if 'log-artifact' in a ['name' ]:
477
- print ("Checking this artifact:" , a ['name' ], "\n " )
478
- artifact_contents = firebase_github .download_artifact (FLAGS .token , a ['id' ])
479
- if artifact_contents :
480
- found_artifacts = True
481
- artifact_data = io .BytesIO (artifact_contents )
482
- artifact_zip = zipfile .ZipFile (artifact_data )
483
- artifact_zip .extractall (path = tmpdir )
480
+ logging .debug ("Attempting to download artifact: %s (ID: %s, Created: %s)" , a ['name' ], a ['id' ], a ['created_at' ])
481
+ # Pass tmpdir to download_artifact to save directly
482
+ artifact_downloaded_path = os .path .join (tmpdir , f"{ a ['id' ]} .zip" )
483
+ # Attempt to download the artifact with a timeout
484
+ download_success = False # Initialize download_success
485
+ try :
486
+ # download_artifact now returns True on success, None on failure.
487
+ if firebase_github .download_artifact (FLAGS .token , a ['id' ], output_path = artifact_downloaded_path ):
488
+ download_success = True
489
+ except requests .exceptions .Timeout :
490
+ logging .warning (f"Timeout while trying to download artifact: { a ['name' ]} (ID: { a ['id' ]} )" )
491
+ # download_success remains False
492
+
493
+ if download_success and os .path .exists (artifact_downloaded_path ):
494
+ try :
495
+ with open (artifact_downloaded_path , "rb" ) as f :
496
+ artifact_contents = f .read ()
497
+ if artifact_contents : # Ensure content was read
498
+ found_artifacts = True
499
+ artifact_data = io .BytesIO (artifact_contents )
500
+ with zipfile .ZipFile (artifact_data ) as artifact_zip : # Use with statement for ZipFile
501
+ artifact_zip .extractall (path = tmpdir )
502
+ logging .info ("Successfully downloaded and extracted artifact: %s" , a ['name' ])
503
+ else :
504
+ logging .warning ("Artifact %s (ID: %s) was downloaded but is empty." , a ['name' ], a ['id' ])
505
+ except zipfile .BadZipFile :
506
+ logging .error ("Failed to open zip file for artifact %s (ID: %s). It might be corrupted or not a zip file." , a ['name' ], a ['id' ])
507
+ except Exception as e :
508
+ logging .error ("An error occurred during artifact processing %s (ID: %s): %s" , a ['name' ], a ['id' ], e )
509
+ finally :
510
+ # Clean up the downloaded zip file whether it was processed successfully or not
511
+ if os .path .exists (artifact_downloaded_path ):
512
+ os .remove (artifact_downloaded_path )
513
+ elif not download_success : # Covers False or None from download_artifact
514
+ # Logging for non-timeout failures is now primarily handled within download_artifact
515
+ # We only log a general failure here if it wasn't a timeout (already logged)
516
+ # and download_artifact indicated failure (returned None).
517
+ # This avoids double logging for specific HTTP errors like 410.
518
+ pass # Most specific logging is now in firebase_github.py
519
+
484
520
if found_artifacts :
485
521
(success , results ) = summarize_test_results .summarize_logs (tmpdir , False , False , True )
486
- print ( "Results:" , success , " " , results , " \n " )
522
+ logging . info ( "Summarized logs results - Success: %s, Results (first 100 chars): %.100s " , success , results )
487
523
run ['log_success' ] = success
488
524
run ['log_results' ] = results
525
+ else :
526
+ logging .warning ("No artifacts could be successfully downloaded and processed for run %s on day %s." , run ['id' ], day )
527
+
489
528
490
529
if not found_artifacts :
491
- # Artifacts expire after some time, so if they are gone, we need
530
+ # Artifacts expire after some time, or download failed, so if they are gone, we need
492
531
# to read the GitHub logs instead. This is much slower, so we
493
532
# prefer to read artifacts instead whenever possible.
494
533
logging .info ("Reading github logs for run %s instead" , run ['id' ])
0 commit comments