danger-ktlint 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1f250c93c48f9c5b8a9eb7aee96760b0f7ca668be636aeaeddfac263f87a242
4
- data.tar.gz: d40bed8d74888a9555fd5bb4e7619281ff38422ecc35064d4fa59f0a39e0e20e
3
+ metadata.gz: fccc83b4c267453f15dbab68b15c2251de27eee1c89fbd7f47800f84f7528816
4
+ data.tar.gz: c97f5f1200761d40f806b18efd9bb29070914857656460935a3d1696198af6e9
5
5
  SHA512:
6
- metadata.gz: 5e819fdad3eb22599362768654c65e1657652b653d6a6268791f63aa26c03feed6fc3dab9c4dd061883d3094ecb4cbf9b704fb603122fbbe1f1cc26477b88e99
7
- data.tar.gz: 817b35c9e78fce50d690c3c56b605d5bfa3e609a7b32785516001eb407e286934d6126a236c736912f153a8c04479da3945ff9b5c07135acf339545c8ccfded6
6
+ metadata.gz: 14aa4af509079aa9ea8faabfc8a41e564fef09d1df5ec53d102898069ca3cdf1770ff9cb5bc3d1f0f484bdc1e08dd6e1ff68df5c2b7f8ad351a2f8f6588dad47
7
+ data.tar.gz: 34a20e7ec1eb556208a56fcd4c42b299a69ffd1a8fa9148888f87af6984aeeee043df9eb0ac5a9d75b10e05c02eb97290cff33d4004c9c8d5ca197678ae38aac
data/CHANGELOG.md CHANGED
@@ -1,7 +1,23 @@
1
+ ## Unreleased
2
+
3
+ ## 0.0.7
4
+
5
+ - Support multiple ktlint result json.
6
+
7
+ ## 0.0.6
8
+
9
+ - Support GitLab and BitBucket server even if `inline_mode: false` is specified.
10
+
11
+ ## 0.0.5
12
+
13
+ ### Fixed
14
+
15
+ - Fixed to not check ktlint binary even when skip_task is specified.
16
+
1
17
  ## 0.0.4
2
18
 
3
19
  ### Added
4
- - ktlint task can be skipped by specifing `ktlint.skip_task = true` and `ktlint.report_file = '...'`
20
+ - ktlint task can be skipped by specifing `ktlint.skip_lint = true` and `ktlint.report_file = '...'`
5
21
 
6
22
  ## 0.0.3
7
23
  ### Added
data/README.md CHANGED
@@ -12,6 +12,8 @@ gem install danger-ktlint
12
12
 
13
13
  You need to install `ktlint` command and set as executable first, see: https://ktlint.github.io/#getting-started.
14
14
 
15
+ If you want to skip ktlint task, for example to only comment on the results of ktlint, no need to install ktlint. See https://github.com/mataku/danger-ktlint#skip-ktlint-task.
16
+
15
17
  ```bash
16
18
  # Example
17
19
  curl --output /usr/local/bin/ktlint -sL https://github.com/pinterest/ktlint/releases/download/$KTLINT_VERSION/ktlint && chmod a+x /usr/local/bin/ktlint
@@ -31,11 +33,24 @@ ktlint.lint
31
33
 
32
34
  Default is `nil`, all comments are sent.
33
35
 
34
- ```bash
36
+ ```shell
35
37
  ktlint.limit = 3
36
38
  ktlint.lint
37
39
  ```
38
40
 
41
+ #### Skip ktlint task
42
+
43
+ Default is false.
44
+
45
+ ```shell
46
+ ktlint.skip_lint = true
47
+ # If skip_lint is specified, report_file must also be specified.
48
+ ktlint.report_file = 'result.json'
49
+ # If you use ktlint in multiple modules app, you can specify multiple ktlint result json.
50
+ # ktlint.report_files_pattern = '**/result.json'
51
+ ktlint.lint
52
+ ```
53
+
39
54
  ## CHANGELOG
40
55
 
