Class: Nucop::Cli

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

Instance Method Summary collapse

Instance Method Details

#diffObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nucop/cli.rb', line 69

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



42
43
44
# File 'lib/nucop/cli.rb', line 42

def diff_enforced
  invoke :diff, nil, options
end

#diff_enforced_githubObject



55
56
57
# File 'lib/nucop/cli.rb', line 55

def diff_enforced_github
  invoke :diff_github, nil, options
end

#diff_githubObject



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/nucop/cli.rb', line 118

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") }
    .join("\n")

  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)

    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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/nucop/cli.rb', line 228

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",
    "--no-server",
    "--format Nucop::Formatters::GitDiffFormatter",
    "--config #{RUBOCOP_DEFAULT_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



249
250
251
# File 'lib/nucop/cli.rb', line 249

def ready_for_promotion
  puts "This is a no-op. Enforced cops are not currently supported."
end

#regen_backlogObject



216
217
218
# File 'lib/nucop/cli.rb', line 216

def regen_backlog
  regenerate_rubocop_todos
end

#rubocop(files = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/nucop/cli.rb', line 186

def rubocop(files = nil)
  puts "Running all cops..."
  config_file = RUBOCOP_DEFAULT_CONFIG_FILE

  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_gem_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



221
222
223
# File 'lib/nucop/cli.rb', line 221

def update_enforced
  puts "This is a no-op. Enforced cops are not currently supported."
end