circleci-coverage_reporter 0.2.0 → 0.3.0

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: 5184426bc67f4c8e7217a623eb7674643204dbc0
4
- data.tar.gz: b060f3721644ebf5d7a0704efbf86a1683923a90
3
+ metadata.gz: e70ba98da0da373e2022747216c6ebb4f383b9cb
4
+ data.tar.gz: 379e075fd8a0a361ce4ec08ec3aa74d65d84c024
5
5
  SHA512:
6
- metadata.gz: 536bc1d1c085a916efffd13a2c95a729a41c4072b5f8cfa80aa3e33909745bf3de4af84b258ce36a0b0e1210a0d0249e496ef8f2f029ff4f2635ad7d9ce63b48
7
- data.tar.gz: 5452ccd5386b7a907a0c1ae2c22a31678c250e6a829ee8b36b14fc6eacdf0abb829fb69ab7cc0fc198b032d7ce443a51d5cfa35f7317782347b72d497c8f9fa9
6
+ metadata.gz: 42b3e6d604b30c7df6fc950464ed002857f859076b5a8b4fba9319f97eb51e4655014d00087a4f91963c2a02622f682787811759cfc8ad1512b9325887b20b46
7
+ data.tar.gz: d711d87cc5ca74dff09051b3eddaed3b2ccead6c3e77dad3e3016103acd64c2f12c0fd8b0dd567460c1a1a6e526ce2ff49cf88145701396764c91cef52ccbf74
data/.rubocop.yml CHANGED
@@ -4,6 +4,10 @@ AllCops:
4
4
  Metrics/LineLength:
5
5
  Max: 125
6
6
 
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'spec/**/*.rb'
10
+
7
11
  Style/AndOr:
8
12
  EnforcedStyle: conditionals
9
13
 
data/CHANGELOG.md CHANGED
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.0] - 2017-03-16
10
+ ### Added
11
+ - Add template configuration
12
+
13
+ ### Changed
14
+ - Remove `CircleCI::CoverageReporter::Client#configuration`
15
+
16
+ ### Fixed
17
+ - Run each example in sandbox
18
+
9
19
  ## [0.2.0] - 2017-03-14
10
20
  ### Added
