rubocop_pr 0.2.1 → 0.2.2

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: aef722f12c0f5f554bd332fc359161edf289e9585995e0b991ed8f55951dd9c3
4
- data.tar.gz: 6fd6c986ed8a4c9549ac86e813fce081029a2cef663263cc69208887dfe4764c
3
+ metadata.gz: 3ff53c3508b18d189344f9f2c7bab5d07f1337772c416784309b840fb8d88d72
4
+ data.tar.gz: dd7fe6f7ad3e2030193a53c17ca48a3a9f17c7dfdd8e523c855cb8dae5d1b80c
5
5
  SHA512:
6
- metadata.gz: fe66335b20d16dafff9f16e701af1189d5cb18acf0f26598b06643478f1d17a35729d997c2b1d26258ea7562c4cc5dad09eaf2133705743806566b7cd66d4bcc
7
- data.tar.gz: b4d56f5d0b50393aa252671ab04cfb850c1df2eb8855fdf0bed9e18f8eaf8e2b3d1426ada8e0d9623a740dc0fd3f4594c751974da0723fe3a7f361b9174879f1
6
+ metadata.gz: b5d22dabd6c3241deb4ee2ceb158892a622cb306c5a0de3f65ebb3a91b8c9eb2642764783179b88bc3ed4b99d507a22ba57f147dfea9d88e52338b3ca1fec22f
7
+ data.tar.gz: 70f7bd4221e41fa826c8a89304aee2a38ceeb76a650bf68fdc9dad451d7ce820fc773aa68e941aa322fc7ffdff5a354181e4aec3e74b9b79ae56b6e044c0dabd
data/.rubocop.yml CHANGED
@@ -8,4 +8,7 @@ Metrics/BlockLength:
8
8
  ExcludedMethods: ['describe', 'context']
9
9
 
10
10
  Metrics/AbcSize:
11
- Max: 20
11
+ Max: 20
12
+
13
+ Metrics/ClassLength:
14
+ Max: 150
data/README.md CHANGED
@@ -38,15 +38,21 @@ Or install it yourself as:
38
38
 
39
39
  ```bash
40
40
  Usage: rubocop_pr [options]
41
- -u, --hub-version [version] Set manually minimum required version of 'hub' utility for github (default: 2.12.3)
42
- -b, --branch [branch] internal branch with '.rubocop_todo.yml' (default: 'rubocop_todo_branch')
43
- -m, --master [branch] branch which will be the base for all PR's (default: 'master')
44
41
  -r, --post-checkout [command] Running after each git checkout (default: "")
45
- -l, --limit [limit] Limit the PS's for one run (default: 10)
46
- -g, --repository [name] Set repository host (default: github)
47
- -o, --origin [origin] origin option for 'git push' (default: 'origin')
48
42
  -c, --continue Continue previous session (default: false)
43
+ -b, --branch [branch] internal branch with '.rubocop_todo.yml' (default: 'rubocop_todo_branch')
44
+ -m, --master [branch] branch which will be the base for all PR's (default: 'master')
49
45
  -v, --version Display version
46
+ -o, --origin [origin] origin option for 'git push' (default: 'origin')
47
+ -u, --hub-version [version] Set manually minimum required version of 'hub' utility for github (default: 2.12.3)
48
+ -i, --issue-labels [labels] Labels for created issues, separated by comma (default: rubocop)
49
+ -p [labels], Labels for created pull requests, separated by comma (default: rubocop)
50
+ --pull-request-labels
51
+ -a, --issue-assignees [name] Issue assignees, separated by comma (default: "")
52
+ -t [name], Pull request assignees, separated by comma (default: "")
53
+ --pull-request-assignees
54
+ -g, --repository [name] Set repository host (default: github)
55
+ -l, --limit [limit] Limit the PS's for one run (default: 10)
50
56
  -h, --help Display help
51
57
  ```
52
58
 
