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
def authorize
message =
if @resource
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{@resource})."
if resource
":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{resource})."
else
":sweat_smile: Couldn't identify you, nor can I authorize you!"
end
......
......@@ -18,6 +18,8 @@ module Gitlab
private
attr_reader :resource
def header_with_list(header, items)
message = [header]
......@@ -67,12 +69,51 @@ module Gitlab
def resource_url
url_for(
[
@resource.project.namespace.becomes(Namespace),
@resource.project,
@resource
resource.project.namespace.becomes(Namespace),
resource.project,
resource
]
)
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
......
......@@ -42,17 +42,11 @@ module Gitlab
]
end
def project_link
"[#{project.full_name}](#{project.web_url})"
end
def author_profile_link
"[#{author.to_reference}](#{url_for(author)})"
end
private
attr_reader :resource
alias_method :issue, :resource
end
end
end
......
......@@ -7,43 +7,25 @@ module Gitlab
include Presenters::IssueBase
def present
if @resource.confidential?
ephemeral_response(close_issue)
if resource.confidential?
ephemeral_response(response_message)
else
in_channel_response(close_issue)
in_channel_response(response_message)
end
end
def already_closed
ephemeral_response(text: "Issue #{@resource.to_reference} is already closed.")
ephemeral_response(text: "Issue #{resource.to_reference} is already closed.")
end
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_message
"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_message
"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
]
}
]
}
end
def pretext(old_issue)
"Moved issue *#{issue_link(old_issue)}* to *#{issue_link(@resource)}*"
response_message(custom_pretext: custom_pretext(old_issue))
end
def fallback_message
"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_message
"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
......
......@@ -7,12 +7,12 @@ module Gitlab
include Presenters::IssueBase
def present
text = if @resource.count >= 5
text = if resource.count >= 5
"Here are the first 5 issues I found:"
elsif @resource.one?
elsif resource.one?
"Here is the only issue I found:"
else
"Here are the #{@resource.count} issues I found:"
"Here are the #{resource.count} issues I found:"
end
ephemeral_response(text: text, attachments: attachments)
......@@ -21,7 +21,7 @@ module Gitlab
private
def attachments
@resource.map do |issue|
resource.map do |issue|
url = "[#{issue.to_reference}](#{url_for([namespace, project, issue])})"
{
......@@ -37,7 +37,7 @@ module Gitlab
end
def project
@project ||= @resource.first.project
@project ||= resource.first.project
end
def namespace
......
......@@ -7,55 +7,36 @@ module Gitlab
include Presenters::IssueBase
def present
if @resource.confidential?
ephemeral_response(show_issue)
if resource.confidential?
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_message
"Issue #{resource.to_reference}: #{resource.title}"
end
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
end
message << " · "
message << ":+1: #{@resource.upvotes} " unless @resource.upvotes.zero?
message << ":-1: #{@resource.downvotes} " unless @resource.downvotes.zero?
message << ":speech_balloon: #{@resource.user_notes_count}" unless @resource.user_notes_count.zero?
message << ":+1: #{resource.upvotes} " unless resource.upvotes.zero?
message << ":-1: #{resource.downvotes} " unless resource.downvotes.zero?
message << ":speech_balloon: #{resource.user_notes_count}" unless resource.user_notes_count.zero?
message.join
end
def pretext
"Issue *#{@resource.to_reference}* from #{project.full_name}"
"Issue *#{resource.to_reference}* from #{project.full_name}"
end
end
end
......
......@@ -6,7 +6,7 @@ module Gitlab
module NoteBase
GREEN = '#38ae67'
def color
def color(_)
GREEN
end
......@@ -18,18 +18,10 @@ module Gitlab
issue.project
end
def project_link
"[#{project.full_name}](#{project.web_url})"
end
def author
resource.author
end
def author_profile_link
"[#{author.to_reference}](#{url_for(author)})"
end
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