Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
require 'json'
require 'git_diff_parser'
# This script takes an input .json file in the standard lint format
# and culls violations that do not occur on changed lines for a given diff file.
# Parse command line options.
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: cull_violations_not_in_diff.rb [options]'
opts.on('-i', '--inputfile FILENAME', 'Input filename') { |v| options[:input] = v }
opts.on('-o', '--output FILENAME', 'Output filename') { |v| options[:output] = v }
opts.on('-d', '--diff FILENAME', 'Input diff file') { |v| options[:diff] = v }
end.parse!
# Set options or use defaults
input_file = options[:input]
output = options[:output]
diff_file = options[:diff]
file_contents = `cat #{input_file}`
json = JSON.parse(file_contents)
patches = GitDiffParser.parse(File.read(diff_file))
culled_violations = []
json['violation'].each do |v|
next if patches.files.include?(v['path']) == false # Skip this one
patch = patches.find_patch_by_file(v['path'])
if patch.changed_line_numbers.include?(v['startLine'])
culled_violations.push(v)
end
end
json['violation'] = culled_violations
output_string = JSON.generate(json)
File.write(output, output_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.