danger-code_coverage 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +32 -0
- data/.rubocop.yml +3873 -0
- data/.travis.yml +34 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +109 -0
- data/Rakefile +28 -0
- data/danger-code_coverage.gemspec +41 -0
- data/lib/code_coverage/coverage_item.rb +13 -0
- data/lib/code_coverage/coverage_parser.rb +77 -0
- data/lib/code_coverage/gem_version.rb +5 -0
- data/lib/code_coverage/markdown_table.rb +53 -0
- data/lib/code_coverage/plugin.rb +155 -0
- data/lib/danger_code_coverage.rb +3 -0
- data/lib/danger_plugin.rb +3 -0
- data/sonar-project.properties +9 -0
- data/spec/assets/coverage_cobertura_python.json +519 -0
- data/spec/assets/coverage_cobertura_single.json +183 -0
- data/spec/assets/coverage_dot_package_single.json +189 -0
- data/spec/assets/coverage_jacoco.json +1004 -0
- data/spec/assets/coverage_jacoco_multiple_unordered.json +328 -0
- data/spec/assets/coverage_jacoco_no_conditional.json +238 -0
- data/spec/assets/coverage_jacoco_single.json +250 -0
- data/spec/assets/coverage_no_files.json +65 -0
- data/spec/assets/missing_json.html +7 -0
- data/spec/code_coverage_spec.rb +134 -0
- data/spec/coverage_parser_spec.rb +85 -0
- data/spec/spec_helper.rb +72 -0
- metadata +239 -0
data/.travis.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
cache:
|
4
|
+
directories:
|
5
|
+
- bundle
|
6
|
+
|
7
|
+
before_install:
|
8
|
+
- gem update --system
|
9
|
+
- gem install bundler
|
10
|
+
|
11
|
+
rvm:
|
12
|
+
- 2.3
|
13
|
+
- 2.4
|
14
|
+
- 2.5
|
15
|
+
- 2.6
|
16
|
+
|
17
|
+
addons:
|
18
|
+
sonarcloud:
|
19
|
+
organization: "kyaak-github"
|
20
|
+
token:
|
21
|
+
secure: "fNNFlZXguNUDr28aUFwNVXw/u1ftl4bf3xD0lXRNIhKzFKcBNEKMa2h8isggtg6l45ok0rml1G9W0FNFBloMAvhkOgxaAin1oRt9aGePXDikikM0dFUKRIPHOKyfzbpJboMfzCln5VG8+IZTMt7fUppkcbsw25pwtu34VJzJKRzeylCR4I5/MWEkZqB+4iwPGkp5oJFs5laMUvIyXO0xZFGPHar/9d21WF3pegXN1ih6gkWPibaAPpPn5DVbjpQJ6tN88089zhOIbqK3CN01xTTVIkA+2DMtvquIHshHkbC2AwbQ7VmQjVeRFKtYqJjyN8WoXIuUDUMlKrGmcExNi/TtxgheFc7JQxTCaE6SGN/bR8jUwSayCBmXg9K75OJncewOMJ4OkMF5MDYqrko9PpmN+uauHZAoAzm/DLYGb0LYzrD02ztk1gakcbRr6sKwneHvmArEgSjrhYHdpZHN/cBngDMa+npcb89EpZ7W8ItZozWvl25oX1fQ/GvDCH3SE3s7w5PNqMs8QpdRAqgqkEE8CTmdgbzTE0eW7cZ6y2FNaeHC87TplUseL42qRjmpdgVFfQOtDkaWgZXRCkPr3pml1cZ54UbHoEmVPXyw/zk0SwfLoS/5hi1uJ8yDacG+fAtM/nTxCv3Iug+/2O/0eHDEqa8vTpLwDxMZOdZabUE="
|
22
|
+
|
23
|
+
script:
|
24
|
+
- bundle exec rake spec
|
25
|
+
- sonar-scanner
|
26
|
+
|
27
|
+
deploy:
|
28
|
+
provider: rubygems
|
29
|
+
api_key:
|
30
|
+
secure: "aWKizbUpasC+1fVFBhGPHq8nFApCZKP87tSAlBl32Awh6gP1NicuUlUHkwArPxOS9iGLP6ZT/9YwD6OkZsjA5jGSikSUOHAh3Cz2iwbcgWYQlHWHyqikHehVS75rkAKBIOvmreesJ6ybiXwPksjjqOKW5wZFyXuqWWUFsc+StEcfsWyezwS50zndm3Z14Rtqh8bbuJIjknhGwXMxj3tnkbYnesDfhTf+7fy/hTNBSg/mt3D5UxeaKhL0nbbB/S10NMFBUlkdlPCxLn0lOaAC8bPvQkkhHbMWMlrBEOBGbx/CXuEdLfDchpFXm/5bT5+/FhW+jMxAZuqGuGxfZp4bCffU3WAKYt8rAw3HynsGR1EYJluGoP6U/KJVsmByMc6mOIzumjYbs6ZPrAoLbAIXCB9R62oyxeOOV4NSe/uLqA1bBvvMLA5EqU5ccng2Qtga2zvMf5zmWBAUMal4Oy0iGZgdseBaNkiBSyirPIzQ0DbQhc0pHh3pi0XtTDvyw/ZU1BMpobx8ZIjBXZD4Nt+YbtqVkMdEe9GD+tx0nkA5OGP+IaqYqhsuqdcsDCCp1t4O/1rGd7wxChIOnM7ajTT6m4EibzNpQH+UOUc21uQt5Bw+JGpVBtOcCIPf0cvCyEYdd1TNlaP6/xXhV8BzbMT9s5th2LHoTLSduBchJCNm+3c="
|
31
|
+
gem: danger-code_coverage
|
32
|
+
on:
|
33
|
+
branch: master
|
34
|
+
rvm: 2.6
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [0.0.1] - 2019-6-22
|
8
|
+
### Added
|
9
|
+
- Initial project
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2019 Martin Schwamberger <kyaak.dev@gmail.com>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
<h1 align="center">danger-code_coverage</h1>
|
2
|
+
|
3
|
+
<div align="center">
|
4
|
+
<!-- Sonar Cloud -->
|
5
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
6
|
+
<img src="https://sonarcloud.io/images/project_badges/sonarcloud-white.svg"
|
7
|
+
alt="Sonar Cloud" />
|
8
|
+
</a>
|
9
|
+
</div>
|
10
|
+
|
11
|
+
</br>
|
12
|
+
|
13
|
+
<div align="center">
|
14
|
+
<!-- Version -->
|
15
|
+
<a href="https://badge.fury.io/rb/danger-code_coverage">
|
16
|
+
<img src="https://badge.fury.io/rb/danger-code_coverage.svg" alt="Version" />
|
17
|
+
</a>
|
18
|
+
<!-- Downloads -->
|
19
|
+
<a href="https://badge.fury.io/rb/danger-code_coverage">
|
20
|
+
<img src="https://img.shields.io/gem/dt/danger-code_coverage.svg" alt="Downloads" />
|
21
|
+
</a>
|
22
|
+
</div>
|
23
|
+
|
24
|
+
<div align="center">
|
25
|
+
<!-- Build Status -->
|
26
|
+
<a href="https://travis-ci.org/Kyaak/danger-code_coverage">
|
27
|
+
<img src="https://img.shields.io/travis/choojs/choo/develop.svg"
|
28
|
+
alt="Build Status" />
|
29
|
+
</a>
|
30
|
+
<!-- Coverage -->
|
31
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
32
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=coverage"
|
33
|
+
alt="Coverage" />
|
34
|
+
</a>
|
35
|
+
</div>
|
36
|
+
|
37
|
+
<div align="center">
|
38
|
+
<!-- Reliability Rating -->
|
39
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
40
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=reliability_rating"
|
41
|
+
alt="Reliability Rating" />
|
42
|
+
</a>
|
43
|
+
<!-- Security Rating -->
|
44
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
45
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=security_rating"
|
46
|
+
alt="Security Rating" />
|
47
|
+
</a>
|
48
|
+
<!-- Maintainabiltiy -->
|
49
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
50
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=sqale_rating"
|
51
|
+
alt="Maintainabiltiy" />
|
52
|
+
</a>
|
53
|
+
</div>
|
54
|
+
|
55
|
+
<div align="center">
|
56
|
+
<!-- Code Smells -->
|
57
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
58
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=code_smells"
|
59
|
+
alt="Code Smells" />
|
60
|
+
</a>
|
61
|
+
<!-- Bugs -->
|
62
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
63
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=bugs"
|
64
|
+
alt="Bugs" />
|
65
|
+
</a>
|
66
|
+
<!-- Vulnerabilities -->
|
67
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
68
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=vulnerabilities"
|
69
|
+
alt="Vulnerabilities" />
|
70
|
+
</a>
|
71
|
+
<!-- Technical Dept -->
|
72
|
+
<a href="https://sonarcloud.io/dashboard?id=Kyaak_danger-code_coverage">
|
73
|
+
<img src="https://sonarcloud.io/api/project_badges/measure?project=Kyaak_danger-code_coverage&metric=sqale_index"
|
74
|
+
alt="Technical Dept" />
|
75
|
+
</a>
|
76
|
+
</div>
|
77
|
+
</br>
|
78
|
+
|
79
|
+
This [danger](https://github.com/danger/danger) plugin generates a coverage overview for modified files :100: <br>
|
80
|
+
|
81
|
+
This plugin is inspired and works only with the jenkins [code-coverage-api-plugin](https://github.com/jenkinsci/code-coverage-api-plugin) :bowing_man:
|
82
|
+
|
83
|
+
## How it looks like
|
84
|
+
|
85
|
+
### Code Coverage :100:
|
86
|
+
|
87
|
+
|**File**|**Total**|**Method**|**Line**|**Conditional**|**Instruction**|
|
88
|
+
|:-|:-:|:-:|:-:|:-:|:-:|
|
89
|
+
|com/example/kyaak/myapplication/MyUtil.java|50.94|13.34|75.0|49.99|65.44|
|
90
|
+
|com/example/kyaak/myapplication/MyController.java|19.94|13.34|30.0|20.99|15.44|
|
91
|
+
|com/example/kyaak/myapplication/MainActivity.java|0.0|0.0|0.0|0.0|0.0|
|
92
|
+
|
93
|
+
## Installation
|
94
|
+
|
95
|
+
$ gem install danger-code_coverage
|
96
|
+
|
97
|
+
## Usage
|
98
|
+
|
99
|
+
code_coverage.report
|
100
|
+
|
101
|
+
## Authentication
|
102
|
+
|
103
|
+
If you run a jenkins server with required authentication you can pass them to `danger-code_coverage`.
|
104
|
+
Create an API token with your CI user and do not pass normal password credentials.
|
105
|
+
|
106
|
+
code_coverage.report(
|
107
|
+
auth_user: "jenkins",
|
108
|
+
auth_token: "MY_TOKEN"
|
109
|
+
)
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require('bundler/gem_tasks')
|
4
|
+
require('rspec/core/rake_task')
|
5
|
+
require('rubocop/rake_task')
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:specs)
|
8
|
+
|
9
|
+
task(default: :specs)
|
10
|
+
|
11
|
+
task :spec do
|
12
|
+
Rake::Task['specs'].invoke
|
13
|
+
Rake::Task['rubocop'].invoke
|
14
|
+
Rake::Task['spec_docs'].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
desc('Run RuboCop on the lib/specs directory')
|
18
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
19
|
+
task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
|
20
|
+
task.fail_on_error = false
|
21
|
+
task.formatters = ['simple', 'json']
|
22
|
+
task.options = ['--out', 'rubocop-result.json']
|
23
|
+
end
|
24
|
+
|
25
|
+
desc('Ensure that the plugin passes `danger plugins lint`')
|
26
|
+
task :spec_docs do
|
27
|
+
sh 'bundle exec danger plugins lint'
|
28
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require('code_coverage/gem_version.rb')
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'danger-code_coverage'
|
9
|
+
spec.version = CodeCoverage::VERSION
|
10
|
+
spec.authors = ['Martin Schwamberger']
|
11
|
+
spec.email = ['kyaak.dev@gmail.com']
|
12
|
+
spec.description = 'Danger plugin for Jenkins-Code-Coverage-Api plugin.'
|
13
|
+
spec.summary = 'Read Jenkins code-coverage reports and comment pull request with coverage for changed files.'
|
14
|
+
spec.homepage = 'https://github.com/Kyaak/danger-code_coverage'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = %x(git ls-files).split($INPUT_RECORD_SEPARATOR)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |file| File.basename(file) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>=2.3.0'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency('danger-plugin-api', '~> 1.0')
|
25
|
+
|
26
|
+
# General ruby development
|
27
|
+
spec.add_development_dependency('bundler', '~> 2.0')
|
28
|
+
spec.add_development_dependency('rake', '~> 12.3')
|
29
|
+
|
30
|
+
# Testing support
|
31
|
+
spec.add_development_dependency('mocha', '~> 1.8')
|
32
|
+
spec.add_development_dependency('rspec', '~> 3.8')
|
33
|
+
spec.add_development_dependency('simplecov', '~> 0.16')
|
34
|
+
spec.add_development_dependency('simplecov-console', '~> 0.4')
|
35
|
+
|
36
|
+
# Linting code and docs
|
37
|
+
spec.add_development_dependency('rubocop', '~> 0.68')
|
38
|
+
spec.add_development_dependency('rubocop-performance', '~> 1.2')
|
39
|
+
spec.add_development_dependency('rubocop-thread_safety', '~> 0.3')
|
40
|
+
spec.add_development_dependency('yard', '~> 0.9')
|
41
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require('code_coverage/coverage_item')
|
4
|
+
require('json')
|
5
|
+
|
6
|
+
module CodeCoverage
|
7
|
+
# Parse json files of code-coverage-api plugin and converts it into an array of [CoverageItem].
|
8
|
+
# Empty array if not parseable.
|
9
|
+
class CoverageParser
|
10
|
+
# Parse json string into [CoverageItem] array.
|
11
|
+
#
|
12
|
+
# @param content Json string to parse.
|
13
|
+
# @return [Array<CoverageItem>] Empty array of filled with [CoverageItem].
|
14
|
+
def parse(content)
|
15
|
+
raw_json =
|
16
|
+
begin
|
17
|
+
JSON.parse(content)
|
18
|
+
rescue JSON::ParserError
|
19
|
+
{}
|
20
|
+
end
|
21
|
+
parse_coverage(raw_json)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def parse_coverage(raw_json)
|
27
|
+
result = []
|
28
|
+
results = raw_json['results']
|
29
|
+
return result unless results
|
30
|
+
|
31
|
+
report_files = results['children']
|
32
|
+
return result unless report_files
|
33
|
+
|
34
|
+
report_files.each do |report|
|
35
|
+
projects = report['children']
|
36
|
+
projects.each do |project|
|
37
|
+
project['children'].each do |directory|
|
38
|
+
directory['children'].each do |file|
|
39
|
+
result << coverage_item(directory['name'], file)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
def coverage_item(parent_name, file_json)
|
49
|
+
elements = file_json['elements']
|
50
|
+
file_name = file_json['name']
|
51
|
+
item_name = +''
|
52
|
+
item_name << "#{parent_name}/" if !dirs?(file_name) && !dots?(parent_name)
|
53
|
+
item_name << file_name
|
54
|
+
|
55
|
+
item = CoverageItem.new
|
56
|
+
item.file = item_name
|
57
|
+
item.method = find_element_ratio(elements, 'Method')
|
58
|
+
item.line = find_element_ratio(elements, 'Line')
|
59
|
+
item.conditional = find_element_ratio(elements, 'Conditional')
|
60
|
+
item.instruction = find_element_ratio(elements, 'Instruction')
|
61
|
+
item
|
62
|
+
end
|
63
|
+
|
64
|
+
def find_element_ratio(elements, name)
|
65
|
+
element = elements.select { |it| it['name'] == name }.first
|
66
|
+
element['ratio'].round(2) if element
|
67
|
+
end
|
68
|
+
|
69
|
+
def dirs?(value)
|
70
|
+
value =~ %r{.+\/\w+\..+}
|
71
|
+
end
|
72
|
+
|
73
|
+
def dots?(value)
|
74
|
+
value =~ %r{.*\..*}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodeCoverage
|
4
|
+
# Generate a markdown table.
|
5
|
+
class MarkdownTable
|
6
|
+
COLUMN_SEPARATOR = '|'
|
7
|
+
HEADER_SEPARATOR = '-'
|
8
|
+
|
9
|
+
# Initialize table generator.
|
10
|
+
def initialize
|
11
|
+
@header = COLUMN_SEPARATOR.dup
|
12
|
+
@header_separator = COLUMN_SEPARATOR.dup
|
13
|
+
@lines = []
|
14
|
+
end
|
15
|
+
|
16
|
+
# Add each entry to the table header
|
17
|
+
def header(*args)
|
18
|
+
args.each_with_index do |item, index|
|
19
|
+
@header << "#{item}#{COLUMN_SEPARATOR}"
|
20
|
+
@header_separator << if index.zero?
|
21
|
+
":#{HEADER_SEPARATOR}#{COLUMN_SEPARATOR}"
|
22
|
+
else
|
23
|
+
":#{HEADER_SEPARATOR}:#{COLUMN_SEPARATOR}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return the number of lines without header items.
|
29
|
+
#
|
30
|
+
# @return [Integer] Number of lines.
|
31
|
+
def size
|
32
|
+
@lines.length
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add a new line entry to the table.
|
36
|
+
#
|
37
|
+
# @param args [String]* Multiple comma separated strings for each column entry.
|
38
|
+
def line(*args)
|
39
|
+
line = COLUMN_SEPARATOR.dup
|
40
|
+
args.each do |item|
|
41
|
+
line << "#{item}#{COLUMN_SEPARATOR}"
|
42
|
+
end
|
43
|
+
@lines << line
|
44
|
+
end
|
45
|
+
|
46
|
+
# Combine all data to a markdown table string.
|
47
|
+
# @return [String] Table.
|
48
|
+
def to_markdown
|
49
|
+
result = +"#{@header}\n#{@header_separator}\n"
|
50
|
+
result << @lines.join("\n")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Danger
|
4
|
+
# Generate code coverage reports on pull requests based on jenkins code-coverage-api-plugin.
|
5
|
+
#
|
6
|
+
# @example Generate report for each changed file.
|
7
|
+
# code_coverage.report
|
8
|
+
#
|
9
|
+
# @example Use auth token.
|
10
|
+
# code_coverage.report(
|
11
|
+
# auth_user: 'user',
|
12
|
+
# auth_token: 'token'
|
13
|
+
# )
|
14
|
+
#
|
15
|
+
# @see Kyaak/danger-code_coverage
|
16
|
+
# @tags danger, jenkins, coverage, code-coverage, analysis
|
17
|
+
class DangerCodeCoverage < Plugin
|
18
|
+
require 'open-uri'
|
19
|
+
require 'code_coverage/markdown_table'
|
20
|
+
require 'code_coverage/coverage_parser'
|
21
|
+
require 'code_coverage/coverage_item'
|
22
|
+
|
23
|
+
EMPTY_COLUMN = '-'
|
24
|
+
TABLE_HEADER_TOTAL = '**Total**'
|
25
|
+
TABLE_HEADER_FILE = '**File**'
|
26
|
+
TABLE_HEADER_METHOD = '**Method**'
|
27
|
+
TABLE_HEADER_LINE = '**Line**'
|
28
|
+
TABLE_HEADER_CONDITIONAL = '**Conditional**'
|
29
|
+
TABLE_HEADER_INSTRUCTION = '**Instruction**'
|
30
|
+
TABLE_TITLE = '### Code Coverage :100:'
|
31
|
+
|
32
|
+
# Initialize code_coverage plugin.
|
33
|
+
def initialize(dangerfile)
|
34
|
+
@target_files = nil
|
35
|
+
@auth = nil
|
36
|
+
@table = CodeCoverage::MarkdownTable.new
|
37
|
+
@table.header(TABLE_HEADER_FILE,
|
38
|
+
TABLE_HEADER_TOTAL,
|
39
|
+
TABLE_HEADER_METHOD,
|
40
|
+
TABLE_HEADER_LINE,
|
41
|
+
TABLE_HEADER_CONDITIONAL,
|
42
|
+
TABLE_HEADER_INSTRUCTION)
|
43
|
+
super(dangerfile)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Create an overview report.
|
47
|
+
#
|
48
|
+
# @param args Configuration settings
|
49
|
+
# @return [void]
|
50
|
+
def report(*args)
|
51
|
+
options = args.first
|
52
|
+
check_auth(options)
|
53
|
+
|
54
|
+
items = coverage_items
|
55
|
+
items.select! { |item| file_in_changeset?(item.file) }
|
56
|
+
items.each(&method(:update_item))
|
57
|
+
items.sort_by!{|item| -item.total}
|
58
|
+
items.each(&method(:add_entry))
|
59
|
+
|
60
|
+
return if @table.size.zero?
|
61
|
+
|
62
|
+
markdown("#{TABLE_TITLE}\n\n#{@table.to_markdown}")
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def update_item(item)
|
68
|
+
item.method = convert_entry(item.method)
|
69
|
+
item.line = convert_entry(item.line)
|
70
|
+
item.conditional = convert_entry(item.conditional)
|
71
|
+
item.instruction = convert_entry(item.instruction)
|
72
|
+
item.total = total(item)
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_entry(item)
|
76
|
+
@table.line(item.file,
|
77
|
+
item.total,
|
78
|
+
item.method,
|
79
|
+
item.line,
|
80
|
+
item.conditional,
|
81
|
+
item.instruction)
|
82
|
+
end
|
83
|
+
|
84
|
+
def convert_entry(value)
|
85
|
+
return EMPTY_COLUMN unless value
|
86
|
+
|
87
|
+
value
|
88
|
+
end
|
89
|
+
|
90
|
+
def total(item)
|
91
|
+
count = 0
|
92
|
+
sum = 0
|
93
|
+
|
94
|
+
unless convert_entry(item.method).eql?(EMPTY_COLUMN)
|
95
|
+
count += 1
|
96
|
+
sum += convert_entry(item.method)
|
97
|
+
end
|
98
|
+
|
99
|
+
unless convert_entry(item.line).eql?(EMPTY_COLUMN)
|
100
|
+
count += 1
|
101
|
+
sum += convert_entry(item.line)
|
102
|
+
end
|
103
|
+
|
104
|
+
unless convert_entry(item.conditional).eql?(EMPTY_COLUMN)
|
105
|
+
count += 1
|
106
|
+
sum += convert_entry(item.conditional)
|
107
|
+
end
|
108
|
+
|
109
|
+
unless convert_entry(item.instruction).eql?(EMPTY_COLUMN)
|
110
|
+
count += 1
|
111
|
+
sum += convert_entry(item.instruction)
|
112
|
+
end
|
113
|
+
|
114
|
+
(sum / count).round(2)
|
115
|
+
end
|
116
|
+
|
117
|
+
def auth_user(options)
|
118
|
+
options && !options[:auth_user].nil? ? options[:auth_user] : nil
|
119
|
+
end
|
120
|
+
|
121
|
+
def auth_token(options)
|
122
|
+
options && !options[:auth_token].nil? ? options[:auth_token] : nil
|
123
|
+
end
|
124
|
+
|
125
|
+
def check_auth(options)
|
126
|
+
user = auth_user(options)
|
127
|
+
token = auth_token(options)
|
128
|
+
return unless user && token
|
129
|
+
|
130
|
+
@auth = {
|
131
|
+
user: user,
|
132
|
+
token: token
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
def file_in_changeset?(file)
|
137
|
+
target_files.include?(file)
|
138
|
+
end
|
139
|
+
|
140
|
+
def target_files
|
141
|
+
@target_files ||= git.modified_files + git.added_files
|
142
|
+
end
|
143
|
+
|
144
|
+
def coverage_items
|
145
|
+
content = coverage_json
|
146
|
+
CodeCoverage::CoverageParser.new.parse(content)
|
147
|
+
end
|
148
|
+
|
149
|
+
def coverage_json
|
150
|
+
options = {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}
|
151
|
+
options[:http_basic_authentication] = [@auth[:user], @auth[:token]] if @auth
|
152
|
+
OpenURI.open_uri("#{ENV['BUILD_URL']}/coverage/result/api/json?depth=5", options).read
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|