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

Commit 2224951

Browse files
authored
Properly handle 400 cases when using the v4 endpoint (#74)
* Properly handle 400 cases when using the v4 endpoint * Fix tests
1 parent b29a9b7 commit 2224951

4 files changed

Lines changed: 32 additions & 6 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.1`
2+
- Properly handle 400 cases when using the v4 endpoint
3+
14
### `0.2.0`
25
- move to the v4 upload endpoint with the v2 as a fallback
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.2.0'
5+
s.version = '0.2.1'
66
s.platform = Gem::Platform::RUBY
77
s.authors = ['codecov']
88
s.email = ['hello@codecov.io']

lib/codecov.rb

Lines changed: 11 additions & 3 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.2.0'
11+
VERSION = '0.2.1'
1212

1313
### CIs
1414
RECOGNIZED_CIS = [
@@ -340,6 +340,8 @@ def upload_to_codecov(ci, report)
340340
puts " query: #{query_without_token}"
341341

342342
response = upload_to_v4(url, gzipped_report, query, query_without_token)
343+
return false if response == false
344+
343345
response || upload_to_v2(url, gzipped_report, query, query_without_token)
344346
end
345347

@@ -360,8 +362,10 @@ def upload_to_v4(url, report, query, query_without_token)
360362
}
361363
)
362364
response = retry_request(req, https)
363-
364-
return unless response.code == '200'
365+
if response.code == '400'
366+
puts response.body.red
367+
return false
368+
end
365369

366370
reports_url = response.body.lines[0]
367371
s3target = response.body.lines[1]
@@ -432,6 +436,10 @@ def format(result)
432436
ci = detect_ci
433437
report = create_report(result)
434438
response = upload_to_codecov(ci, report)
439+
if response == false
440+
report['result'] = { 'uploaded' => false }
441+
return report
442+
end
435443

436444
report['result'] = JSON.parse(response)
437445
handle_report_response(report)

test/test_codecov.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def stub_file(filename, coverage)
3232
stub('SimpleCov::SourceFile', filename: filename, lines: lines)
3333
end
3434

35-
def upload
35+
def upload(success=true)
3636
formatter = SimpleCov::Formatter::Codecov.new
3737
result = stub('SimpleCov::Result', files: [
3838
stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
@@ -42,14 +42,20 @@ def upload
4242
data = formatter.format(result)
4343
puts data
4444
puts data['params']
45+
if success
46+
assert_successful_upload(data)
47+
end
48+
data
49+
end
50+
51+
def assert_successful_upload(data)
4552
assert_equal(data['result']['uploaded'], true)
4653
assert_equal(data['result']['message'], 'Coverage reports upload successfully')
4754
assert_equal(data['meta']['version'], 'codecov-ruby/v' + SimpleCov::Formatter::Codecov::VERSION)
4855
assert_equal(data['coverage'].to_json, {
4956
'lib/something.rb' => [nil, 1, 0, 0, nil, 1, nil],
5057
'lib/somefile.rb' => [nil, 1, nil, 1, 1, 1, 0, 0, nil, 1, nil]
5158
}.to_json)
52-
data
5359
end
5460

5561
def setup
@@ -513,4 +519,13 @@ def test_filenames_are_shortened_correctly
513519
'path/lib/path_somefile.rb' => [nil]
514520
}.to_json)
515521
end
522+
523+
def test_invalid_token
524+
ENV['CODECOV_TOKEN'] = 'fake'
525+
result = upload(false)
526+
assert_equal(false, result['result']['uploaded'])
527+
branch = `git rev-parse --abbrev-ref HEAD`.strip
528+
assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
529+
assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
530+
end
516531
end

0 commit comments

Comments
 (0)