gitlab-org--gitlab-foss/lib/gitlab/slash_commands/presenters/base.rb

78 lines
1.8 KiB
Ruby
Raw Normal View History

2017-01-19 08:22:09 +00:00
module Gitlab
module SlashCommands
2017-01-19 08:22:09 +00:00
module Presenters
class Base
include Gitlab::Routing
2017-01-19 08:22:09 +00:00
def initialize(resource = nil)
@resource = resource
end
2017-01-19 08:22:09 +00:00
def display_errors
message = header_with_list("The action was not successful, because:", @resource.errors.full_messages)
2017-01-19 08:22:09 +00:00
ephemeral_response(text: message)
end
2017-01-19 08:22:09 +00:00
private
2017-01-19 08:22:09 +00:00
def header_with_list(header, items)
message = [header]
2017-01-19 08:22:09 +00:00
items.each do |item|
message << "- #{item}"
end
2017-01-19 08:22:09 +00:00
message.join("\n")
end
2017-01-19 08:22:09 +00:00
def ephemeral_response(message)
response = {
response_type: :ephemeral,
status: 200
}.merge(message)
2017-01-19 08:22:09 +00:00
format_response(response)
end
2017-01-19 08:22:09 +00:00
def in_channel_response(message)
response = {
response_type: :in_channel,
status: 200
}.merge(message)
2017-01-19 08:22:09 +00:00
format_response(response)
end
2017-01-19 08:22:09 +00:00
def format_response(response)
response[:text] = format(response[:text]) if response.key?(:text)
if response.key?(:attachments)
2017-01-19 08:22:09 +00: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-19 08:22:09 +00:00
response
end
2017-01-19 08:22:09 +00:00
# Convert Markdown to slacks format
def format(string)
Slack::Notifier::LinkFormatter.format(string)
end
2017-01-19 08:22:09 +00:00
def resource_url
url_for(
[
@resource.project.namespace.becomes(Namespace),
@resource.project,
@resource
]
)
end
end
end
end
end