@@ -0,0 +1,54 @@
1
+ module RubocopPr
2
+ class CLI
3
+ # Cop processor
4
+ class ProcessCop
5
+ attr_reader :options, :git, :repository, :cop
6
+
7
+ def initialize(git:, repository:, cop:, options:)
8
+ @git = git
9
+ @repository = repository
10
+ @cop = cop
11
+ @options = options
12
+ end
13
+
14
+ def call
15
+ return false if exit_early?
16
+ checkout_to_target_branch_throw_master_branch
17
+ git.commit_all(issue.title)
18
+ git.push
19
+ create_pr
20
+ end
21
+
22
+ private
23
+
24
+ def rubocop
25
+ @rubocop ||= RubocopPr::Rubocop.new(git: git)
26
+ end
27
+
28
+ def exit_early?
29
+ return true if options.continue && cop_was_processed? || git.status.blank?
30
+ Rubocop.correct!
31
+
32
+ git.checkout Rubocop::TODO_FILENAME
33
+ git.status.blank?
34
+ end
35
+
36
+ def cop_was_processed?
37
+ git.branch.match cop.branch
38
+ end
39
+
40
+ def issue
41
+ @issue ||= repository.issue(cop: cop, **options.to_h).create
42
+ end
43
+
44
+ def checkout_to_target_branch_throw_master_branch
45
+ git.checkout(options.master_branch)
46
+ git.checkout("#{issue.number}-#{cop.branch}")
47
+ end
48
+
49
+ def create_pr
50
+ repository.pull_request(cop: cop, body: "Closes ##{issue.number}", **options.to_h).create
51
+ end
52
+ end
53
+ end
54
+ end
@@ -15,35 +15,19 @@ module RubocopPr
15
15
  def run!
16
16
  EnvironmentChecker.call(repository, options)
17
17
  run
18
+ git.checkout Rubocop::TODO_FILENAME
18
19
  git.checkout(options.master_branch)
19
20
  end
20
21
 
21
22
  private
22
23
 
23
24
  def run
24
- rubocop.inject(0) do |counter, cop|
25
- next counter if git.status.blank?
26
- next counter if options.continue && git.branch =~ /#{branch_suffix(cop)}\s/
27
- next counter unless rubocop.corrected?
28
- process_cop(cop)
25
+ rubocop.each do |cop|
26
+ break if options.limit <= 0
29
27
 
30
- break if counter >= options.limit - 1
31
- counter + 1
28
+ ProcessCop.new(git: git, repository: repository, cop: cop, options: options).call
29
+ options.limit -= 1
32
30
  end
33
31
  end
34
-
35
- def process_cop(cop)
36
- git.checkout(options.master_branch)
37
- title = "Fix Rubocop #{cop} warnings"
38
- issue_number = repository.create_issue(title: title)
39
- git.checkout("#{issue_number}-#{branch_suffix(cop)}")
40
- git.commit_all(title)
41
- git.push
42
- repository.create_pull_request(title: title, body: "Closes ##{issue_number}")
43
- end
44
-
45
- def branch_suffix(cop)
46
- "rubocop-fix-#{cop.underscore.tr('/_', '-')}"
47
- end
48
32
  end
49
33
  end
@@ -0,0 +1,18 @@
1
+ module RubocopPr
2
+ # Simple representation of the Cop
3
+ class Cop
4
+ attr_reader :name
5
+
6
+ def initialize(name:)
7
+ @name = name
8
+ end
9
+
10
+ def to_s
11
+ name.to_s
12
+ end
13
+
14
+ def branch
15
+ "rubocop-fix-#{name.underscore.tr('/_', '-')}"
16
+ end
17
+ end
18
+ end
@@ -17,24 +17,19 @@ module RubocopPr
17
17
 
18
18
  private
19
19
 
20
- def build_parser # rubocop:disable Metrics/MethodLength
21
- @parser = OptionParser.new do |opts|
20
+ attr_accessor :opts
21
+
22
+ # options will be printed in order, as they are declared in this file
23
+ def build_parser
24
+ @parser = OptionParser.new do |op|
25
+ self.opts = op
22
26
  opts.banner = 'Usage: rubocop_pr [options]'
23
27
 
