danger-ktlint 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: 60e18440651f2b42cb1d710fa157ee9391aa80894dd1e31bf2b43a7198cb80e3
4
- data.tar.gz: b507278d2b29a2361afe704699c17c12373adb2ad82d91e7b44e44aec35dc77e
3
+ metadata.gz: b1f250c93c48f9c5b8a9eb7aee96760b0f7ca668be636aeaeddfac263f87a242
4
+ data.tar.gz: d40bed8d74888a9555fd5bb4e7619281ff38422ecc35064d4fa59f0a39e0e20e
5
5
  SHA512:
6
- metadata.gz: cd061ab55bbc11530c44686f8d1d872363428624fa63065e8e2631d8f703da7d853f1abb7692bd1e8dcf65e2bb38831cd70de2d4da45c05ba2385c8a9148668d
7
- data.tar.gz: 8e28ec29136da819926e73dc6e5263fb9150455caf15cffb29b6ea98cf888bd742ad660ac4466a6a96ca62cf4da0738d4981dd9d79ee4eb48c5331a0ffa76c88
6
+ metadata.gz: 5e819fdad3eb22599362768654c65e1657652b653d6a6268791f63aa26c03feed6fc3dab9c4dd061883d3094ecb4cbf9b704fb603122fbbe1f1cc26477b88e99
7
+ data.tar.gz: 817b35c9e78fce50d690c3c56b605d5bfa3e609a7b32785516001eb407e286934d6126a236c736912f153a8c04479da3945ff9b5c07135acf339545c8ccfded6
@@ -0,0 +1,19 @@
1
+ name: test
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ name: Run spec
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: 3.0.1
15
+ bundler-cache: true
16
+ - name: Run spec
17
+ run: |
18
+ bundle exec rspec spec
19
+
data/CHANGELOG.md CHANGED
@@ -1,11 +1,16 @@
1
- ## 0.3.0
1
+ ## 0.0.4
2
+
3
+ ### Added
4
+ - ktlint task can be skipped by specifing `ktlint.skip_task = true` and `ktlint.report_file = '...'`
5
+
6
+ ## 0.0.3
2
7
  ### Added
3
8
  - `limit` parameter to set the maximum number of comments of ktlint results.
4
9
 
5
- ## 0.2.0
10
+ ## 0.0.2
6
11
  ### Added
7
12
  - Inline comment feature (`inline_mode: true` with ktlint.lint)
8
13
 
9
- ## 0.1.0
14
+ ## 0.0.1
10
15
  ### Added
11
16
  - Run ktlint by ktlint.lint
data/README.md CHANGED
@@ -5,7 +5,7 @@ Lint kotlin files only changed files in a pull request using ktlint command lint
5
5
  ## Installation
6
6
 
7
7
  ```ruby
8
- $ gem install danger-ktlint
8
+ gem install danger-ktlint
9
9
  ```
10
10
 
11
11
  ## Usage
@@ -14,7 +14,7 @@ You need to install `ktlint` command and set as executable first, see: https://k
14
14
 
15
15
  ```bash
16
16
  # Example
17
- $ curl --output /usr/local/bin/ktlint -sL https://github.com/pinterest/ktlint/releases/download/$KTLINT_VERSION/ktlint && chmod a+x /usr/local/bin/ktlint
17
+ curl --output /usr/local/bin/ktlint -sL https://github.com/pinterest/ktlint/releases/download/$KTLINT_VERSION/ktlint && chmod a+x /usr/local/bin/ktlint
18
18
  ```
19
19
 
20
20
  Add this to Dangerfile.
@@ -23,7 +23,7 @@ Add this to Dangerfile.
23
23
  ktlint.lint
24
24
 
25
25
  # If you want inline comments, specify `ktlint.lint` with `inline_mode: true`
26
- ktlint.lint(inline_mode: true)
26
+ # ktlint.lint(inline_mode: true)
27
27
  ```
28
28
 
29
29
  ### Options
@@ -36,7 +36,6 @@ ktlint.limit = 3
36
36
  ktlint.lint
37
37
  ```
38
38
 
39
-
40
39
  ## CHANGELOG
41
40
 
42
41
  See [CHANGELOG.md](https://github.com/mataku/danger-ktlint/blob/master/CHANGELOG.md).
@@ -44,6 +43,7 @@ See [CHANGELOG.md](https://github.com/mataku/danger-ktlint/blob/master/CHANGELOG
44
43
  ## TODO
45
44
 
46
45
  - filtering: false (default: filtering: true behavior)
46
+ - XML report_file (Currently only JSON is supported.)
47
47
 
48
48
  ## Development
49
49
 
@@ -1,3 +1,3 @@
1
1
  module Ktlint
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
data/lib/ktlint/plugin.rb CHANGED
@@ -9,6 +9,8 @@ module Danger
9
9
  # TODO: Lint all files if `filtering: false`
10
10
  attr_accessor :filtering
11
11
 
12
+ attr_accessor :skip_lint, :report_file
13
+
12
14
  def limit
13
15
  @limit ||= nil
14
16
  end
@@ -33,15 +35,16 @@ module Danger
33
35
  end
34
36
 
35
37
  targets = target_files(git.added_files + git.modified_files)
36
- return if targets.empty?
37
38
 
38
- results = JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`)
39
- return if results.empty?
39
+ results = ktlint_results(targets)
40
+ if results.nil? || results.empty?
41
+ return
42
+ end
40
43
 
41
44
  if inline_mode
42
- send_inline_comments(results)
45
+ send_inline_comments(results, targets)
43
46
  else
44
- send_markdown_comment(results)
47
+ send_markdown_comment(results, targets)
45
48
  end
46
49
  end
47
50
 
@@ -61,12 +64,14 @@ module Danger
61
64
  # ]
62
65
  # }
63
66
  # ]
64
- def send_markdown_comment(results)
67
+ def send_markdown_comment(results, targets)
65
68
  catch(:loop_break) do
66
69
  count = 0
67
70
  results.each do |result|
68
71
  result['errors'].each do |error|
69
- file = "#{result['file']}#L#{error['line']}"
72
+ file_path = relative_file_path(result['file'])
73
+ next unless targets.include?(file_path)
74
+ file = "#{file_path}#L#{error['line']}"
70
75
  message = "#{github.html_link(file)}: #{error['message']}"
71
76
  fail(message)
72
77
  unless limit.nil?
@@ -80,12 +85,14 @@ module Danger
80
85
  end
81
86
  end
82
87
 
83
- def send_inline_comments(results)
88
+ def send_inline_comments(results, targets)
84
89
  catch(:loop_break) do
85
90
  count = 0
86
91
  results.each do |result|
87
92
  result['errors'].each do |error|
88
- file = result['file']
93
+ file_path = relative_file_path(result['file'])
94
+ next unless targets.include?(file_path)
95
+
89
96
  message = error['message']
90
97
  line = error['line']
91
98
  fail(message, file: result['file'], line: line)
@@ -106,10 +113,47 @@ module Danger
106
113
  end
107
114
  end
108
115
 
116
+ # Make it a relative path so it can compare it to git.added_files
117
+ def relative_file_path(file_path)
118
+ file_path.gsub(/#{pwd}\//, '')
119
+ end
120
+
109
121
  private
110
122
 
123
+ def pwd
124
+ @pwd ||= `pwd`.chomp
125
+ end
126
+
111
127
  def ktlint_exists?
112
128
  system 'which ktlint > /dev/null 2>&1'
113
129
  end
130
+
131
+ def ktlint_results(targets)
132
+ if skip_lint
133
+ # 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)
146
+ end
147
+ else
148
+ unless ktlint_exists?
149
+ fail("Couldn't find ktlint command. Install first.")
150
+ return
151
+ end
152
+
153
+ return if targets.empty?
154
+
155
+ JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`)
156
+ end
157
+ end
114
158
  end
115
159
  end
data/spec/ktlint_spec.rb CHANGED
@@ -9,6 +9,10 @@ module Danger
9
9
  expect(Danger::DangerKtlint.new(nil)).to be_a Danger::Plugin
10
10
  end
11
11
 
12
+ before do
13
+ allow_any_instance_of(Kernel).to receive(:`).with('pwd').and_return('/home/mataku')
14
+ end
15
+
12
16
  describe '#lint' do
13
17
  before do
14
18
  allow_any_instance_of(Danger::DangerfileGitPlugin).to receive(:added_files).and_return(['app/src/main/java/com/mataku/Model.kt'])
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mataku
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-13 00:00:00.000000000 Z
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -129,7 +129,7 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
- - ".circleci/config.yml"
132
+ - ".github/workflows/test.yml"
133
133
  - ".gitignore"
134
134
  - ".rubocop.yml"
135
135
  - CHANGELOG.md
@@ -150,7 +150,7 @@ homepage: https://github.com/mataku/danger-ktlint
150
150
  licenses:
151
151
  - MIT
152
152
  metadata: {}
153
- post_install_message:
153
+ post_install_message:
154
154
  rdoc_options: []
155
155
  require_paths:
156
156
  - lib
@@ -165,8 +165,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  requirements: []
168
- rubygems_version: 3.1.2
169
- signing_key:
168
+ rubygems_version: 3.2.15
169
+ signing_key:
170
170
  specification_version: 4
171
171
  summary: Lint kotlin files using ktlint command line interface.
172
172
  test_files:
data/.circleci/config.yml DELETED
@@ -1,27 +0,0 @@
1
- version: 2
2
- jobs:
3
- build:
4
- docker:
5
- - image: ruby:2.5.3
6
- steps:
7
- - checkout
8
- - restore_cache:
9
- keys:
10
- - v1-dependencies-{{ checksum "Gemfile.lock" }}
11
- # fallback to using the latest cache if no exact match is found
12
- - v1-dependencies-
13
-
14
- - run:
15
- name: Install Dependencies
16
- command: |
17
- bundle install --jobs=4 --path vendor/bundle
18
-
19
- - save_cache:
20
- paths:
21
- - vendor/bundle
22
- key: v1-dependencies-{{ checksum "Gemfile.lock" }}
23
-
24
- - run:
25
- name: Run RSpec
26
- command: |
27
- bundle exec rspec