Class: Nucop::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/nucop/cli.rb

Instance Method Summary collapse

Instance Method Details

#diffObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nucop/cli.rb', line 43

def diff
  puts "Running on files changed relative to '#{options[:"commit-spec"]}' (specify using the 'commit-spec' option)"
  diff_filter = options[:"added-only"] ? "A" : "d"
  diff_base = capture_std_out("git merge-base HEAD #{options[:"commit-spec"]}").chomp

  files, diff_status = Open3.capture2("git diff #{diff_base} --diff-filter=#{diff_filter} --name-only | grep \"\\.rb$\"")

  if diff_status != 0
    if options[:exit]
      puts "There are no rb files present in diff. Exiting."
      exit 0
    else
      puts "There are no rb files present in diff."
      return true
    end
  end

  if options[:ignore] && File.exist?(options[:diffignore_file]) && !File.empty?(options[:diffignore_file])
    files, non_ignored_diff_status = Open3.capture2("grep -v -f #{options[:diffignore_file]}", stdin_data: files)

    if non_ignored_diff_status != 0
      if options[:exit]
        puts "There are no non-ignored rb files present in diff. Exiting."
        exit 0
      else
        puts "There are no non-ignored rb files present in diff."
        return true
      end
    end
  end

  no_violations_detected = invoke :rubocop, [multi_line_to_single_line(files)], options

  exit 1 unless no_violations_detected
  return true unless options[:exit]
  exit 0
end

#diff_enforcedObject



18
19
20
# File 'lib/nucop/cli.rb', line 18

def diff_enforced
  invoke :diff, nil, options.merge(only: cops_to_enforce.join(","))
end

#diff_enforced_githubObject



30
31
32
# File 'lib/nucop/cli.rb', line 30

def diff_enforced_github
  invoke :diff_github, nil, options.merge(only: cops_to_enforce.join(","))
end

#diff_githubObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nucop/cli.rb', line 91

def diff_github
  puts "Running on files changed relative to '#{options[:"commit-spec"]}' (specify using the 'commit-spec' option)"
  diff_head = capture_std_out("git rev-parse HEAD").chomp
  diff_base = options[:"commit-spec"]
  repository = capture_std_out("git remote get-url origin | sed 's/[email protected]://; s/.git//'").chomp

  uri = URI("https://api.github.com/repos/#{repository}/compare/#{diff_base}...#{diff_head}")
  request = Net::HTTP::Get.new(uri)
  request["Accept"] = "application/vnd.github+json"
  request["Authorization"] = "Bearer #{options[:"github-authorization-token"]}"
  request["X-GitHub-Api-Version"] = "2022-11-28"

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }

  if response.code != "200"
    puts "Error fetching data from Github: #{response.code} -- #{response.body}"
    return true unless options[:exit]

    exit 0
  end

  commit_data = JSON.parse(response.body)

  diff_filter = options[:"added-only"] ? proc { |status| status == "added" } : proc { |status| status != "removed" }
  files = commit_data["files"]
    .filter { |file_data| diff_filter.call(file_data["status"]) }
    .map { |file_data| file_data["filename"] }
    .filter { |file_name| file_name.include?(".rb") }

  if files.empty?
    if options[:exit]
      puts "There are no rb files present in diff. Exiting."
      exit 0
    else
      puts "There are no rb files present in diff."
      return true
    end
  end

  if options[:ignore] && File.exist?(options[:diffignore_file]) && !File.empty?(options[:diffignore_file])
    files, non_ignored_diff_status = Open3.capture2("grep -v -f #{options[:diffignore_file]}", stdin_data: files.join("\n"))

    if non_ignored_diff_status != 0
      if options[:exit]
        puts "There are no non-ignored rb files present in diff. Exiting."
        exit 0
      else
        puts "There are no non-ignored rb files present in diff."
        return true
      end
    end
  end

  no_violations_detected = invoke :rubocop, [multi_line_to_single_line(files)], options

  exit 1 unless no_violations_detected
  return true unless options[:exit]

  exit 0
end

#modified_linesObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/nucop/cli.rb', line 204

def modified_lines
  diff_files, diff_status = Open3.capture2("git diff #{options[:"commit-spec"]} --diff-filter=d --name-only | grep \"\\.rb$\"")

  exit 1 unless diff_status.exitstatus.zero?

  command = [
    "bundle exec rubocop",
    "--parallel",
    "--format Nucop::Formatters::GitDiffFormatter",
    "--config #{options[:rubocop_todo_config_file]}",
    multi_line_to_single_line(diff_files).to_s
  ].join(" ")

  # HACK: use ENVVAR to parameterize GitDiffFormatter
  system({ "RUBOCOP_COMMIT_SPEC" => options[:"commit-spec"] }, command)
end

#ready_for_promotionObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/nucop/cli.rb', line 223

def ready_for_promotion
  finder = Helpers::NextCopForPromotion.new(options[:rubocop_todo_file])
  todo_config = YAML.load_file(options[:rubocop_todo_file])

  puts "The following cop(s) are ready to be promoted to enforced. Good luck!"
  puts "Remember to run `nucop:regen_backlog` to capture your hard work."
  puts
  finder.find(options["n"].to_i).each do |todo|
    puts "#{todo.name} with #{todo.offenses} offenses:"
    puts

    files = todo_config.fetch(todo.name, {}).fetch("Exclude", [])

    system("bundle exec rubocop --parallel --config #{options[:rubocop_todo_config_file]} --only #{todo.name} #{files.join(' ')}")
    puts("*" * 100) if options["n"] > 1
    puts
  end
end

#regen_backlogObject



192
193
194
195
# File 'lib/nucop/cli.rb', line 192

def regen_backlog
  regenerate_rubocop_todos
  update_enforced_cops
end

#rubocop(files = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/nucop/cli.rb', line 158

def rubocop(files = nil)
  print_cops_being_run(options[:only])
  config_file = options[:"exclude-backlog"] ? RUBOCOP_DEFAULT_CONFIG_FILE : options[:rubocop_todo_config_file]
  rubocop_requires = [
    "--require rubocop-rspec",
    "--require rubocop-performance",
    "--require rubocop-rails"
  ]

  formatters = []
  formatters << "--format Nucop::Formatters::JUnitFormatter --out #{options[:junit_report]}" if options[:junit_report]
  formatters << "--format json --out #{options[:json]}" if options[:json]
  formatters << "--format progress" if formatters.any?

  command = [
    "bundle exec rubocop",
    "--no-server",
    "--parallel",
    rubocop_requires.join(" "),
    formatters.join(" "),
    "--force-exclusion",
    "--config", config_file,
    pass_through_option(options, "auto-correct"),
    pass_through_option(options, "autocorrect"),
    pass_through_option(options, "autocorrect-all"),
    pass_through_flag(options, "only"),
    files
  ].join(" ")

  system(command)
end

#update_enforcedObject



198
199
200
# File 'lib/nucop/cli.rb', line 198

def update_enforced
  update_enforced_cops
end