2016-11-15 03:55:56 -05:00
|
|
|
module Mattermost
|
|
|
|
class Presenter
|
|
|
|
class << self
|
|
|
|
COMMAND_PREFIX = '/gitlab'.freeze
|
|
|
|
|
|
|
|
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."
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-11-16 12:28:38 -05:00
|
|
|
def help(messages)
|
2016-11-15 03:55:56 -05:00
|
|
|
messages = ["Available commands:"]
|
|
|
|
|
2016-11-16 12:28:38 -05:00
|
|
|
messages.each do |messsage|
|
|
|
|
messages << "- #{message}"
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
response_type: :ephemeral,
|
|
|
|
text: messages.join("\n")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_found
|
|
|
|
{
|
|
|
|
response_type: :ephemeral,
|
|
|
|
text: "404 not found! GitLab couldn't find what your were looking for! :boom:",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def present(resource)
|
|
|
|
return not_found unless resource
|
|
|
|
|
|
|
|
if resource.respond_to?(:count)
|
|
|
|
if resource.count > 1
|
|
|
|
return multiple_resources(resource)
|
|
|
|
elsif resource.count == 0
|
|
|
|
return not_found
|
|
|
|
else
|
|
|
|
resource = resource.first
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
2016-11-15 15:50:27 -05:00
|
|
|
|
|
|
|
single_resource(resource)
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
private
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def single_resource(resource)
|
|
|
|
message = title(resource)
|
|
|
|
message << "\n\n#{resource.description}" if resource.description
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
{
|
|
|
|
response_type: :in_channel,
|
|
|
|
text: message
|
|
|
|
}
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def multiple_resources(resources)
|
|
|
|
message = "Multiple results were found:\n"
|
|
|
|
message << resources.map { |resource| " #{title(resource)}" }.join("\n")
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
{
|
|
|
|
response_type: :ephemeral,
|
|
|
|
text: message
|
|
|
|
}
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
def title(resource)
|
|
|
|
"### [#{resource.to_reference} #{resource.title}](#{url(resource)})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def url(resource)
|
|
|
|
helper = Rails.application.routes.url_helpers
|
2016-11-15 03:55:56 -05:00
|
|
|
|
2016-11-15 15:50:27 -05:00
|
|
|
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
|
|
|
|
end
|
2016-11-15 03:55:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|