circleci-coverage_reporter 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27d4c9b0082fd4b0d64fe44a189c3de5264df6d9
4
- data.tar.gz: 9913f567b1631f239340409dd5dafc6b120c6892
3
+ metadata.gz: 7043f473068bec23109876b4e38bf8aca96410f0
4
+ data.tar.gz: bfeb330edb4de8f150cb1fd4bd0183d33fee6720
5
5
  SHA512:
6
- metadata.gz: 8eb86412bcf79cdb7582697158b9866492ab895cfd11ceb09380eb323583273b16cc4114160e6e28043bc54d4537478532bb7f383e1b94e7e8cb4633854873f6
7
- data.tar.gz: 0123ffcd0dbaa933b133e9d5c38acd0abdf55be4bb5512184184d56f858a33583b33ca9a8af5f759e18c67477faba227984a8d9c6165dbd98c6c0e42ae8c16b8
6
+ metadata.gz: 32ade229dc33219e38c324737c98d8600b70c2bb4a792a486274cd16ccd1e7e0ce1b0ffc886a3251aa4b4a6ac440bed6dcdbad5b8e9d65dd3478e6413550a0f0
7
+ data.tar.gz: ca44ef9e3ca8faebec5b3510a48bb9142ebac517b66727373445d71e2f96c0d57fac9c4e112827827408e3ffd4d05ec0ba678734b6fbd1fda8546d2332ff04c3
data/.rubocop.yml CHANGED
@@ -4,6 +4,9 @@ AllCops:
4
4
  Metrics/LineLength:
5
5
  Max: 125
6
6
 
7
+ Style/AndOr:
8
+ EnforcedStyle: conditionals
9
+
7
10
  Style/Documentation:
8
11
  Enabled: false
9
12
 
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.3] - 2017-03-12
10
+ ### Fixed
11
+ - Raise `RequestError` if some API reqeust fails
12
+ - Show `NaN` if .last_run.json does not exist
13
+
7
14
  ## [0.1.2] - 2017-03-12
8
15
  ### Fixed
9
16
 
@@ -19,5 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
19
26
 
20
27
  - Initial release
21
28
 
29
+ [Unreleased]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.3...HEAD
30
+ [0.1.3]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.2...v0.1.3
22
31
  [0.1.2]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.1...v0.1.2
23
32
  [0.1.1]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.0...v0.1.1
data/README.md CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  **CircleCI::CoverageReporter** reports test coverage to your GitHub repository.
6
6
 
