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 ...@@ -26,19 +26,26 @@ module Epics
def set_new_parent def set_new_parent
return unless new_parent && new_parent_different? return unless new_parent && new_parent_different?
moving_object.parent = new_parent service = create_issuable_links(new_parent)
validate_new_parent return unless service[:status] == :error
service[:message]
end end
def new_parent_different? def new_parent_different?
params[:new_parent_id] != GitlabSchema.id_from_object(moving_object.parent) params[:new_parent_id] != GitlabSchema.id_from_object(moving_object.parent)
end end
def validate_new_parent def create_issuable_links(parent)
return unless moving_object.respond_to?(:valid_parent?) service, issuable = if moving_object.is_a?(Epic)
return if moving_object.valid_parent? [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 end
def move! def move!
...@@ -110,6 +117,10 @@ module Epics ...@@ -110,6 +117,10 @@ module Epics
if new_parent if new_parent
return false unless can?(current_user, :admin_epic, new_parent.group) 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) 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 end
true true
......
...@@ -215,6 +215,10 @@ RSpec.describe Epics::TreeReorderService do ...@@ -215,6 +215,10 @@ RSpec.describe Epics::TreeReorderService do
let!(:tree_object_1) { epic1 } let!(:tree_object_1) { epic1 }
let!(:tree_object_2) { epic2 } 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 context 'when user does not have permissions to admin the previous parent' do
let(:other_group) { create(:group) } let(:other_group) { create(:group) }
let(:other_epic) { create(:epic, group: other_group) } let(:other_epic) { create(:epic, group: other_group) }
...@@ -260,6 +264,19 @@ RSpec.describe Epics::TreeReorderService do ...@@ -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.' it_behaves_like 'error for the tree update', 'You don\'t have permissions to move the objects.'
end 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 context 'when moving is successful' do
it 'updates the links relative positions' do it 'updates the links relative positions' do
subject 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