24
- add_hub_version_option(opts)
25
- add_rubocop_todo_branch_option(opts)
26
- add_master_branch_option(opts)
27
- add_post_checkout_option(opts)
28
- add_limit_option(opts)
29
- add_repository_option(opts)
30
- add_git_origin_option(opts)
31
- add_continue_option(opts)
32
- add_version_option(opts)
33
- add_on_tail(opts)
28
+ private_methods(false).map(&:to_s).select { |m| m.start_with?('add_') }.each { |m| send m }
34
29
  end
35
30
  end
36
31
 
37
- def add_limit_option(opts)
32
+ def add_limit_option
38
33
  @options.limit = 10
39
34
  msg = "Limit the PS's for one run (default: 10)"
40
35
  opts.on('-l [limit]', '--limit [limit]', Integer, msg) do |v|
@@ -42,7 +37,7 @@ module RubocopPr
42
37
  end
43
38
  end
44
39
 
45
- def add_post_checkout_option(opts)
40
+ def add_post_checkout_option
46
41
  @options.post_checkout = ''
47
42
  msg = 'Running after each git checkout (default: "")'
48
43
  opts.on('-r [command]', '--post-checkout [command]', String, msg) do |v|
@@ -50,7 +45,7 @@ module RubocopPr
50
45
  end
51
46
  end
52
47
 
53
- def add_rubocop_todo_branch_option(opts)
48
+ def add_rubocop_todo_branch_option
54
49
  @options.rubocop_todo_branch = 'rubocop_todo_branch'
55
50
  msg = "internal branch with '.rubocop_todo.yml' (default: 'rubocop_todo_branch')"
56
51
  opts.on('-b [branch]', '--branch [branch]', String, msg) do |v|
@@ -58,7 +53,7 @@ module RubocopPr
58
53
  end
59
54
  end
60
55
 
61
- def add_master_branch_option(opts)
56
+ def add_master_branch_option
62
57
  @options.master_branch = 'master'
63
58
  msg = "branch which will be the base for all PR's (default: 'master')"
64
59
  opts.on('-m [branch]', '--master [branch]', String, msg) do |v|
@@ -66,7 +61,7 @@ module RubocopPr
66
61
  end
67
62
  end
68
63
 
69
- def add_git_origin_option(opts)
64
+ def add_git_origin_option
70
65
  @options.git_origin = 'origin'
71
66
  msg = "origin option for 'git push' (default: 'origin')"
72
67
  opts.on('-o [origin]', '--origin [origin]', String, msg) do |v|
@@ -74,7 +69,7 @@ module RubocopPr
74
69
  end
75
70
  end
76
71
 
77
- def add_hub_version_option(opts)
72
+ def add_hub_version_option
78
73
  @options.hub_version = HUB_VERSION
79
74
  msg = "Set manually minimum required version of 'hub' utility for github (default: #{HUB_VERSION})"
80
75
  opts.on('-u [version] ', '--hub-version [version]', msg) do |v|
@@ -82,7 +77,39 @@ module RubocopPr
82
77
  end
83
78
  end
84
79
 
85
- def add_repository_option(opts)
80
+ def add_issue_labels_option
81
+ @options.issue_labels = ['rubocop']
82
+ msg = 'Labels for created issues, separated by comma (default: rubocop)'
83
+ opts.on('-i [labels] ', '--issue-labels [labels]', Array, msg) do |v|
84
+ @options.issue_labels = v
85
+ end
86
+ end
87
+
88
+ def add_pull_request_labels_option
89
+ @options.pull_request_labels = ['rubocop']
90
+ msg = 'Labels for created pull requests, separated by comma (default: rubocop)'
91
+ opts.on('-p [labels] ', '--pull-request-labels [labels]', Array, msg) do |v|
92
+ @options.pull_request_labels = v
93
+ end
94
+ end
95
+
96
+ def add_issue_assignees_option
97
+ @options.issue_assignees = []
98
+ msg = 'Issue assignees, separated by comma (default: "")'
99
+ opts.on('-a [name] ', '--issue-assignees [name]', Array, msg) do |v|
100
+ @options.issue_assignees = v
101
+ end
102
+ end
103
+
104
+ def add_pull_request_assignees_option
105
+ @options.pull_request_assignees = []
106
+ msg = 'Pull request assignees, separated by comma (default: "")'
107
+ opts.on('-t [name] ', '--pull-request-assignees [name]', Array, msg) do |v|
108
+ @options.pull_request_assignees = v
109
+ end
110
+ end
111
+
112
+ def add_repository_option
86
113
  @options.repository = 'github'
