Render format dependent links

This commit is contained in:
Kamil Trzcinski 2016-12-16 00:00:54 +01:00
parent dc995daf90
commit cc83aded33
No known key found for this signature in database
GPG Key ID: 4505F5C7E12C6A5A
6 changed files with 148 additions and 115 deletions

View File

@ -33,10 +33,11 @@ class ChatSlashCommandsService < Service
user = find_chat_user(params) user = find_chat_user(params)
unless user unless user
url = authorize_chat_name_url(params) url = authorize_chat_name_url(params)
return Gitlab::ChatCommands::Presenter.authorize_chat_name(url) return presenter.authorize_chat_name(url)
end end
Gitlab::ChatCommands::Command.new(project, user, params).execute Gitlab::ChatCommands::Command.new(project, user,
params.merge(presenter_format: presenter_format)).execute
end end
private private
@ -48,4 +49,12 @@ class ChatSlashCommandsService < Service
def authorize_chat_name_url(params) def authorize_chat_name_url(params)
ChatNames::AuthorizeUserService.new(self, params).execute ChatNames::AuthorizeUserService.new(self, params).execute
end end
def presenter
Gitlab::ChatCommands::Presenter.new(presenter_format)
end
def presenter_format
throw NotImplementedError
end
end end

View File

@ -18,4 +18,8 @@ class MattermostSlashCommandsService < ChatService
def to_param def to_param
'mattermost_slash_commands' 'mattermost_slash_commands'
end end
def presenter_format
'mattermost'
end
end end

View File

@ -12,4 +12,8 @@ class SlackSlashCommandsService < ChatSlashCommandsService
def to_param def to_param
'slack_slash_commands' 'slack_slash_commands'
end end
def presenter_format
'slack'
end
end end

View File

@ -42,6 +42,10 @@ module Gitlab
def find_by_iid(iid) def find_by_iid(iid)
collection.find_by(iid: iid) collection.find_by(iid: iid)
end end
def presenter
Gitlab::ChatCommands::Presenter.new(params[:presenter_format])
end
end end
end end
end end

View File

@ -48,15 +48,15 @@ module Gitlab
end end
def help(messages) def help(messages)
Mattermost::Presenter.help(messages, params[:command]) presenter.help(messages, params[:command])
end end
def access_denied def access_denied
Mattermost::Presenter.access_denied presenter.access_denied
end end
def present(resource) def present(resource)
Mattermost::Presenter.present(resource) presenter.present(resource)
end end
end end
end end

View File

@ -1,131 +1,143 @@
module Gitlab module Gitlab
class ChatCommands class ChatCommands
class Presenter class Presenter
class << self include Gitlab::Routing.url_helpers
include Gitlab::Routing.url_helpers
def authorize_chat_name(url) attr_reader :format
message = if url
":wave: Hi there! Before I do anything for you, please <#{url}|connect your GitLab account>." def initialize(format)
else @format = format
":sweat_smile: Couldn't identify you, nor can I autorize you!" end
end
def authorize_chat_name(url)
message = if url
":wave: Hi there! Before I do anything for you, please #{link(url, 'connect your GitLab account')}."
else
":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 help(commands, trigger) def present(subject)
if commands.none? return not_found unless subject
ephemeral_response("No commands configured")
else
commands.map! { |command| "#{trigger} #{command}" }
message = header_with_list("Available commands", commands)
ephemeral_response(message) if subject.is_a?(Gitlab::ChatCommands::Result)
end show_result(subject)
end elsif subject.respond_to?(:count)
if subject.many?
def present(subject) multiple_resources(subject)
return not_found unless subject elsif subject.none?
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
def access_denied
ephemeral_response("Whoops! That action is not allowed. This incident will be #{link('https://xkcd.com/838/', 'reported')}.")
end
private
def show_result(result)
case result.type
when :success
in_channel_response(result.message)
else
ephemeral_response(result.message)
end
end
def not_found
ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:")
end
def single_resource(resource)
return error(resource) if resource.errors.any? || !resource.persisted?
message = "#{title(resource)}:"
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)
message = header_with_list("The action was not successful, because:", resource.errors.messages)
ephemeral_response(message)
end
def title(resource)
reference = resource.try(:to_reference) || resource.try(:id)
title = resource.try(:title) || resource.try(:name)
link(url(resource), "#{reference} #{title}")
end
def header_with_list(header, items)
message = [header]
items.each do |item|
message << "- #{item}"
end end
def access_denied message.join("\n")
ephemeral_response("Whoops! That action is not allowed. This incident will be <https://xkcd.com/838/|reported>.") end
end
private def url(resource)
url_for(
[
resource.project.namespace.becomes(Namespace),
resource.project,
resource
]
)
end
def show_result(result) def ephemeral_response(message)
case result.type {
when :success response_type: :ephemeral,
in_channel_response(result.message) text: message,
else status: 200
ephemeral_response(result.message) }
end end
end
def not_found def in_channel_response(message)
ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:") {
end response_type: :in_channel,
text: message,
status: 200
}
end
def single_resource(resource) def link(url, title)
return error(resource) if resource.errors.any? || !resource.persisted? case format
when 'slack' then "<#{url}|#{title}>"
message = "#{title(resource)}:" when 'mattermost' then "[#{title}](#{url})"
message << "\n\n#{resource.description}" if resource.try(:description) else then title
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)
message = header_with_list("The action was not successful, because:", resource.errors.messages)
ephemeral_response(message)
end
def title(resource)
reference = resource.try(:to_reference) || resource.try(:id)
title = resource.try(:title) || resource.try(:name)
"<#{url(resource)}|#{reference} #{title}>"
end
def header_with_list(header, items)
message = [header]
items.each do |item|
message << "- #{item}"
end
message.join("\n")
end
def url(resource)
url_for(
[
resource.project.namespace.becomes(Namespace),
resource.project,
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
end end