rubocop_challenger 1.1.2 → 1.2.0

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: 252260c3691779a498961865b583b2274f5d961264a2e59a63635442e4734a4a
4
- data.tar.gz: 7ee8534542acb8ca0a32a50435aa9b0d4dec16ca6258cc9c8b2623b8b80bcaf1
3
+ metadata.gz: ca4060e23f09a0c2a36203f0ce089fe50c91d789ba6ed19a108633ee5f874048
4
+ data.tar.gz: cd0fd5ab050e01188cf86513734ba468c754cda0dcf946315fb51766e60bc2f9
5
5
  SHA512:
6
- metadata.gz: e0a0215bc77a300f97f1e1fdba58767ad868c6e723d9a6783dbb288ceef4a29d49dad6c8958c6754b267df1d973ceae61d758557cbab8e8e26d7717fe83e1971
7
- data.tar.gz: 6ce3ac68d4d3084cd9cf29023b6197600192c93abdbf235af3348cfa7cd810c24edfd7b6b344f31f6b8888cd8d29a2b387a34c58eb7de592acaf26c3560e5244
6
+ metadata.gz: a67157938b07fa52da89dbcbc3ef4dbcdd244207131dbe7dbcca31823c13de13d98810380a99e61c96f1535c873ee20265eb0e28a0d408780334235887c3a6b4
7
+ data.tar.gz: 3d1fa7adc9d4cb3b6516faf6aebced9609098e69275fb92dbce0af7589a0edb7b308d9f8e4209f2f869d0a708f89b5c36c58c98298d392a08a420b438118d6f0
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-02-21 12:01:12 +0900 using RuboCop version 0.65.0.
3
+ # on 2019-02-27 22:11:30 +0900 using RuboCop version 0.65.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop_challenger (1.1.2)
4
+ rubocop_challenger (1.2.0)
5
5
  octokit
6
6
  rubocop
7
7
  rubocop-rspec
@@ -14,19 +14,28 @@ GEM
14
14
  addressable (2.5.2)
15
15
  public_suffix (>= 2.0.2, < 4.0)
16
16
  ast (2.4.0)
17
+ byebug (11.0.0)
18
+ coderay (1.1.2)
17
19
  diff-lcs (1.3)
18
20
  docile (1.3.1)
19
21
  faraday (0.15.4)
20
22
  multipart-post (>= 1.2, < 3)
21
23
  jaro_winkler (1.5.2)
22
- json (2.1.0)
24
+ json (2.2.0)
25
+ method_source (0.9.2)
23
26
  multipart-post (2.0.0)
24
27
  octokit (4.13.0)
25
28
  sawyer (~> 0.8.0, >= 0.5.3)
26
- parallel (1.13.0)
29
+ parallel (1.14.0)
27
30
  parser (2.6.0.0)
28
31
  ast (~> 2.4.0)
29
32
  powerpack (0.1.2)
33
+ pry (0.12.2)
34
+ coderay (~> 1.1.0)
35
+ method_source (~> 0.9.0)
36
+ pry-byebug (3.7.0)
37
+ byebug (~> 11.0)
38
+ pry (~> 0.10)
30
39
  psych (3.1.0)
31
40
  public_suffix (3.0.3)
32
41
  rainbow (3.0.0)
@@ -75,6 +84,7 @@ PLATFORMS
75
84
 
76
85
  DEPENDENCIES
77
86
  bundler (~> 1.16)
87
+ pry-byebug
78
88
  rake (~> 10.0)
79
89
  rspec
80
90
  rspec_junit_formatter
data/README.md CHANGED
@@ -104,8 +104,6 @@ Options:
104
104
  # Default: master
105
105
  l, [--labels=one two three] # Label to give to Pull Request
106
106
  # Default: ["rubocop challenge"]
107
- [--regenerate-rubocop-todo], [--no-regenerate-rubocop-todo] # Rerun `$ rubocop --auto-gen-config` after autocorrect
108
- # Default: true
109
107
  [--no-commit] # No commit after autocorrect
110
108
 
111
109
  Run `$ rubocop --auto-correct` and create PR to GitHub repo
@@ -15,6 +15,7 @@ version_file = File.read(VERSION_FILE_PATH)
15
15
  # Modify the file contents
16
16
  version_file.sub!('# frozen_string_literal: true', '')
17
17
  version_file.gsub!("\n", " \n ")
18
+ version_file.concat("\nputs 'hoge'; puts 'fuga'")
18
19
 
