2016-11-15 03:55:56 -05:00
|
|
|
module Mattermost
|
|
|
|
class Presenter
|
|
|
|
class << self
|
2016-11-18 05:38:54 -05:00
|
|
|
include Gitlab::Routing.url_helpers
|
2016-11-17 06:57:27 -05:00
|
|
|
|
2016-11-17 06:06:45 -05:00
|
|
|
def authorize_chat_name(url)
|
2016-11-18 04:00:40 -05:00
|
|
|
message = if url
|
|
|
|
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
|
|
|
|
else
|
|
|
|
":sweat_smile: Couldn't identify you, nor can I autorize you!"
|
|
|
|
end
|
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)
|
2016-11-18 07:29:47 -05:00
|
|
|
if commands.none?
|
2016-11-18 06:08:30 -05:00
|
|
|
ephemeral_response("No commands configured")
|
2016-11-17 15:27:12 -05:00
|
|
|
else
|
2016-11-18 07:29:47 -05:00
|
|
|
commands.map! { |command| "#{trigger} #{command}" }
|
2016-11-17 15:27:12 -05:00
|
|
|
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-21 07:47:18 -05:00
|
|
|
def present(subject)
|
|
|
|
return not_found unless subject
|
|
|
|
|
|
|
|
if subject.is_a?(Gitlab::ChatCommands::Result)
|
|
|
|
show_result(subject)
|
|
|
|
elsif subject.respond_to?(:count)
|
2016-11-21 12:22:03 -05:00
|
|
|
if subject.many?
|
2016-11-21 07:47:18 -05:00
|
|
|
multiple_resources(subject)
|
2016-11-21 12:22:03 -05:00
|
|
|
elsif subject.none?
|
2016-11-21 07:47:18 -05:00
|
|
|
not_found
|
2016-11-15 15:50:27 -05:00
|
|
|
else
|
2016-11-21 07:47:18 -05:00
|
|
|
single_resource(subject)
|
2016-11-15 15:50:27 -05:00
|
|
|
end
|
2016-11-21 07:47:18 -05:00
|
|
|
else
|
|
|
|
single_resource(subject)
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
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-21 07:47:18 -05:00
|
|
|
def show_result(result)
|
|
|
|
ephemeral_response(result.message)
|
|
|
|
end
|
|
|
|
|
2016-11-17 15:27:12 -05:00
|
|
|
def not_found
|
2016-11-18 07:29:47 -05:00
|
|
|
ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:")
|
2016-11-17 15:27:12 -05:00
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def single_resource(resource)
|
2016-11-18 05:38:54 -05:00
|
|
|
return error(resource) if resource.errors.any? || !resource.persisted?
|
2016-11-17 06:06:45 -05:00
|
|
|
|
|
|
|
message = "### #{title(resource)}"
|
2016-11-21 07:47:18 -05:00
|
|
|
message << "\n\n#{resource.description}" if resource.try(: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-18 07:29:47 -05:00
|
|
|
message = header_with_list("The action was not successful, 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-21 07:47:18 -05:00
|
|
|
reference = resource.try(:to_reference) || resource.try(:id)
|
|
|
|
title = resource.try(:title) || resource.try(:name)
|
|
|
|
|
|
|
|
"[#{reference} #{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
|