Commit aeb6a782 authored by Eugenia Grieff's avatar Eugenia Grieff

Use existing services to create epic links

- When the reordered object changes parent we need
to use the existing IssuableLinks::CreateService
corresponding to the moved object class
- Adjust permission to check that the user can
admin_epic_link
parent 1e0ff62a
......@@ -26,19 +26,26 @@ module Epics
def set_new_parent
return unless new_parent && new_parent_different?
moving_object.parent = new_parent
validate_new_parent
service = create_issuable_links(new_parent)
return unless service[:status] == :error
service[:message]
end
def new_parent_different?
params[:new_parent_id] != GitlabSchema.id_from_object(moving_object.parent)
end
def validate_new_parent
return unless moving_object.respond_to?(:valid_parent?)
return if moving_object.valid_parent?
def create_issuable_links(parent)
service, issuable = if moving_object.is_a?(Epic)
[EpicLinks::CreateService, moving_object]
elsif moving_object.is_a?(EpicIssue)
[EpicIssues::CreateService, moving_object.issue]
end
return unless service.present?
moving_object.errors[:parent]&.first
service.new(parent, current_user, { target_issuable: issuable }).execute
end
def move!
......@@ -110,6 +117,10 @@ module Epics
if new_parent
return false unless can?(current_user, :admin_epic, new_parent.group)
return false unless moving_object.parent && can?(current_user, :admin_epic, moving_object.parent.group)
if moving_object.is_a?(Epic)
return false unless can?(current_user, :admin_epic_link, moving_object.parent.group)
end
end
true
......
......@@ -215,6 +215,10 @@ RSpec.describe Epics::TreeReorderService do
let!(:tree_object_1) { epic1 }
let!(:tree_object_2) { epic2 }
before do
stub_licensed_features(epics: true, subepics: true)
end
context 'when user does not have permissions to admin the previous parent' do
let(:other_group) { create(:group) }
let(:other_epic) { create(:epic, group: other_group) }
......@@ -260,6 +264,19 @@ RSpec.describe Epics::TreeReorderService do
it_behaves_like 'error for the tree update', 'You don\'t have permissions to move the objects.'
end
context 'when new parent is another epic and subepics feature is disabled' do
let(:other_epic) { create(:epic, group: group) }
let(:new_parent_id) { GitlabSchema.id_from_object(epic) }
let(:epic3) { create(:epic, parent: other_epic, group: group) }
let(:tree_object_2) { epic3 }
before do
stub_licensed_features(epics: true, subepics: false)
end
it_behaves_like 'error for the tree update', 'You don\'t have permissions to move the objects.'
end
context 'when moving is successful' do
it 'updates the links relative positions' do
subject
......
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