1- require 'yaml'
2-
31class CommitMonitor
42 include Sidekiq ::Worker
53 sidekiq_options :queue => :miq_bot_glacial , :retry => false
@@ -9,36 +7,20 @@ class CommitMonitor
97
108 include SidekiqWorkerMixin
119
12- # commit handlers expect to handle a specific commit at a time.
13- #
14- # Example: A commit message checker that will check for URLs and act upon them.
15- def self . commit_handlers
16- @commit_handlers ||= handlers_for ( :commit )
17- end
18-
19- # commit_range handlers expect to handle a range of commits as a group.
20- #
21- # Example: A style/syntax/warning checker on a PR branch, where we only want
22- # to check the new commits, but as a group, since newer commits may fix
23- # issues in prior commits.
24- def self . commit_range_handlers
25- @commit_range_handlers ||= handlers_for ( :commit_range ) . select { |h | !h . respond_to? ( :perform_batch_async ) }
26- end
27-
28- # branch handlers expect to handle an entire branch at once.
29- #
30- # Example: A PR branch mergability tester to see if the entire branch can be
31- # merged or not.
32- def self . branch_handlers
33- @branch_handlers ||= handlers_for ( :branch )
10+ def self . handlers
11+ @handlers ||= begin
12+ workers_path = Rails . root . join ( "app/workers" )
13+ Dir . glob ( workers_path . join ( "commit_monitor_handlers/*.rb" ) ) . collect do |f |
14+ path = Pathname . new ( f ) . relative_path_from ( workers_path ) . to_s
15+ path . chomp ( ".rb" ) . classify . constantize
16+ end
17+ end
3418 end
3519
36- # batch handlers expect to handle a batch of workers at once and will need
37- # a wider range of information
38- #
39- # Example: A general commenter to GitHub for a number of issues
40- def self . batch_handlers
41- @batch_handlers ||= handlers_for ( :commit_range ) . select { |h | h . respond_to? ( :perform_batch_async ) }
20+ def self . handlers_for ( branch )
21+ handlers . select do |h |
22+ h . handled_branch_modes . include? ( branch . mode ) && h . enabled_for? ( branch . repo )
23+ end
4224 end
4325
4426 def perform
@@ -67,7 +49,6 @@ def process_repo(repo)
6749 sorted_branches = repo . branches . sort_by { |b | b . pull_request? ? 1 : -1 }
6850
6951 sorted_branches . each do |branch |
70- @new_commits_details = nil
7152 @branch = branch
7253 process_branch
7354 end
@@ -112,17 +93,6 @@ def compare_commits_list(left, right)
11293 { :same => same , :left_only => left_only , :right_only => right_only }
11394 end
11495
115- def new_commits_details
116- @new_commits_details ||=
117- new_commits . each_with_object ( { } ) do |commit , h |
118- git_commit = branch . git_service . commit ( commit )
119- h [ commit ] = {
120- "message" => git_commit . full_message ,
121- "files" => git_commit . diff . file_status . keys
122- }
123- end
124- end
125-
12696 def save_branch_record
12797 attrs = { :last_checked_on => Time . now . utc }
12898 attrs [ :last_commit ] = new_commits . last if new_commits . any?
@@ -135,102 +105,10 @@ def save_branch_record
135105 branch . update_columns ( attrs )
136106 end
137107
138- #
139- # Handler processing methods
140- #
141-
142- def self . handlers_for ( type )
143- workers_path = Rails . root . join ( "app/workers" )
144- Dir . glob ( workers_path . join ( "commit_monitor_handlers/#{ type } /*.rb" ) ) . collect do |f |
145- path = Pathname . new ( f ) . relative_path_from ( workers_path ) . to_s
146- path . chomp ( ".rb" ) . classify . constantize
147- end
148- end
149- private_class_method ( :handlers_for )
150-
151- def filter_handlers ( handlers )
152- handlers . select do |h |
153- h . handled_branch_modes . include? ( branch . mode ) && h . enabled_for? ( repo )
154- end
155- end
156-
157- def commit_handlers
158- filter_handlers ( self . class . commit_handlers )
159- end
160-
161- def commit_range_handlers
162- filter_handlers ( self . class . commit_range_handlers )
163- end
164-
165- def branch_handlers
166- filter_handlers ( self . class . branch_handlers )
167- end
168-
169- def batch_handlers
170- filter_handlers ( self . class . batch_handlers )
171- end
172-
173108 def process_handlers
174- process_commit_handlers if process_commit_handlers?
175- process_commit_range_handlers if process_commit_range_handlers?
176- process_branch_handlers if process_branch_handlers?
177- process_batch_handlers if process_batch_handlers?
178- end
179-
180- def process_commit_handlers?
181- commit_handlers . any? && new_commits . any?
182- end
183-
184- def process_commit_range_handlers?
185- commit_range_handlers . any? && new_commits . any?
186- end
187-
188- def process_branch_handlers?
189- branch_handlers . any? && send ( "process_#{ branch . mode } _branch_handlers?" )
190- end
191-
192- def process_batch_handlers?
193- batch_handlers . any? && new_commits . any?
194- end
195-
196- def process_pr_branch_handlers?
197- parent_branch_new_commits = statistics . fetch_path ( "master" , :new_commits )
198- new_commits . any? || parent_branch_new_commits . any?
199- end
200-
201- def process_regular_branch_handlers?
202- new_commits . any?
203- end
204-
205- def process_commit_handlers
206- new_commits_details . each do |commit , details |
207- commit_handlers . each do |h |
208- logger . info ( "Queueing #{ h . name . split ( "::" ) . last } for commit #{ commit } on branch #{ branch . name } " )
209- h . perform_async ( branch . id , commit , details )
210- end
211- end
212- end
213-
214- def process_commit_range_handlers
215- commit_range = [ new_commits . first , new_commits . last ] . uniq . join ( ".." )
216-
217- commit_range_handlers . each do |h |
218- logger . info ( "Queueing #{ h . name . split ( "::" ) . last } for commit range #{ commit_range } on branch #{ branch . name } " )
219- h . perform_async ( branch . id , new_commits )
220- end
221- end
222-
223- def process_branch_handlers
224- branch_handlers . each do |h |
225- logger . info ( "Queueing #{ h . name . split ( "::" ) . last } for branch #{ branch . name } " )
226- h . perform_async ( branch . id )
227- end
228- end
229-
230- def process_batch_handlers
231- batch_handlers . each do |h |
232- logger . info ( "Queueing #{ h . name } for branch #{ branch . name } " )
233- h . perform_batch_async ( branch . id , new_commits_details )
109+ self . class . handlers_for ( branch ) . each do |handler |
110+ method = handler . respond_to? ( :perform_batch_async ) ? :perform_batch_async : :perform_async
111+ handler . public_send ( method , branch . id , new_commits )
234112 end
235113 end
236114end
0 commit comments