1818import de .retest .recheck .report .TestReport ;
1919import kong .unirest .HttpResponse ;
2020import kong .unirest .Unirest ;
21+ import kong .unirest .UnirestException ;
2122import lombok .extern .slf4j .Slf4j ;
2223
2324@ Slf4j
@@ -34,15 +35,18 @@ public void save( final URI identifier, final T element ) throws IOException {
3435 kryoPersistence .save ( identifier , element );
3536
3637 if ( isAggregatedReport ( identifier ) && element instanceof TestReport ) {
38+ final TestReport report = (TestReport ) element ;
3739 try {
38- saveToCloud ( identifier , (TestReport ) element );
39- } catch ( final Exception e ) {
40- if ( ((TestReport ) element ).containsChanges () ) {
41- log .error ( "The upload of the test report failed. The test report contains changes." );
42- throw e ;
43- } else {
40+ saveToCloud ( report , Files .readAllBytes ( Paths .get ( identifier ) ) );
41+ } catch ( final IOException e ) {
42+ if ( !report .containsChanges () ) {
4443 log .warn (
45- "The upload of the test report failed. The test report does not contain any changes (excluding metadata differences)." );
44+ "Could not read report '{}' for upload. Ignoring exception because the report does not have any differences." ,
45+ identifier , e );
46+ } else {
47+ log .error ( "Could not read report '{}' for upload. Rethrowing because report has differences." ,
48+ identifier , e );
49+ throw e ;
4650 }
4751 }
4852 }
@@ -58,29 +62,46 @@ private List<String> getTestClasses( final TestReport report ) {
5862 .collect ( Collectors .toList () );
5963 }
6064
61- private void saveToCloud ( final URI identifier , final TestReport report ) throws IOException {
65+ private void saveToCloud ( final TestReport report , final byte [] data ) {
6266 final HttpResponse <String > uploadUrlResponse = getUploadUrl ();
63-
64- final ReportUploadMetadata metadata = ReportUploadMetadata .builder () //
65- .location ( identifier ) //
66- .uploadUrl ( uploadUrlResponse .getBody () ) //
67- .testClasses ( getTestClasses ( report ) ) //
68- .build ();
69-
7067 if ( uploadUrlResponse .isSuccess () ) {
71- uploadReport ( metadata );
68+ final ReportUploadContainer metadata = ReportUploadContainer .builder () //
69+ .reportName ( String .join ( ", " , getTestClasses ( report ) ) ) //
70+ .data ( data ) //
71+ .uploadUrl ( uploadUrlResponse .getBody () ) //
72+ .build ();
73+ final boolean hasChanges = report .containsChanges ();
74+
75+ final int maxAttempts = RecheckProperties .getInstance ().rehubReportUploadAttempts ();
76+ for ( int remainingAttempts = maxAttempts - 1 ; remainingAttempts >= 0 ; remainingAttempts -- ) {
77+ try {
78+ uploadReport ( metadata );
79+ break ; // Successful, abort retry
80+ } catch ( final UnirestException e ) {
81+ if ( !hasChanges ) {
82+ log .warn (
83+ "Failed to upload report. Ignoring exception because the report does not have any differences." ,
84+ e );
85+ break ;
86+ }
87+ if ( remainingAttempts == 0 ) {
88+ log .error (
89+ "Failed to upload report. Aborting, because maximum retries have been reached. If this happens often, consider increasing the property '{}={}'." ,
90+ RecheckProperties .REHUB_REPORT_UPLOAD_ATTEMPTS , maxAttempts , e );
91+ throw e ;
92+ } else {
93+ log .warn ( "Failed to upload report. Retrying another {} times." , remainingAttempts , e );
94+ }
95+ }
96+ }
7297 }
7398 }
7499
75- private void uploadReport ( final ReportUploadMetadata metadata ) throws IOException {
76- final String reportName = metadata .getTestClasses () //
77- .stream () //
78- .collect ( Collectors .joining ( ", " ) );
100+ private void uploadReport ( final ReportUploadContainer metadata ) {
79101 final long start = System .currentTimeMillis ();
80-
81102 final HttpResponse <?> uploadResponse = Unirest .put ( metadata .getUploadUrl () ) //
82- .header ( "x-amz-meta-report-name" , abbreviate ( reportName , MAX_REPORT_NAME_LENGTH ) ) //
83- .body ( Files . readAllBytes ( Paths . get ( metadata .getLocation () ) ) ) //
103+ .header ( "x-amz-meta-report-name" , abbreviate ( metadata . getReportName () , MAX_REPORT_NAME_LENGTH ) ) //
104+ .body ( metadata .getData ( ) ) //
84105 .asEmpty ();
85106
86107 if ( uploadResponse .isSuccess () ) {
0 commit comments