forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinter.rb
More file actions
189 lines (161 loc) · 4.67 KB
/
Copy pathlinter.rb
File metadata and controls
189 lines (161 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
$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,
auto_correct: false,
boyscout_mode: true,
campsite_mode: true,
comment_post_processing: proc { |comments| comments },
custom_comment_generation: false,
env_sha: ENV['SHA'] || ENV['GERRIT_PATCHSET_REVISION'],
file_regex: /./,
generate_comment_proc: proc { },
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,
skip_wips: 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_wips && wip?
puts "WIP detected, #{linter_name} will not run."
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
# TODO: generate from DEFAULT_OPTIONS
attr_reader :append_files_to_command,
:auto_correct,
:boyscout_mode,
:campsite_mode,
:command,
:comment_post_processing,
:default_boyscout_mode,
:custom_comment_generation,
:env_sha,
:file_regex,
:format,
:generate_comment_proc,
:gergich_capture,
:gerrit_patchset,
:heavy_mode,
:heavy_mode_proc,
:include_git_dir_in_output,
:linter_name,
:plugin,
:severe_levels,
:skip_file_size_check,
:skip_wips
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, campsite: campsite_mode)
end
def wip?
dr_diff.wip?
end
def changes
dr_diff.changes
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 generate_comments
if custom_comment_generation
generate_comment_proc.call(changes: changes, auto_correct: auto_correct)
else
comments
end
end
def publish_comments
processed_comments = comment_post_processing.call(generate_comments)
unless processed_comments.size > 0
puts "-- -- -- -- -- -- -- -- -- -- --"
puts "No relevant #{linter_name} errors found!"
puts "-- -- -- -- -- -- -- -- -- -- --"
exit(0)
end
if gerrit_patchset
publish_gergich_comments(processed_comments)
else
publish_local_comments(processed_comments)
if auto_correct
puts "Errors detected and possibly auto corrected."
puts "Fix and/or git add the corrections and try to commit again."
exit 1
end
end
end
def publish_gergich_comments(comments)
require "gergich"
draft = Gergich::Draft.new
cover_comments, comments = comments.partition do |comment|
comment[:cover_message]
end
comments.each do |comment|
draft.add_comment comment[:path],
comment[:position],
comment[:message],
comment[:severity]
end
cover_comments.each do |cover_comment|
draft.add_message(cover_comment[:message])
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)
unless comment[:cover_message]
message += " #{comment[:path].colorize(:light_blue)}:#{comment[:position]}"
end
message += " => #{comment[:message].colorize(:green)}"
puts message
end
end