11
21
  - Support [Flow](flowtype.org) coverage reported by [flow-coverage-report](https://github.com/rpl/flow-coverage-report)
@@ -21,20 +31,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
21
31
 
22
32
  ## [0.1.2] - 2017-03-12
23
33
  ### Fixed
24
-
25
34
  - Set `nil` to previous_build_number if CIRCLE_PREVIOUS_BUILD_NUM environment variable is empty
26
35
 
27
36
  ## [0.1.1] - 2017-03-12
28
37
  ### Fixed
29
-
30
38
  - Ignore base_build if it is on master branch
31
39
 
32
40
  ## 0.1.0 - 2017-03-12
33
41
  ### Added
34
-
35
42
  - Initial release
36
43
 
37
- [Unreleased]: https://github.com/increments/circleci-coverage_reporter/compare/v0.2.0...HEAD
44
+ [Unreleased]: https://github.com/increments/circleci-coverage_reporter/compare/v0.3.0...HEAD
45
+ [0.3.0]: https://github.com/increments/circleci-coverage_reporter/compare/v0.2.0...v0.3.0
38
46
  [0.2.0]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.3...v0.2.0
39
47
  [0.1.3]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.2...v0.1.3
40
48
  [0.1.2]: https://github.com/increments/circleci-coverage_reporter/compare/v0.1.1...v0.1.2
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # CircleCI::CoverageReporter
2
2
 
3
3
  [![CircleCI](https://circleci.com/gh/increments/circleci-coverage_reporter.svg?style=svg)](https://circleci.com/gh/increments/circleci-coverage_reporter)
4
+ [![Gem](https://img.shields.io/gem/v/circleci-coverage_reporter.svg)](https://rubygems.org/gems/circleci-coverage_reporter)
4
5
 
5
6
  **CircleCI::CoverageReporter** reports test coverage to your GitHub repository.
6
7
 
@@ -94,6 +95,23 @@ CircleCI::CoverageReporter.configure do |config|
94
95
  end
95
96
  ```
96
97
 
98
+ ## Configuring
99
+ ### Template
100
+
101
+ You can change template for comment body in ERB format with `reports` (an array of
102
+ [`Report`](http://www.rubydoc.info/gems/circleci-coverage_reporter/CircleCI/CoverageReporter/Report))
103
+ and `vcs_type` (always `"github"`).
104
+
105
+ ```rb
106
+ CircleCI::CoverageReporter.configure do |config|
107
+ config.tempalte = <<-'ERB'
108
+ <%- reports.each do |report| -%>
109
+ [<%= report.type %>](<%= report.url %>) <%= report.coverage %>%
110
+ <%- end -%>
111
+ ERB
112
+ end
113
+ ```
114
+
97
115
  ## License
98
116
 
99
117
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -12,7 +12,7 @@ module CircleCI
12
12
 
13
13
  # @return [Boolean] whether it is active
14
14
  def active?
15
- File.directory?(File.join(CoverageReporter.configuration.artifacts_dir, path))
15
+ File.directory?(File.join(configuration.artifacts_dir, path))
16
16
  end
17
17
 
18
18
  # @param base_build [Build, nil]
@@ -21,7 +21,7 @@ module CircleCI
21
21
  def report(base_build, previous_build)
22
22
  Report.new(
23
23
  self,
24
- current: create_current_result,
24
+ create_current_result,
25
25
  base: create_build_result(base_build),
26
26
  previous: create_build_result(previous_build)
27
27
  )
@@ -47,9 +47,9 @@ module CircleCI
47
47
  raise NotImplementedError
48
48
  end
49
49
 
50
- # @return [Client]
51
- def client
52
- CoverageReporter.client
50
+ # @return [Configuration]
51
+ def configuration
52
+ CoverageReporter.configuration
53
53
  end
54
54
  end
55
55
  end
@@ -7,9 +7,9 @@ module CircleCI
7
7
  @token = token
8
8
  end
9
9
 
10
- # @param reports [Array<Report>]
10
+ # @param body [String]
11
11
  # @return [void]
12
- def create_comment(reports) # rubocop:disable Lint/UnusedMethodArgument
12
+ def create_comment(body) # rubocop:disable Lint/UnusedMethodArgument
13
13
  raise NotImplementedError
14
14
  end
15
15
 
@@ -9,14 +9,6 @@ module CircleCI
9
9
  class Client
10
10
  CIRCLECI_ENDPOINT = 'https://circleci.com/api/v1.1'.freeze
11
11
 
12
- # @return [Configuration]
13
- attr_reader :configuration
14
-
15
- # @param configuration [Configuration]
16
- def initialize(configuration)
17
- @configuration = configuration
18
- end
19
-
20
12
  # @param build_number [Integer, nil]
21
13
  # @return [Build, nil]
22
14
  # @raise [RequestError]
@@ -56,6 +48,11 @@ module CircleCI
56
48
 
57
49
  private
58
50
 
51
+ # @return [Configuration]
52
+ def configuration
53
+ CoverageReporter.configuration
54
+ end
55
+
59
56
  # @param build_number [Integer]
60
57
  # @return [String]
61
58
  def artifacts_url(build_number)
@@ -8,11 +8,25 @@ module CircleCI
8
8
  SimpleCov::Reporter.new,
9
9
  Flow::Reporter.new
10
10
  ].freeze
11
+ DEFAULT_TEMPLATE = <<-'ERB'.freeze
12
+ <%- reports.each do |report| -%>
13
+ <%
14
+ link = "[#{report.reporter.name}](#{report.current_result.url})"
15
+ emoji = report.base_diff.nil? ? nil : report.base_diff.positive? ? ' :chart_with_upwards_trend:' : ' :chart_with_downwards_trend:'
16
+ base_progress = report.base_diff ? "[master](#{report.base_result.url}): #{report.pretty_base_diff}" : nil
17
+ branch_progress = report.branch_diff ? "[previous](#{report.previous_result.url}): #{report.pretty_branch_diff}" : nil
18
+ progresses = [base_progress, branch_progress].compact
19
+ progress = progresses.empty? ? nil : " (#{progresses.join(', ')})"
20
+ -%>
21
+ <%= link %>: <%= report.current_result.coverage %>%<%= emoji %><%= progress %>
22
+ <%- end -%>
23
+ ERB
24
+ DEFAULT_TEMPLATE_TRIM_MODE = '-'.freeze
11
25
  DEFAULT_VCS_TYPE = 'github'.freeze
12
26
 
13
27
  attr_accessor :circleci_token, :vcs_token
14
28
  attr_writer :artifacts_dir, :base_revision, :current_build_number, :current_revision, :previous_build_number,
15
- :reporters, :repository_name, :user_name, :vcs_type
29
+ :reporters, :repository_name, :template, :template_safe_mode, :template_safe_mode, :user_name, :vcs_type
16
30
 
17
31
  # @return [String]
18
32
  def project
@@ -59,6 +73,16 @@ module CircleCI
59
73
  @repository_name ||= ENV['CIRCLE_PROJECT_REPONAME']
60
74
  end
61
75
 
76
+ # @return [String]
77
+ def template
78
+ @template ||= DEFAULT_TEMPLATE
79
+ end
80
+
81
+ # @return [String, nil]
82
+ def template_trim_mode
83
+ @template_trim_mode ||= DEFAULT_TEMPLATE_TRIM_MODE
84
+ end
85
+
62
86
  # @return [String]
63
87
  def user_name
64
88
  @user_name ||= ENV['CIRCLE_PROJECT_USERNAME']
@@ -38,9 +38,9 @@ module CircleCI
38
38
  File.join(configuration.artifacts_dir, path, name)
39
39
  end
40
40
 
41
- # @return [Client]
41
+ # @return [Configuration]
42
42
  def configuration
43
- CoverageReporter.client.configuration
43
+ CoverageReporter.configuration
44
44
  end
45
45
  end
46
46
  end
@@ -5,31 +5,30 @@ module CircleCI
5
5
  module CoverageReporter
6
6
  class GitHubClient < AbstractVCSClient
7
7
  # @note Implement {AbstractVCSClient#create_comment}
8
- # @param reports [Array<Report>]
8
+ # @param body [String]
9
9
  # @return [void]
10
10
  # @raise [RequestError]
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?
11
+ def create_comment(body)
12
+ resp = request(body)
13
+ raise RequestError.new(JSON.parse(resp.body)['message'], resp) unless resp.success?
15
14
  end
16
15
 
17
16
  private
18
17
 
19
- # @param reports [Array<Report>]
18
+ # @param body [String]
20
19
  # @return [Faraday::Response]
21
- def request(reports)
20
+ def request(body)
22
21
  Faraday.new(url: 'https://api.github.com').post do |req|
23
22
  req.url ['/repos', configuration.project, 'commits', configuration.current_revision, 'comments'].join('/')
24
23
  req.headers['Authorization'] = "token #{token}"
25
24
  req.headers['Content-Type'] = 'application/json'
26
- req.body = JSON.generate(body: reports.join("\n"))
25
+ req.body = JSON.generate(body: body)
27
26
  end
28
27
  end
29
28
 
30
- # @return [Client]
29
+ # @return [Configuration]
31
30
  def configuration
32
- CoverageReporter.client.configuration
31
+ CoverageReporter.configuration
33
32
  end
34
33
  end
35
34
  end
@@ -1,70 +1,79 @@
1
1
  module CircleCI
2
2
  module CoverageReporter
3
3
  class Report
4
+ # @return [AbstractReporter]
5
+ attr_reader :reporter
6
+
7
+ # @return [AbstractResult]
8
+ attr_reader :current_result
9
+
10
+ # @return [AbstractResult, nil]
11
+ attr_reader :base_result
12
+
13
+ # @return [AbstractResult, nil]
14
+ attr_reader :previous_result
15
+
4
16
  # @param reporter [AbstractReporter]
5
17
  # @param current [AbstractResult]
6
18
  # @param base [AbstractResult, nil]
7
19
  # @param previous [AbstractResult, nil]
8
- def initialize(reporter, current:, base:, previous:)
20
+ def initialize(reporter, current, base: nil, previous: nil)
9
21
  @reporter = reporter
10
22
  @current_result = current
11
23
  @base_result = base
12
24
  @previous_result = previous
13
25
  end
14
26
 
15
- # @return [String]
16
- def to_s
17
- "#{link}: #{current_result.coverage}%#{emoji}#{progress}"
27
+ # @return [String] coverage reporter name
28
+ def type
29
+ reporter.name
18
30
  end
19
31
 
20
- private
21
-
22
- attr_reader :reporter, :current_result, :base_result, :previous_result
32
+ # @return [Float] coverage percent of the current build result
33
+ def coverage
34
+ current_result.coverage
35
+ end
23
36
 
24
- # @return [String]
25
- def link
26
- "[#{reporter.name}](#{current_result.url})"
37
+ # @return [String] URL for current coverage build result
38
+ def url
39
+ current_result.url
27
40
  end
28
41
 
29
42
  # @return [String, nil]
30
- def emoji
31
- return unless base_result
32
- if current_result.coverage < base_result.coverage
33
- ' :chart_with_downwards_trend:'
34
- elsif current_result.coverage > base_result.coverage
35
- ' :chart_with_upwards_trend:'
36
- end
43
+ def pretty_base_diff
44
+ return unless base_diff
45
+ pretty_diff(base_diff.round(2))
37
46
  end
38
47
 
39
48
  # @return [String, nil]
40
- def progress
41
- elements = [base_progress, branch_progress].compact
42
- elements.empty? ? nil : " (#{elements.join(', ')})"
49
+ def pretty_branch_diff
50
+ return unless branch_diff
51
+ pretty_diff(branch_diff.round(2))
43
52
  end
44
53
 
45
- # @return [String, nil]
46
- def base_progress
54
+ # @return [Float, nil]
55
+ def base_diff
47
56
  return unless base_result
48
- "[master](#{base_result.url}): #{diff(current_result, base_result)}"
57
+ current_result.coverage - base_result.coverage
49
58
  end
50
59
 
51
- # @return [String, nil]
52
- def branch_progress
60
+ # @return [Float, nil]
61
+ def branch_diff
53
62
  return unless previous_result
54
- "[previous](#{previous_result.url}): #{diff(current_result, previous_result)}"
63
+ current_result.coverage - previous_result.coverage
55
64
  end
56
65
 
57
- # @param after_result [AbstractResult]
58
- # @param before_result [AbstractResult]
66
+ private
67
+
68
+ # @param diff [Float]
59
69
  # @return [String]
60
- def diff(after_result, before_result)
61
- value = (after_result.coverage - before_result.coverage).round(2)
62
- if value.nan?
70
+ def pretty_diff(diff)
71
+ if diff.nan?
63
72
  'NaN'
64
- elsif value.positive?
65
- "+#{value}"
66
- elsif value.negative?
67
- value.to_s
73
+ elsif diff.positive?
74
+ "+#{diff}"
75
+ elsif diff.negative?
76
+ diff.to_s
68
77
  else
69
78
  '±0'
70
79
  end
@@ -0,0 +1,24 @@
1
+ require 'erb'
2
+
3
+ module CircleCI
4
+ module CoverageReporter
5
+ class ReportsRenderer
6
+ # @param reports [Array<Report>]
7
+ # @return [String]
8
+ def render(reports)
9
+ ERB.new(
10
+ CoverageReporter.configuration.template,
11
+ nil,
12
+ CoverageReporter.configuration.template_trim_mode
13
+ ).result(binding)
14
+ end
15
+
16
+ private
17
+
18
+ # @return [String]
19
+ def vcs_type
20
+ CoverageReporter.configuration.vcs_type
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative 'github_client'
2
+ require_relative 'reports_renderer'
2
3
 
3
4
  module CircleCI
4
5
  module CoverageReporter
@@ -6,7 +7,7 @@ module CircleCI
6
7
  # @return [void]
7
8
  def run
8
9
  reports = reporters.map { |reporter| reporter.report(base_build, previous_build) }
9
- vcs_client.create_comment(reports)
10
+ vcs_client.create_comment(reports_renderer.render(reports))
10
11
  end
11
12
 
12
13
  # @return [void]
@@ -22,11 +23,16 @@ previous_build | #{previous_build.inspect}
22
23
 
23
24
  private
24
25
 
26
+ # @return [ReportsRenderer]
27
+ def reports_renderer
28
+ ReportsRenderer.new
29
+ end
30
+
25
31
  # @return [AbstractVCSClient]
26
32
  def vcs_client
27
- case client.configuration.vcs_type
33
+ case configuration.vcs_type
28
34
  when 'github'
29
- GitHubClient.new(client.configuration.vcs_token)
35
+ GitHubClient.new(configuration.vcs_token)
30
36
  else
31
37
  raise NotImplementedError
32
38
  end
@@ -47,24 +53,29 @@ previous_build | #{previous_build.inspect}
47
53
  CoverageReporter.client
48
54
  end
49
55
 
56
+ # @return [Configuration]
57
+ def configuration
58
+ CoverageReporter.configuration
59
+ end
60
+
50
61
  # @return [String, nil]
51
62
  def base_revision
52
- client.configuration.base_revision
63
+ configuration.base_revision
53
64
  end
54
65
 
55
66
  # @return [Integer, nil]
56
67
  def previous_build_number
57
- client.configuration.previous_build_number
68
+ configuration.previous_build_number
58
69
  end
59
70
 
60
71
  # @return [Array<AbstractReporter>]
61
72
  def reporters
62
- client.configuration.reporters
73
+ configuration.reporters
63
74
  end
64
75
 
65
76
  # @return [Integer, nil]
66
77
  def base_build_number
67
- return if client.configuration.base_revision == client.configuration.current_revision
78
+ return if configuration.base_revision == configuration.current_revision
68
79
  @base_build_number ||= client.build_number_by_revision(base_revision, branch: 'master')
69
80
  end
70
81
  end
@@ -0,0 +1,32 @@
1
+ require_relative '../coverage_reporter'
2
+ require_relative 'client'
3
+ require_relative 'configuration'
4
+
5
+ module CircleCI
6
+ module CoverageReporter
7
+ # A sandbox isolates the enclosed code into an environment that looks 'new'
8
+ # meaning globally accessed objects are reset for the duration of the sandbox.
9
+ #
10
+ # @note This module is not normally available. You must require
11
+ # `circleci/coverage_reporter/sandbox` to load it.
12
+ module Sandbox
13
+ # Execute a provided block with CircleCI::CoverageReporter global objects(
14
+ # configuration, client) reset.
15
+ #
16
+ # @yield [Configuration]
17
+ # @return [void]
18
+ def self.sandboxed
19
+ orig_config = CoverageReporter.configuration
20
+ orig_client = CoverageReporter.client
21
+
22
+ CoverageReporter.configuration = Configuration.new
23
+ CoverageReporter.client = Client.new
24
+
25
+ yield CoverageReporter.configuration
26
+ ensure
27
+ CoverageReporter.configuration = orig_config
28
+ CoverageReporter.client = orig_client
29
+ end
30
+ end
31
+ end
32
+ end
@@ -38,9 +38,9 @@ module CircleCI
38
38
  File.join(configuration.artifacts_dir, path, name)
39
39
  end
40
40
 
41
- # @return [Client]
41
+ # @return [Configuration]
42
42
  def configuration
43
- CoverageReporter.client.configuration
43
+ CoverageReporter.configuration
44
44
  end
45
45
  end
46
46
  end
@@ -2,7 +2,7 @@ module CircleCI
2
2
  module CoverageReporter
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 2
5
+ MINOR = 3
6
6
  PATCH = 0
7
7
 
8
8
  def self.to_s
@@ -5,6 +5,12 @@ require_relative 'coverage_reporter/runner'
5
5
 
6
6
  module CircleCI
7
7
  module CoverageReporter
8
+ class << self
9
+ # Setters for shared global objects
10
+ # @api private
11
+ attr_writer :client, :configuration
12
+ end
13
+
8
14
  # @return [Configuration]
9
15
  def self.configuration
10
16
  @configuration ||= Configuration.new
@@ -12,7 +18,7 @@ module CircleCI
12
18
 
13
19
  # @return [Client]
14
20
  def self.client
15
- @client ||= Client.new(configuration)
21
+ @client ||= Client.new
16
22
  end
17
23
 
18
24
  # Yields the global configuration to a block.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circleci-coverage_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuku Takahashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-13 00:00:00.000000000 Z
11
+ date: 2017-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -72,7 +72,9 @@ files:
72
72
  - lib/circleci/coverage_reporter/github_client.rb
73
73
  - lib/circleci/coverage_reporter/rake_task.rb
74
74
  - lib/circleci/coverage_reporter/report.rb
75
+ - lib/circleci/coverage_reporter/reports_renderer.rb
75
76
  - lib/circleci/coverage_reporter/runner.rb
77
+ - lib/circleci/coverage_reporter/sandbox.rb
76
78
  - lib/circleci/coverage_reporter/simplecov/build_result.rb
77
79
  - lib/circleci/coverage_reporter/simplecov/current_result.rb
78
80
  - lib/circleci/coverage_reporter/simplecov/reporter.rb