From 1b4fdb9893af28606b7594ee656438c7ef21e9d8 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Wed, 16 Nov 2016 18:28:38 +0100 Subject: Rename from service, and move to lib/gitlab --- lib/gitlab/chat_commands/base_command.rb | 55 +++++++++++++++++++++++ lib/gitlab/chat_commands/command.rb | 49 ++++++++++++++++++++ lib/gitlab/chat_commands/issue_command.rb | 17 +++++++ lib/gitlab/chat_commands/issue_create.rb | 16 +++++++ lib/gitlab/chat_commands/issue_search.rb | 17 +++++++ lib/gitlab/chat_commands/issue_show.rb | 17 +++++++ lib/gitlab/chat_commands/merge_request_command.rb | 17 +++++++ lib/gitlab/chat_commands/merge_request_search.rb | 17 +++++++ lib/gitlab/chat_commands/merge_request_show.rb | 17 +++++++ 9 files changed, 222 insertions(+) create mode 100644 lib/gitlab/chat_commands/base_command.rb create mode 100644 lib/gitlab/chat_commands/command.rb create mode 100644 lib/gitlab/chat_commands/issue_command.rb create mode 100644 lib/gitlab/chat_commands/issue_create.rb create mode 100644 lib/gitlab/chat_commands/issue_search.rb create mode 100644 lib/gitlab/chat_commands/issue_show.rb create mode 100644 lib/gitlab/chat_commands/merge_request_command.rb create mode 100644 lib/gitlab/chat_commands/merge_request_search.rb create mode 100644 lib/gitlab/chat_commands/merge_request_show.rb (limited to 'lib/gitlab/chat_commands') 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+(?[^\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 -- cgit v1.2.1