87
114
  msg = 'Set repository host (default: github)'
88
115
  opts.on('-g [name] ', '--repository [name]', msg) do |v|
@@ -90,21 +117,21 @@ module RubocopPr
90
117
  end
91
118
  end
92
119
 
93
- def add_continue_option(opts)
120
+ def add_continue_option
94
121
  @options.continue = false
95
122
  opts.on('-c', '--continue', 'Continue previous session (default: false)') do |_v|
96
123
  @options.continue = true
97
124
  end
98
125
  end
99
126
 
100
- def add_version_option(opts)
127
+ def add_version_option
101
128
  opts.on('-v', '--version', 'Display version') do
102
129
  puts RubocopPr::VERSION
103
130
  exit
104
131
  end
105
132
  end
106
133
 
107
- def add_on_tail(opts)
134
+ def add_on_tail
108
135
  opts.on_tail('-h', '--help', 'Display help') do
109
136
  puts opts
110
137
  exit
@@ -2,18 +2,72 @@ module RubocopPr
2
2
  module Repositories
3
3
  # Github repository
4
4
  class Github < RubocopPr::Repository
5
- # The representation of the Issue
6
- class Issue
7
- attr_reader :title, :body
5
+ # Base class for working with `hub` utility
6
+ class Base
7
+ attr_reader :title, :body, :cop, :number
8
8
 
9
- def initialize(title:, **other)
10
- @title = title
11
- @body = other[:body] || default_body
9
+ def initialize(cop:, **options)
10
+ @cop = cop
11
+ @options = options
12
+ @title = options.delete(:title) || default_title
13
+ @body = options.delete(:body) || default_body
12
14
  end
13
15
 
14
16
  def create
15
- link = `hub issue create -m '#{title}' -m '#{body}'`
16
- link.split('/').last.to_i
17
+ @number = `#{build_command}`.split('/').last.to_i
18
+ self
19
+ end
20
+
21
+ def assignees
22
+ @assignees ||= Array options[:assignees]
23
+ end
24
+
25
+ def labels
26
+ @labels ||= Array options[:labels]
27
+ end
28
+
29
+ def build_command
30
+ [command, cli_options].join ' '
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :options
36
+
37
+ def command
38
+ raise NotImplemented
39
+ end
40
+
41
+ def cli_options
42
+ [].tap do |opt|
43
+ opt << "-m '#{title}'"
44
+ opt << "-m '#{body}'" unless body.blank?
45
+ opt << "-a '#{assignees.join(',')}'" unless assignees.blank?
46
+ opt << "-l '#{labels.join(',')}'" unless labels.blank?
47
+ end.join(' ')
48
+ end
49
+
50
+ def default_title
51
+ "Fix Rubocop #{cop} warnings"
52
+ end
53
+
54
+ def default_body
55
+ ''
56
+ end
57
+ end
58
+
59
+ # The representation of the Issue
60
+ class Issue < Base
61
+ def initialize(cop:, **opt)
62
+ super
63
+ @assignees = Array opt[:issue_assignees]
64
+ @labels = Array opt[:issue_labels]
65
+ end
66
+
67
+ private
68
+
69
+ def command
70
+ 'hub issue create'
17
71
  end
18
72
 
19
73
  def default_body
@@ -23,17 +77,17 @@ module RubocopPr
23
77
  end
24
78
 
25
79
  # The representation of the PR
26
- class PullRequest
27
- attr_reader :title, :body
28
-
29
- def initialize(title:, **other)
30
- @title = title
31
- @body = other[:body] || ''
80
+ class PullRequest < Base
81
+ def initialize(cop:, **opt)
82
+ super
83
+ @assignees = Array opt[:pull_request_assignees]
84
+ @labels = Array opt[:pull_request_labels]
32
85
  end
33
86
 
