diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-16 00:00:54 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-16 17:51:08 +0100 |
commit | cc83aded33471a3e2f943bd9a760f689d30f901e (patch) | |
tree | 38b32ea32f8668f10e06e00ddffc20a69203dd91 /lib | |
parent | dc995daf90e67b5531dd7fbb8a958507f46e4eb6 (diff) | |
download | gitlab-ce-cc83aded33471a3e2f943bd9a760f689d30f901e.tar.gz |
Render format dependent links
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/chat_commands/base_command.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/chat_commands/command.rb | 6 | ||||
-rw-r--r-- | lib/gitlab/chat_commands/presenter.rb | 200 |
3 files changed, 113 insertions, 97 deletions
diff --git a/lib/gitlab/chat_commands/base_command.rb b/lib/gitlab/chat_commands/base_command.rb index 25da8474e95..156bb826f86 100644 --- a/lib/gitlab/chat_commands/base_command.rb +++ b/lib/gitlab/chat_commands/base_command.rb @@ -42,6 +42,10 @@ module Gitlab def find_by_iid(iid) collection.find_by(iid: iid) end + + def presenter + Gitlab::ChatCommands::Presenter.new(params[:presenter_format]) + end end end end diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb index b0d3fdbc48a..c5c54cf7cfc 100644 --- a/lib/gitlab/chat_commands/command.rb +++ b/lib/gitlab/chat_commands/command.rb @@ -48,15 +48,15 @@ module Gitlab end def help(messages) - Mattermost::Presenter.help(messages, params[:command]) + presenter.help(messages, params[:command]) end def access_denied - Mattermost::Presenter.access_denied + presenter.access_denied end def present(resource) - Mattermost::Presenter.present(resource) + presenter.present(resource) end end end diff --git a/lib/gitlab/chat_commands/presenter.rb b/lib/gitlab/chat_commands/presenter.rb index 14c78ac39aa..e151513cbd5 100644 --- a/lib/gitlab/chat_commands/presenter.rb +++ b/lib/gitlab/chat_commands/presenter.rb @@ -1,131 +1,143 @@ module Gitlab class ChatCommands class Presenter - class << self - include Gitlab::Routing.url_helpers + include Gitlab::Routing.url_helpers - def authorize_chat_name(url) - message = if url - ":wave: Hi there! Before I do anything for you, please <#{url}|connect your GitLab account>." - else - ":sweat_smile: Couldn't identify you, nor can I autorize you!" - end + attr_reader :format - ephemeral_response(message) - end + def initialize(format) + @format = format + 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) + 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 + 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) end + end - def present(subject) - return not_found unless subject - - 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 + def present(subject) + return not_found unless subject + + 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 + single_resource(subject) end + end - def access_denied - ephemeral_response("Whoops! That action is not allowed. This incident will be <https://xkcd.com/838/|reported>.") - end + def access_denied + ephemeral_response("Whoops! That action is not allowed. This incident will be #{link('https://xkcd.com/838/', 'reported')}.") + end - private + private - def show_result(result) - case result.type - when :success - in_channel_response(result.message) - else - ephemeral_response(result.message) - end + 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 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? + 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) + message = "#{title(resource)}:" + message << "\n\n#{resource.description}" if resource.try(:description) - in_channel_response(message) - end + in_channel_response(message) + end - def multiple_resources(resources) - resources.map! { |resource| title(resource) } + def multiple_resources(resources) + 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) - end + ephemeral_response(message) + end - def error(resource) - message = header_with_list("The action was not successful, because:", resource.errors.messages) + def error(resource) + message = header_with_list("The action was not successful, because:", resource.errors.messages) - ephemeral_response(message) - end + ephemeral_response(message) + end - def title(resource) - reference = resource.try(:to_reference) || resource.try(:id) - title = resource.try(:title) || resource.try(:name) + def title(resource) + reference = resource.try(:to_reference) || resource.try(:id) + title = resource.try(:title) || resource.try(:name) - "<#{url(resource)}|#{reference} #{title}>" - end + link(url(resource), "#{reference} #{title}") + end - def header_with_list(header, items) - message = [header] + def header_with_list(header, items) + message = [header] - items.each do |item| - message << "- #{item}" - end - - message.join("\n") + items.each do |item| + message << "- #{item}" end - def url(resource) - url_for( - [ - resource.project.namespace.becomes(Namespace), - resource.project, - resource - ] - ) - end + message.join("\n") + end - def ephemeral_response(message) - { - response_type: :ephemeral, - text: message, - status: 200 - } - 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 - def in_channel_response(message) - { - response_type: :in_channel, - text: message, - status: 200 - } + def link(url, title) + case format + when 'slack' then "<#{url}|#{title}>" + when 'mattermost' then "[#{title}](#{url})" + else then title end end end |