Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 14:06:45 +0300
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 23:34:24 +0300
commitd4def9cbcd664b7067e7f9f4ea8be54463bd1d50 (patch)
tree10c5fe56054be36687146877b4000286334e76e7 /lib/mattermost
parent1b4fdb9893af28606b7594ee656438c7ef21e9d8 (diff)
Incorporate feedback, improve presenter class
[ci skip]
Diffstat (limited to 'lib/mattermost')
-rw-r--r--lib/mattermost/presenter.rb81
1 files changed, 44 insertions, 37 deletions
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb
index 3db55d6bd51..0f2beb2cd6b 100644
--- a/lib/mattermost/presenter.rb
+++ b/lib/mattermost/presenter.rb
@@ -1,35 +1,24 @@
module Mattermost
class Presenter
class << self
- COMMAND_PREFIX = '/gitlab'.freeze
+ def authorize_chat_name(url)
+ message = "Hi there! We've yet to get acquainted! Please [introduce yourself](#{url})!"
- def authorize_chat_name(params)
- url = ChatNames::RequestService.new(service, params).execute
-
- {
- response_type: :ephemeral,
- message: "You are not authorized. Click this [link](#{url}) to authorize."
- }
+ ephemeral_response(message)
end
- def help(messages)
- messages = ["Available commands:"]
+ def help(messages, command)
+ message = ["Available commands:"]
messages.each do |messsage|
- messages << "- #{message}"
+ message << "- #{command} #{message}"
end
- {
- response_type: :ephemeral,
- text: messages.join("\n")
- }
+ ephemeral_response(messages.join("\n"))
end
def not_found
- {
- response_type: :ephemeral,
- text: "404 not found! GitLab couldn't find what your were looking for! :boom:",
- }
+ ephemeral_response("404 not found! GitLab couldn't find what your were looking for! :boom:")
end
def present(resource)
@@ -51,38 +40,56 @@ module Mattermost
private
def single_resource(resource)
- message = title(resource)
+ return error(resource) if resource.errors.any?
+
+ message = "### #{title(resource)}"
message << "\n\n#{resource.description}" if resource.description
- {
- response_type: :in_channel,
- text: message
- }
+ in_channel_response(message)
end
def multiple_resources(resources)
message = "Multiple results were found:\n"
- message << resources.map { |resource| " #{title(resource)}" }.join("\n")
+ message << resources.map { |resource| "- #{title(resource)}" }.join("\n")
- {
- response_type: :ephemeral,
- text: message
- }
+ ephemeral_response(message)
+ end
+
+ def error(resource)
+ message = "The action was not succesfull because:\n"
+ message << resource.errors.messages.map { |message| "- #{message}" }.join("\n")
+
+ ephemeral_response(resource.errors.messages.join("\n")
end
def title(resource)
- "### [#{resource.to_reference} #{resource.title}](#{url(resource)})"
+ "[#{resource.to_reference} #{resource.title}](#{url(resource)})"
end
def url(resource)
- helper = Rails.application.routes.url_helpers
+ polymorphic_url(
+ [
+ resource.project.namespace.becomes(Namespace),
+ resource.project,
+ resource
+ ],
+ id: resource_id,
+ routing_type: :url
+ )
+ end
- case resource
- when Issue
- helper.namespace_project_issue_url(resource.project.namespace.becomes(Namespace), resource.project, resource)
- when MergeRequest
- helper.namespace_project_merge_request_url(resource.project.namespace.becomes(Namespace), resource.project, resource)
- end
+ def ephemeral_response(message)
+ {
+ response_type: :ephemeral,
+ text: message
+ }
+ end
+
+ def in_channel_response(message)
+ {
+ response_type: :in_channel,
+ text: message
+ }
end
end
end