summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-11-16 18:28:38 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 21:34:24 +0100
commit1b4fdb9893af28606b7594ee656438c7ef21e9d8 (patch)
tree420a687514aedcc68c5a2dbe649123e3a52fdb02 /lib
parent8c8bc07d32f1103bb7996b499ead6ad6eb5bd337 (diff)
downloadgitlab-ce-1b4fdb9893af28606b7594ee656438c7ef21e9d8.tar.gz
Rename from service, and move to lib/gitlab
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/chat_commands/base_command.rb55
-rw-r--r--lib/gitlab/chat_commands/command.rb49
-rw-r--r--lib/gitlab/chat_commands/issue_command.rb17
-rw-r--r--lib/gitlab/chat_commands/issue_create.rb16
-rw-r--r--lib/gitlab/chat_commands/issue_search.rb17
-rw-r--r--lib/gitlab/chat_commands/issue_show.rb17
-rw-r--r--lib/gitlab/chat_commands/merge_request_command.rb17
-rw-r--r--lib/gitlab/chat_commands/merge_request_search.rb17
-rw-r--r--lib/gitlab/chat_commands/merge_request_show.rb17
-rw-r--r--lib/mattermost/presenter.rb7
10 files changed, 225 insertions, 4 deletions
diff --git a/lib/gitlab/chat_commands/base_command.rb b/lib/gitlab/chat_commands/base_command.rb
new file mode 100644
index 00000000000..13dc2a0f3e8
--- /dev/null
+++ b/lib/gitlab/chat_commands/base_command.rb
@@ -0,0 +1,55 @@
+module Gitlab
+ module ChatCommands
+ class BaseCommand
+ QUERY_LIMIT = 5
+
+ def self.match(_)
+ raise NotImplementedError
+ end
+
+ def self.help_message
+ raise NotImplementedError
+ end
+
+ def self.available?(_)
+ raise NotImplementedError
+ end
+
+ def execute(_)
+ raise NotImplementedError
+ end
+
+ def collection
+ raise NotImplementedError
+ end
+
+ attr_accessor :project, :current_user, :params
+
+ def initialize(project, user, params = {})
+ @project, @current_user, @params = project, user, params.dup
+ end
+
+ private
+
+ def can?(object, action, subject)
+ Ability.allowed?(object, action, subject)
+ end
+
+ def present(resource)
+ Mattermost::Presenter.present(resource)
+ end
+
+ def find_by_iid(iid)
+ resource = collection.find_by(iid: iid)
+
+ readable?(resource) ? resource : nil
+ end
+
+ def search_results(query)
+ collection.search(query).limit(QUERY_LIMIT).select do |resource|
+ readable?(resource)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/command.rb b/lib/gitlab/chat_commands/command.rb
new file mode 100644
index 00000000000..06d09ab0e24
--- /dev/null
+++ b/lib/gitlab/chat_commands/command.rb
@@ -0,0 +1,49 @@
+module Gitlab
+ module ChatCommands
+ class Command < BaseCommand
+ COMMANDS = [
+ Gitlab::ChatCommands::IssueShow,
+ Gitlab::ChatCommands::IssueSearch,
+ Gitlab::ChatCommands::IssueCreate,
+
+ Gitlab::ChatCommands::MergeRequestShow,
+ Gitlab::ChatCommands::MergeRequestSearch,
+ ].freeze
+
+ def execute
+ klass, match = fetch_klass
+
+ return help(help_messages) unless klass.try(:available?, project)
+
+ klass.new(project, current_user, params).execute(match)
+ end
+
+ private
+
+ def fetch_klass
+ match = nil
+ service = COMMANDS.find do |klass|
+ if klass.available?(project)
+ false
+ else
+ match = klass.match(command)
+ end
+ end
+
+ [service, match]
+ end
+
+ def help_messages
+ COMMANDS.map do |klass|
+ next unless klass.available?(project)
+
+ klass.help_message
+ end.compact
+ end
+
+ def command
+ params[:text]
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/issue_command.rb b/lib/gitlab/chat_commands/issue_command.rb
new file mode 100644
index 00000000000..2426b3714b7
--- /dev/null
+++ b/lib/gitlab/chat_commands/issue_command.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class IssueCommand < BaseCommand
+ def self.available?(project)
+ project.issues_enabled? && project.default_issues_tracker?
+ end
+
+ def collection
+ project.issues
+ end
+
+ def readable?(issue)
+ can?(current_user, :read_issue, issue)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/issue_create.rb b/lib/gitlab/chat_commands/issue_create.rb
new file mode 100644
index 00000000000..c424e845402
--- /dev/null
+++ b/lib/gitlab/chat_commands/issue_create.rb
@@ -0,0 +1,16 @@
+module Gitlab
+ module ChatCommands
+ class IssueCreate < BaseCommand
+ def self.match(text)
+ /\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text)
+ end
+
+ def execute(match)
+ title = match[:title]
+ description = match[:description]
+
+ present Issues::CreateService.new(project, current_user, title: title, description: description).execute
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/issue_search.rb b/lib/gitlab/chat_commands/issue_search.rb
new file mode 100644
index 00000000000..4169e2a7a88
--- /dev/null
+++ b/lib/gitlab/chat_commands/issue_search.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class IssueSearch < IssueCommand
+ def self.match(text)
+ /\Aissue\s+search\s+(?<query>.*)/.match(text)
+ end
+
+ def self.help_message
+ "issue search <query>"
+ end
+
+ def execute(match)
+ present search_results(match[:query])
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/issue_show.rb b/lib/gitlab/chat_commands/issue_show.rb
new file mode 100644
index 00000000000..e5530df31cc
--- /dev/null
+++ b/lib/gitlab/chat_commands/issue_show.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class IssueShow < IssueCommand
+ def self.match(text)
+ /\Aissue\s+show\s+(?<iid>\d+)/.match(text)
+ end
+
+ def self.help_message
+ "issue show <id>"
+ end
+
+ def execute(match)
+ present find_by_iid(match[:iid])
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/merge_request_command.rb b/lib/gitlab/chat_commands/merge_request_command.rb
new file mode 100644
index 00000000000..e0f69a49afd
--- /dev/null
+++ b/lib/gitlab/chat_commands/merge_request_command.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class MergeRequestCommand < BaseCommand
+ def self.available?(project)
+ project.merge_requests_enabled?
+ end
+
+ def collection
+ project.merge_requests
+ end
+
+ def readable?(_)
+ can?(current_user, :read_merge_request, project)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/merge_request_search.rb b/lib/gitlab/chat_commands/merge_request_search.rb
new file mode 100644
index 00000000000..caecb1a788e
--- /dev/null
+++ b/lib/gitlab/chat_commands/merge_request_search.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class MergeRequestSearch < MergeRequestCommand
+ def self.match(text)
+ /\Amergerequest\s+search\s+(?<query>.*)/.match(text)
+ end
+
+ def self.help_message
+ "mergerequest search <query>"
+ end
+
+ def execute(match)
+ present search_results(match[:query])
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/chat_commands/merge_request_show.rb b/lib/gitlab/chat_commands/merge_request_show.rb
new file mode 100644
index 00000000000..7ed5445e4c2
--- /dev/null
+++ b/lib/gitlab/chat_commands/merge_request_show.rb
@@ -0,0 +1,17 @@
+module Gitlab
+ module ChatCommands
+ class MergeRequestShow < MergeRequestCommand
+ def self.match(text)
+ /\Amergerequest\s+show\s+(?<iid>\d+)/.match(text)
+ end
+
+ def self.help_message
+ "mergerequest show <id>"
+ end
+
+ def execute(match)
+ present find_by_iid(match[:iid])
+ end
+ end
+ end
+end
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb
index e1502e3f9ba..3db55d6bd51 100644
--- a/lib/mattermost/presenter.rb
+++ b/lib/mattermost/presenter.rb
@@ -12,12 +12,11 @@ module Mattermost
}
end
- # TODO figure out how I know which are available or not
- def help_message(commands)
+ def help(messages)
messages = ["Available commands:"]
- commands.each do |sub_command, attrs|
- messages << "\t#{COMMAND_PREFIX} #{attrs[:help_message]}"
+ messages.each do |messsage|
+ messages << "- #{message}"
end
{