Commit 592f26c6 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Extract Slash command response into a base class

It reduces duplication of a Slack response message structure
by extracting "attachments" definition into a base class.
parent 4ca52208
......@@ -18,6 +18,8 @@ module Gitlab
private
attr_reader :resource
def header_with_list(header, items)
message = [header]
......@@ -67,9 +69,9 @@ module Gitlab
def resource_url
url_for(
[
@resource.project.namespace.becomes(Namespace),
@resource.project,
@resource
resource.project.namespace.becomes(Namespace),
resource.project,
resource
]
)
end
......@@ -81,6 +83,37 @@ module Gitlab
def author_profile_link
"[#{author.to_reference}](#{url_for(author)})"
end
def response_message(custom_pretext: pretext)
{
attachments: [
{
title: "#{issue.title} · #{issue.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: fallback,
pretext: custom_pretext,
text: text,
color: color(resource),
fields: fields,
mrkdwn_in: fields_with_markdown
}
]
}
end
def pretext
''
end
def text
''
end
def fields_with_markdown
%i(title pretext fields)
end
end
end
end
......
......@@ -45,6 +45,8 @@ module Gitlab
private
attr_reader :resource
alias_method :issue, :resource
end
end
end
......
......@@ -8,9 +8,9 @@ module Gitlab
def present
if @resource.confidential?
ephemeral_response(close_issue)
ephemeral_response(response_message)
else
in_channel_response(close_issue)
in_channel_response(response_message)
end
end
......@@ -20,30 +20,12 @@ module Gitlab
private
def close_issue
{
attachments: [
{
title: "#{@resource.title} · #{@resource.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: "Closed issue #{@resource.to_reference}: #{@resource.title}",
pretext: pretext,
color: color(@resource),
fields: fields,
mrkdwn_in: [
:title,
:pretext,
:fields
]
}
]
}
def fallback
"Closed issue #{issue.to_reference}: #{issue.title}"
end
def pretext
"I closed an issue on #{author_profile_link}'s behalf: *#{@resource.to_reference}* in #{project_link}"
"I closed an issue on #{author_profile_link}'s behalf: *#{issue.to_reference}* in #{project_link}"
end
end
end
......
......@@ -7,31 +7,13 @@ module Gitlab
include Presenters::NoteBase
def present
ephemeral_response(new_note)
ephemeral_response(response_message)
end
private
def new_note
{
attachments: [
{
title: "#{issue.title} · #{issue.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: "New comment on #{issue.to_reference}: #{issue.title}",
pretext: pretext,
color: color,
fields: fields,
mrkdwn_in: [
:title,
:pretext,
:fields
]
}
]
}
def fallback
"New comment on #{issue.to_reference}: #{issue.title}"
end
def pretext
......
......@@ -19,30 +19,15 @@ module Gitlab
private
def moved_issue(old_issue)
{
attachments: [
{
title: "#{@resource.title} · #{@resource.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: "Issue #{@resource.to_reference}: #{@resource.title}",
pretext: pretext(old_issue),
color: color(@resource),
fields: fields,
mrkdwn_in: [
:title,
:pretext,
:text,
:fields
]
}
]
}
response_message(custom_pretext: custom_pretext(old_issue))
end
def pretext(old_issue)
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(@resource)}*"
def fallback
"Issue #{issue.to_reference}: #{issue.title}"
end
def custom_pretext(old_issue)
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(issue)}*"
end
def issue_link(issue)
......
......@@ -7,36 +7,21 @@ module Gitlab
include Presenters::IssueBase
def present
in_channel_response(new_issue)
in_channel_response(response_message)
end
private
def new_issue
{
attachments: [
{
title: "#{@resource.title} · #{@resource.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: "New issue #{@resource.to_reference}: #{@resource.title}",
pretext: pretext,
color: color(@resource),
fields: fields,
mrkdwn_in: [
:title,
:pretext,
:text,
:fields
]
}
]
}
def fallback
"New issue #{issue.to_reference}: #{issue.title}"
end
def fields_with_markdown
%i(title pretext text fields)
end
def pretext
"I created an issue on #{author_profile_link}'s behalf: *#{@resource.to_reference}* in #{project_link}"
"I created an issue on #{author_profile_link}'s behalf: *#{issue.to_reference}* in #{project_link}"
end
end
end
......
......@@ -8,35 +8,16 @@ module Gitlab
def present
if @resource.confidential?
ephemeral_response(show_issue)
ephemeral_response(response_message)
else
in_channel_response(show_issue)
in_channel_response(response_message)
end
end
private
def show_issue
{
attachments: [
{
title: "#{@resource.title} · #{@resource.to_reference}",
title_link: resource_url,
author_name: author.name,
author_icon: author.avatar_url,
fallback: "Issue #{@resource.to_reference}: #{@resource.title}",
pretext: pretext,
text: text,
color: color(@resource),
fields: fields,
mrkdwn_in: [
:pretext,
:text,
:fields
]
}
]
}
def fallback
"Issue #{@resource.to_reference}: #{@resource.title}"
end
def text
......
......@@ -6,7 +6,7 @@ module Gitlab
module NoteBase
GREEN = '#38ae67'
def color
def color(_)
GREEN
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment