Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit b29a9b7

Browse files
authored
Use the v4 upload endpoint (#71)
* First attempt at v4 * force json on v4 * Make v4 painful * Better logging messaging * Create new https * Fix v4 * cleanup
1 parent 9ff6f79 commit b29a9b7

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### `0.2.0`
2+
- move to the v4 upload endpoint with the v2 as a fallback
3+
14
### `0.1.20`
25
- fix critical upload issues on V2 endpoint
36

codecov.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |s|
44
s.name = 'codecov'
5-
s.version = '0.1.20'
5+
s.version = '0.2.0'
66
s.platform = Gem::Platform::RUBY
77
s.authors = ['codecov']
88
s.email = ['hello@codecov.io']

lib/codecov.rb

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require 'zlib'
99

1010
class 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

Comments
 (0)