codecov 0.2.6 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/codecov.rb +54 -19
  3. data/test/test_codecov.rb +60 -2
  4. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b2e67c7bb37041adf9adb9dbe2fd8f9c2966131d338961785a29b25090e7206
4
- data.tar.gz: 2ada93e046953b7e77397831259a21fcaef09012f4ea0c2c4584668b080d68a6
3
+ metadata.gz: d4eb349d94b8cd78d2046ebcb9f50a707888877a5f95166c597c75dfeebe91d3
4
+ data.tar.gz: 25defdc6f6f2ba3d89d77d261064dedf00c90a08d563da3c82fdb31cedbfc3df
5
5
  SHA512:
6
- metadata.gz: faf56288e9f75ee77f230da561da1ee87e3305fa7220e4b5962e2011e7f7d8c1f3c1a234a386ac698b00ad8999f500787f6b218c78c9009cb25bc096c056093c
7
- data.tar.gz: 233cbebb75102d82b62daf1087a490493ee3cef84de7e91163bc8c5040076f2f1798ebde32aa7971c2234c306adcc13c7a3abcc1929489c1aba124a319f195cb
6
+ metadata.gz: 501d46bed8fe251466e84eb383484171b4bb7470dc8060764873de61b11e5fd5efa31c75250b42bd517cdef289c7587841ba8b2a65200e89bb6fd79210e26bab
7
+ data.tar.gz: 6cc38ecc3d86b30507a1bf29347e195fb48833f5ea106a1206f7897f9dee492f75f334f507aaa34366a25b05ece066bc024c2d9fc083b2b9b8667d24013ef2c8
@@ -4,11 +4,10 @@ require 'uri'
4
4
  require 'json'
5
5
  require 'net/http'
6
6
  require 'simplecov'
7
- require 'colorize'
8
7
  require 'zlib'
9
8
 
10
9
  class SimpleCov::Formatter::Codecov
11
- VERSION = '0.2.6'
10
+ VERSION = '0.2.11'
12
11
 
13
12
  ### CIs
