88require 'zlib'
99
1010class SimpleCov ::Formatter ::Codecov
11- VERSION = '0.1.20 '
11+ VERSION = '0.2.0 '
1212
1313 ### CIs
1414 RECOGNIZED_CIS = [
@@ -339,7 +339,61 @@ def upload_to_codecov(ci, report)
339339 puts " url: #{ url } "
340340 puts " query: #{ query_without_token } "
341341
342- upload_to_v2 ( url , gzipped_report , query , query_without_token )
342+ response = upload_to_v4 ( url , gzipped_report , query , query_without_token )
343+ response || upload_to_v2 ( url , gzipped_report , query , query_without_token )
344+ end
345+
346+ def upload_to_v4 ( url , report , query , query_without_token )
347+ uri = URI . parse ( url . chomp ( '/' ) + '/upload/v4' )
348+ https = Net ::HTTP . new ( uri . host , uri . port )
349+ https . use_ssl = !url . match ( /^https/ ) . nil?
350+
351+ puts [ '-> ' . green , 'Pinging Codecov' ] . join ( ' ' )
352+ puts "#{ url } /#{ uri . path } ?#{ query_without_token } "
353+
354+ req = Net ::HTTP ::Post . new (
355+ "#{ uri . path } ?#{ query } " ,
356+ {
357+ 'X-Reduced-Redundancy' => 'false' ,
358+ 'X-Content-Encoding' => 'application/x-gzip' ,
359+ 'Content-Type' => 'text/plain'
360+ }
361+ )
362+ response = retry_request ( req , https )
363+
364+ return unless response . code == '200'
365+
366+ reports_url = response . body . lines [ 0 ]
367+ s3target = response . body . lines [ 1 ]
368+ puts [ '-> ' . green , 'Uploading to' ] . join ( ' ' )
369+ puts s3target
370+
371+ uri = URI ( s3target )
372+ https = Net ::HTTP . new ( uri . host , uri . port )
373+ https . use_ssl = true
374+ req = Net ::HTTP ::Put . new (
375+ s3target ,
376+ {
377+ 'Content-Encoding' => 'gzip' ,
378+ 'Content-Type' => 'text/plain'
379+ }
380+ )
381+ req . body = report
382+ res = retry_request ( req , https )
383+ if res . body == ''
384+ {
385+ 'uploaded' => true ,
386+ 'url' => reports_url ,
387+ 'meta' => {
388+ 'status' => res . code
389+ } ,
390+ 'message' => 'Coverage reports upload successfully'
391+ } . to_json
392+ else
393+ puts [ '-> ' . black , 'Could not upload reports via v4 API, defaulting to v2' ] . join ( ' ' )
394+ puts res . body . red
395+ nil
396+ end
343397 end
344398
345399 def upload_to_v2 ( url , report , query , query_without_token )
@@ -359,7 +413,8 @@ def upload_to_v2(url, report, query, query_without_token)
359413 }
360414 )
361415 req . body = report
362- retry_request ( req , https )
416+ res = retry_request ( req , https )
417+ res &.body
363418 end
364419
365420 def handle_report_response ( report )
@@ -378,7 +433,7 @@ def format(result)
378433 report = create_report ( result )
379434 response = upload_to_codecov ( ci , report )
380435
381- report [ 'result' ] = JSON . parse ( response . body )
436+ report [ 'result' ] = JSON . parse ( response )
382437 handle_report_response ( report )
383438 net_blockers ( :on )
384439 report
0 commit comments