19
20
  # Write the origin ruby file
20
21
  File.write(VERSION_FILE_PATH, version_file)
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_runtime_dependency 'yard'
30
30
 
31
31
  spec.add_development_dependency 'bundler', '~> 1.16'
32
+ spec.add_development_dependency 'pry-byebug'
32
33
  spec.add_development_dependency 'rake', '~> 10.0'
33
34
  spec.add_development_dependency 'rspec'
34
35
  spec.add_development_dependency 'rspec_junit_formatter'
@@ -9,11 +9,13 @@ require 'rubocop_challenger/errors'
9
9
  require 'rubocop_challenger/version'
10
10
  require 'rubocop_challenger/command_line'
11
11
  require 'rubocop_challenger/rubocop/rule'
12
+ require 'rubocop_challenger/rubocop/config_editor'
12
13
  require 'rubocop_challenger/rubocop/todo_reader'
13
14
  require 'rubocop_challenger/rubocop/todo_writer'
14
15
  require 'rubocop_challenger/rubocop/command'
15
16
  require 'rubocop_challenger/rubocop/challenge'
16
17
  require 'rubocop_challenger/rubocop/yardoc'
18
+ require 'rubocop_challenger/go'
17
19
  require 'rubocop_challenger/cli'
18
20
  require 'rubocop_challenger/git/command'
19
21
  require 'rubocop_challenger/github/client'
@@ -41,19 +41,14 @@ module RubocopChallenger
41
41
  default: ['rubocop challenge'],
42
42
  aliases: :l,
43
43
  desc: 'Label to give to Pull Request'
44
- option :'regenerate-rubocop-todo',
45
- type: :boolean,
46
- default: true,
47
- desc: 'Rerun `$ rubocop --auto-gen-config` after autocorrect'
48
44
  option :'no-commit',
49
45
  type: :boolean,
50
46
  default: false,
51
47
  desc: 'No commit after autocorrect'
52
48
  def go
53
- regenerate_rubocop_todo
54
- target_rule = rubocop_challenge
55
- regenerate_rubocop_todo
56
- create_pull_request(target_rule)
49
+ Go.new(options).exec
50
+ rescue Errors::NoAutoCorrectableRule => e
51
+ color_puts e.message, CommandLine::YELLOW
57
52
  rescue StandardError => e
58
53
  color_puts e.message, CommandLine::RED
59
54
  exit_process!
@@ -74,54 +69,6 @@ module RubocopChallenger
74
69
 
75
70
  private
76
71
 
77
- def pr_creater
78
- @pr_creater ||= Github::PrCreater.new(
79
- branch: "rubocop-challenge/#{timestamp}",
80
- user_name: options[:name],
81
- user_email: options[:email]
82
- )
83
- end
84
-
85
- def rubocop_challenge
86
- target_rule = Rubocop::Challenge.exec(options[:file_path], options[:mode])
87
- pr_creater.commit ":police_car: #{target_rule.title}"
88
- target_rule
89
- end
90
-
91
- def regenerate_rubocop_todo
92
- return unless options[:'regenerate-rubocop-todo']
93
-
94
- pr_creater.commit ':police_car: regenerate rubocop todo' do
95
- Rubocop::Command.new.auto_gen_config
96
- end
97
- end
98
-
99
- def create_pull_request(rule)
100
- pr_creater_options = generate_pr_creater_options(rule)
101
- return if options[:'no-commit']
102
-
103
- pr_creater.create_pr(pr_creater_options)
104
- end
105
-
106
- def generate_pr_creater_options(rule)
107
- {
108
- title: "#{rule.title}-#{timestamp}",
109
- body: generate_pr_body(rule),
110
- base: options[:base],
111
- labels: options[:labels]
112
- }
113
- end
114
-
115
- def generate_pr_body(rule)
116
- Github::PrTemplate
117
- .new(rule, options[:template])
118
- .generate_pullrequest_markdown
119
- end
120
-
121
- def timestamp
122
- @timestamp ||= Time.now.strftime('%Y%m%d%H%M%S')
123
- end
124
-
125
72
  # Exit process (Mainly for mock when testing)
126
73
  def exit_process!
127
74
  exit!
@@ -3,5 +3,12 @@
3
3
  module RubocopChallenger
4
4
  module Errors
5
5
  class ExistUncommittedModify < StandardError; end
