Commit 1b4fdb98 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Rename from service, and move to lib/gitlab

parent 8c8bc07d
module Mattermost
module Commands
class IssueSearchService < IssueService
def execute
present search_results
end
end
end
end
module Mattermost
module Commands
class IssueShowService < IssueService
def execute
present find_by_iid
end
end
end
end
module Mattermost
module Commands
class MergeRequestSearchService < MergeRequestService
def execute
present search_results
end
end
end
end
module Mattermost
module Commands
class MergeRequestShowService < MergeRequestService
def execute
present find_by_iid
end
end
end
end
module Mattermost
class SlashCommandService < BaseService
def self.registry
@registry ||= Hash.new({})
end
def self.command(command, sub_command, klass, help_message)
registry[command][sub_command] = { klass: klass, help_message: help_message }
end
command 'issue', 'show', Mattermost::Commands::IssueShowService, 'issue show <id>'
command 'issue', 'search', Mattermost::Commands::IssueSearchService, 'issue search <query>'
command 'issue', 'create', Mattermost::Commands::IssueCreateService, 'issue create my title'
command 'mergerequest', 'show', Mattermost::Commands::MergeRequestShowService, 'mergerequest show <id>'
command 'mergerequest', 'search', Mattermost::Commands::MergeRequestSearchService, 'mergerequest search <query>'
def execute
command, subcommand = parse_command
#TODO think how to do this to support ruby 2.1
service = registry.dig(command, subcommand, :klass)
return help_messages(registry) unless service.try(:available?, project)
service.new(project, current_user, params).execute
end
private
def parse_command
params[:text].split.first(2)
end
def registry
self.class.registry
end
end
end
module Mattermost module Gitlab
module Commands module ChatCommands
class BaseService < ::BaseService class BaseCommand
QUERY_LIMIT = 5 QUERY_LIMIT = 5
def execute def self.match(_)
raise NotImplementedError raise NotImplementedError
end end
def available? def self.help_message
raise NotImplementedError
end
def self.available?(_)
raise NotImplementedError
end
def execute(_)
raise NotImplementedError raise NotImplementedError
end end
...@@ -15,33 +23,33 @@ module Mattermost ...@@ -15,33 +23,33 @@ module Mattermost
raise NotImplementedError raise NotImplementedError
end end
attr_accessor :project, :current_user, :params
def initialize(project, user, params = {})
@project, @current_user, @params = project, user, params.dup
end
private private
def can?(object, action, subject)
Ability.allowed?(object, action, subject)
end
def present(resource) def present(resource)
Mattermost::Presenter.present(resource) Mattermost::Presenter.present(resource)
end end
def find_by_iid def find_by_iid(iid)
resource = collection.find_by(iid: iid) resource = collection.find_by(iid: iid)
readable?(resource) ? resource : nil readable?(resource) ? resource : nil
end end
def search_results def search_results(query)
collection.search(query).limit(QUERY_LIMIT).select do |resource| collection.search(query).limit(QUERY_LIMIT).select do |resource|
readable?(resource) readable?(resource)
end end
end end
# params[:text] = issue search <search query>
def query
params[:text].split[2..-1].join(' ')
end
# params[:text] = 'mergerequest show 123'
def iid
params[:text].split[2]
end
end end
end end
end end
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
module Mattermost module Gitlab
module Commands module ChatCommands
class IssueService < Mattermost::Commands::BaseService class IssueCommand < BaseCommand
def self.available?(project) def self.available?(project)
project.issues_enabled? && project.default_issues_tracker? project.issues_enabled? && project.default_issues_tracker?
end end
......
module Mattermost module Gitlab
module Commands module ChatCommands
class IssueCreateService < IssueService class IssueCreate < BaseCommand
def execute def self.match(text)
title, description = parse_command /\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text)
present Issues::CreateService.new(project, current_user, title: title, description: description).execute
end end
private def execute(match)
def parse_command
match = params[:text].match(/\Aissue create (?<title>.*)\n*/)
title = match[:title] title = match[:title]
description = match.post_match description = match[:description]
[title, description] present Issues::CreateService.new(project, current_user, title: title, description: description).execute
end end
end end
end end
......
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
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
module Mattermost module Gitlab
module Commands module ChatCommands
class MergeRequestService < Mattermost::Commands::BaseService class MergeRequestCommand < BaseCommand
def self.available?(project) def self.available?(project)
project.merge_requests_enabled? project.merge_requests_enabled?
end end
......
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
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
...@@ -12,12 +12,11 @@ module Mattermost ...@@ -12,12 +12,11 @@ module Mattermost
} }
end end
# TODO figure out how I know which are available or not def help(messages)
def help_message(commands)
messages = ["Available commands:"] messages = ["Available commands:"]
commands.each do |sub_command, attrs| messages.each do |messsage|
messages << "\t#{COMMAND_PREFIX} #{attrs[:help_message]}" messages << "- #{message}"
end end
{ {
......
require 'spec_helper' require 'spec_helper'
describe Mattermost::SlashCommandService, service: true do describe Gitlab::ChatCommands::Command, service: true do
let(:project) { build(:project) } let(:project) { create(:project) }
let(:user) { build(:user) } let(:user) { create(:user) }
let(:params) { { text: 'issue show 1' } } let(:params) { { text: 'issue show 1' } }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user, params).execute }
......
require 'spec_helper' require 'spec_helper'
describe Mattermost::Commands::IssueCreateService, service: true do describe Gitlab::ChatCommands::IssueCreate, service: true do
describe '#execute' do describe '#execute' do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:params) { { text: "issue create bird is the word" } } let(:regex_match) { described_class.match("issue create bird is the word") }
before { project.team << [user, :master] } before { project.team << [user, :master] }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user).execute(regex_match) }
context 'without description' do context 'without description' do
it 'creates the issue' do it 'creates the issue' do
...@@ -23,7 +23,7 @@ describe Mattermost::Commands::IssueCreateService, service: true do ...@@ -23,7 +23,7 @@ describe Mattermost::Commands::IssueCreateService, service: true do
context 'with description' do context 'with description' do
let(:description) { "Surfin bird" } let(:description) { "Surfin bird" }
let(:params) { { text: "issue create The bird is the word\n#{description}" } } let(:regex_match) { described_class.match("issue create bird is the word\n#{description}") }
before { subject } before { subject }
......
require 'spec_helper' require 'spec_helper'
describe Mattermost::Commands::IssueSearchService, service: true do describe Gitlab::ChatCommands::IssueSearch, service: true do
describe '#execute' do describe '#execute' do
let!(:issue) { create(:issue, title: 'The bird is the word') } let!(:issue) { create(:issue, title: 'The bird is the word') }
let(:project) { issue.project } let(:project) { issue.project }
let(:user) { issue.author } let(:user) { issue.author }
let(:params) { { text: "issue search bird is the" } } let(:regex_match) { described_class.match("issue search bird is the") }
before { project.team << [user, :master] } before { project.team << [user, :master] }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user).execute(regex_match) }
context 'without results' do context 'without results' do
let(:params) { { text: "issue search no results for this one" } } let(:regex_match) { described_class.match("issue search no results for this one") }
it "returns nil" do it "returns nil" do
expect(subject[:response_type]).to be :ephemeral expect(subject[:response_type]).to be :ephemeral
......
require 'spec_helper' require 'spec_helper'
describe Mattermost::Commands::IssueShowService, service: true do describe Gitlab::ChatCommands::IssueShow, service: true do
describe '#execute' do describe '#execute' do
let(:issue) { create(:issue) } let(:issue) { create(:issue) }
let(:project) { issue.project } let(:project) { issue.project }
let(:user) { issue.author } let(:user) { issue.author }
let(:params) { { text: "issue show #{issue.iid}" } } let(:regex_match) { described_class.match("issue show #{issue.iid}") }
before { project.team << [user, :master] } before { project.team << [user, :master] }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user).execute(regex_match) }
context 'the issue exists' do context 'the issue exists' do
it 'returns the issue' do it 'returns the issue' do
expect(subject[:response_type]).to be :in_channel expect(subject[:response_type]).to be :in_channel
expect(subject[:text]).to match issue.title expect(subject[:text]).to match issue.title
end end
end end
context 'the issue does not exist' do context 'the issue does not exist' do
let(:params) { { text: "issue show 12345" } } let(:regex_match) { described_class.match("issue show 1234") }
it "returns nil" do it "returns nil" do
expect(subject[:response_type]).to be :ephemeral expect(subject[:response_type]).to be :ephemeral
......
require 'spec_helper' require 'spec_helper'
describe Mattermost::Commands::MergeRequestSearchService, service: true do describe Gitlab::ChatCommands::MergeRequestSearch, service: true do
describe '#execute' do describe '#execute' do
let!(:merge_request) { create(:merge_request, title: 'The bird is the word') } let!(:merge_request) { create(:merge_request, title: 'The bird is the word') }
let(:project) { merge_request.source_project } let(:project) { merge_request.source_project }
let(:user) { merge_request.author } let(:user) { merge_request.author }
let(:params) { { text: "mergerequest search #{merge_request.title}" } } let(:regex_match) { described_class.match("mergerequest search #{merge_request.title}") }
before { project.team << [user, :master] } before { project.team << [user, :master] }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user, {}).execute(regex_match) }
context 'the merge request exists' do context 'the merge request exists' do
it 'returns the merge request' do it 'returns the merge request' do
...@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do ...@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
end end
context 'no results can be found' do context 'no results can be found' do
let(:params) { { text: "mergerequest search 12345" } } let(:regex_match) { described_class.match("mergerequest search 12334") }
it "returns a 404 message" do it "returns a 404 message" do
expect(subject[:response_type]).to be :ephemeral expect(subject[:response_type]).to be :ephemeral
...@@ -27,4 +27,10 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do ...@@ -27,4 +27,10 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
end end
end end
end end
describe 'self.match' do
it 'matches a valid query' do
expect(described_class.match("mergerequest search my title here")).to be_truthy
end
end
end end
require 'spec_helper' require 'spec_helper'
describe Mattermost::Commands::MergeRequestShowService, service: true do describe Gitlab::ChatCommands::MergeRequestShow, service: true do
describe '#execute' do describe '#execute' do
let!(:merge_request) { create(:merge_request) } let!(:merge_request) { create(:merge_request) }
let(:project) { merge_request.source_project } let(:project) { merge_request.source_project }
let(:user) { merge_request.author } let(:user) { merge_request.author }
let(:params) { { text: "mergerequest show #{merge_request.iid}" } } let(:regex_match) { described_class.match("mergerequest show #{merge_request.iid}") }
before { project.team << [user, :master] } before { project.team << [user, :master] }
subject { described_class.new(project, user, params).execute } subject { described_class.new(project, user).execute(regex_match) }
context 'the merge request exists' do context 'the merge request exists' do
it 'returns the merge request' do it 'returns the merge request' do
...@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do ...@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
end end
context 'the merge request does not exist' do context 'the merge request does not exist' do
let(:params) { { text: "mergerequest show 12345" } } let(:regex_match) { described_class.match("mergerequest show 12345") }
it "returns nil" do it "returns nil" do
expect(subject[:response_type]).to be :ephemeral expect(subject[:response_type]).to be :ephemeral
...@@ -27,4 +27,11 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do ...@@ -27,4 +27,11 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
end end
end end
end end
describe "self.match" do
it 'matches valid strings' do
expect(described_class.match("mergerequest show 123")).to be_truthy
expect(described_class.match("mergerequest show sdf23")).to be_falsy
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment