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 ...@@ -18,6 +18,8 @@ module Gitlab
private private
attr_reader :resource
def header_with_list(header, items) def header_with_list(header, items)
message = [header] message = [header]
...@@ -67,9 +69,9 @@ module Gitlab ...@@ -67,9 +69,9 @@ module Gitlab
def resource_url def resource_url
url_for( url_for(
[ [
@resource.project.namespace.becomes(Namespace), resource.project.namespace.becomes(Namespace),
@resource.project, resource.project,
@resource resource
] ]
) )
end end
...@@ -81,6 +83,37 @@ module Gitlab ...@@ -81,6 +83,37 @@ module Gitlab
def author_profile_link def author_profile_link
"[#{author.to_reference}](#{url_for(author)})" "[#{author.to_reference}](#{url_for(author)})"
end 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 end
end end
......
...@@ -45,6 +45,8 @@ module Gitlab ...@@ -45,6 +45,8 @@ module Gitlab
private private
attr_reader :resource attr_reader :resource
alias_method :issue, :resource
end end
end end
end end
......
...@@ -8,9 +8,9 @@ module Gitlab ...@@ -8,9 +8,9 @@ module Gitlab
def present def present
if @resource.confidential? if @resource.confidential?
ephemeral_response(close_issue) ephemeral_response(response_message)
else else
in_channel_response(close_issue) in_channel_response(response_message)
end end
end end
...@@ -20,30 +20,12 @@ module Gitlab ...@@ -20,30 +20,12 @@ module Gitlab
private private
def close_issue def fallback
{ "Closed issue #{issue.to_reference}: #{issue.title}"
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
]
}
]
}
end end
def pretext 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 end
end end
......
...@@ -7,31 +7,13 @@ module Gitlab ...@@ -7,31 +7,13 @@ module Gitlab
include Presenters::NoteBase include Presenters::NoteBase
def present def present
ephemeral_response(new_note) ephemeral_response(response_message)
end end
private private
def new_note def fallback
{ "New comment on #{issue.to_reference}: #{issue.title}"
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
]
}
]
}
end end
def pretext def pretext
......
...@@ -19,30 +19,15 @@ module Gitlab ...@@ -19,30 +19,15 @@ module Gitlab
private private
def moved_issue(old_issue) def moved_issue(old_issue)
{ response_message(custom_pretext: custom_pretext(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
]
}
]
}
end end
def pretext(old_issue) def fallback
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(@resource)}*" "Issue #{issue.to_reference}: #{issue.title}"
end
def custom_pretext(old_issue)
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(issue)}*"
end end
def issue_link(issue) def issue_link(issue)
......
...@@ -7,36 +7,21 @@ module Gitlab ...@@ -7,36 +7,21 @@ module Gitlab
include Presenters::IssueBase include Presenters::IssueBase
def present def present
in_channel_response(new_issue) in_channel_response(response_message)
end end
private private
def new_issue def fallback
{ "New issue #{issue.to_reference}: #{issue.title}"
attachments: [ end
{
title: "#{@resource.title} · #{@resource.to_reference}", def fields_with_markdown
title_link: resource_url, %i(title pretext text fields)
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
]
}
]
}
end end
def pretext 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 end
end end
......
...@@ -8,35 +8,16 @@ module Gitlab ...@@ -8,35 +8,16 @@ module Gitlab
def present def present
if @resource.confidential? if @resource.confidential?
ephemeral_response(show_issue) ephemeral_response(response_message)
else else
in_channel_response(show_issue) in_channel_response(response_message)
end end
end end
private private
def show_issue def fallback
{ "Issue #{@resource.to_reference}: #{@resource.title}"
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
]
}
]
}
end end
def text def text
......
...@@ -6,7 +6,7 @@ module Gitlab ...@@ -6,7 +6,7 @@ module Gitlab
module NoteBase module NoteBase
GREEN = '#38ae67' GREEN = '#38ae67'
def color def color(_)
GREEN GREEN
end 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