dry up linters

test plan:
* see test commit verifying linters still work

Change-Id: I406c218309e824618869c9b5f3841af8387bf836
Reviewed-on: https://gerrit.instructure.com/98329
Reviewed-by: Simon Williams <simon@instructure.com>
Tested-by: Jenkins
Reviewed-by: Shawn Meredith <shawn@instructure.com>
Product-Review: Shawn Meredith <shawn@instructure.com>
QA-Review: Shawn Meredith <shawn@instructure.com>
This commit is contained in:
Landon Wilkins 2016-12-21 16:30:57 -07:00
parent 04f0d46b08
commit 65744119ca
5 changed files with 195 additions and 228 deletions

View File

@ -1,53 +1,13 @@
#!/usr/bin/env ruby
$:.push File.expand_path("../../gems/dr_diff/lib", __FILE__)
require 'dr_diff'
require 'json'
require_relative './linter'
def pretty_comment(comment)
message = ""
message += "[#{comment[:severity]}]".colorize(:yellow)
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
message += " => #{comment[:message].colorize(:green)}"
puts message
end
def gerrit_patchset?
!!ENV['GERRIT_PATCHSET_REVISION']
end
env_sha = ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION']
dr_diff = DrDiff::Manager.new(sha: env_sha)
ruby_files = dr_diff.files(/\.rb$/)
if ruby_files.size == 0
puts "No ruby file changes found, skipping Brakeman check!"
exit 0
end
comments = dr_diff.comments(
linter_options = {
linter_name: "brakeman",
file_regex: /\.rb$/,
format: 'brakeman',
command: 'bundle exec brakeman --quiet --format json --confidence-level 2',
include_git_dir_in_output: !gerrit_patchset?
)
append_files_to_command: false,
}
unless comments.size > 0
puts '-- -- -- -- -- -- -- -- -- -- --'
puts 'No relevant Brakeman errors found!'
puts '-- -- -- -- -- -- -- -- -- -- --'
exit 0
end
if gerrit_patchset?
require "gergich"
draft = Gergich::Draft.new
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
end
else
require 'colorize'
comments.each { |comment| pretty_comment(comment) }
end
brakeman = Linter.new(linter_options)
brakeman.run

View File

@ -1,58 +1,19 @@
#!/usr/bin/env ruby
$:.push File.expand_path("../../gems/dr_diff/lib", __FILE__)
require 'dr_diff'
require 'json'
require_relative './linter'
def pretty_comment(comment)
message = ""
message += "[#{comment[:severity]}]".colorize(:yellow)
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
message += " => #{comment[:message].colorize(:green)}"
puts message
end
def gerrit_patchset?
!!ENV['GERRIT_PATCHSET_REVISION']
end
env_sha = ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION']
dr_diff = DrDiff::Manager.new(sha: env_sha)
ESLINT_FILE_REGEX = /\.jsx?$/
js_files = dr_diff.files(ESLINT_FILE_REGEX)
if js_files.size == 0
puts "No js or jsx file changes found!"
exit 0
end
comments = dr_diff.comments(format: "eslint",
command: "node_modules/.bin/eslint #{js_files.join(' ')}",
include_git_dir_in_output: !gerrit_patchset?)
# This section should be removed when we start -2ing patchsets
comments = comments.map do |comment|
comment[:severity] = 'warn'
comment
end
unless comments.size > 0
puts "-- -- -- -- -- -- -- -- -- -- --"
puts "No relevant js or jsx errors found!"
puts "-- -- -- -- -- -- -- -- -- -- --"
exit(0)
end
if gerrit_patchset?
require "gergich"
draft = Gergich::Draft.new
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
linter_options = {
linter_name: "eslint",
file_regex: /\.jsx?$/,
format: "eslint",
command: "node_modules/.bin/eslint",
append_files_to_command: true,
comment_post_processing: proc do |comments|
# This section should be removed when we start -2ing patchsets
comments.each do |comment|
comment[:severity] = 'warn'
end
end
else
require 'colorize'
comments.each { |comment| pretty_comment(comment) }
end
}
eslint = Linter.new(linter_options)
eslint.run

138
script/linter.rb Normal file
View File

@ -0,0 +1,138 @@
$LOAD_PATH.push File.expand_path("../../gems/dr_diff/lib", __FILE__)
require 'dr_diff'
require 'json'
class Linter
DEFAULT_OPTIONS = {
append_files_to_command: false,
boyscout_mode: true,
comment_post_processing: proc {},
env_sha: ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION'],
file_regex: /./,
gerrit_patchset: !!ENV['GERRIT_PATCHSET_REVISION'],
heavy_mode: false,
heavy_mode_proc: proc {},
include_git_dir_in_output: !!!ENV['GERRIT_PATCHSET_REVISION'],
plugin: ENV['GERRIT_PROJECT'],
skip_file_size_check: false,
}.freeze
def initialize(options = {})
options = DEFAULT_OPTIONS.merge(options)
if options[:plugin] == 'canvas-lms'
options[:plugin] = nil
end
options.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
def run
if git_dir && !Dir.exist?(git_dir)
puts "No plugin #{plugin} found"
exit 0
end
if !skip_file_size_check && files.size == 0
puts "No #{file_regex} file changes found, skipping #{linter_name} check!"
exit 0
end
if heavy_mode
heavy_mode_proc.call(files)
else
publish_comments
end
end
private
attr_reader :append_files_to_command,
:boyscout_mode,
:command,
:comment_post_processing,
:default_boyscout_mode,
:env_sha,
:file_regex,
:format,
:gerrit_patchset,
:heavy_mode,
:heavy_mode_proc,
:include_git_dir_in_output,
:linter_name,
:plugin,
:severe_levels,
:skip_file_size_check
def git_dir
@git_dir ||= plugin && "gems/plugins/#{plugin}/"
end
def severe_levels
boyscout_mode ? %w(error warn info) : %w(error warn)
end
def dr_diff
@dr_diff ||= ::DrDiff::Manager.new(git_dir: git_dir, sha: env_sha)
end
def files
@files ||= dr_diff.files(file_regex)
end
def full_command
if append_files_to_command
"#{command} #{files.join(' ')}"
else
command
end
end
def comments
@comments ||= dr_diff.comments(format: format,
command: full_command,
include_git_dir_in_output: include_git_dir_in_output,
severe_levels: severe_levels)
end
def publish_comments
unless comments.size > 0
puts "-- -- -- -- -- -- -- -- -- -- --"
puts "No relevant #{linter_name} errors found!"
puts "-- -- -- -- -- -- -- -- -- -- --"
exit(0)
end
if gerrit_patchset
publish_gergich_comments(comments)
else
publish_local_comments(comments)
end
end
def publish_gergich_comments(comments)
require "gergich"
draft = Gergich::Draft.new
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
end
end
def publish_local_comments(comments)
require 'colorize'
comments.each { |comment| pretty_comment(comment) }
end
def pretty_comment(comment)
message = ""
message += "[#{comment[:severity]}]".colorize(:yellow)
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
message += " => #{comment[:message].colorize(:green)}"
puts message
end
end

View File

@ -1,84 +1,30 @@
#!/usr/bin/env ruby
$:.push File.expand_path("../../gems/dr_diff/lib", __FILE__)
require 'dr_diff'
require 'json'
require_relative './linter'
require 'optparse'
require 'rubocop'
def pretty_comment(comment)
message = ""
message += "[#{comment[:severity]}]".colorize(:yellow)
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
message += " => #{comment[:message].colorize(:green)}"
puts message
end
def gerrit_patchset?
!!ENV['GERRIT_PATCHSET_REVISION']
end
def run_heavy_mode(ruby_files)
args = ruby_files
args_index = ARGV.index('--')
if args_index
args.concat(ARGV[(args_index + 1)..-1])
linter_options = {
linter_name: "Rubocop",
file_regex: /\.rb$/,
format: "rubocop",
command: "bundle exec rubocop",
append_files_to_command: true,
severe_levels: [],
default_boyscout_mode: false,
heavy_mode_proc: proc do |ruby_files|
args = ruby_files
args_index = ARGV.index('--')
if args_index
args.concat(ARGV[(args_index + 1)..-1])
end
RuboCop::CLI.new.run(args)
end
RuboCop::CLI.new.run(args)
end
env_sha = ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION']
heavy_mode = false
boyscout_mode = false
plugin = ENV['GERRIT_PROJECT']
plugin = nil if plugin == 'canvas-lms'
}
OptionParser.new do |opts|
opts.on("--heavy") { heavy_mode = true }
opts.on("--boy-scout") { boyscout_mode = true }
opts.on("--plugin PLUGIN") { |v| plugin = v }
opts.on("--heavy") { linter_options[:heavy_mode] = true }
opts.on("--boy-scout") { linter_options[:boyscout_mode] = true }
opts.on("--plugin PLUGIN") { |v| linter_options[:plugin] = v }
end.parse!
git_dir = "gems/plugins/#{plugin}/" if plugin
if git_dir && !Dir.exist?(git_dir)
puts "No plugin #{plugin} found"
exit 0
end
dr_diff = DrDiff::Manager.new(git_dir: git_dir, sha: env_sha)
ruby_files = dr_diff.files(/\.rb$/)
if ruby_files.size == 0
puts "No ruby file changes found, skipping Rubocop check!"
exit 0
end
if heavy_mode
run_heavy_mode(ruby_files)
else
severe_levels = boyscout_mode ? %w(error warn info) : %w(error warn)
comments = dr_diff.comments(format: "rubocop",
command: "bundle exec rubocop #{ruby_files.join(' ')}",
include_git_dir_in_output: !gerrit_patchset?,
severe_levels: severe_levels)
unless comments.size > 0
puts "-- -- -- -- -- -- -- -- -- -- --"
puts "No relevant rubocop errors found!"
puts "-- -- -- -- -- -- -- -- -- -- --"
exit(0)
end
if gerrit_patchset?
require "gergich"
draft = Gergich::Draft.new
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
end
else
require 'colorize'
comments.each { |comment| pretty_comment(comment) }
end
end
rlint = Linter.new(linter_options)
rlint.run

View File

@ -1,52 +1,14 @@
#!/usr/bin/env ruby
$:.push File.expand_path("../../gems/dr_diff/lib", __FILE__)
require 'dr_diff'
require 'json'
require_relative './linter'
def pretty_comment(comment)
message = ""
message += "[#{comment[:severity]}]".colorize(:yellow)
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
message += " => #{comment[:message].colorize(:green)}"
puts message
end
linter_options = {
linter_name: "stylelint",
file_regex: /\.s?css$/,
format: 'stylelint',
command: 'node_modules/.bin/stylelint',
append_files_to_command: true,
default_boyscout_mode: false
}
def gerrit_patchset?
!!ENV['GERRIT_PATCHSET_REVISION']
end
env_sha = ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION']
dr_diff = DrDiff::Manager.new(sha: env_sha)
STYLELINT_FILE_REGEX = /\.s?css$/
scss_files = dr_diff.files(STYLELINT_FILE_REGEX)
if scss_files.size == 0
puts "No scss or css file changes found!"
exit 0
end
comments = dr_diff.comments(format: "stylelint",
command: "node_modules/.bin/stylelint #{scss_files.join(' ')}",
include_git_dir_in_output: !gerrit_patchset?)
unless comments.size > 0
puts "-- -- -- -- -- -- -- -- -- -- --"
puts "No relevant scss or css errors found!"
puts "-- -- -- -- -- -- -- -- -- -- --"
exit(0)
end
if gerrit_patchset?
require "gergich"
draft = Gergich::Draft.new
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
end
else
require 'colorize'
comments.each { |comment| pretty_comment(comment) }
end
stylelint = Linter.new(linter_options)
stylelint.run