41
56
  See [CHANGELOG.md](https://github.com/mataku/danger-ktlint/blob/master/CHANGELOG.md).
@@ -43,7 +58,9 @@ See [CHANGELOG.md](https://github.com/mataku/danger-ktlint/blob/master/CHANGELOG
43
58
  ## TODO
44
59
 
45
60
  - filtering: false (default: filtering: true behavior)
46
- - XML report_file (Currently only JSON is supported.)
61
+ - Allow plain or html report_file (Currently only JSON is supported.)
62
+ - Install ktlint and use it if ktlint binary does not exist
63
+ - Support for services other than GitHub
47
64
 
48
65
  ## Development
49
66
 
@@ -1,3 +1,3 @@
1
1
  module Ktlint
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.7".freeze
3
3
  end
data/lib/ktlint/plugin.rb CHANGED
@@ -1,15 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
 
3
5
  module Danger
4
6
  class DangerKtlint < Plugin
5
-
6
- class UnexpectedLimitTypeError < StandardError
7
+ class UnexpectedLimitTypeError < StandardError; end
8
+
9
+ class UnsupportedServiceError < StandardError
10
+ def initialize(message = 'Unsupported service! Currently supported services are GitHub, GitLab and BitBucket server.')
11
+ super(message)
12
+ end
7
13
  end
8
14
 
15
+ AVAILABLE_SERVICES = [:github, :gitlab, :bitbucket_server]
16
+
9
17
  # TODO: Lint all files if `filtering: false`
10
18
  attr_accessor :filtering
11
19
 
12
- attr_accessor :skip_lint, :report_file
20
+ attr_accessor :skip_lint, :report_file, :report_files_pattern
13
21
 
14
22
  def limit
15
23
  @limit ||= nil
@@ -29,9 +37,8 @@ module Danger
29
37
  # @return [void]
30
38
  # def lint(inline_mode: false)
31
39
  def lint(inline_mode: false)
32
- unless ktlint_exists?
33
- fail("Couldn't find ktlint command. Install first.")
34
- return
40
+ unless supported_service?
41
+ raise UnsupportedServiceError.new
35
42
  end
36
43
 
37
44
  targets = target_files(git.added_files + git.modified_files)
@@ -50,7 +57,7 @@ module Danger
50
57
 
51
58
  # Comment to a PR by ktlint result json
52
59
  #
53
- # // Sample ktlint result
60
+ # // Sample single ktlint result
54
61
  # [
55
62
  # {
56
63
  # "file": "app/src/main/java/com/mataku/Model.kt",
@@ -64,20 +71,22 @@ module Danger
64
71
  # ]
65
72
  # }
66
73
  # ]
67
- def send_markdown_comment(results, targets)
74
+ def send_markdown_comment(ktlint_results, targets)
68
75
  catch(:loop_break) do
69
76
  count = 0
70
- results.each do |result|
71
- result['errors'].each do |error|
72
- file_path = relative_file_path(result['file'])
73
- next unless targets.include?(file_path)
74
- file = "#{file_path}#L#{error['line']}"
75
- message = "#{github.html_link(file)}: #{error['message']}"
76
- fail(message)
77
- unless limit.nil?
78
- count += 1
79
- if count >= limit
80
- throw(:loop_break)
77
+ ktlint_results.each do |ktlint_result|
78
+ ktlint_result.each do |result|
79
+ result['errors'].each do |error|
80
+ file_path = relative_file_path(result['file'])
81
+ next unless targets.include?(file_path)
82
+
83
+ message = "#{file_html_link(file_path, error['line'])}: #{error['message']}"
84
+ fail(message)
85
+ unless limit.nil?
86
+ count += 1
87
+ if count >= limit
88
+ throw(:loop_break)
89
+ end
81
90
  end
82
91
  end
83
92
  end
@@ -85,21 +94,22 @@ module Danger
85
94
  end
86
95
  end
87
96
 
88
- def send_inline_comments(results, targets)
97
+ def send_inline_comments(ktlint_results, targets)
89
98
  catch(:loop_break) do
90
99
  count = 0
91
- results.each do |result|
92
- result['errors'].each do |error|
93
- file_path = relative_file_path(result['file'])
94
- next unless targets.include?(file_path)
95
-
96
- message = error['message']
97
- line = error['line']
98
- fail(message, file: result['file'], line: line)
99
- unless limit.nil?
100
- count += 1
101
- if count >= limit
102
- throw(:loop_break)
100
+ ktlint_results.each do |ktlint_result|
101
+ ktlint_result.each do |result|
102
+ result['errors'].each do |error|
103
+ file_path = relative_file_path(result['file'])
104
+ next unless targets.include?(file_path)
105
+ message = error['message']
106
+ line = error['line']
107
+ fail(message, file: result['file'], line: line)
108
+ unless limit.nil?
109
+ count += 1
110
+ if count >= limit
111
+ throw(:loop_break)
112
+ end
103
113
  end
104
114
  end
105
115
  end
@@ -120,6 +130,20 @@ module Danger
120
130
 
121
131
  private
122
132
 
133
+ def file_html_link(file_path, line_number)
134
+ file = if danger.scm_provider == :github
135
+ "#{file_path}#L#{line_number}"
136
+ else
137
+ file_path
138
+ end
139
+ scm_provider_klass.html_link(file)
140
+ end
141
+
142
+ # `eval` may be dangerous, but it does not accept any input because it accepts only defined as danger.scm_provider
143
+ def scm_provider_klass
144
+ @scm_provider_klass ||= eval(danger.scm_provider.to_s)
145
+ end
146
+
123
147
  def pwd
124
148
  @pwd ||= `pwd`.chomp
125
149
  end
@@ -131,18 +155,10 @@ module Danger
131
155
  def ktlint_results(targets)
132
156
  if skip_lint
133
157
  # TODO: Allow XML
134
- if report_file.nil? || report_file.empty?
135
- fail("If skip_lint is specified, You must specify ktlint report json file with `ktlint.report_file=...` in your Dangerfile.")
136
- return
137
- end
138
-
139
- unless File.exists?(report_file)
140
- fail("Couldn't find ktlint result json file.\nYou must specify it with `ktlint.report_file=...` in your Dangerfile.")
141
- return
142
- end
143
-
144
- File.open(report_file) do |f|
145
- JSON.load(f)
158
+ ktlint_result_files.map do |file|
159
+ File.open(file) do |f|
160
+ JSON.load(f)
161
+ end
146
162
  end
147
163
  else
148
164
  unless ktlint_exists?
@@ -152,7 +168,21 @@ module Danger
152
168
 
153
169
  return if targets.empty?
154
170
 
155
- JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`)
171
+ [JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`)]
172
+ end
173
+ end
174
+
175
+ def supported_service?
176
+ AVAILABLE_SERVICES.include?(danger.scm_provider.to_sym)
177
+ end
178
+
179
+ def ktlint_result_files
180
+ if !report_file.nil? && !report_file.empty? && File.exists?(report_file)
181
+ [report_file]
182
+ elsif !report_files_pattern.nil? && !report_files_pattern.empty?
183
+ Dir.glob(report_files_pattern)
184
+ else
185
+ fail("Couldn't find ktlint result json file.\nYou must specify it with `ktlint.report_file=...` or `ktlint.report_files_pattern=...` in your Dangerfile.")
156
186
  end
