From fa12c1b52c23151690ca005594086f9a00baee73 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Mon, 12 Dec 2016 11:20:17 +0100 Subject: PoC of multiple presenters for chat messages [ci skip] --- .../mattermost_slash_commands_service.rb | 8 +++-- lib/gitlab/chat_commands/command.rb | 6 ++-- lib/gitlab/chat_commands/issue_create.rb | 4 +-- .../chat_commands/presenters/mattermost/access.rb | 4 +++ .../presenters/mattermost/new_issue.rb | 23 ++++++++++++++ lib/gitlab/chat_commands/presenters/new_issue.rb | 37 ---------------------- .../chat_commands/presenters/slack/new_issue.rb | 37 ++++++++++++++++++++++ 7 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 lib/gitlab/chat_commands/presenters/mattermost/access.rb create mode 100644 lib/gitlab/chat_commands/presenters/mattermost/new_issue.rb delete mode 100644 lib/gitlab/chat_commands/presenters/new_issue.rb create mode 100644 lib/gitlab/chat_commands/presenters/slack/new_issue.rb diff --git a/app/models/project_services/mattermost_slash_commands_service.rb b/app/models/project_services/mattermost_slash_commands_service.rb index ebb3b7f8203..c38f3e3d5e7 100644 --- a/app/models/project_services/mattermost_slash_commands_service.rb +++ b/app/models/project_services/mattermost_slash_commands_service.rb @@ -8,7 +8,7 @@ class MattermostSlashCommandsService < ChatService end def title - 'Mattermost Command' + 'Mattermost Slash Commands' end def description @@ -34,11 +34,15 @@ class MattermostSlashCommandsService < ChatService return Gitlab::ChatCommands::Presenters::Access.new(url).authorize end - Gitlab::ChatCommands::Command.new(project, user, params).execute + Gitlab::ChatCommands::Command.new(project, user, params).execute(presenter_strategy) end private + def presenter_strategy + Gitlab::ChatCommands::Presenters::Mattermost + end + def find_chat_user(params) ChatNames::FindUserService.new(self, params).execute end diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb index 880ddd8b9d0..4b730b0e7fc 100644 --- a/lib/gitlab/chat_commands/command.rb +++ b/lib/gitlab/chat_commands/command.rb @@ -8,14 +8,14 @@ module Gitlab Gitlab::ChatCommands::Deploy, ].freeze - def execute + def execute(presenter) command, match = match_command if command if command.allowed?(project, current_user) - command.new(project, current_user, params).execute(match) + command.new(project, current_user, params).execute(presenter_module, match) else - Gitlab::ChatCommands::Presenters::Access.new(match).access_denied + presenter::Access.new(match).access_denied end else Gitlab::ChatCommands::Help.new(project, current_user, params).execute(available_commands) diff --git a/lib/gitlab/chat_commands/issue_create.rb b/lib/gitlab/chat_commands/issue_create.rb index 1f860f8a6e4..608cab52394 100644 --- a/lib/gitlab/chat_commands/issue_create.rb +++ b/lib/gitlab/chat_commands/issue_create.rb @@ -15,12 +15,12 @@ module Gitlab can?(user, :create_issue, project) end - def execute(match) + def execute(presenter, match) title = match[:title] description = match[:description].to_s.rstrip issue = create_issue(title: title, description: description) - Gitlab::ChatCommands::Presenters::NewIssue.new(issue).present + persenter::NewIssue.new(issue).present end def create_issue(title:, description:) diff --git a/lib/gitlab/chat_commands/presenters/mattermost/access.rb b/lib/gitlab/chat_commands/presenters/mattermost/access.rb new file mode 100644 index 00000000000..1714258448e --- /dev/null +++ b/lib/gitlab/chat_commands/presenters/mattermost/access.rb @@ -0,0 +1,4 @@ +module Gitlab::ChatCommands::Presenters::Mattermost::Access + class Access < Gitlab::ChatCommands::Presenters::Access + end +end diff --git a/lib/gitlab/chat_commands/presenters/mattermost/new_issue.rb b/lib/gitlab/chat_commands/presenters/mattermost/new_issue.rb new file mode 100644 index 00000000000..bb828a684b7 --- /dev/null +++ b/lib/gitlab/chat_commands/presenters/mattermost/new_issue.rb @@ -0,0 +1,23 @@ +module Gitlab::ChatCommands::Presenters::Mattermost + class NewIssue < Gitlab::ChatCommands::Presenters::Issuable + def present + if @resource.errors.any? + display_errors + else + in_channel_response(new_issue) + end + end + + def new_issue + message = [ + "A new issue was opened by #{author.to_reference} on #{project.to_reference}", + "___", + "![#{author.name}](#{author.avatar_url}) #{author.name}", + "**#{@resource.title} ยท #{resource.to_reference}**", + "___" + ].join("\n") + + ephemeral_response(text: message) + end + end +end diff --git a/lib/gitlab/chat_commands/presenters/new_issue.rb b/lib/gitlab/chat_commands/presenters/new_issue.rb deleted file mode 100644 index bfbae3bee9a..00000000000 --- a/lib/gitlab/chat_commands/presenters/new_issue.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Gitlab::ChatCommands::Presenters - class NewIssue < Gitlab::ChatCommands::Presenters::Issuable - def present - if @resource.errors.any? - display_errors - else - in_channel_response(new_issue) - end - end - - def new_issue - { - attachments: [ - { - title: @resource.title, - title_link: resource_url, - author_name: author.name, - author_icon: author.avatar_url, - fallback: "Issue created: #{@resource.title}", - pretext: pretext, - mrkdwn_in: [ - :title, - :pretext - ] - } - ] - } - end - - def pretext - profile_link = "[#{author.to_reference}](#{user_url(author)})" - project_link = "[#{project.to_reference}](#{project.web_url})" - - "New issue by #{profile_link} on #{project_link}" - end - end -end diff --git a/lib/gitlab/chat_commands/presenters/slack/new_issue.rb b/lib/gitlab/chat_commands/presenters/slack/new_issue.rb new file mode 100644 index 00000000000..bfbae3bee9a --- /dev/null +++ b/lib/gitlab/chat_commands/presenters/slack/new_issue.rb @@ -0,0 +1,37 @@ +module Gitlab::ChatCommands::Presenters + class NewIssue < Gitlab::ChatCommands::Presenters::Issuable + def present + if @resource.errors.any? + display_errors + else + in_channel_response(new_issue) + end + end + + def new_issue + { + attachments: [ + { + title: @resource.title, + title_link: resource_url, + author_name: author.name, + author_icon: author.avatar_url, + fallback: "Issue created: #{@resource.title}", + pretext: pretext, + mrkdwn_in: [ + :title, + :pretext + ] + } + ] + } + end + + def pretext + profile_link = "[#{author.to_reference}](#{user_url(author)})" + project_link = "[#{project.to_reference}](#{project.web_url})" + + "New issue by #{profile_link} on #{project_link}" + end + end +end -- cgit v1.2.1