Commit ec916fc3 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'refactor-slash-commands-presenters' into 'master'

Refactor slash commands presenters

Closes #35416

See merge request gitlab-org/gitlab!19947
parents 0cce5877 62810d8c
...@@ -34,8 +34,8 @@ module Gitlab ...@@ -34,8 +34,8 @@ module Gitlab
def authorize def authorize
message = message =
if @resource if resource
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{@resource})." ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{resource})."
else else
":sweat_smile: Couldn't identify you, nor can I authorize you!" ":sweat_smile: Couldn't identify you, nor can I authorize you!"
end end
......
...@@ -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,12 +69,51 @@ module Gitlab ...@@ -67,12 +69,51 @@ 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
def project_link
"[#{project.full_name}](#{project.web_url})"
end
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_message,
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
......
...@@ -42,17 +42,11 @@ module Gitlab ...@@ -42,17 +42,11 @@ module Gitlab
] ]
end end
def project_link
"[#{project.full_name}](#{project.web_url})"
end
def author_profile_link
"[#{author.to_reference}](#{url_for(author)})"
end
private private
attr_reader :resource attr_reader :resource
alias_method :issue, :resource
end end
end end
end end
......
...@@ -7,43 +7,25 @@ module Gitlab ...@@ -7,43 +7,25 @@ module Gitlab
include Presenters::IssueBase include Presenters::IssueBase
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
def already_closed def already_closed
ephemeral_response(text: "Issue #{@resource.to_reference} is already closed.") ephemeral_response(text: "Issue #{resource.to_reference} is already closed.")
end end
private private
def close_issue def fallback_message
{ "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_message
{ "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: [ end
{
title: "#{@resource.title} · #{@resource.to_reference}", def fallback_message
title_link: resource_url, "Issue #{issue.to_reference}: #{issue.title}"
author_name: author.name, end
author_icon: author.avatar_url,
fallback: "Issue #{@resource.to_reference}: #{@resource.title}", def custom_pretext(old_issue)
pretext: pretext(old_issue), "Moved issue *#{issue_link(old_issue)}* to *#{issue_link(issue)}*"
color: color(@resource),
fields: fields,
mrkdwn_in: [
:title,
:pretext,
:text,
:fields
]
}
]
}
end
def pretext(old_issue)
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(@resource)}*"
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_message
{ "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
......
...@@ -7,12 +7,12 @@ module Gitlab ...@@ -7,12 +7,12 @@ module Gitlab
include Presenters::IssueBase include Presenters::IssueBase
def present def present
text = if @resource.count >= 5 text = if resource.count >= 5
"Here are the first 5 issues I found:" "Here are the first 5 issues I found:"
elsif @resource.one? elsif resource.one?
"Here is the only issue I found:" "Here is the only issue I found:"
else else
"Here are the #{@resource.count} issues I found:" "Here are the #{resource.count} issues I found:"
end end
ephemeral_response(text: text, attachments: attachments) ephemeral_response(text: text, attachments: attachments)
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
private private
def attachments def attachments
@resource.map do |issue| resource.map do |issue|
url = "[#{issue.to_reference}](#{url_for([namespace, project, issue])})" url = "[#{issue.to_reference}](#{url_for([namespace, project, issue])})"
{ {
...@@ -37,7 +37,7 @@ module Gitlab ...@@ -37,7 +37,7 @@ module Gitlab
end end
def project def project
@project ||= @resource.first.project @project ||= resource.first.project
end end
def namespace def namespace
......
...@@ -7,55 +7,36 @@ module Gitlab ...@@ -7,55 +7,36 @@ module Gitlab
include Presenters::IssueBase include Presenters::IssueBase
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_message
{ "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
message = ["**#{status_text(@resource)}**"] message = ["**#{status_text(resource)}**"]
if @resource.upvotes.zero? && @resource.downvotes.zero? && @resource.user_notes_count.zero? if resource.upvotes.zero? && resource.downvotes.zero? && resource.user_notes_count.zero?
return message.join return message.join
end end
message << " · " message << " · "
message << ":+1: #{@resource.upvotes} " unless @resource.upvotes.zero? message << ":+1: #{resource.upvotes} " unless resource.upvotes.zero?
message << ":-1: #{@resource.downvotes} " unless @resource.downvotes.zero? message << ":-1: #{resource.downvotes} " unless resource.downvotes.zero?
message << ":speech_balloon: #{@resource.user_notes_count}" unless @resource.user_notes_count.zero? message << ":speech_balloon: #{resource.user_notes_count}" unless resource.user_notes_count.zero?
message.join message.join
end end
def pretext def pretext
"Issue *#{@resource.to_reference}* from #{project.full_name}" "Issue *#{resource.to_reference}* from #{project.full_name}"
end end
end end
end end
......
...@@ -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
...@@ -18,18 +18,10 @@ module Gitlab ...@@ -18,18 +18,10 @@ module Gitlab
issue.project issue.project
end end
def project_link
"[#{project.full_name}](#{project.web_url})"
end
def author def author
resource.author resource.author
end end
def author_profile_link
"[#{author.to_reference}](#{url_for(author)})"
end
def fields def fields
[ [
{ {
......
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