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

Commit

Permalink
Properly handle 400 cases when using the v4 endpoint (#74)
Browse files Browse the repository at this point in the history
* Properly handle 400 cases when using the v4 endpoint

* Fix tests
  • Loading branch information
thomasrockhu authored Jul 15, 2020
1 parent b29a9b7 commit 2224951
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### `0.2.1`
- Properly handle 400 cases when using the v4 endpoint

### `0.2.0`
- move to the v4 upload endpoint with the v2 as a fallback

Expand Down
2 changes: 1 addition & 1 deletion codecov.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'codecov'
s.version = '0.2.0'
s.version = '0.2.1'
s.platform = Gem::Platform::RUBY
s.authors = ['codecov']
s.email = ['hello@codecov.io']
Expand Down
14 changes: 11 additions & 3 deletions lib/codecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require 'zlib'

class SimpleCov::Formatter::Codecov
VERSION = '0.2.0'
VERSION = '0.2.1'

### CIs
RECOGNIZED_CIS = [
Expand Down Expand Up @@ -340,6 +340,8 @@ def upload_to_codecov(ci, report)
puts " query: #{query_without_token}"

response = upload_to_v4(url, gzipped_report, query, query_without_token)
return false if response == false

response || upload_to_v2(url, gzipped_report, query, query_without_token)
end

Expand All @@ -360,8 +362,10 @@ def upload_to_v4(url, report, query, query_without_token)
}
)
response = retry_request(req, https)

return unless response.code == '200'
if response.code == '400'
puts response.body.red
return false
end

reports_url = response.body.lines[0]
s3target = response.body.lines[1]
Expand Down Expand Up @@ -432,6 +436,10 @@ def format(result)
ci = detect_ci
report = create_report(result)
response = upload_to_codecov(ci, report)
if response == false
report['result'] = { 'uploaded' => false }
return report
end

report['result'] = JSON.parse(response)
handle_report_response(report)
Expand Down
19 changes: 17 additions & 2 deletions test/test_codecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def stub_file(filename, coverage)
stub('SimpleCov::SourceFile', filename: filename, lines: lines)
end

def upload
def upload(success=true)
formatter = SimpleCov::Formatter::Codecov.new
result = stub('SimpleCov::Result', files: [
stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
Expand All @@ -42,14 +42,20 @@ def upload
data = formatter.format(result)
puts data
puts data['params']
if success
assert_successful_upload(data)
end
data
end

def assert_successful_upload(data)
assert_equal(data['result']['uploaded'], true)
assert_equal(data['result']['message'], 'Coverage reports upload successfully')
assert_equal(data['meta']['version'], 'codecov-ruby/v' + SimpleCov::Formatter::Codecov::VERSION)
assert_equal(data['coverage'].to_json, {
'lib/something.rb' => [nil, 1, 0, 0, nil, 1, nil],
'lib/somefile.rb' => [nil, 1, nil, 1, 1, 1, 0, 0, nil, 1, nil]
}.to_json)
data
end

def setup
Expand Down Expand Up @@ -513,4 +519,13 @@ def test_filenames_are_shortened_correctly
'path/lib/path_somefile.rb' => [nil]
}.to_json)
end

def test_invalid_token
ENV['CODECOV_TOKEN'] = 'fake'
result = upload(false)
assert_equal(false, result['result']['uploaded'])
branch = `git rev-parse --abbrev-ref HEAD`.strip
assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
end
end

0 comments on commit 2224951

Please sign in to comment.