Skip to content

Resolve auto-correctable RuboCop offenses (Exclude-only) in .rubocop_todo/**/*.yml

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

COMMUNITY CONTRIBUTORS, PLEASE ONLY PICKUP ONE COP VIOLATION TO ALLOW OTHER NEW CONTRIBUTORS TO LEARN FROM THESE TOO

Problem

.rubocop_todo/**/*.yml contains auto-correctable (RuboCop) offenses (grep -hr auto-correct .rubocop_todo -A 1 | grep "^[A-Z]") which can be fixed automatically.

Implementation guide

Resolve auto-correctable rules which are enabled by letting RuboCop auto-correct them automatically 🎉

  1. Verify that the you are working on is already enabled. See also #369268 (closed)
  2. Pick a offense from The List below e.g. Layout/ClosingHeredocIndentation
  3. Create a branch (e.g. 239356-abc/fix-Layout/ClosingHeredocIndentation)
    • Info: Using this issue ID (239356) in a branch name will add required labels and reference to this issue in the new merge request.
    • Info: Using your username or initials (e.g. abc/ above) in the branch name will help to prevent branch naming clashes with other contributors also working on this issue.
  4. Delete the rule from the corresponding .rubocop_todo/ YAML file (for example https://gitlab.com/gitlab-org/gitlab/-/blob/fdeea9247566ca45fece68605a838ebc659b62db/.rubocop_todo/gitlab/rails/safe_format.yml)
  5. Auto-correct RuboCop offenses via e.g. bundle exec rubocop --autocorrect --only Layout/ClosingHeredocIndentation
  6. Check changed files and adjust if needed
    • Suggestion: In order to keep MRs short one can fix 10-15 files in one go. Depending on the amount of changes in the file.
  7. Commit and create a merge request
  8. Mention this merge request in this issue
  9. 🎉

The List

📣 A list containing all auto-correctable offenses

How to regenerate the list?

  1. Copy+paste the Script from below 👇 as save as regenerate.rb
  2. Copy the The List from above ☝️ in Markdown format
  3. Run regenerate.rb
  4. Paste the copied list from above ☝️ and terminate input with Ctrl-D
  5. Copy+paste the newly generated list in the The List section

Script

script
#!/usr/bin/env ruby
# frozen_string_literal: true

# From https://gitlab.com/gitlab-org/gitlab/-/issues/239356#the-list

require "yaml"

BURNDOWN_CHART_URL = "https://ck3g.gitlab.io/rubocop-burndown/"

sha = `git rev-parse HEAD`.chomp

template = <<~MD
- [ ] [`%{rule_name}`](https://gitlab.com/gitlab-org/gitlab/-/blob/#{sha}/%{path}) (%{entries} entries) [Chart](#{BURNDOWN_CHART_URL}#%{rule_name})
MD

warn "Paste the markdown bullet point list from https://gitlab.com/gitlab-org/gitlab/-/issues/239356: (Terminate with Ctrl-D)"
current = $stdin.readlines.to_h { |line| [line[/`(\S+)`/, 1], line] } # rubocop:disable Rails/IndexBy
puts "Received #{current.keys.count} lines\n\n"

todos = Dir.glob(".rubocop_todo/**/*.yml").to_h do |path|
  next [nil, []] unless /^# Cop supports --auto-?correct/.match?(File.read(path))

  yaml = YAML.safe_load_file(path)
  name, config = yaml.first
  entries = config.fetch('Exclude', []) || []
  [name, [path, entries.size]]
end

todos.delete(nil)

(current.keys | todos.keys).uniq.sort.each do |name|
  path, entries = todos[name]
  current_line = current[name]

  if current_line && !path
    current_line.sub!('- [ ]', '- [x]')
    current_line.sub!(/\d+ entries/, '0 entries')
    current_line.chomp!
    current_line += " [Chart](#{BURNDOWN_CHART_URL}##{name})"
    puts current_line
  elsif path
    puts format(template, rule_name: name, path: path, entries: entries)
  end
end
Edited by Vitali Tatarintev