157
187
  end
158
188
  end
@@ -0,0 +1,19 @@
1
+ [
2
+ {
3
+ "file": "app/src/main/java/com/mataku/Model2.kt",
4
+ "errors": [
5
+ {
6
+ "line": 46,
7
+ "column": 1,
8
+ "message": "Unexpected blank line(s) before \"}\"",
9
+ "rule": "no-blank-line-before-rbrace"
10
+ },
11
+ {
12
+ "line": 47,
13
+ "column": 1,
14
+ "message": "Unexpected blank line(s) before \"}\"",
15
+ "rule": "no-blank-line-before-rbrace"
16
+ }
17
+ ]
18
+ }
19
+ ]
data/spec/ktlint_spec.rb CHANGED
@@ -11,6 +11,7 @@ module Danger
11
11
 
12
12
  before do
13
13
  allow_any_instance_of(Kernel).to receive(:`).with('pwd').and_return('/home/mataku')
14
+ allow_any_instance_of(Kernel).to receive(:`).with('which less').and_return(0)
14
15
  end
15
16
 
16
17
  describe '#lint' do
@@ -55,6 +56,22 @@ module Danger
55
56
  expect(dangerfile.status_report[:errors].size).to eq(2)
56
57
  end
57
58
  end
59
+
60
+ context 'GitLab' do
61
+ let(:dangerfile) { testing_dangerfile_for_gitlab }
62
+
63
+ before do
64
+ allow_any_instance_of(Kernel).to receive(:system).with('which ktlint > /dev/null 2>&1').and_return(true)
65
+ allow_any_instance_of(Kernel).to receive(:`).with('ktlint app/src/main/java/com/mataku/Model.kt --reporter=json --relative').and_return(dummy_ktlint_result)
66
+ allow_any_instance_of(Danger::DangerfileGitLabPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
67
+ allow_any_instance_of(Danger::DangerfileGitLabPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
68
+ end
69
+
70
+ it do
71
+ plugin.lint
72
+ expect(dangerfile.status_report[:errors].size).to eq(2)
73
+ end
74
+ end
58
75
  end
59
76
 
60
77
  describe '#limit' do
@@ -66,7 +83,7 @@ module Danger
66
83
 
67
84
  context 'integer value is set to limit' do
68
85
  it 'raises no errors' do
69
- expect { plugin.limit = 1 }.not_to raise_error(DangerKtlint::UnexpectedLimitTypeError)
86
+ expect { plugin.limit = 1 }.not_to raise_error
70
87
  end
71
88
  end
72
89
  end
@@ -90,5 +107,46 @@ module Danger
90
107
  end
91
108
  end
92
109
  end
110
+
111
+ describe '#skip_lint' do
112
+ context 'skip_lint: true' do
113
+ before do
114
+ allow_any_instance_of(Danger::DangerfileGitPlugin).to receive(:added_files).and_return(['app/src/main/java/com/mataku/Model.kt'])
115
+ allow_any_instance_of(Danger::DangerfileGitPlugin).to receive(:modified_files).and_return([])
116
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt#L46').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
117
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt#L47').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
118
+
119
+ allow(plugin).to receive(:system).with('which ktlint > /dev/null 2>&1').and_return(true)
120
+ plugin.report_file = './spec/fixtures/ktlint_result.json'
121
+ plugin.skip_lint = true
122
+ end
123
+
124
+ it do
125
+ expect(plugin).not_to have_received(:system).with('which ktlint > /dev/null 2>&1')
126
+ plugin.lint(inline_mode: false)
127
+ end
128
+ end
129
+
130
+ context 'report_files_pattern is specified' do
131
+ before do
132
+ allow_any_instance_of(Danger::DangerfileGitPlugin).to receive(:added_files).and_return(['app/src/main/java/com/mataku/Model.kt', 'app/src/main/java/com/mataku/Model2.kt'])
133
+ allow_any_instance_of(Danger::DangerfileGitPlugin).to receive(:modified_files).and_return([])
134
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt#L46').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
135
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model.kt#L47').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model.kt'>Model.kt</a>")
136
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model2.kt#L46').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model2.kt'>Model2.kt</a>")
137
+ allow_any_instance_of(Danger::DangerfileGitHubPlugin).to receive(:html_link).with('app/src/main/java/com/mataku/Model2.kt#L47').and_return("<a href='https://gitlab.com/mataku/android/blob/561827e46167077b5e53515b4b7349b8ae04610b/Model2.kt'>Model2.kt</a>")
138
+ #
139
+ allow(plugin).to receive(:system).with('which ktlint > /dev/null 2>&1').and_return(true)
140
+ plugin.report_files_pattern = "**/ktlint_result*.json"
141
+ plugin.skip_lint = true
142
+ end
143
+
144
+ it do
145
+ expect(plugin).not_to have_received(:system).with('which ktlint > /dev/null 2>&1')
146
+ plugin.lint(inline_mode: false)
147
+ expect(dangerfile.status_report[:errors].size).to eq(4)
148
+ end
149
+ end
150
+ end
93
151
  end
94
152
  end
data/spec/spec_helper.rb CHANGED
@@ -46,24 +46,41 @@ def testing_ui
46
46
  end
47
47
  # rubocop:enable Lint/NestedMethodDefinition
48
48
 
49
- # Example environment (ENV) that would come from
50
- # running a PR on TravisCI
51
49
  def testing_env
52
50
  {
53
- "HAS_JOSH_K_SEAL_OF_APPROVAL" => "true",
54
- "TRAVIS_PULL_REQUEST" => "800",
55
- "TRAVIS_REPO_SLUG" => "artsy/eigen",
56
- "TRAVIS_COMMIT_RANGE" => "759adcbd0d8f...13c4dc8bb61d",
51
+ "BITRISE_PULL_REQUEST" => "4",
52
+ "BITRISE_IO" => "true",
53
+ "GIT_REPOSITORY_URL" => "git@github.com:artsy/eigen",
57
54
  "DANGER_GITHUB_API_TOKEN" => "123sbdq54erfsd3422gdfio"
58
55
  }
59
56
  end
60
57
 
58
+ def testing_env_for_gitlab
59
+ {
60
+ "BITRISE_PULL_REQUEST" => "4",
61
+ "BITRISE_IO" => "true",
62
+ "GIT_REPOSITORY_URL" => "git@gitlab.com:artsy/eigen",
63
+ "DANGER_GITLAB_API_TOKEN" => "123sbdq54erfsd3422gdfio"
64
+ }
65
+ end
66
+
61
67
  # A stubbed out Dangerfile for use in tests
62
68
  def testing_dangerfile
63
69
  env = Danger::EnvironmentManager.new(testing_env)
64
70
  Danger::Dangerfile.new(env, testing_ui)
65
71
  end
66
72
 
73
+ def testing_dangerfile_for_gitlab
74
+ env = Danger::EnvironmentManager.new(testing_env_for_gitlab)
75
+ Danger::Dangerfile.new(env, testing_ui)
76
+ end
77
+
67
78
  def dummy_ktlint_result
68
- File.read(File.expand_path('../fixtures/ktlint_result.txt', __FILE__)).chomp
79
+ File.read(File.expand_path('../fixtures/ktlint_result.json', __FILE__)).chomp
80
+ end
81
+
82
+ def dummy_ktlint_result_2
83
+ File.read(File.expand_path('../fixtures/ktlint_result_2.json', __FILE__)).chomp
69
84
  end
85
+
86
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-ktlint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - mataku
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-20 00:00:00.000000000 Z
11
+ date: 2022-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -143,7 +143,8 @@ files:
143
143
  - lib/danger_plugin.rb
144
144
  - lib/ktlint/gem_version.rb
145
145
  - lib/ktlint/plugin.rb
146
- - spec/fixtures/ktlint_result.txt
146
+ - spec/fixtures/ktlint_result.json
147
+ - spec/fixtures/ktlint_result_2.json
147
148
  - spec/ktlint_spec.rb
148
149
  - spec/spec_helper.rb
149
150
  homepage: https://github.com/mataku/danger-ktlint
@@ -165,11 +166,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  - !ruby/object:Gem::Version
166
167
  version: '0'
167
168
  requirements: []
168
- rubygems_version: 3.2.15
169
+ rubygems_version: 3.2.32
169
170
  signing_key:
170
171
  specification_version: 4
171
172
  summary: Lint kotlin files using ktlint command line interface.
172
173
  test_files:
173
- - spec/fixtures/ktlint_result.txt
174
+ - spec/fixtures/ktlint_result.json
175
+ - spec/fixtures/ktlint_result_2.json
174
176
  - spec/ktlint_spec.rb
175
177
  - spec/spec_helper.rb