14
13
  RECOGNIZED_CIS = [
@@ -18,6 +17,7 @@ class SimpleCov::Formatter::Codecov
18
17
  BITRISE = 'Bitrise CI',
19
18
  BUILDKITE = 'Buildkite CI',
20
19
  CIRCLE = 'Circle CI',
20
+ CODEBUILD = 'Codebuild CI',
21
21
  CODESHIP = 'Codeship CI',
22
22
  DRONEIO = 'Drone CI',
23
23
  GITHUB = 'GitHub Actions',
@@ -59,6 +59,8 @@ class SimpleCov::Formatter::Codecov
59
59
  BUILDKITE
60
60
  elsif (ENV['CI'] == 'true') && (ENV['CIRCLECI'] == 'true')
61
61
  CIRCLE
62
+ elsif ENV['CODEBUILD_CI'] == 'true'
63
+ CODEBUILD
62
64
  elsif (ENV['CI'] == 'true') && (ENV['CI_NAME'] == 'codeship')
63
65
  CODESHIP
64
66
  elsif ((ENV['CI'] == 'true') || (ENV['CI'] == 'drone')) && (ENV['DRONE'] == 'true')
@@ -86,7 +88,7 @@ class SimpleCov::Formatter::Codecov
86
88
  end
87
89
 
88
90
  if !RECOGNIZED_CIS.include?(ci)
89
- puts ['x>'.red, 'No CI provider detected.'].join(' ')
91
+ puts [red('x>'), 'No CI provider detected.'].join(' ')
90
92
  else
91
93
  puts "==> #{ci} detected"
92
94
  end
@@ -158,6 +160,15 @@ class SimpleCov::Formatter::Codecov
158
160
  params[:pr] = ENV['CIRCLE_PR_NUMBER']
159
161
  params[:branch] = ENV['CIRCLE_BRANCH']
160
162
  params[:commit] = ENV['CIRCLE_SHA1']
163
+ when CODEBUILD
164
+ # https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
165
+ params[:service] = 'codebuild'
166
+ params[:branch] = ENV['CODEBUILD_WEBHOOK_HEAD_REF'].split('/')[2]
167
+ params[:build] = ENV['CODEBUILD_BUILD_ID']
168
+ params[:commit] = ENV['CODEBUILD_RESOLVED_SOURCE_VERSION']
169
+ params[:job] = ENV['CODEBUILD_BUILD_ID']
170
+ params[:slug] = ENV['CODEBUILD_SOURCE_REPO_URL'].match(/.*github.com\/(?<slug>.*).git/)['slug']
171
+ params[:pr] = ENV['CODEBUILD_SOURCE_VERSION'].match(/pr\/(?<pr>.*)/)['pr'] if ENV['CODEBUILD_SOURCE_VERSION']
161
172
  when CODESHIP
162
173
  # https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
163
174
  params[:service] = 'codeship'
@@ -308,6 +319,7 @@ class SimpleCov::Formatter::Codecov
308
319
  puts 'Error uploading coverage reports to Codecov. Sorry'
309
320
  puts e.class.name
310
321
  puts e
322
+ puts "Backtrace:\n\t#{e.backtrace}"
311
323
  return response
312
324
  end
313
325
 
@@ -325,7 +337,7 @@ class SimpleCov::Formatter::Codecov
325
337
  end
326
338
 
327
339
  def gzip_report(report)
328
- puts ['==>'.green, 'Gzipping contents'].join(' ')
340
+ puts [green('==>'), 'Gzipping contents'].join(' ')
329
341
 
330
342
  io = StringIO.new
331
343
  gzip = Zlib::GzipWriter.new(io)
@@ -337,6 +349,7 @@ class SimpleCov::Formatter::Codecov
337
349
 
338
350
  def upload_to_codecov(ci, report)
339
351
  url = ENV['CODECOV_URL'] || 'https://codecov.io'
352
+ is_enterprise = url != 'https://codecov.io'
340
353
 
341
354
  params = build_params(ci)
342
355
  params_secret_token = params.clone
@@ -350,12 +363,15 @@ class SimpleCov::Formatter::Codecov
350
363
  report['params'] = params
351
364
  report['query'] = query
352
365
 
353
- puts ['==>'.green, 'Uploading reports'].join(' ')
366
+ puts [green('==>'), 'Uploading reports'].join(' ')
354
367
  puts " url: #{url}"
355
368
  puts " query: #{query_without_token}"
356
369
 
357
- response = upload_to_v4(url, gzipped_report, query, query_without_token)
358
- return false if response == false
370
+ response = false
371
+ unless is_enterprise
372
+ response = upload_to_v4(url, gzipped_report, query, query_without_token)
373
+ return false if response == false
374
+ end
359
375
 
360
376
  response || upload_to_v2(url, gzipped_report, query, query_without_token)
361
377
  end
@@ -365,7 +381,7 @@ class SimpleCov::Formatter::Codecov
365
381
  https = Net::HTTP.new(uri.host, uri.port)
366
382
  https.use_ssl = !url.match(/^https/).nil?
367
383
 
368
- puts ['-> '.green, 'Pinging Codecov'].join(' ')
384
+ puts [green('-> '), 'Pinging Codecov'].join(' ')
369
385
  puts "#{url}#{uri.path}?#{query_without_token}"
370
386
 
371
387
  req = Net::HTTP::Post.new(
@@ -378,13 +394,13 @@ class SimpleCov::Formatter::Codecov
378
394
  )
379
395
  response = retry_request(req, https)
380
396
  if !response&.code || response.code == '400'
381
- puts response&.body&.red
397
+ puts red(response&.body)
382
398
  return false
383
399
  end
384
400
 
385
401
  reports_url = response.body.lines[0]
386
402
  s3target = response.body.lines[1]
387
- puts ['-> '.green, 'Uploading to'].join(' ')
403
+ puts [green('-> '), 'Uploading to'].join(' ')
388
404
  puts s3target
389
405
 
390
406
  uri = URI(s3target)
@@ -399,7 +415,7 @@ class SimpleCov::Formatter::Codecov
399
415
  )
400
416
  req.body = report
401
417
  res = retry_request(req, https)
402
- if res.body == ''
418
+ if res&.body == ''
403
419
  {
404
420
  'uploaded' => true,
405
421
  'url' => reports_url,
@@ -409,8 +425,8 @@ class SimpleCov::Formatter::Codecov
409
425
  'message' => 'Coverage reports upload successfully'
410
426
  }.to_json
411
427
  else
412
- puts ['-> '.black, 'Could not upload reports via v4 API, defaulting to v2'].join(' ')
413
- puts res.body.red
428
+ puts [black('-> '), 'Could not upload reports via v4 API, defaulting to v2'].join(' ')
429
+ puts red(res&.body || 'nil')
414
430
  nil
415
431
  end
416
432
  end
@@ -420,12 +436,13 @@ class SimpleCov::Formatter::Codecov
420
436
  https = Net::HTTP.new(uri.host, uri.port)
421
437
  https.use_ssl = !url.match(/^https/).nil?
422
438
 
