Commit 6d11e387 authored by Peter Leitzen's avatar Peter Leitzen Committed by Sean McGivern

Implement QuickActions::TargetService

parent aeabf075
class Projects::AutocompleteSourcesController < Projects::ApplicationController
before_action :load_autocomplete_service
before_action :load_autocomplete_service, except: [:members]
def members
render json: ::Projects::ParticipantsService.new(@project, current_user).execute(target)
......@@ -32,6 +32,8 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController
end
def target
@autocomplete_service.target(params[:type], params[:type_id])
QuickActions::TargetService
.new(project, current_user)
.execute(params[:type], params[:type_id])
end
end
......@@ -29,9 +29,9 @@ class PreviewMarkdownService < BaseService
end
def find_commands_target
Projects::AutocompleteService
QuickActions::TargetService
.new(project, current_user)
.target(commands_target_type, commands_target_id)
.execute(commands_target_type, commands_target_id)
end
def commands_target_type
......
......@@ -51,16 +51,5 @@ module Projects
QuickActions::InterpretService.new(project, current_user).available_commands(noteable)
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
# 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
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
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