34
- def create
35
- link = `hub pull-request create -m '#{title}' -m '#{body}'`
36
- link.split('/').last.to_i
87
+ private
88
+
89
+ def command
90
+ 'hub pull-request create'
37
91
  end
38
92
  end
39
93
 
@@ -42,12 +96,12 @@ module RubocopPr
42
96
  super + [RubocopPr::Repositories::Github::Checks::VerifyHubVersion]
43
97
  end
44
98
 
45
- def create_issue(*args)
46
- Issue.new(*args).create
99
+ def issue(*args)
100
+ Issue.new(*args)
47
101
  end
48
102
 
49
- def create_pull_request(*args)
50
- PullRequest.new(*args).create
103
+ def pull_request(*args)
104
+ PullRequest.new(*args)
51
105
  end
52
106
  end
53
107
  end
@@ -5,6 +5,16 @@ module RubocopPr
5
5
 
6
6
  TODO_FILENAME = '.rubocop_todo.yml'.freeze
7
7
 
8
+ class << self
9
+ def generate_todo
10
+ system 'bundle exec rubocop --auto-gen-config'
11
+ end
12
+
13
+ def correct!
14
+ system 'bundle exec rubocop -a'
15
+ end
16
+ end
17
+
8
18
  attr_reader :branch, :git
9
19
 
10
20
  def initialize(**options)
@@ -13,36 +23,25 @@ module RubocopPr
13
23
  end
14
24
 
15
25
  def todo
16
- YAML.safe_load(read_or_generate_todo)
26
+ @todo ||= YAML.safe_load(read_or_generate_todo)
17
27
  end
18
28
 
19
29
  def each
20
- git.checkout(branch)
21
- todos_backup = todo
22
- todos_backup.keys.sort.reverse_each do |cop|
30
+ todo.keys.sort.reverse_each do |cop|
23
31
  git.checkout(branch)
24
- todos = todos_backup.dup
25
- todos.delete cop
26
- File.open(TODO_FILENAME, 'w') { |f| f.write todos.blank? ? '' : YAML.dump(todos) }
27
- yield cop
32
+ File.open(TODO_FILENAME, 'w') do |f|
33
+ f.write todo.except(cop.to_s).blank? ? '' : YAML.dump(todo.except(cop.to_s))
34
+ end
35
+ yield Cop.new(name: cop)
28
36
  end
29
37
  end
30
38
 
31
39
  def read_or_generate_todo
40
+ git.checkout(branch)
32
41
  return File.read(TODO_FILENAME) if File.exist?(TODO_FILENAME)
33
- generate_todo
42
+ self.class.generate_todo
34
43
  git.commit_all('Generate initial Rubocop todo file')
35
44
  File.read(TODO_FILENAME)
36
45
  end
37
-
38
- def generate_todo
39
- `bundle exec rubocop --auto-gen-config`
40
- end
41
-
42
- def corrected?
43
- `bundle exec rubocop -a`
44
- git.checkout TODO_FILENAME
45
- !git.status.blank?
46
- end
47
46
  end
48
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubocopPr
4
- VERSION = '0.2.1'.freeze
4
+ VERSION = '0.2.2'.freeze
5
5
  end
data/lib/rubocop_pr.rb CHANGED
@@ -10,6 +10,8 @@ require 'pry'
10
10
  require 'rubocop_pr/version'
11
11
  require 'rubocop_pr/options'
12
12
  require 'rubocop_pr/environment_checker'
13
+ require 'rubocop_pr/cli/process_cop'
14
+ require 'rubocop_pr/cop'
13
15
  require 'rubocop_pr/rubocop'
14
16
  require 'rubocop_pr/git'
15
17
  require 'rubocop_pr/repository'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop_pr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kvokka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2019-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -152,6 +152,8 @@ files:
152
152
  - exe/rubocop_pr
153
153
  - lib/rubocop_pr.rb
154
154
  - lib/rubocop_pr/cli.rb
155
+ - lib/rubocop_pr/cli/process_cop.rb
156
+ - lib/rubocop_pr/cop.rb
155
157
  - lib/rubocop_pr/environment_checker.rb
156
158
  - lib/rubocop_pr/git.rb
157
159
  - lib/rubocop_pr/options.rb