Implement QuickActions::TargetService
This commit is contained in:
parent
2881886417
commit
82337dd684
5 changed files with 113 additions and 15 deletions
|
@ -1,5 +1,5 @@
|
||||||
class Projects::AutocompleteSourcesController < Projects::ApplicationController
|
class Projects::AutocompleteSourcesController < Projects::ApplicationController
|
||||||
before_action :load_autocomplete_service
|
before_action :load_autocomplete_service, except: [:members]
|
||||||
|
|
||||||
def members
|
def members
|
||||||
render json: ::Projects::ParticipantsService.new(@project, current_user).execute(target)
|
render json: ::Projects::ParticipantsService.new(@project, current_user).execute(target)
|
||||||
|
@ -32,6 +32,8 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def target
|
def target
|
||||||
@autocomplete_service.target(params[:type], params[:type_id])
|
QuickActions::TargetService
|
||||||
|
.new(project, current_user)
|
||||||
|
.execute(params[:type], params[:type_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,9 +29,9 @@ class PreviewMarkdownService < BaseService
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_commands_target
|
def find_commands_target
|
||||||
Projects::AutocompleteService
|
QuickActions::TargetService
|
||||||
.new(project, current_user)
|
.new(project, current_user)
|
||||||
.target(commands_target_type, commands_target_id)
|
.execute(commands_target_type, commands_target_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def commands_target_type
|
def commands_target_type
|
||||||
|
|
|
@ -51,16 +51,5 @@ module Projects
|
||||||
|
|
||||||
QuickActions::InterpretService.new(project, current_user).available_commands(noteable)
|
QuickActions::InterpretService.new(project, current_user).available_commands(noteable)
|
||||||
end
|
end
|
||||||
|
|
||||||
def target(type, type_id)
|
|
||||||
case type&.downcase
|
|
||||||
when 'issue'
|
|
||||||
IssuesFinder.new(current_user, project_id: project.id).find_by(iid: type_id) || project.issues.build
|
|
||||||
when 'mergerequest'
|
|
||||||
MergeRequestsFinder.new(current_user, project_id: project.id).find_by(iid: type_id) || project.merge_requests.build
|
|
||||||
when 'commit'
|
|
||||||
project.commit(type_id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
32
app/services/quick_actions/target_service.rb
Normal file
32
app/services/quick_actions/target_service.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module QuickActions
|
||||||
|
class TargetService < BaseService
|
||||||
|
def execute(type, type_id)
|
||||||
|
case type&.downcase
|
||||||
|
when 'issue'
|
||||||
|
issue(type_id)
|
||||||
|
when 'mergerequest'
|
||||||
|
merge_request(type_id)
|
||||||
|
when 'commit'
|
||||||
|
commit(type_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def issue(type_id)
|
||||||
|
IssuesFinder.new(current_user, project_id: project.id).find_by(iid: type_id) || project.issues.build
|
||||||
|
end
|
||||||
|
|
||||||
|
def merge_request(type_id)
|
||||||
|
MergeRequestsFinder.new(current_user, project_id: project.id).find_by(iid: type_id) || project.merge_requests.build
|
||||||
|
end
|
||||||
|
|
||||||
|
def commit(type_id)
|
||||||
|
return nil unless type_id
|
||||||
|
|
||||||
|
project.commit(type_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
75
spec/services/quick_actions/target_service_spec.rb
Normal file
75
spec/services/quick_actions/target_service_spec.rb
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe QuickActions::TargetService do
|
||||||
|
let(:project) { create(:project) }
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
let(:service) { described_class.new(project, user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
project.add_maintainer(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#execute' do
|
||||||
|
shared_examples 'no target' do |type_id:|
|
||||||
|
it 'returns nil' do
|
||||||
|
target = service.execute(type, type_id)
|
||||||
|
|
||||||
|
expect(target).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples 'find target' do
|
||||||
|
it 'returns the target' do
|
||||||
|
found_target = service.execute(type, target_id)
|
||||||
|
|
||||||
|
expect(found_target).to eq(target)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
shared_examples 'build target' do |type_id:|
|
||||||
|
it 'builds a new target' do
|
||||||
|
target = service.execute(type, type_id)
|
||||||
|
|
||||||
|
expect(target.project).to eq(project)
|
||||||
|
expect(target).to be_new_record
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for issue' do
|
||||||
|
let(:target) { create(:issue, project: project) }
|
||||||
|
let(:target_id) { target.iid }
|
||||||
|
let(:type) { 'Issue' }
|
||||||
|
|
||||||
|
it_behaves_like 'find target'
|
||||||
|
it_behaves_like 'build target', type_id: nil
|
||||||
|
it_behaves_like 'build target', type_id: -1
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for merge request' do
|
||||||
|
let(:target) { create(:merge_request, source_project: project) }
|
||||||
|
let(:target_id) { target.iid }
|
||||||
|
let(:type) { 'MergeRequest' }
|
||||||
|
|
||||||
|
it_behaves_like 'find target'
|
||||||
|
it_behaves_like 'build target', type_id: nil
|
||||||
|
it_behaves_like 'build target', type_id: -1
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for commit' do
|
||||||
|
let(:project) { create(:project, :repository) }
|
||||||
|
let(:target) { project.commit }
|
||||||
|
let(:target_id) { target.sha }
|
||||||
|
let(:type) { 'Commit' }
|
||||||
|
|
||||||
|
it_behaves_like 'find target'
|
||||||
|
it_behaves_like 'no target', type_id: 'invalid_sha'
|
||||||
|
it_behaves_like 'no target', type_id: nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for unknown type' do
|
||||||
|
let(:type) { 'unknown' }
|
||||||
|
|
||||||
|
it_behaves_like 'no target', type_id: :unused
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue