Skip to content

Commit

Permalink
Custom reviewer responder (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanxu authored Feb 24, 2021
1 parent afd1062 commit 49963e8
Show file tree
Hide file tree
Showing 30 changed files with 560 additions and 73 deletions.
18 changes: 8 additions & 10 deletions app/lib/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ def respond(message)

# Update the body of the issue between marks
def update_body(start_mark, end_mark, new_text)
new_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "#{start_mark}#{new_text}#{end_mark}")
update_issue({ body: new_body })
@issue_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "#{start_mark}#{new_text}#{end_mark}")
update_issue({ body: @issue_body })
end

# Add text at the end of the body of the issue
def append_to_body(text)
new_body = issue_body + text
update_issue({ body: new_body })
@issue_body = issue_body + text
update_issue({ body: @issue_body })
end

# Remove a block of text from the body of the issue optionally including start/end marks
def delete_from_body(start_mark, end_mark, delete_marks=false)
if delete_marks
new_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "")
@issue_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "")
else
new_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "#{start_mark}#{end_mark}")
@issue_body = issue_body.gsub(/#{start_mark}.*#{end_mark}/im, "#{start_mark}#{end_mark}")
end
update_issue({ body: new_body })
update_issue({ body: @issue_body })
end

# Invite a user to collaborate in the repo
Expand Down Expand Up @@ -83,9 +83,7 @@ def update_value(value_name, text)

# Update list in issue's body between HTML comments
def update_list(list_name, text)
start_mark = "<!--#{list_name}-list-->"
end_mark = "<!--end-#{list_name}-list-->"
update_body(start_mark, end_mark, text)
update_value("#{list_name}-list", text)
end

# Replace an assigned user from the assignees list of the issue
Expand Down
2 changes: 1 addition & 1 deletion app/lib/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def issue

# Return the body of issue
def issue_body
@issue_body = context.issue_body
@issue_body ||= context.issue_body
@issue_body ||= issue.body
end

Expand Down
8 changes: 8 additions & 0 deletions app/lib/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class Responder
attr_accessor :match_data
attr_accessor :context

def self.keyname(key)
@key = key.to_s
end

def self.key
@key || self.name
end


def initialize(settings, params)
settings[:env] = default_settings.merge(settings[:env] || {})
Expand Down
56 changes: 31 additions & 25 deletions app/lib/responder_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,13 @@

class ResponderRegistry

RESPONDER_MAPPING = {
"help" => HelpResponder,
"hello" => HelloResponder,
"basic_command" => BasicCommandResponder,
"assign_reviewer_n" => AssignReviewerNResponder,
"remove_reviewer_n" => RemoveReviewerNResponder,
"assign_editor" => AssignEditorResponder,
"remove_editor" => RemoveEditorResponder,
"invite" => InviteResponder,
"set_value" => SetValueResponder,
"list_of_values" => ListOfValuesResponder,
"add_remove_assignee" => AddAndRemoveAssigneeResponder,
"label_command" => LabelCommandResponder,
"thanks" => ThanksResponder,
"welcome" => WelcomeResponder,
"welcome_template" => WelcomeTemplateResponder,
"close_issue_command" => CloseIssueCommandResponder,
"external_service" => ExternalServiceResponder,
"add_remove_checklist" => AddAndRemoveUserChecklistResponder,
"check_references" => CheckReferencesResponder,
"repo_checks" => RepoChecksResponder,
}

attr_accessor :responders
attr_accessor :config
attr_reader :responders_map


def initialize(config)
@responders_map = ResponderRegistry.available_responders
@responders ||= Array.new
@config = config
load_responders!
Expand Down Expand Up @@ -58,13 +38,39 @@ def load_responders!
params.each do |responder_instances|
responder_instances.each_pair do |instance_name, subparams|
subparams = {} if subparams.nil?
add_responder(RESPONDER_MAPPING[name].new(config, Sinatra::IndifferentHash[name: instance_name.to_s].merge(subparams)))
add_responder(@responders_map[name].new(config, Sinatra::IndifferentHash[name: instance_name.to_s].merge(subparams)))
end
end
else
add_responder(RESPONDER_MAPPING[name].new(config, params))
add_responder(@responders_map[name].new(config, params))
end
end
end

# Create a map of all the classes in the files located in the /responders dir
def self.available_responders
available_responders = {}
responder_files = Dir["#{File.expand_path '../../responders', __FILE__}/**/*.rb"].map do |f|
f.match(/.*app\/responders\/(.*).rb/)[1]
end

responder_files = responder_files.compact.sort
responder_classes = responder_files.map do |path|
path.split("/").map do |subpath|
subpath.split('_').each(&:capitalize!).join
end.join('::')
end

responder_classes.each do |name|
begin
responder_class = Object.const_get(name)
available_responders[responder_class.key] = responder_class
rescue NameError => err
logger.warn("There is a mismatch in a Responder class name/module: #{err.message}")
end
end

available_responders
end

def log_error(responder, error)
Expand Down
2 changes: 2 additions & 0 deletions app/responders/add_and_remove_assignee_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class AddAndRemoveAssigneeResponder < Responder

keyname :add_remove_assignee

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} (add|remove) assignee: (\S+)\s*\z/i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class AddAndRemoveUserChecklistResponder < Responder

keyname :add_remove_checklist

def define_listening
required_params :template_file

Expand Down
2 changes: 2 additions & 0 deletions app/responders/assign_editor_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class AssignEditorResponder < Responder

keyname :assign_editor

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} assign (\S+) as editor\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/assign_reviewer_n_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class AssignReviewerNResponder < Responder

keyname :assign_reviewer_n

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} assign (\S+) as reviewer (\S+)\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/basic_command_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class BasicCommandResponder < Responder

keyname :basic_command

def define_listening
required_params :command

Expand Down
2 changes: 2 additions & 0 deletions app/responders/check_references_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class CheckReferencesResponder < Responder

keyname :check_references

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} check references(?: from branch ([\w-]+))?\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/close_issue_command_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class CloseIssueCommandResponder < Responder

keyname :close_issue_command

def define_listening
required_params :command

Expand Down
2 changes: 2 additions & 0 deletions app/responders/external_service_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class ExternalServiceResponder < Responder

keyname :external_service

def define_listening
required_params :name, :command, :url

Expand Down
2 changes: 2 additions & 0 deletions app/responders/hello_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class HelloResponder < Responder

keyname :hello

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A(Hello|Hi) @#{@bot_name}\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/help_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class HelpResponder < Responder

keyname :help

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} #{help_command}\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/invite_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class InviteResponder < Responder

keyname :invite

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} invite (\S+)\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/label_command_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class LabelCommandResponder < Responder

keyname :label_command

def define_listening
required_params :command
check_labels_present
Expand Down
2 changes: 2 additions & 0 deletions app/responders/list_of_values_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class ListOfValuesResponder < Responder

keyname :list_of_values

def define_listening
required_params :name

Expand Down
2 changes: 2 additions & 0 deletions app/responders/remove_editor_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class RemoveEditorResponder < Responder

keyname :remove_editor

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} remove editor\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/remove_reviewer_n_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class RemoveReviewerNResponder < Responder

keyname :remove_reviewer_n

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} remove reviewer (\S+)\s*\z/i
Expand Down
2 changes: 2 additions & 0 deletions app/responders/repo_checks_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class RepoChecksResponder < Responder

keyname :repo_checks

def define_listening
@event_action = "issue_comment.created"
@event_regex = /\A@#{@bot_name} check repository(?: from branch ([\w-]+))?\s*\z/i
Expand Down
Loading

0 comments on commit 49963e8

Please sign in to comment.