summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2017-01-10 19:43:58 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2017-01-30 09:24:05 +0100
commitdc6921bdbbabd08be4426345140cb507b286eac7 (patch)
tree2c402abd7b9578019f36f4a0336b34acc646cf18 /lib
parentb525aff665f139cd12ac5a6df78d722427e759cc (diff)
downloadgitlab-ce-dc6921bdbbabd08be4426345140cb507b286eac7.tar.gz
Chat Commands have presenters
This improves the styling and readability of the code. This is supported by both Mattermost and Slack.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/chat_commands/base_command.rb4
-rw-r--r--lib/gitlab/chat_commands/command.rb22
-rw-r--r--lib/gitlab/chat_commands/deploy.rb24
-rw-r--r--lib/gitlab/chat_commands/issue_create.rb18
-rw-r--r--lib/gitlab/chat_commands/issue_search.rb10
-rw-r--r--lib/gitlab/chat_commands/issue_show.rb8
-rw-r--r--lib/gitlab/chat_commands/presenter.rb131
-rw-r--r--lib/gitlab/chat_commands/presenters/access.rb22
-rw-r--r--lib/gitlab/chat_commands/presenters/base.rb73
-rw-r--r--lib/gitlab/chat_commands/presenters/deploy.rb24
-rw-r--r--lib/gitlab/chat_commands/presenters/issuable.rb33
-rw-r--r--lib/gitlab/chat_commands/presenters/list_issues.rb32
-rw-r--r--lib/gitlab/chat_commands/presenters/show_issue.rb38
-rw-r--r--lib/mattermost/error.rb3
-rw-r--r--lib/mattermost/session.rb160
15 files changed, 266 insertions, 336 deletions
diff --git a/lib/gitlab/chat_commands/base_command.rb b/lib/gitlab/chat_commands/base_command.rb
index 4fe53ce93a9..25da8474e95 100644
--- a/lib/gitlab/chat_commands/base_command.rb
+++ b/lib/gitlab/chat_commands/base_command.rb
@@ -42,10 +42,6 @@ module Gitlab
def find_by_iid(iid)
collection.find_by(iid: iid)
end
-
- def presenter
- Gitlab::ChatCommands::Presenter.new
- end
end
end
end
diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb
index 145086755e4..ac7ee868402 100644
--- a/lib/gitlab/chat_commands/command.rb
+++ b/lib/gitlab/chat_commands/command.rb
@@ -13,9 +13,9 @@ module Gitlab
if command
if command.allowed?(project, current_user)
- present command.new(project, current_user, params).execute(match)
+ command.new(project, current_user, params).execute(match)
else
- access_denied
+ Gitlab::ChatCommands::Presenters::Access.new.access_denied
end
else
help(help_messages)
@@ -25,7 +25,7 @@ module Gitlab
def match_command
match = nil
service = available_commands.find do |klass|
- match = klass.match(command)
+ match = klass.match(params[:text])
end
[service, match]
@@ -42,22 +42,6 @@ module Gitlab
klass.available?(project)
end
end
-
- def command
- params[:text]
- end
-
- def help(messages)
- presenter.help(messages, params[:command])
- end
-
- def access_denied
- presenter.access_denied
- end
-
- def present(resource)
- presenter.present(resource)
- end
end
end
end
diff --git a/lib/gitlab/chat_commands/deploy.rb b/lib/gitlab/chat_commands/deploy.rb
index 7127d2f6d04..458d90f84e8 100644
--- a/lib/gitlab/chat_commands/deploy.rb
+++ b/lib/gitlab/chat_commands/deploy.rb
@@ -1,8 +1,6 @@
module Gitlab
module ChatCommands
class Deploy < BaseCommand
- include Gitlab::Routing.url_helpers
-
def self.match(text)
/\Adeploy\s+(?<from>\S+.*)\s+to+\s+(?<to>\S+.*)\z/.match(text)
end
@@ -24,35 +22,29 @@ module Gitlab
to = match[:to]
actions = find_actions(from, to)
- return unless actions.present?
- if actions.one?
- play!(from, to, actions.first)
+ if actions.none?
+ Gitlab::ChatCommands::Presenters::Deploy.new(nil).no_actions
+ elsif actions.one?
+ action = play!(from, to, actions.first)
+ Gitlab::ChatCommands::Presenters::Deploy.new(action).present(from, to)
else
- Result.new(:error, 'Too many actions defined')
+ Gitlab::ChatCommands::Presenters::Deploy.new(actions).too_many_actions
end
end
private
def play!(from, to, action)
- new_action = action.play(current_user)
-
- Result.new(:success, "Deployment from #{from} to #{to} started. Follow the progress: #{url(new_action)}.")
+ action.play(current_user)
end
def find_actions(from, to)
environment = project.environments.find_by(name: from)
- return unless environment
+ return [] unless environment
environment.actions_for(to).select(&:starts_environment?)
end
-
- def url(subject)
- polymorphic_url(
- [subject.project.namespace.becomes(Namespace), subject.project, subject]
- )
- end
end
end
end
diff --git a/lib/gitlab/chat_commands/issue_create.rb b/lib/gitlab/chat_commands/issue_create.rb
index cefb6775db8..a06f13b0f72 100644
--- a/lib/gitlab/chat_commands/issue_create.rb
+++ b/lib/gitlab/chat_commands/issue_create.rb
@@ -2,7 +2,7 @@ module Gitlab
module ChatCommands
class IssueCreate < IssueCommand
def self.match(text)
- # we can not match \n with the dot by passing the m modifier as than
+ # we can not match \n with the dot by passing the m modifier as than
# the title and description are not seperated
/\Aissue\s+(new|create)\s+(?<title>[^\n]*)\n*(?<description>(.|\n)*)/.match(text)
end
@@ -19,8 +19,24 @@ module Gitlab
title = match[:title]
description = match[:description].to_s.rstrip
+ issue = create_issue(title: title, description: description)
+
+ if issue.errors.any?
+ presenter(issue).display_errors
+ else
+ presenter(issue).present
+ end
+ end
+
+ private
+
+ def create_issue(title:, description:)
Issues::CreateService.new(project, current_user, title: title, description: description).execute
end
+
+ def presenter(issue)
+ Gitlab::ChatCommands::Presenters::ShowIssue.new(issue)
+ end
end
end
end
diff --git a/lib/gitlab/chat_commands/issue_search.rb b/lib/gitlab/chat_commands/issue_search.rb
index 51bf80c800b..e2d3a0f466a 100644
--- a/lib/gitlab/chat_commands/issue_search.rb
+++ b/lib/gitlab/chat_commands/issue_search.rb
@@ -10,7 +10,15 @@ module Gitlab
end
def execute(match)
- collection.search(match[:query]).limit(QUERY_LIMIT)
+ issues = collection.search(match[:query]).limit(QUERY_LIMIT)
+
+ if issues.none?
+ Presenters::Access.new(issues).not_found
+ elsif issues.one?
+ Presenters::ShowIssue.new(issues.first).present
+ else
+ Presenters::ListIssues.new(issues).present
+ end
end
end
end
diff --git a/lib/gitlab/chat_commands/issue_show.rb b/lib/gitlab/chat_commands/issue_show.rb
index 2a45d49cf6b..9f3e1b9a64b 100644
--- a/lib/gitlab/chat_commands/issue_show.rb
+++ b/lib/gitlab/chat_commands/issue_show.rb
@@ -10,7 +10,13 @@ module Gitlab
end
def execute(match)
- find_by_iid(match[:iid])
+ issue = find_by_iid(match[:iid])
+
+ if issue
+ Gitlab::ChatCommands::Presenters::ShowIssue.new(issue).present
+ else
+ Gitlab::ChatCommands::Presenters::Access.new.not_found
+ end
end
end
end
diff --git a/lib/gitlab/chat_commands/presenter.rb b/lib/gitlab/chat_commands/presenter.rb
deleted file mode 100644
index 8930a21f406..00000000000
--- a/lib/gitlab/chat_commands/presenter.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-module Gitlab
- module ChatCommands
- class Presenter
- include Gitlab::Routing
-
- 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.none?
- not_found
- elsif subject.one?
- single_resource(subject.first)
- else
- multiple_resources(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)
- titles = resources.map { |resource| title(resource) }
-
- message = header_with_list("Multiple results were found:", titles)
-
- 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
diff --git a/lib/gitlab/chat_commands/presenters/access.rb b/lib/gitlab/chat_commands/presenters/access.rb
new file mode 100644
index 00000000000..6d18d745608
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/access.rb
@@ -0,0 +1,22 @@
+module Gitlab::ChatCommands::Presenters
+ class Access < Gitlab::ChatCommands::Presenters::Base
+ def access_denied
+ ephemeral_response(text: "Whoops! This action is not allowed. This incident will be [reported](https://xkcd.com/838/).")
+ end
+
+ def not_found
+ ephemeral_response(text: "404 not found! GitLab couldn't find what you were looking for! :boom:")
+ end
+
+ def authorize
+ message =
+ 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 autorize you!"
+ end
+
+ ephemeral_response(text: message)
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/presenters/base.rb b/lib/gitlab/chat_commands/presenters/base.rb
new file mode 100644
index 00000000000..0897025d85f
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/base.rb
@@ -0,0 +1,73 @@
+module Gitlab::ChatCommands::Presenters
+ class Base
+ include Gitlab::Routing.url_helpers
+
+ def initialize(resource = nil)
+ @resource = resource
+ end
+
+ def display_errors
+ message = header_with_list("The action was not successful, because:", @resource.errors.full_messages)
+
+ ephemeral_response(text: message)
+ end
+
+ private
+
+ def header_with_list(header, items)
+ message = [header]
+
+ items.each do |item|
+ message << "- #{item}"
+ end
+
+ message.join("\n")
+ end
+
+ def ephemeral_response(message)
+ response = {
+ response_type: :ephemeral,
+ status: 200
+ }.merge(message)
+
+ format_response(response)
+ end
+
+ def in_channel_response(message)
+ response = {
+ response_type: :in_channel,
+ status: 200
+ }.merge(message)
+
+ format_response(response)
+ end
+
+ def format_response(response)
+ response[:text] = format(response[:text]) if response.has_key?(:text)
+
+ if response.has_key?(:attachments)
+ response[:attachments].each do |attachment|
+ attachment[:pretext] = format(attachment[:pretext]) if attachment[:pretext]
+ attachment[:text] = format(attachment[:text]) if attachment[:text]
+ end
+ end
+
+ response
+ end
+
+ # Convert Markdown to slacks format
+ def format(string)
+ Slack::Notifier::LinkFormatter.format(string)
+ end
+
+ def resource_url
+ url_for(
+ [
+ @resource.project.namespace.becomes(Namespace),
+ @resource.project,
+ @resource
+ ]
+ )
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/presenters/deploy.rb b/lib/gitlab/chat_commands/presenters/deploy.rb
new file mode 100644
index 00000000000..4f6333812ff
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/deploy.rb
@@ -0,0 +1,24 @@
+module Gitlab::ChatCommands::Presenters
+ class Deploy < Gitlab::ChatCommands::Presenters::Base
+ def present(from, to)
+ message = "Deployment started from #{from} to #{to}. [Follow its progress](#{resource_url})."
+ in_channel_response(text: message)
+ end
+
+ def no_actions
+ ephemeral_response(text: "No action found to be executed")
+ end
+
+ def too_many_actions
+ ephemeral_response(text: "Too many actions defined")
+ end
+
+ private
+
+ def resource_url
+ polymorphic_url(
+ [ @resource.project.namespace.becomes(Namespace), @resource.project, @resource]
+ )
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/presenters/issuable.rb b/lib/gitlab/chat_commands/presenters/issuable.rb
new file mode 100644
index 00000000000..9623387f188
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/issuable.rb
@@ -0,0 +1,33 @@
+module Gitlab::ChatCommands::Presenters
+ class Issuable < Gitlab::ChatCommands::Presenters::Base
+ private
+
+ def project
+ @resource.project
+ end
+
+ def author
+ @resource.author
+ end
+
+ def fields
+ [
+ {
+ title: "Assignee",
+ value: @resource.assignee ? @resource.assignee.name : "_None_",
+ short: true
+ },
+ {
+ title: "Milestone",
+ value: @resource.milestone ? @resource.milestone.title : "_None_",
+ short: true
+ },
+ {
+ title: "Labels",
+ value: @resource.labels.any? ? @resource.label_names : "_None_",
+ short: true
+ }
+ ]
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/presenters/list_issues.rb b/lib/gitlab/chat_commands/presenters/list_issues.rb
new file mode 100644
index 00000000000..5a7b3fca5c2
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/list_issues.rb
@@ -0,0 +1,32 @@
+module Gitlab::ChatCommands::Presenters
+ class ListIssues < Gitlab::ChatCommands::Presenters::Base
+ def present
+ ephemeral_response(text: "Here are the issues I found:", attachments: attachments)
+ end
+
+ private
+
+ def attachments
+ @resource.map do |issue|
+ state = issue.open? ? "Open" : "Closed"
+
+ {
+ fallback: "Issue #{issue.to_reference}: #{issue.title}",
+ color: "#d22852",
+ text: "[#{issue.to_reference}](#{url_for([namespace, project, issue])}) ยท #{issue.title} (#{state})",
+ mrkdwn_in: [
+ "text"
+ ]
+ }
+ end
+ end
+
+ def project
+ @project ||= @resource.first.project
+ end
+
+ def namespace
+ @namespace ||= project.namespace.becomes(Namespace)
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/presenters/show_issue.rb b/lib/gitlab/chat_commands/presenters/show_issue.rb
new file mode 100644
index 00000000000..2a89c30b972
--- /dev/null
+++ b/lib/gitlab/chat_commands/presenters/show_issue.rb
@@ -0,0 +1,38 @@
+module Gitlab::ChatCommands::Presenters
+ class ShowIssue < Gitlab::ChatCommands::Presenters::Issuable
+ def present
+ in_channel_response(show_issue)
+ end
+
+ private
+
+ def show_issue
+ {
+ attachments: [
+ {
+ title: @resource.title,
+ title_link: resource_url,
+ author_name: author.name,
+ author_icon: author.avatar_url,
+ fallback: "#{@resource.to_reference}: #{@resource.title}",
+ text: text,
+ fields: fields,
+ mrkdwn_in: [
+ :title,
+ :text
+ ]
+ }
+ ]
+ }
+ end
+
+ def text
+ 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
+ end
+ end
+end
diff --git a/lib/mattermost/error.rb b/lib/mattermost/error.rb
deleted file mode 100644
index 014df175be0..00000000000
--- a/lib/mattermost/error.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-module Mattermost
- class Error < StandardError; end
-end
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
deleted file mode 100644
index 377cb7b1021..00000000000
--- a/lib/mattermost/session.rb
+++ /dev/null
@@ -1,160 +0,0 @@
-module Mattermost
- class NoSessionError < Mattermost::Error
- def message
- 'No session could be set up, is Mattermost configured with Single Sign On?'
- end
- end
-
- class ConnectionError < Mattermost::Error; end
-
- # This class' prime objective is to obtain a session token on a Mattermost
- # instance with SSO configured where this GitLab instance is the provider.
- #
- # The process depends on OAuth, but skips a step in the authentication cycle.
- # For example, usually a user would click the 'login in GitLab' button on
- # Mattermost, which would yield a 302 status code and redirects you to GitLab
- # to approve the use of your account on Mattermost. Which would trigger a
- # callback so Mattermost knows this request is approved and gets the required
- # data to create the user account etc.
- #
- # This class however skips the button click, and also the approval phase to
- # speed up the process and keep it without manual action and get a session
- # going.
- class Session
- include Doorkeeper::Helpers::Controller
- include HTTParty
-
- LEASE_TIMEOUT = 60
-
- base_uri Settings.mattermost.host
-
- attr_accessor :current_resource_owner, :token
-
- def initialize(current_user)
- @current_resource_owner = current_user
- end
-
- def with_session
- with_lease do
- raise Mattermost::NoSessionError unless create
-
- begin
- yield self
- rescue Errno::ECONNREFUSED
- raise Mattermost::NoSessionError
- ensure
- destroy
- end
- end
- end
-
- # Next methods are needed for Doorkeeper
- def pre_auth
- @pre_auth ||= Doorkeeper::OAuth::PreAuthorization.new(
- Doorkeeper.configuration, server.client_via_uid, params)
- end
-
- def authorization
- @authorization ||= strategy.request
- end
-
- def strategy
- @strategy ||= server.authorization_request(pre_auth.response_type)
- end
-
- def request
- @request ||= OpenStruct.new(parameters: params)
- end
-
- def params
- Rack::Utils.parse_query(oauth_uri.query).symbolize_keys
- end
-
- def get(path, options = {})
- handle_exceptions do
- self.class.get(path, options.merge(headers: @headers))
- end
- end
-
- def post(path, options = {})
- handle_exceptions do
- self.class.post(path, options.merge(headers: @headers))
- end
- end
-
- private
-
- def create
- return unless oauth_uri
- return unless token_uri
-
- @token = request_token
- @headers = {
- Authorization: "Bearer #{@token}"
- }
-
- @token
- end
-
- def destroy
- post('/api/v3/users/logout')
- end
-
- def oauth_uri
- return @oauth_uri if defined?(@oauth_uri)
-
- @oauth_uri = nil
-
- response = get("/api/v3/oauth/gitlab/login", follow_redirects: false)
- return unless 300 <= response.code && response.code < 400
-
- redirect_uri = response.headers['location']
- return unless redirect_uri
-
- @oauth_uri = URI.parse(redirect_uri)
- end
-
- def token_uri
- @token_uri ||=
- if oauth_uri
- authorization.authorize.redirect_uri if pre_auth.authorizable?
- end
- end
-
- def request_token
- response = get(token_uri, follow_redirects: false)
-
- if 200 <= response.code && response.code < 400
- response.headers['token']
- end
- end
-
- def with_lease
- lease_uuid = lease_try_obtain
- raise NoSessionError unless lease_uuid
-
- begin
- yield
- ensure
- Gitlab::ExclusiveLease.cancel(lease_key, lease_uuid)
- end
- end
-
- def lease_key
- "mattermost:session"
- end
-
- def lease_try_obtain
- lease = ::Gitlab::ExclusiveLease.new(lease_key, timeout: LEASE_TIMEOUT)
- lease.try_obtain
- end
-
- def handle_exceptions
- yield
- rescue HTTParty::Error => e
- raise Mattermost::ConnectionError.new(e.message)
- rescue Errno::ECONNREFUSED
- raise Mattermost::ConnectionError.new(e.message)
- end
- end
-end