2017-01-19 03:22:09 -05:00
|
|
|
module Gitlab
|
2017-05-31 01:50:53 -04:00
|
|
|
module SlashCommands
|
2017-01-19 03:22:09 -05:00
|
|
|
module Presenters
|
|
|
|
class Base
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2017-01-19 03:22:09 -05:00
|
|
|
|
|
|
|
def initialize(resource = nil)
|
|
|
|
@resource = resource
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def display_errors
|
|
|
|
message = header_with_list("The action was not successful, because:", @resource.errors.full_messages)
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
ephemeral_response(text: message)
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
private
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def header_with_list(header, items)
|
|
|
|
message = [header]
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
items.each do |item|
|
|
|
|
message << "- #{item}"
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
message.join("\n")
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def ephemeral_response(message)
|
|
|
|
response = {
|
|
|
|
response_type: :ephemeral,
|
|
|
|
status: 200
|
|
|
|
}.merge(message)
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
format_response(response)
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def in_channel_response(message)
|
|
|
|
response = {
|
|
|
|
response_type: :in_channel,
|
|
|
|
status: 200
|
|
|
|
}.merge(message)
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
format_response(response)
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def format_response(response)
|
2017-06-02 13:11:26 -04:00
|
|
|
response[:text] = format(response[:text]) if response.key?(:text)
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if response.key?(:attachments)
|
2017-01-19 03:22:09 -05:00
|
|
|
response[:attachments].each do |attachment|
|
|
|
|
attachment[:pretext] = format(attachment[:pretext]) if attachment[:pretext]
|
|
|
|
attachment[:text] = format(attachment[:text]) if attachment[:text]
|
|
|
|
end
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
response
|
2017-01-10 13:43:58 -05:00
|
|
|
end
|
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
# Convert Markdown to slacks format
|
|
|
|
def format(string)
|
|
|
|
Slack::Notifier::LinkFormatter.format(string)
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
|
2017-01-19 03:22:09 -05:00
|
|
|
def resource_url
|
|
|
|
url_for(
|
|
|
|
[
|
|
|
|
@resource.project.namespace.becomes(Namespace),
|
|
|
|
@resource.project,
|
|
|
|
@resource
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-01-10 13:43:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|