DEV Community

Cover image for Aggregate RuboCop ToDo count
r7kamura
r7kamura

Posted on • Edited on

Aggregate RuboCop ToDo count

Are you all fighting RuboCop offenses?

Today I have written a simple Ruby script to aggregate the number of offenses in .rubocop_todo.yml, so let me share it in this article.

# aggregate_rubocop_todo_offenses.rb
require 'date'

# Aggregate up to 365 days in advance.
365.times do |i|

  # Get the 1st commit hash before `i` days.
  # The format `%H` means "commit hash". See `git log --help` for more info.
  commit_sha = `git log -1 --format='%H' --before=#{i}.day`.rstrip

  # Get the .rubocop_todo.yml content at the commit.
  rubocop_todo_content = `git show #{commit_sha}:.rubocop_todo.yml`.rstrip

  # Get the total count of offenses in the .rubocop_todo.yml.
  # The offenses count is described in the form like `# Offense count: 42` per cop.
  offenses_count = rubocop_todo_content.scan(/count: (\d+)/).flatten.map(&:to_i).sum

  # Output date and count in TSV format.
  puts [
    Date.today - i,
    offenses_count
  ].join("\t")
end
Enter fullscreen mode Exit fullscreen mode

Run the above script and you will see the following output:

$ ruby aggregate_rubocop_todo_offenses.rb
2022-07-26      164761
2022-07-25      164763
2022-07-24      164764
2022-07-23      165303
2022-07-22      165303
2022-07-21      165296
2022-07-20      165290
...
2021-11-27      193170
Enter fullscreen mode Exit fullscreen mode

In such cases, it's convenient to paste the data into Google Spreadsheet to generate a chart.

chart

It's a crazy amount, but slowly decreasing in these days. Good...

In fact, I recently created a GitHub Action workflow that automatically corrects offenses and continuously creates new pull requests, so offenses count is gradually decreasing only by pushing merge button, but I'll get to that in another time.

If you are interested, please add your reactions! đŸ¦„ ✨

Top comments (1)

Collapse
 
harry_wood profile image
Harry Wood

Neat idea. I was thinking we could send rubocop stats like this into our grafana monitoring set-up, but I like the way you're managing to graph stats into the past too.

Instead of looping through days it could loop through commits which change .rubocop_todo.yml. Might be more efficient, but I guess it depends how busy the repo is.