7
+ ## Example
8
+
9
+ ![image](https://cloud.githubusercontent.com/assets/96157/23824715/e16f0eac-06be-11e7-81e8-f818b14a52b4.png)
10
+
7
11
  ## Getting started
8
12
 
9
13
  1. Add CircleCI::CoverageReporter to your `Gemfile` and `bundle install`:
@@ -18,14 +22,14 @@
18
22
  require 'circleci/coverage_reporter/rake_task' if ENV['CIRCLECI']
19
23
  ```
20
24
 
21
- 3. Issue CircleCI and GitHub token and add them to build environment variables with the following nameing convention:
25
+ 3. Issue CircleCI and GitHub tokens and add them to build environment variables as follows:
22
26
 
23
27
  Name | Value
24
28
  -----------------------------------|----------------------------------------------------------------
25
29
  `COVERAGE_REPORTER_CIRCLECI_TOKEN` | CircleCI API token with "view-builds" scope
26
30
  `COVERAGE_REPORTER_VCS_TOKEN` | GitHub personal access token with "repo" or "public_repo" scope
27
31
 
28
- 4. Add the following steop to your `circle.yml`:
32
+ 4. Add the following step to your `circle.yml`:
29
33
 
30
34
  ```yaml
31
35
  test:
@@ -33,23 +37,41 @@
33
37
  - bundle exec circleci:report_coverage
34
38
  ```
35
39
 
36
- ## Example
40
+ ## Run manually
37
41
 
38
- ![image](https://cloud.githubusercontent.com/assets/96157/23824715/e16f0eac-06be-11e7-81e8-f818b14a52b4.png)
39
-
40
- ## Configuring CircleCI::CoverageReporter
42
+ You must configure `circleci_token` and `vcr_token` before `CircleCI::CoverageReporter.run`:
41
43
 
42
44
  ```ruby
43
45
  CircleCI::CoverageReporter.configure do |config|
44
46
  config.circleci_token = YOUR_CIRCLECI_API_TOKEN
45
47
  config.vcr_token = YOUR_GITHUB_PERSONAL_ACCESS_TOKEN
46
48
  end
49
+
50
+ CircleCI::CoverageReporter.run
47
51
  ```
48
52
 
49
- then:
53
+ ## Reporters
54
+ ### SimpleCov
55
+
56
+ `CircleCI::CoverageReporter::SimpleCov::Reporter` handles coverage files generated by
57
+ [SimpleCov](https://github.com/colszowka/simplecov). It is used by default.
58
+
59
+ It expects that coverage files are located in `$CIRCLE_ARTIFACTS/coverage` directory:
50
60
 
51
61
  ```ruby
52
- CircleCI::CoverageReporter.run
62
+ # spec/spec_helper.rb
63
+ require 'simplecov'
64
+ # Save to CircleCI's artifacts directory if we're on CircleCI
65
+ SimpleCov.coverage_dir(File.join(ENV['CIRCLE_ARTIFACTS'], 'coverage')) if ENV['CIRCLECI']
66
+ SimpleCov.start
67
+ ```
68
+
69
+ If you put files in directory, say `$CIRCLE_ARTIFACTS/foo/bar`, you have to set reporter as follows:
70
+
71
+ ```ruby
72
+ CircleCI::CoverageReporter.configure do |config|
73
+ config.reporters = [CircleCI::CoverageReporter::SimpleCov::Reporter.new('foo/bar')]
74
+ end
53
75
  ```
54
76
 
55
77
  ## License
@@ -2,6 +2,7 @@ require 'faraday'
2
2
 
3
3
  require_relative 'artifact'
4
4
  require_relative 'build'
5
+ require_relative 'errors'
5
6
 
6
7
  module CircleCI
7
8
  module CoverageReporter
@@ -18,17 +19,23 @@ module CircleCI
18
19
 
19
20
  # @param build_number [Integer, nil]
20
21
  # @return [Build, nil]
22
+ # @raise [RequestError]
21
23
  def single_build(build_number)
22
24
  return unless build_number
23
- create_build(JSON.parse(get(single_build_url(build_number)).body))
25
+ resp = get(single_build_url(build_number))
26
+ body = JSON.parse(resp.body)
27
+ raise RequestError.new(body['message'], resp) unless resp.success?
28
+ create_build(body)
24
29
  end
25
30
 
26
31
  # @param build_number [Integer]
27
32
  # @return [Array<Artifact>]
33
+ # @raise [RequestError]
28
34
  def artifacts(build_number)
29
- JSON.parse(get(artifacts_url(build_number)).body).map do |hash|
30
- Artifact.new(hash['path'], hash['pretty_path'], hash['node_index'], hash['url'])
31
- end
35
+ resp = get(artifacts_url(build_number))
36
+ body = JSON.parse(resp.body)
37
+ raise RequestError.new(body['message'], resp) unless resp.success?
38
+ body.map(&method(:create_artifact))
32
39
  end
33
40
 
34
41
  # @param revision [String]
@@ -64,8 +71,12 @@ module CircleCI
64
71
 
65
72
  # @param branch [String, nil]
66
73
  # @return [Array<Build>]
74
+ # @raise [RequestError]
67
75
  def recent_builds(branch)
68
- JSON.parse(get(recent_builds_url(branch), limit: 100).body).map(&method(:create_build))
76
+ resp = get(recent_builds_url(branch), limit: 100)
77
+ body = JSON.parse(resp.body)
78
+ raise RequestError.new(body['message'], resp) unless resp.success?
79
+ body.map(&method(:create_build))
69
80
  end
70
81
 
71
82
  # @param branch [String, nil]
@@ -93,6 +104,12 @@ module CircleCI
93
104
  ].join('/')
94
105
  end
95
106
 
107
+ # @param hash [Hash]
108
+ # @return [Artifact]
109
+ def create_artifact(hash)
110
+ Artifact.new(hash['path'], hash['pretty_path'], hash['node_index'], hash['url'])
111
+ end
112
+
96
113
  # @param hash [Hash]
97
114
  # @return [Build]
98
115
  def create_build(hash)
@@ -0,0 +1,17 @@
1
+ module CircleCI
2
+ module CoverageReporter
3
+ class Error < StandardError; end
4
+
5
+ class RequestError < Error
6
+ attr_reader :resp
7
+
8
+ # @param message [String]
9
+ # @param resp [Faraday::Response]
10
+ def initialize(message, resp)
11
+ @message = message
12
+ @resp = resp
13
+ super(message)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative 'abstract_vcs_client'
2
+ require_relative 'errors'
2
3
 
3
4
  module CircleCI
4
5
  module CoverageReporter
@@ -6,7 +7,18 @@ module CircleCI
6
7
  # @note Implement {AbstractVCSClient#create_comment}
7
8
  # @param reports [Array<Report>]
8
9
  # @return [void]
10
+ # @raise [RequestError]
9
11
  def create_comment(reports)
12
+ resp = request(reports)
13
+ body = JSON.parse(resp.body)
14
+ raise RequestError.new(body['message'], resp) unless resp.success?
15
+ end
16
+
17
+ private
18
+
19
+ # @param reports [Array<Report>]
20
+ # @return [Faraday::Response]
21
+ def request(reports)
10
22
  Faraday.new(url: 'https://api.github.com').post do |req|
11
23
  req.url ['/repos', configuration.project, 'commits', configuration.current_revision, 'comments'].join('/')
12
24
  req.headers['Authorization'] = "token #{token}"
@@ -15,8 +27,6 @@ module CircleCI
15
27
  end
16
28
  end
17
29
 
18
- private
19
-
20
30
  # @return [Client]
21
31
  def configuration
22
32
  CoverageReporter.client.configuration
@@ -59,7 +59,9 @@ module CircleCI
59
59
  # @return [String]
60
60
  def diff(after_result, before_result)
61
61
  value = (after_result.coverage - before_result.coverage).round(2)
62
- if value.positive?
62
+ if value.nan?
63
+ 'NaN'
64
+ elsif value.positive?
63
65
  "+#{value}"
64
66
  elsif value.negative?
65
67
  value.to_s
@@ -5,12 +5,24 @@ module CircleCI
5
5
  class Runner
6
6
  # @return [void]
7
7
  def run
8
+ dump
8
9
  reports = reporters.map { |reporter| reporter.report(base_build, previous_build) }
9
10
  vcs_client.create_comment(reports)
10
11
  end
11
12
 
12
13
  private
13
14
 
15
+ # @return [void]
16
+ def dump
17
+ puts <<-EOF
18
+ Runner | Value
19
+ ------------------|-----------------------------------------------------------------------------------
20
+ base_build | #{base_build.inspect}
21
+ base_build_number | #{base_build_number.inspect}
22
+ previous_build | #{previous_build.inspect}
23
+ EOF
24
+ end
25
+
14
26
  # @return [AbstractVCSClient]
15
27
  def vcs_client
16
28
  case client.configuration.vcs_type
@@ -12,13 +12,15 @@ module CircleCI
12
12
  # @note Implement {AbstractResult#coverage}
13
13
  # @return [Float]
14
14
  def coverage
15
- JSON.parse(find_artifact('.last_run.json').body)['result']['covered_percent']
15
+ last_run_json = find_artifact('.last_run.json') or return Float::NaN
16
+ JSON.parse(last_run_json.body)['result']['covered_percent']
16
17
  end
17
18
 
18
19
  # @note Implement {AbstractResult#url}
19
20
  # @return [String]
20
21
  def url
21
- find_artifact('index.html').url
22
+ index_html = find_artifact('index.html') or return '#'
23
+ index_html.url
22
24
  end
23
25
 
24
26
  private
@@ -3,7 +3,7 @@ module CircleCI
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 2
6
+ PATCH = 3
7
7
 
8
8
  def self.to_s
9
9
  [MAJOR, MINOR, PATCH].join('.')
@@ -23,24 +23,26 @@ module CircleCI
23
23
 
24
24
  # @return [void]
25
25
  def self.run
26
- Runner.new.run
27
- rescue
28
26
  dump
29
- raise
27
+ Runner.new.run
30
28
  end
31
29
 
32
30
  # @return [void]
33
- def self.dump
31
+ def self.dump # rubocop:disable AbcSize
34
32
  puts <<-EOF
35
- CircleCI::CoverageReporter.configure do |config|
36
- config.base_revision = '#{configuration.base_revision}'
37
- config.current_build_number = '#{configuration.current_build_number}'
38
- config.current_revision = '#{configuration.current_revision}'
39
- config.previous_build_number = '#{configuration.previous_build_number}'
40
- config.repository_name = '#{configuration.repository_name}'
41
- config.user_name = '#{configuration.user_name}'
42
- config.vcs_type = '#{configuration.vcs_type}'
43
- end
33
+ Configuration | Value
34
+ ----------------------|----------------------------------------------------------------------------
35
+ artifacts_dir | #{configuration.artifacts_dir.inspect}
36
+ base_revision | #{configuration.base_revision.inspect}
37
+ circleci_token | #{configuration.circleci_token[-4..-1].rjust(40, '*').inspect}
38
+ current_build_number | #{configuration.current_build_number.inspect}
39
+ current_revision | #{configuration.current_revision.inspect}
40
+ previous_build_number | #{configuration.previous_build_number.inspect}
41
+ reporters | #{configuration.reporters.inspect}
42
+ repository_name | #{configuration.repository_name.inspect}
43
+ user_name | #{configuration.user_name.inspect}
44
+ vcs_token | #{configuration.vcs_token[-4..-1].rjust(40, '*').inspect}
45
+ vcs_type | #{configuration.vcs_type.inspect}
44
46
  EOF
45
47
  end
46
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circleci-coverage_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuku Takahashi
@@ -64,6 +64,7 @@ files:
64
64
  - lib/circleci/coverage_reporter/build.rb
65
65
  - lib/circleci/coverage_reporter/client.rb
66
66
  - lib/circleci/coverage_reporter/configuration.rb
67
+ - lib/circleci/coverage_reporter/errors.rb
67
68
  - lib/circleci/coverage_reporter/github_client.rb
68
69
  - lib/circleci/coverage_reporter/rake_task.rb
69
70
  - lib/circleci/coverage_reporter/report.rb