423
- puts ['-> '.green, 'Uploading to Codecov'].join(' ')
424
- puts "#{url}/#{uri.path}?#{query_without_token}"
439
+ puts [green('-> '), 'Uploading to Codecov'].join(' ')
440
+ puts "#{url}#{uri.path}?#{query_without_token}"
425
441
 
426
442
  req = Net::HTTP::Post.new(
427
443
  "#{uri.path}?#{query}",
428
444
  {
445
+ 'Accept' => 'application/json',
429
446
  'Content-Encoding' => 'gzip',
430
447
  'Content-Type' => 'text/plain',
431
448
  'X-Content-Encoding' => 'gzip'
@@ -440,7 +457,7 @@ class SimpleCov::Formatter::Codecov
440
457
  if report['result']['uploaded']
441
458
  puts " View reports at #{report['result']['url']}"
442
459
  else
443
- puts ' X> Failed to upload coverage reports'.red
460
+ puts red(' X> Failed to upload coverage reports')
444
461
  end
445
462
  end
446
463
 
@@ -492,16 +509,21 @@ class SimpleCov::Formatter::Codecov
492
509
  ].freeze
493
510
 
494
511
  invalid_directories = [
495
- 'node_modules/'
512
+ 'node_modules/',
513
+ 'public/',
514
+ 'storage/',
515
+ 'tmp/',
516
+ 'vendor/'
496
517
  ]
497
518
 
498
- puts ['==>'.green, 'Appending file network'].join(' ')
519
+ puts [green('==>'), 'Appending file network'].join(' ')
499
520
  network = []
500
521
  Dir['**/*'].keep_if do |file|
501
- if File.file?(file) && !file.end_with?(*invalid_file_types) && !file.include?(*invalid_directories)
522
+ if File.file?(file) && !file.end_with?(*invalid_file_types) && invalid_directories.none? { |dir| file.include?(dir) }
502
523
  network.push(file)
503
524
  end
504
525
  end
526
+
505
527
  network.push('<<<<<< network')
506
528
  network
507
529
  end
@@ -581,4 +603,17 @@ class SimpleCov::Formatter::Codecov
581
603
 
582
604
  true
583
605
  end
606
+
607
+ # Convenience color methods
608
+ def black(str)
609
+ str.nil? ? '' : "\e[30m#{str}\e[0m"
610
+ end
611
+
612
+ def red(str)
613
+ str.nil? ? '' : "\e[31m#{str}\e[0m"
614
+ end
615
+
616
+ def green(str)
617
+ str.nil? ? '' : "\e[32m#{str}\e[0m"
618
+ end
584
619
  end
@@ -6,7 +6,19 @@ class TestCodecov < Minitest::Test
6
6
  CI = SimpleCov::Formatter::Codecov.new.detect_ci
7
7
 
8
8
  REALENV =
9
- if CI == SimpleCov::Formatter::Codecov::GITHUB
9
+ if CI == SimpleCov::Formatter::Codecov::CIRCLE
10
+ {
11
+ 'CIRCLECI' => ENV['CIRCLECI'],
12
+ 'CIRCLE_BUILD_NUM' => ENV['CIRCLE_BUILD_NUM'],
13
+ 'CIRCLE_NODE_INDEX' => ENV['CIRCLE_NODE_INDEX'],
14
+ 'CIRCLE_PROJECT_REPONAME' => ENV['CIRCLE_PROJECT_REPONAME'],
15
+ 'CIRCLE_PROJECT_USERNAME' => ENV['CIRCLE_PROJECT_USERNAME'],
16
+ 'CIRCLE_REPOSITORY_URL' => ENV['CIRCLE_REPOSITORY_URL'],
17
+ 'CIRCLE_PR_NUMBER' => ENV['CIRCLE_PR_NUMBER'],
18
+ 'CIRCLE_BRANCH' => ENV['CIRCLE_BRANCH'],
19
+ 'CIRCLE_SHA1' => ENV['CIRCLE_SHA1']
20
+ }
21
+ elsif CI == SimpleCov::Formatter::Codecov::GITHUB
10
22
  {
11
23
  'GITHUB_ACTIONS' => ENV['GITHUB_ACTIONS'],
12
24
  'GITHUB_HEAD_REF' => ENV['GITHUB_HEAD_REF'],
@@ -51,6 +63,7 @@ class TestCodecov < Minitest::Test
51
63
  end
52
64
 
53
65
  def upload(success = true)
66
+ WebMock.enable!
54
67
  formatter = SimpleCov::Formatter::Codecov.new
55
68
  result = stub('SimpleCov::Result', files: [
56
69
  stub_file('/path/lib/something.rb', [1, 0, 0, nil, 1, nil]),
@@ -67,7 +80,7 @@ class TestCodecov < Minitest::Test
67
80
  end
68
81
 
69
82
  def success_stubs
70
- stub_request(:post, %r{https:\/\/codecov.io\/upload})
83
+ stub_request(:post, %r{https:\/\/codecov.io\/upload\/v4})
71
84
  .to_return(
72
85
  status: 200,
73
86
  body: "https://codecov.io/gh/fake\n" \
@@ -92,6 +105,7 @@ class TestCodecov < Minitest::Test
92
105
 
93
106
  def setup
94
107
  ENV['CI'] = nil
108
+ ENV['CIRCLECI'] = nil
95
109
  ENV['GITHUB_ACTIONS'] = nil
96
110
  ENV['TRAVIS'] = nil
97
111
  end
@@ -153,6 +167,12 @@ class TestCodecov < Minitest::Test
153
167
  ENV['CIRCLE_PROJECT_USERNAME'] = nil
154
168
  ENV['CIRCLE_SHA1'] = nil
155
169
  ENV['CIRCLECI'] = nil
170
+ ENV['CODEBUILD_CI'] = nil
171
+ ENV['CODEBUILD_BUILD_ID'] = nil
172
+ ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = nil
173
+ ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = nil
174
+ ENV['CODEBUILD_SOURCE_VERSION'] = nil
175
+ ENV['CODEBUILD_SOURCE_REPO_URL'] = nil
156
176
  ENV['CODECOV_ENV'] = nil
157
177
  ENV['CODECOV_SLUG'] = nil
158
178
  ENV['CODECOV_TOKEN'] = nil
@@ -215,6 +235,23 @@ class TestCodecov < Minitest::Test
215
235
  assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
216
236
  end
217
237
 
238
+ def test_enterprise
239
+ stub = stub_request(:post, %r{https:\/\/example.com\/upload\/v2})
240
+ .to_return(
241
+ status: 200,
242
+ body: "{\"id\": \"12345678-1234-abcd-ef12-1234567890ab\", \"message\": \"Coverage reports upload successfully\", \"meta\": { \"status\": 200 }, \"queued\": true, \"uploaded\": true, \"url\": \"https://example.com/github/codecov/codecov-bash/commit/2f6b51562b93e72c610671644fe2a303c5c0e8e5\"}"
243
+ )
244
+
245
+ ENV['CODECOV_URL'] = 'https://example.com'
246
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
247
+ result = upload
248
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
249
+ assert_equal('12345678-1234-abcd-ef12-1234567890ab', result['result']['id'])
250
+ branch = `git rev-parse --abbrev-ref HEAD`.strip
251
+ assert_equal(branch != 'HEAD' ? branch : 'master', result['params'][:branch])
252
+ assert_equal(`git rev-parse HEAD`.strip, result['params'][:commit])
253
+ end
254
+
218
255
  def test_travis
219
256
  ENV['CI'] = 'true'
220
257
  ENV['TRAVIS'] = 'true'
@@ -581,6 +618,27 @@ class TestCodecov < Minitest::Test
581
618
  assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
582
619
  end
583
620
 
621
+ def test_codebuild
622
+ ENV['CODEBUILD_CI'] = "true"
623
+ ENV['CODEBUILD_BUILD_ID'] = "codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3"
624
+ ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = 'd653b934ed59c1a785cc1cc79d08c9aaa4eba73b'
625
+ ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = 'refs/heads/master'
626
+ ENV['CODEBUILD_SOURCE_VERSION'] = 'pr/123'
627
+ ENV['CODEBUILD_SOURCE_REPO_URL'] = 'https://github.com/owner/repo.git'
628
+ ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
629
+
630
+ result = upload
631
+
632
+ assert_equal("codebuild", result['params'][:service])
633
+ assert_equal("d653b934ed59c1a785cc1cc79d08c9aaa4eba73b", result['params'][:commit])
634
+ assert_equal("codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3", result['params'][:build])
635
+ assert_equal("codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3", result['params'][:job])
636
+ assert_equal("owner/repo", result['params'][:slug])
637
+ assert_equal("master", result['params'][:branch])
638
+ assert_equal("123", result['params'][:pr])
639
+ assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
640
+ end
641
+
584
642
  def test_filenames_are_shortened_correctly
585
643
  ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
586
644
 
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codecov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - codecov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-18 00:00:00.000000000 Z
11
+ date: 2020-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: colorize
14
+ name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: json
28
+ name: simplecov
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,13 +39,13 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: simplecov
42
+ name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :runtime
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest
56
+ name: minitest-ci
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="