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 Commands
class BaseService < ::BaseService
module Gitlab
module ChatCommands
class BaseCommand
QUERY_LIMIT = 5
def execute
def self.match(_)
raise NotImplementedError
end
def available?
def self.help_message
raise NotImplementedError
end
def self.available?(_)
raise NotImplementedError
end
def execute(_)
raise NotImplementedError
end
......@@ -15,33 +23,33 @@ module Mattermost
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
def find_by_iid(iid)
resource = collection.find_by(iid: iid)
readable?(resource) ? resource : nil
end
def search_results
def search_results(query)
collection.search(query).limit(QUERY_LIMIT).select do |resource|
readable?(resource)
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
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 Commands
class IssueService < Mattermost::Commands::BaseService
module Gitlab
module ChatCommands
class IssueCommand < BaseCommand
def self.available?(project)
project.issues_enabled? && project.default_issues_tracker?
end
......
module Mattermost
module Commands
class IssueCreateService < IssueService
def execute
title, description = parse_command
present Issues::CreateService.new(project, current_user, title: title, description: description).execute
module Gitlab
module ChatCommands
class IssueCreate < BaseCommand
def self.match(text)
/\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/.match(text)
end
private
def parse_command
match = params[:text].match(/\Aissue create (?<title>.*)\n*/)
def execute(match)
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
......
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 Commands
class MergeRequestService < Mattermost::Commands::BaseService
module Gitlab
module ChatCommands
class MergeRequestCommand < BaseCommand
def self.available?(project)
project.merge_requests_enabled?
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
}
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
{
......
require 'spec_helper'
describe Mattermost::SlashCommandService, service: true do
let(:project) { build(:project) }
let(:user) { build(:user) }
describe Gitlab::ChatCommands::Command, service: true do
let(:project) { create(:project) }
let(:user) { create(:user) }
let(:params) { { text: 'issue show 1' } }
subject { described_class.new(project, user, params).execute }
......
require 'spec_helper'
describe Mattermost::Commands::IssueCreateService, service: true do
describe Gitlab::ChatCommands::IssueCreate, service: true do
describe '#execute' do
let(:project) { create(:empty_project) }
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] }
subject { described_class.new(project, user, params).execute }
subject { described_class.new(project, user).execute(regex_match) }
context 'without description' do
it 'creates the issue' do
......@@ -23,7 +23,7 @@ describe Mattermost::Commands::IssueCreateService, service: true do
context 'with description' do
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 }
......
require 'spec_helper'
describe Mattermost::Commands::IssueSearchService, service: true do
describe Gitlab::ChatCommands::IssueSearch, service: true do
describe '#execute' do
let!(:issue) { create(:issue, title: 'The bird is the word') }
let(:project) { issue.project }
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] }
subject { described_class.new(project, user, params).execute }
subject { described_class.new(project, user).execute(regex_match) }
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
expect(subject[:response_type]).to be :ephemeral
......
require 'spec_helper'
describe Mattermost::Commands::IssueShowService, service: true do
describe Gitlab::ChatCommands::IssueShow, service: true do
describe '#execute' do
let(:issue) { create(:issue) }
let(:project) { issue.project }
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] }
subject { described_class.new(project, user, params).execute }
subject { described_class.new(project, user).execute(regex_match) }
context 'the issue exists' do
it 'returns the issue' do
expect(subject[:response_type]).to be :in_channel
expect(subject[:text]).to match issue.title
end
end
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
expect(subject[:response_type]).to be :ephemeral
......
require 'spec_helper'
describe Mattermost::Commands::MergeRequestSearchService, service: true do
describe Gitlab::ChatCommands::MergeRequestSearch, service: true do
describe '#execute' do
let!(:merge_request) { create(:merge_request, title: 'The bird is the word') }
let(:project) { merge_request.source_project }
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] }
subject { described_class.new(project, user, params).execute }
subject { described_class.new(project, user, {}).execute(regex_match) }
context 'the merge request exists' do
it 'returns the merge request' do
......@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
end
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
expect(subject[:response_type]).to be :ephemeral
......@@ -27,4 +27,10 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
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
require 'spec_helper'
describe Mattermost::Commands::MergeRequestShowService, service: true do
describe Gitlab::ChatCommands::MergeRequestShow, service: true do
describe '#execute' do
let!(:merge_request) { create(:merge_request) }
let(:project) { merge_request.source_project }
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] }
subject { described_class.new(project, user, params).execute }
subject { described_class.new(project, user).execute(regex_match) }
context 'the merge request exists' do
it 'returns the merge request' do
......@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
end
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
expect(subject[:response_type]).to be :ephemeral
......@@ -27,4 +27,11 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
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
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