6
+
7
+ # Raise if no auto-correctable rule in the `.rubocop_todo.yml`.
8
+ class NoAutoCorrectableRule < StandardError
9
+ def initialize
10
+ super 'There is no auto-correctable rule'
11
+ end
12
+ end
6
13
  end
7
14
  end
@@ -54,6 +54,19 @@ module RubocopChallenger
54
54
  true
55
55
  end
56
56
 
57
+ # Stubing each PrCreater methods for testing
58
+ class Mock
59
+ def initialize(*); end
60
+
61
+ def commit(*)
62
+ yield if block_given?
63
+ end
64
+
65
+ def create_pr(*)
66
+ true
67
+ end
68
+ end
69
+
57
70
  private
58
71
 
59
72
  attr_reader :git, :github, :topic_branch, :initial_sha1
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubocopChallenger
4
+ # Executes Rubocop Challenge flow
5
+ class Go
6
+ include CommandLine
7
+
8
+ # @param options [Hash] describe_options_here
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ # @raise [Errors::NoAutoCorrectableRule]
14
+ def exec
15
+ regenerate_rubocop_todo!
16
+ corrected_rule = rubocop_challenge!
17
+ regenerate_rubocop_todo!
18
+ if auto_correct_incomplete?(corrected_rule)
19
+ add_ignore_list!(corrected_rule)
20
+ end
21
+ create_pull_request!(corrected_rule)
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :options
27
+
28
+ # Re-generate .rubocop_todo.yml and run git commit.
29
+ def regenerate_rubocop_todo!
30
+ pr_creater.commit ':police_car: regenerate rubocop todo' do
31
+ Rubocop::Command.new.auto_gen_config
32
+ end
33
+ end
34
+
35
+ # Run rubocop challenge.
36
+ #
37
+ # @return [Rubocop::Rule] The corrected rule
38
+ def rubocop_challenge!
39
+ corrected_rule =
40
+ Rubocop::Challenge.exec(options[:file_path], options[:mode])
41
+ pr_creater.commit ":police_car: #{corrected_rule.title}"
42
+ corrected_rule
43
+ end
44
+
45
+ # GitHub PR creater instance.
46
+ def pr_creater
47
+ @pr_creater ||= Github::PrCreater.new(
48
+ branch: "rubocop-challenge/#{timestamp}",
49
+ user_name: options[:name],
50
+ user_email: options[:email]
51
+ )
52
+ end
53
+
54
+ # Check the challenge result. When the challenge successed, the rule dose
55
+ # not exist in the .rubocop_todo.yml after regenerate it too.
56
+ #
57
+ # @param rule [Rubocop::Rule] The target rule
58
+ # @return [Boolean] Return true if the challenge successed
59
+ def auto_correct_incomplete?(rule)
60
+ todo_reader = Rubocop::TodoReader.new(options[:file_path])
61
+ todo_reader.all_rules.include?(rule)
62
+ end
63
+
64
+ # If still exist the rule, the rule regard as cannot correct automatically
65
+ # then add to ignore list and it is not chosen as target rule from next
66
+ # time.
67
+ #
68
+ # @param rule [Rubocop::Rule] The target rule
69
+ def add_ignore_list!(rule)
70
+ pr_creater.commit ':police_car: add the rule to the ignore list' do
71
+ config_editor = Rubocop::ConfigEditor.new
72
+ config_editor.add_ignore(rule)
73
+ config_editor.save
74
+ end
75
+ message = <<~MSG
76
+ Rubocop Challenger has executed auto-correcting but it is incomplete.
77
+ Therefore the rule add to ignore list.
78
+ MSG
79
+ color_puts message, CommandLine::YELLOW
80
+ end
81
+
82
+ # Create a PR with description of what modification were made.
83
+ #
84
+ # @param rule [Rubocop::Rule] The target rule
85
+ def create_pull_request!(rule)
86
+ pr_creater_options = generate_pr_creater_options(rule)
87
+ return if options[:'no-commit']
88
+
89
+ pr_creater.create_pr(pr_creater_options)
90
+ end
91
+
92
+ def generate_pr_creater_options(rule)
93
+ {
94
+ title: "#{rule.title}-#{timestamp}",
95
+ body: generate_pr_body(rule),
96
+ base: options[:base],
97
+ labels: options[:labels]
98
+ }
99
+ end
100
+
101
+ def generate_pr_body(rule)
102
+ Github::PrTemplate
103
+ .new(rule, options[:template])
104
+ .generate_pullrequest_markdown
105
+ end
106
+
107
+ def timestamp
108
+ @timestamp ||= Time.now.strftime('%Y%m%d%H%M%S')
109
+ end
110
+ end
111
+ end
@@ -19,6 +19,7 @@ module RubocopChallenger
19
19
  @todo_writer = Rubocop::TodoWriter.new(file_path)
