danger-spotbugs 0.0.1

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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in danger-spotbugs.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,165 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ danger-spotbugs (0.0.1)
5
+ danger-plugin-api (~> 1.0)
6
+ oga (~> 2.15)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ansi (1.5.0)
14
+ ast (2.4.2)
15
+ claide (1.0.3)
16
+ claide-plugins (0.9.2)
17
+ cork
18
+ nap
19
+ open4 (~> 1.3)
20
+ coderay (1.1.3)
21
+ colored2 (3.1.2)
22
+ cork (0.3.0)
23
+ colored2 (~> 3.1)
24
+ danger (8.2.3)
25
+ claide (~> 1.0)
26
+ claide-plugins (>= 0.9.2)
27
+ colored2 (~> 3.1)
28
+ cork (~> 0.1)
29
+ faraday (>= 0.9.0, < 2.0)
30
+ faraday-http-cache (~> 2.0)
31
+ git (~> 1.7)
32
+ kramdown (~> 2.3)
33
+ kramdown-parser-gfm (~> 1.0)
34
+ no_proxy_fix
35
+ octokit (~> 4.7)
36
+ terminal-table (>= 1, < 4)
37
+ danger-plugin-api (1.0.0)
38
+ danger (> 2.0)
39
+ diff-lcs (1.4.4)
40
+ faraday (1.3.0)
41
+ faraday-net_http (~> 1.0)
42
+ multipart-post (>= 1.2, < 3)
43
+ ruby2_keywords
44
+ faraday-http-cache (2.2.0)
45
+ faraday (>= 0.8)
46
+ faraday-net_http (1.0.1)
47
+ ffi (1.15.0)
48
+ formatador (0.2.5)
49
+ git (1.8.1)
50
+ rchardet (~> 1.8)
51
+ guard (2.16.2)
52
+ formatador (>= 0.2.4)
53
+ listen (>= 2.7, < 4.0)
54
+ lumberjack (>= 1.0.12, < 2.0)
55
+ nenv (~> 0.1)
56
+ notiffany (~> 0.0)
57
+ pry (>= 0.9.12)
58
+ shellany (~> 0.0)
59
+ thor (>= 0.18.1)
60
+ guard-compat (1.2.1)
61
+ guard-rspec (4.7.3)
62
+ guard (~> 2.1)
63
+ guard-compat (~> 1.1)
64
+ rspec (>= 2.99.0, < 4.0)
65
+ kramdown (2.3.1)
66
+ rexml
67
+ kramdown-parser-gfm (1.1.0)
68
+ kramdown (~> 2.0)
69
+ listen (3.0.8)
70
+ rb-fsevent (~> 0.9, >= 0.9.4)
71
+ rb-inotify (~> 0.9, >= 0.9.7)
72
+ lumberjack (1.2.8)
73
+ method_source (1.0.0)
74
+ multipart-post (2.1.1)
75
+ nap (1.1.0)
76
+ nenv (0.3.0)
77
+ no_proxy_fix (0.1.2)
78
+ notiffany (0.1.3)
79
+ nenv (~> 0.1)
80
+ shellany (~> 0.0)
81
+ octokit (4.20.0)
82
+ faraday (>= 0.9)
83
+ sawyer (~> 0.8.0, >= 0.5.3)
84
+ oga (2.15)
85
+ ast
86
+ ruby-ll (~> 2.1)
87
+ open4 (1.3.4)
88
+ parallel (1.20.1)
89
+ parser (3.0.0.0)
90
+ ast (~> 2.4.1)
91
+ pry (0.14.0)
92
+ coderay (~> 1.1)
93
+ method_source (~> 1.0)
94
+ public_suffix (4.0.6)
95
+ rainbow (3.0.0)
96
+ rake (10.5.0)
97
+ rb-fsevent (0.10.4)
98
+ rb-inotify (0.10.1)
99
+ ffi (~> 1.0)
100
+ rchardet (1.8.0)
101
+ regexp_parser (2.1.1)
102
+ rexml (3.2.4)
103
+ rspec (3.10.0)
104
+ rspec-core (~> 3.10.0)
105
+ rspec-expectations (~> 3.10.0)
106
+ rspec-mocks (~> 3.10.0)
107
+ rspec-core (3.10.1)
108
+ rspec-support (~> 3.10.0)
109
+ rspec-expectations (3.10.1)
110
+ diff-lcs (>= 1.2.0, < 2.0)
111
+ rspec-support (~> 3.10.0)
112
+ rspec-mocks (3.10.2)
113
+ diff-lcs (>= 1.2.0, < 2.0)
114
+ rspec-support (~> 3.10.0)
115
+ rspec-support (3.10.2)
116
+ rubocop (1.11.0)
117
+ parallel (~> 1.10)
118
+ parser (>= 3.0.0.0)
119
+ rainbow (>= 2.2.2, < 4.0)
120
+ regexp_parser (>= 1.8, < 3.0)
121
+ rexml
122
+ rubocop-ast (>= 1.2.0, < 2.0)
123
+ ruby-progressbar (~> 1.7)
124
+ unicode-display_width (>= 1.4.0, < 3.0)
125
+ rubocop-ast (1.4.1)
126
+ parser (>= 2.7.1.5)
127
+ rubocop-rake (0.5.1)
128
+ rubocop
129
+ rubocop-rspec (2.2.0)
130
+ rubocop (~> 1.0)
131
+ rubocop-ast (>= 1.1.0)
132
+ ruby-ll (2.1.2)
133
+ ansi
134
+ ast
135
+ ruby-progressbar (1.11.0)
136
+ ruby2_keywords (0.0.4)
137
+ sawyer (0.8.2)
138
+ addressable (>= 2.3.5)
139
+ faraday (> 0.8, < 2.0)
140
+ shellany (0.0.1)
141
+ terminal-table (3.0.0)
142
+ unicode-display_width (~> 1.1, >= 1.1.1)
143
+ thor (1.1.0)
144
+ unicode-display_width (1.7.0)
145
+ yard (0.9.26)
146
+
147
+ PLATFORMS
148
+ ruby
149
+
150
+ DEPENDENCIES
151
+ bundler (~> 2.2.14)
152
+ danger-spotbugs!
153
+ guard (~> 2.16.2)
154
+ guard-rspec (~> 4.7.3)
155
+ listen (~> 3.0.8)
156
+ pry (~> 0.14.0)
157
+ rake (~> 10.5.0)
158
+ rspec (~> 3.10.0)
159
+ rubocop (~> 1.11.0)
160
+ rubocop-rake (~> 0.5.1)
161
+ rubocop-rspec (~> 2.2.0)
162
+ yard (~> 0.9.26)
163
+
164
+ BUNDLED WITH
165
+ 2.2.14
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # A guardfile for making Danger Plugins
2
+ # For more info see https://github.com/guard/guard#readme
3
+
4
+ # To run, use `bundle exec guard`.
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec' do
7
+ require 'guard/rspec/dsl'
8
+ dsl = Guard::RSpec::Dsl.new(self)
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Mathieu Rul <mathroule@gmail.com>
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,24 @@
1
+ # Danger SpotBugs
2
+
3
+ Checks on your Gradle project's Java source files.
4
+ This is done using [SpotBugs](https://spotbugs.github.io)
5
+ Results are passed out as tables in markdown.
6
+
7
+ This plugin is inspired from https://github.com/kazy1991/danger-findbugs.
8
+
9
+ ## Installation
10
+
11
+ $ gem install danger-spotbugs
12
+
13
+ ## Usage
14
+
15
+ Methods and attributes from this plugin are available in
16
+ your `Dangerfile` under the `spotbugs` namespace.
17
+
18
+ ## Development
19
+
20
+ 1. Clone this repo
21
+ 2. Run `bundle install` to setup dependencies.
22
+ 3. Run `bundle exec rake spec` to run the tests.
23
+ 4. Use `bundle exec guard` to automatically have tests run as you make changes.
24
+ 5. Make your changes.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:specs)
6
+
7
+ task default: :specs
8
+
9
+ task :spec do
10
+ Rake::Task['specs'].invoke
11
+ Rake::Task['rubocop'].invoke
12
+ Rake::Task['spec_docs'].invoke
13
+ end
14
+
15
+ desc 'Run RuboCop on the lib/specs directory'
16
+ RuboCop::RakeTask.new(:rubocop) do |task|
17
+ task.patterns = %w[lib/**/*.rb spec/**/*.rb]
18
+ end
19
+
20
+ desc 'Ensure that the plugin passes `danger plugins lint`'
21
+ task :spec_docs do
22
+ sh 'bundle exec danger plugins lint'
23
+ end
@@ -0,0 +1,53 @@
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 'spotbugs/gem_version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'danger-spotbugs'
9
+ spec.version = Spotbugs::VERSION
10
+ spec.authors = ['Mathieu Rul']
11
+ spec.email = ['mathroule@gmail.com']
12
+ spec.description = 'A Danger plugin for SpotBugs.'
13
+ spec.summary = 'A Danger plugin for SpotBugs, see https://spotbugs.github.io.'
14
+ spec.homepage = 'https://github.com/mathroule/danger-spotbugs'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'danger-plugin-api', '~> 1.0'
23
+ spec.add_runtime_dependency 'oga', '~> 2.15'
24
+
25
+ # General ruby development
26
+ spec.add_development_dependency 'bundler', '~> 2.2.14'
27
+ spec.add_development_dependency 'rake', '~> 10.5.0'
28
+
29
+ # Testing support
30
+ spec.add_development_dependency 'rspec', '~> 3.10.0'
31
+
32
+ # Linting code and docs
33
+ spec.add_development_dependency 'rubocop', '~> 1.11.0'
34
+ spec.add_development_dependency 'rubocop-rake', '~> 0.5.1'
35
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.2.0'
36
+ spec.add_development_dependency 'yard', '~> 0.9.26'
37
+
38
+ # Makes testing easy via `bundle exec guard`
39
+ spec.add_development_dependency 'guard', '~> 2.16.2'
40
+ spec.add_development_dependency 'guard-rspec', '~> 4.7.3'
41
+
42
+ # If you want to work on older builds of ruby
43
+ spec.add_development_dependency 'listen', '~> 3.0.8'
44
+
45
+ # This gives you the chance to run a REPL inside your tests
46
+ # via:
47
+ #
48
+ # require 'pry'
49
+ # binding.pry
50
+ #
51
+ # This will stop test execution and let you inspect the results
52
+ spec.add_development_dependency 'pry', '~> 0.14.0'
53
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spotbugs/plugin'
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spotbugs/gem_version'
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Represent a BugInstance.
4
+ class BugInstance
5
+ RANK_ERROR_THRESHOLD = 4
6
+ attr_reader :absolute_path, :relative_path
7
+ attr_accessor :source_dirs, :bug_instance
8
+
9
+ def initialize(prefix, source_dirs, bug_instance)
10
+ @source_dirs = source_dirs
11
+ @bug_instance = bug_instance
12
+
13
+ source_path = bug_instance.xpath('SourceLine').attribute('sourcepath').first.value.to_s
14
+ @absolute_path = get_absolute_path(source_path)
15
+
16
+ prefix += (prefix.end_with?(file_separator) ? '' : file_separator)
17
+ @relative_path = if @absolute_path.start_with?(prefix)
18
+ @absolute_path[prefix.length, @absolute_path.length - prefix.length]
19
+ else
20
+ @absolute_path
21
+ end
22
+ end
23
+
24
+ def rank
25
+ @rank ||= bug_instance.attribute('rank').value.to_i
26
+ end
27
+
28
+ def type
29
+ @type ||= rank > RANK_ERROR_THRESHOLD ? :warn : :fail
30
+ end
31
+
32
+ def line
33
+ @line ||= bug_instance.xpath('SourceLine').attribute('start').first.value.to_i
34
+ end
35
+
36
+ def description
37
+ @description ||= bug_instance.xpath('LongMessage').text
38
+ end
39
+
40
+ private
41
+
42
+ def get_absolute_path(source_path)
43
+ @source_dirs.map do |source_dir|
44
+ return source_dir if source_dir.end_with?(source_path)
45
+ end
46
+ end
47
+
48
+ def file_separator
49
+ File::ALT_SEPARATOR || File::SEPARATOR
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spotbugs
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Danger
4
+ # Checks on your Gradle project's Java source files.
5
+ # This is done using [SpotBugs](https://spotbugs.github.io)
6
+ # Results are passed out as tables in markdown.
7
+ #
8
+ # @example Running SpotBugs with its basic configuration
9
+ #
10
+ # spotbugs.report
11
+ #
12
+ # @example Running SpotBugs with a specific Gradle task or report file (glob accepted)
13
+ #
14
+ # spotbugs.gradle_task = 'module:spotbugsRelease' # default: 'spotbugsRelease'
15
+ # spotbugs.report_file = 'module/build/reports/spotbugs/release.xml' # default: 'app/build/reports/spotbugs/release.xml'
16
+ # spotbugs.report
17
+ #
18
+ # @example Running SpotBugs with a specific root path
19
+ #
20
+ # spotbugs.root_path = '/Users/developer/project' # default: result of `git rev-parse --show-toplevel`
21
+ # spotbugs.report
22
+ #
23
+ # @example Running SpotBugs with an array of report files (glob accepted)
24
+ #
25
+ # spotbugs.report_files = ['modules/**/build/reports/spotbugs/release.xml', 'app/build/reports/spotbugs/release.xml']
26
+ # spotbugs.report
27
+ #
28
+ # @example Running SpotBugs without running a Gradle task
29
+ #
30
+ # spotbugs.skip_gradle_task = true # default: false
31
+ # spotbugs.report
32
+ #
33
+ # @example Running SpotBugs without inline comment
34
+ #
35
+ # spotbugs.report(inline_mode: false) # default: true
36
+ #
37
+ # @see mathroule/danger-spotbugs
38
+ # @tags java, android, spotbugs
39
+ #
40
+ class DangerSpotbugs < Plugin
41
+ require_relative './entity/bug_instance'
42
+
43
+ # Custom Gradle task to run.
44
+ # This is useful when your project has different flavors.
45
+ # Defaults to 'spotbugsRelease'.
46
+ #
47
+ # @return [String]
48
+ attr_writer :gradle_task
49
+
50
+ # A getter for `gradle_task`, returning 'spotbugsRelease' if value is nil.
51
+ #
52
+ # @return [String]
53
+ def gradle_task
54
+ @gradle_task ||= 'spotbugsRelease'
55
+ end
56
+
57
+ # Skip Gradle task.
58
+ # If you skip Gradle task, for example project does not manage Gradle.
59
+ # Defaults to `false`.
60
+ #
61
+ # @return [Bool]
62
+ attr_writer :skip_gradle_task
63
+
64
+ # A getter for `skip_gradle_task`, returning false if value is nil.
65
+ #
66
+ # @return [Boolean]
67
+ def skip_gradle_task
68
+ @skip_gradle_task ||= false
69
+ end
70
+
71
+ # An absolute path to a root.
72
+ # To comment errors to VCS, this needs to know relative path of files from the root.
73
+ # Defaults to result of 'git rev-parse --show-toplevel'.
74
+ #
75
+ # @return [String]
76
+ attr_writer :root_path
77
+
78
+ # A getter for `root_path`, returning result of `git rev-parse --show-toplevel` if value is nil.
79
+ #
80
+ # @return [String]
81
+ def root_path
82
+ @root_path ||= `git rev-parse --show-toplevel`.chomp
83
+ end
84
+
85
+ # Location of report file.
86
+ # If your SpotBugs task outputs to a different location, you can specify it here.
87
+ # Defaults to 'app/build/reports/spotbugs/release.xml'.
88
+ #
89
+ # @return [String]
90
+ attr_writer :report_file
91
+
92
+ # A getter for `report_file`, returning 'app/build/reports/spotbugs/release.xml' if value is nil.
93
+ #
94
+ # @return [String]
95
+ def report_file
96
+ @report_file ||= 'app/build/reports/spotbugs/release.xml'
97
+ end
98
+
99
+ # Location of report files.
100
+ # If your SpotBugs task outputs to a different location, you can specify it here.
101
+ # Defaults to ['app/build/reports/spotbugs/release.xml'].
102
+ #
103
+ # @return [Array[String]]
104
+ attr_writer :report_files
105
+
106
+ # A getter for `report_files`, returning ['app/build/reports/spotbugs/release.xml'] if value is nil.
107
+ #
108
+ # @return [Array[String]]
109
+ def report_files
110
+ @report_files ||= [report_file]
111
+ end
112
+
113
+ # Calls SpotBugs task of your Gradle project.
114
+ # It fails if `gradlew` cannot be found inside current directory.
115
+ # It fails if `report_file` cannot be found inside current directory.
116
+ # It fails if `report_files` is empty.
117
+ #
118
+ # @param [Boolean] inline_mode Report as inline comment, defaults to [true].
119
+ #
120
+ # @return [Array[PmdFile]]
121
+ def report(inline_mode: true)
122
+ unless skip_gradle_task
123
+ raise('Could not find `gradlew` inside current directory') unless gradlew_exists?
124
+
125
+ exec_gradle_task
126
+ end
127
+
128
+ report_files_expanded = Dir.glob(report_files).sort
129
+ raise("Could not find matching SpotBugs report files for #{report_files} inside current directory") if report_files_expanded.empty?
130
+
131
+ do_comment(report_files_expanded, inline_mode)
132
+ end
133
+
134
+ private
135
+
136
+ # Check gradlew file exists in current directory.
137
+ #
138
+ # @return [Boolean]
139
+ def gradlew_exists?
140
+ !`ls gradlew`.strip.empty?
141
+ end
142
+
143
+ # Run Gradle task.
144
+ #
145
+ # @return [void]
146
+ def exec_gradle_task
147
+ system "./gradlew #{gradle_task}"
148
+ end
149
+
150
+ # A getter for `spotbugs_report`, returning SpotBugs report.
151
+ #
152
+ # @param [String] report_file The report file.
153
+ #
154
+ # @return [Oga::XML::Document]
155
+ def spotbugs_report(report_file)
156
+ require 'oga'
157
+ Oga.parse_xml(File.open(report_file))
158
+ end
159
+
160
+ # A getter for current updated files.
161
+ #
162
+ # @return [Array[String]]
163
+ def target_files
164
+ @target_files ||= (git.modified_files - git.deleted_files) + git.added_files
165
+ end
166
+
167
+ # A getter for SpotBugs issues, returning SpotBugs issues.
168
+ #
169
+ # @param [String] report_file The report file.
170
+ #
171
+ # @return [Array[BugInstance]]
172
+ def spotbugs_issues(report_file)
173
+ spotbugs_report = spotbugs_report(report_file)
174
+
175
+ source_dirs = spotbugs_report.xpath('//BugCollection//SrcDir').map(&:text)
176
+
177
+ spotbugs_report.xpath('//BugCollection//BugInstance').map do |bug_instance|
178
+ BugInstance.new(root_path, source_dirs, bug_instance)
179
+ end
180
+ end
181
+
182
+ # Generate report and send inline comment with Danger's warn or fail method.
183
+ #
184
+ # @param [Boolean] inline_mode Report as inline comment, defaults to [true].
185
+ #
186
+ # @return [Array[PmdFile]]
187
+ def do_comment(report_files, inline_mode)
188
+ spotbugs_issues = []
189
+
190
+ report_files.each do |report_file|
191
+ spotbugs_issues(report_file).each do |bug_instance|
192
+ next unless target_files.include? bug_instance.relative_path
193
+
194
+ spotbugs_issues.push(bug_instance)
195
+
196
+ send_comment(bug_instance, inline_mode)
197
+ end
198
+ end
199
+
200
+ spotbugs_issues
201
+ end
202
+
203
+ def send_comment(bug_instance, inline_mode)
204
+ if inline_mode
205
+ send(bug_instance.type, bug_instance.description, file: bug_instance.relative_path, line: bug_instance.line)
206
+ else
207
+ send(bug_instance.type, "#{bug_instance.relative_path} : #{bug_instance.description} at #{bug_instance.line}")
208
+ end
209
+ end
210
+ end
211
+ end