gitlab-org--gitlab-foss/lib/mattermost/presenter.rb

132 lines
3.3 KiB
Ruby
Raw Normal View History

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
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
ephemeral_response(message)
end
def help(commands, trigger)
2016-11-18 07:29:47 -05:00
if commands.none?
ephemeral_response("No commands configured")
else
2016-11-18 07:29:47 -05:00
commands.map! { |command| "#{trigger} #{command}" }
message = header_with_list("Available commands", commands)
ephemeral_response(message)
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
else
2016-11-21 07:47:18 -05:00
single_resource(subject)
end
2016-11-21 07:47:18 -05:00
else
single_resource(subject)
end
end
def access_denied
ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
end
private
2016-11-21 07:47:18 -05:00
def show_result(result)
2016-11-21 13:40:04 -05:00
case result.type
when :success
in_channel_response(result.message)
else
ephemeral_response(result.message)
end
2016-11-21 07:47:18 -05:00
end
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:")
end
def single_resource(resource)
2016-11-18 05:38:54 -05:00
return error(resource) if resource.errors.any? || !resource.persisted?
message = "### #{title(resource)}"
2016-11-21 07:47:18 -05:00
message << "\n\n#{resource.description}" if resource.try(:description)
in_channel_response(message)
end
def multiple_resources(resources)
resources.map! { |resource| title(resource) }
message = header_with_list("Multiple results were found:", resources)
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)
ephemeral_response(message)
end
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)})"
end
def header_with_list(header, items)
message = [header]
items.each do |item|
message << "- #{item}"
end
message.join("\n")
end
def url(resource)
2016-11-17 06:57:27 -05:00
url_for(
[
resource.project.namespace.becomes(Namespace),
resource.project,
2016-11-17 06:57:27 -05:00
resource
]
)
end
def ephemeral_response(message)
{
response_type: :ephemeral,
text: message,
status: 200
}
end
def in_channel_response(message)
{
response_type: :in_channel,
text: message,
status: 200
}
end
end
end
end