2016-11-15 03:55:56 -05:00
|
|
|
module Mattermost
|
|
|
|
class Presenter
|
|
|
|
class << self
|
2016-11-17 06:57:27 -05:00
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
def authorize_chat_name(url)
|
2016-11-17 15:27:12 -05:00
|
|
|
message = ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
ephemeral_response(message)
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
def help(commands, trigger)
|
|
|
|
if commands.count == 0
|
|
|
|
ephemeral_response("No commands configured") unless messages.count > 1
|
|
|
|
else
|
|
|
|
message = header_with_list("Available commands", commands)
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
ephemeral_response(message)
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def present(resource)
|
|
|
|
return not_found unless resource
|
|
|
|
|
|
|
|
if resource.respond_to?(:count)
|
|
|
|
if resource.count > 1
|
|
|
|
return multiple_resources(resource)
|
|
|
|
elsif resource.count == 0
|
|
|
|
return not_found
|
|
|
|
else
|
|
|
|
resource = resource.first
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
2016-11-15 15:50:27 -05:00
|
|
|
|
|
|
|
single_resource(resource)
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
def access_denied
|
|
|
|
ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
private
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
def not_found
|
|
|
|
ephemeral_response("404 not found! GitLab couldn't find what your were looking for! :boom:")
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def single_resource(resource)
|
2016-11-17 06:06:45 -05:00
|
|
|
return error(resource) if resource.errors.any?
|
|
|
|
|
|
|
|
message = "### #{title(resource)}"
|
2016-11-15 15:50:27 -05:00
|
|
|
message << "\n\n#{resource.description}" if resource.description
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
in_channel_response(message)
|
2016-11-15 15:50:27 -05:00
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def multiple_resources(resources)
|
2016-11-17 15:27:12 -05:00
|
|
|
resources.map! { |resource| title(resource) }
|
|
|
|
|
|
|
|
message = header_with_list("Multiple results were found:", resources)
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
ephemeral_response(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(resource)
|
2016-11-17 15:27:12 -05:00
|
|
|
message = header_with_list("The action was not succesful, because:", resource.errors.messages)
|
2016-11-17 06:06:45 -05:00
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
ephemeral_response(message)
|
2016-11-15 15:50:27 -05:00
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def title(resource)
|
2016-11-17 06:06:45 -05:00
|
|
|
"[#{resource.to_reference} #{resource.title}](#{url(resource)})"
|
2016-11-15 15:50:27 -05:00
|
|
|
end
|
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
def header_with_list(header, items)
|
|
|
|
message = [header]
|
|
|
|
|
|
|
|
items.each do |item|
|
|
|
|
message << "- #{item}"
|
|
|
|
end
|
|
|
|
|
|
|
|
message.join("\n")
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def url(resource)
|
2016-11-17 06:57:27 -05:00
|
|
|
url_for(
|
2016-11-17 06:06:45 -05:00
|
|
|
[
|
|
|
|
resource.project.namespace.becomes(Namespace),
|
|
|
|
resource.project,
|
2016-11-17 06:57:27 -05:00
|
|
|
resource
|
|
|
|
]
|
2016-11-17 06:06:45 -05:00
|
|
|
)
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
def ephemeral_response(message)
|
|
|
|
{
|
|
|
|
response_type: :ephemeral,
|
2016-11-17 15:27:12 -05:00
|
|
|
text: message,
|
|
|
|
status: 200
|
2016-11-17 06:06:45 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_channel_response(message)
|
|
|
|
{
|
|
|
|
response_type: :in_channel,
|
2016-11-17 15:27:12 -05:00
|
|
|
text: message,
|
|
|
|
status: 200
|
2016-11-17 06:06:45 -05:00
|
|
|
}
|
2016-11-15 15:50:27 -05:00
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|