BigW Consortium Gitlab

Rename Mattermost::Presenter to Presenter

parent ed880e49
module Mattermost module Gitlab
class Presenter class ChatCommands
class << self class Presenter
include Gitlab::Routing.url_helpers class << self
include Gitlab::Routing.url_helpers
def authorize_chat_name(url)
message = if url def authorize_chat_name(url)
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})." message = if url
else ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
":sweat_smile: Couldn't identify you, nor can I autorize you!" else
end ":sweat_smile: Couldn't identify you, nor can I autorize you!"
end
ephemeral_response(message)
end
def help(commands, trigger)
if commands.none?
ephemeral_response("No commands configured")
else
commands.map! { |command| "#{trigger} #{command}" }
message = header_with_list("Available commands", commands)
ephemeral_response(message) ephemeral_response(message)
end end
end
def present(subject) def help(commands, trigger)
return not_found unless subject if commands.none?
ephemeral_response("No commands configured")
else
commands.map! { |command| "#{trigger} #{command}" }
message = header_with_list("Available commands", commands)
if subject.is_a?(Gitlab::ChatCommands::Result) ephemeral_response(message)
show_result(subject) end
elsif subject.respond_to?(:count) end
if subject.many?
multiple_resources(subject) def present(subject)
elsif subject.none? return not_found unless subject
not_found
if subject.is_a?(Gitlab::ChatCommands::Result)
show_result(subject)
elsif subject.respond_to?(:count)
if subject.many?
multiple_resources(subject)
elsif subject.none?
not_found
else
single_resource(subject)
end
else else
single_resource(subject) single_resource(subject)
end end
else
single_resource(subject)
end end
end
def access_denied def access_denied
ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).") ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
end end
private private
def show_result(result) def show_result(result)
case result.type case result.type
when :success when :success
in_channel_response(result.message) in_channel_response(result.message)
else else
ephemeral_response(result.message) ephemeral_response(result.message)
end
end end
end
def not_found def not_found
ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:") ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:")
end end
def single_resource(resource) def single_resource(resource)
return error(resource) if resource.errors.any? || !resource.persisted? return error(resource) if resource.errors.any? || !resource.persisted?
message = "### #{title(resource)}" message = "### #{title(resource)}"
message << "\n\n#{resource.description}" if resource.try(:description) message << "\n\n#{resource.description}" if resource.try(:description)
in_channel_response(message) in_channel_response(message)
end end
def multiple_resources(resources) def multiple_resources(resources)
resources.map! { |resource| title(resource) } resources.map! { |resource| title(resource) }
message = header_with_list("Multiple results were found:", resources) message = header_with_list("Multiple results were found:", resources)
ephemeral_response(message) ephemeral_response(message)
end end
def error(resource) def error(resource)
message = header_with_list("The action was not successful, because:", resource.errors.messages) message = header_with_list("The action was not successful, because:", resource.errors.messages)
ephemeral_response(message) ephemeral_response(message)
end end
def title(resource) def title(resource)
reference = resource.try(:to_reference) || resource.try(:id) reference = resource.try(:to_reference) || resource.try(:id)
title = resource.try(:title) || resource.try(:name) title = resource.try(:title) || resource.try(:name)
"[#{reference} #{title}](#{url(resource)})" "[#{reference} #{title}](#{url(resource)})"
end end
def header_with_list(header, items) def header_with_list(header, items)
message = [header] message = [header]
items.each do |item| items.each do |item|
message << "- #{item}" message << "- #{item}"
end end
message.join("\n") message.join("\n")
end end
def url(resource) def url(resource)
url_for( url_for(
[ [
resource.project.namespace.becomes(Namespace), resource.project.namespace.becomes(Namespace),
resource.project, resource.project,
resource resource
] ]
) )
end end
def ephemeral_response(message) def ephemeral_response(message)
{ {
response_type: :ephemeral, response_type: :ephemeral,
text: message, text: message,
status: 200 status: 200
} }
end end
def in_channel_response(message) def in_channel_response(message)
{ {
response_type: :in_channel, response_type: :in_channel,
text: message, text: message,
status: 200 status: 200
} }
end
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment