summaryrefslogtreecommitdiff
path: root/lib/mattermost
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2016-12-20 09:41:37 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2016-12-20 09:41:37 +0000
commit52278412c7350b8087ef4ffc688c79e4593369cc (patch)
tree8f6769b3563db7c5d8ebaf01e2b1e939579913e6 /lib/mattermost
parent7572a31430cd4dbd6ed8d3da5f06010858d7c487 (diff)
parente06f88effa842c73d3827593f8d28846207bfca0 (diff)
downloadgitlab-ce-52278412c7350b8087ef4ffc688c79e4593369cc.tar.gz
Merge branch 'zj-kamil-slack-slash-commands' into 'master'
Slack slash commands ## What does this MR do? Implement Slack Slash Commands by utilizing generalized Mattermost presenter to fulfill Slack requirements. ## Why was this MR needed? We want to expose Slack Slash Commands as a first-class service. ## What are the relevant issue numbers? Supersedes !8007 Closes #22182 See merge request !8126
Diffstat (limited to 'lib/mattermost')
-rw-r--r--lib/mattermost/presenter.rb131
1 files changed, 0 insertions, 131 deletions
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb
deleted file mode 100644
index 67eda983a74..00000000000
--- a/lib/mattermost/presenter.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-module Mattermost
- class Presenter
- class << self
- include Gitlab::Routing.url_helpers
-
- def authorize_chat_name(url)
- message = if url
- ":wave: Hi there! Before I do anything for you, please [connect your GitLab account](#{url})."
- else
- ":sweat_smile: Couldn't identify you, nor can I autorize you!"
- end
-
- ephemeral_response(message)
- end
-
- def help(commands, trigger)
- if commands.none?
- ephemeral_response("No commands configured")
- else
- commands.map! { |command| "#{trigger} #{command}" }
- message = header_with_list("Available commands", commands)
-
- ephemeral_response(message)
- end
- end
-
- def present(subject)
- return not_found unless subject
-
- if subject.is_a?(Gitlab::ChatCommands::Result)
- show_result(subject)
- elsif subject.respond_to?(:count)
- if subject.many?
- multiple_resources(subject)
- elsif subject.none?
- not_found
- else
- single_resource(subject)
- end
- else
- single_resource(subject)
- end
- end
-
- def access_denied
- ephemeral_response("Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
- end
-
- private
-
- def show_result(result)
- case result.type
- when :success
- in_channel_response(result.message)
- else
- ephemeral_response(result.message)
- end
- end
-
- def not_found
- ephemeral_response("404 not found! GitLab couldn't find what you were looking for! :boom:")
- end
-
- def single_resource(resource)
- return error(resource) if resource.errors.any? || !resource.persisted?
-
- message = "### #{title(resource)}"
- message << "\n\n#{resource.description}" if resource.try(:description)
-
- in_channel_response(message)
- end
-
- def multiple_resources(resources)
- resources.map! { |resource| title(resource) }
-
- message = header_with_list("Multiple results were found:", resources)
-
- ephemeral_response(message)
- end
-
- def error(resource)
- message = header_with_list("The action was not successful, because:", resource.errors.messages)
-
- ephemeral_response(message)
- end
-
- def title(resource)
- reference = resource.try(:to_reference) || resource.try(:id)
- title = resource.try(:title) || resource.try(:name)
-
- "[#{reference} #{title}](#{url(resource)})"
- end
-
- def header_with_list(header, items)
- message = [header]
-
- items.each do |item|
- message << "- #{item}"
- end
-
- message.join("\n")
- end
-
- def url(resource)
- url_for(
- [
- resource.project.namespace.becomes(Namespace),
- resource.project,
- resource
- ]
- )
- end
-
- def ephemeral_response(message)
- {
- response_type: :ephemeral,
- text: message,
- status: 200
- }
- end
-
- def in_channel_response(message)
- {
- response_type: :in_channel,
- text: message,
- status: 200
- }
- end
- end
- end
-end