20
20
  end
21
21
 
22
+ # @raise [Errors::NoAutoCorrectableRule]
22
23
  def exec
23
24
  verify_target_rule
24
25
  todo_writer.delete_rule(target_rule)
@@ -29,8 +30,7 @@ module RubocopChallenger
29
30
  def verify_target_rule
30
31
  return unless target_rule.nil?
31
32
 
32
- puts 'There is no auto-correctable rule'
33
- exit
33
+ raise Errors::NoAutoCorrectableRule
34
34
  end
35
35
 
36
36
  def target_rule
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module RubocopChallenger
6
+ module Rubocop
7
+ # To edit rubocop_challenger config file
8
+ class ConfigEditor
9
+ DEFAULT_FILE_PATH = '.rubocop_challenge.yml'
10
+
11
+ attr_reader :data, :file_path
12
+
13
+ def initialize(file_path: DEFAULT_FILE_PATH)
14
+ @file_path = file_path
15
+ @data = FileTest.exist?(file_path) ? YAML.load_file(file_path) : {}
16
+ end
17
+
18
+ # Get ignored rules
19
+ #
20
+ # @return [Array<String>] Ignored rules
21
+ def ignored_rules
22
+ data['Ignore'] || []
23
+ end
24
+
25
+ # Add ignore rule to the config data
26
+ #
27
+ # @param rules [Array<Rubocop::Rule>] The target rules
28
+ def add_ignore(*rules)
29
+ data['Ignore'] ||= []
30
+ rules.each { |rule| data['Ignore'] << rule.title }
31
+ data['Ignore'].sort!.uniq!
32
+ end
33
+
34
+ # Save setting to the config file as YAML
35
+ def save
36
+ File.open(file_path, 'w') do |file|
37
+ YAML.dump(data, file)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -14,6 +14,14 @@ module RubocopChallenger
14
14
  @offense_count = extract_offense_count
15
15
  end
16
16
 
17
+ # Compare to other rule
18
+ #
19
+ # @param other [Rule] Target of comparison
20
+ # @return [Boolean] Return true in case of coincidence
21
+ def ==(other)
22
+ title == other.title
23
+ end
24
+
17
25
  def <=>(other)
18
26
  offense_count <=> other.offense_count
19
27
  end
@@ -37,9 +37,17 @@ module RubocopChallenger
37
37
  .read(rubocop_todo_file_path)
38
38
  .split(/\n{2,}/)
39
39
  .map! { |content| Rule.new(content) }
40
- .reject! { |rule| rule.offense_count.zero? }
40
+ .reject! { |rule| invalid?(rule) }
41
41
  .sort!
42
42
  end
43
+
44
+ def invalid?(rule)
45
+ rule.offense_count.zero? || ignored_rules.include?(rule.title)
46
+ end
47
+
48
+ def ignored_rules
49
+ @ignored_rules ||= ConfigEditor.new.ignored_rules
50
+ end
43
51
  end
44
52
  end
45
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubocopChallenger
4
- VERSION = '1.1.2'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop_challenger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryosuke_sato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2019-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -168,7 +182,6 @@ files:
168
182
  - Gemfile
169
183
  - Gemfile.lock
170
184
  - LICENSE
171
- - LICENSE.txt
172
185
  - README.md
173
186
  - Rakefile
174
187
  - bin/console
@@ -188,8 +201,10 @@ files:
188
201
  - lib/rubocop_challenger/github/client.rb
189
202
  - lib/rubocop_challenger/github/pr_creater.rb
190
203
  - lib/rubocop_challenger/github/pr_template.rb
204
+ - lib/rubocop_challenger/go.rb
191
205
  - lib/rubocop_challenger/rubocop/challenge.rb
192
206
  - lib/rubocop_challenger/rubocop/command.rb
207
+ - lib/rubocop_challenger/rubocop/config_editor.rb
193
208
  - lib/rubocop_challenger/rubocop/rule.rb
194
209
  - lib/rubocop_challenger/rubocop/todo_reader.rb
195
210
  - lib/rubocop_challenger/rubocop/todo_writer.rb
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018 ryosuke_sato
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.