Skip to content

Instantly share code, notes, and snippets.

@in8finity
Last active September 19, 2019 19:53
Show Gist options
  • Save in8finity/796e6fa3418d28342e4dd5745bfa9b29 to your computer and use it in GitHub Desktop.
Save in8finity/796e6fa3418d28342e4dd5745bfa9b29 to your computer and use it in GitHub Desktop.
Extract offences on lines changed in branch
#!/usr/bin/env ruby
def exec_rubocop(file_name)
response = %x{rubocop -c ./.rubocop.yml #{file_name}}
response = response.split("\n")
response.grep(/\d*:\d*/)
end
def commits_to_load
%x{git log --format=format:%h $(git merge-base HEAD develop)..HEAD}.split("\n")
end
def commits_regexp
commits_to_load.join("|")
end
def changed_lines(file)
lines = %x{git --no-pager blame HEAD -- "#{file}"}.split("\n")
lines.grep(/#{commits_regexp}/).map do |row|
row.match(/(\d*)\)/)[1]
end
end
def files_to_check
%x{git diff --name-only HEAD $(git merge-base HEAD develop) | grep -v erb}.split("\n")
end
def extract_rubocop_line_number(row)
# spec/reports/company_spec.rb:710:101: C: Line is too long. [124/100]
line_number_match = row.match(/:(\d*):\d*/)
return line_number_match[1].to_i if line_number_match
end
def file_path_in_subtree(file)
items = file.split(File::SEPARATOR)
items = items[1..items.length] # wheely-core/ -- cut here -- lib/wheely-core/...
items.join(File::SEPARATOR)
end
problems = true
files_to_check.each do |f|
file = file_path_in_subtree(f)
puts "Processing #{file}"
rubocop_result = exec_rubocop(file)
changed_lines = changed_lines(file).each_with_object({}){|n,a| a[n.to_i] = true}
rubocop_result.map do |row|
# puts row if row.downcase.match(/complex|size/)
line_number = extract_rubocop_line_number(row)
if line_number
puts row if changed_lines[line_number]
problems = false
end
end
end
exit(problems)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment