Commit b6da269c authored by Felipe Artur's avatar Felipe Artur

Add RelatedEpicsLinks::DestroyService

Add service to allow destruction of RelatedEpicLinks

Changelog: other
EE: true
parent 39f74244
......@@ -4,11 +4,13 @@ module IssuableLinks
class DestroyService < BaseService
include IncidentManagement::UsageData
attr_reader :link, :current_user
attr_reader :link, :current_user, :source, :target
def initialize(link, user)
@link = link
@current_user = user
@source = link.source
@target = link.target
end
def execute
......@@ -22,6 +24,11 @@ module IssuableLinks
private
def create_notes
SystemNoteService.unrelate_issuable(source, target, current_user)
SystemNoteService.unrelate_issuable(target, source, current_user)
end
def after_destroy
create_notes
track_event
......
......@@ -4,23 +4,10 @@ module IssueLinks
class DestroyService < IssuableLinks::DestroyService
private
def source
@source ||= link.source
end
def target
@target ||= link.target
end
def permission_to_remove_relation?
can?(current_user, :admin_issue_link, source) && can?(current_user, :admin_issue_link, target)
end
def create_notes
SystemNoteService.unrelate_issue(source, target, current_user)
SystemNoteService.unrelate_issue(target, source, current_user)
end
def track_event
track_incident_action(current_user, target, :incident_unrelate)
end
......
......@@ -53,8 +53,8 @@ module SystemNoteService
::SystemNotes::IssuablesService.new(noteable: noteable, project: noteable.project, author: user).relate_issue(noteable_ref)
end
def unrelate_issue(noteable, noteable_ref, user)
::SystemNotes::IssuablesService.new(noteable: noteable, project: noteable.project, author: user).unrelate_issue(noteable_ref)
def unrelate_issuable(noteable, noteable_ref, user)
::SystemNotes::IssuablesService.new(noteable: noteable, project: noteable.project, author: user).unrelate_issuable(noteable_ref)
end
# Called when the due_date of a Noteable is changed
......
......@@ -26,8 +26,8 @@ module SystemNotes
# "removed the relation with gitlab-foss#9001"
#
# Returns the created Note object
def unrelate_issue(noteable_ref)
body = "removed the relation with #{noteable_ref.to_reference(noteable.project)}"
def unrelate_issuable(noteable_ref)
body = "removed the relation with #{noteable_ref.to_reference(noteable.resource_parent)}"
issue_activity_counter.track_issue_unrelated_action(author: author) if noteable.is_a?(Issue)
......
......@@ -4,6 +4,13 @@ module EpicIssues
class DestroyService < IssuableLinks::DestroyService
extend ::Gitlab::Utils::Override
def initialize(link, user)
@link = link
@current_user = user
@source = link.epic
@target = link.issue
end
private
override :after_destroy
......@@ -18,14 +25,6 @@ module EpicIssues
::Gitlab::UsageDataCounters::EpicActivityUniqueCounter.track_epic_issue_removed(author: current_user)
end
def source
@source ||= link.epic
end
def target
@target ||= link.issue
end
def permission_to_remove_relation?
can?(current_user, :admin_epic_issue, target) && can?(current_user, :read_epic, source)
end
......
# frozen_string_literal: true
module Epics
module RelatedEpicLinks
class DestroyService < ::IssuableLinks::DestroyService
private
def permission_to_remove_relation?
can?(current_user, :admin_related_epic_link, source) && can?(current_user, :admin_related_epic_link, target)
end
def track_event
# No op
end
def not_found_message
'No Related Epic Link found'
end
end
end
end
......@@ -2,6 +2,11 @@
module FeatureFlagIssues
class DestroyService < IssuableLinks::DestroyService
def initialize(link, user)
@link = link
@current_user = user
end
def permission_to_remove_relation?
can?(current_user, :admin_feature_flag, link.feature_flag)
end
......
......@@ -88,4 +88,15 @@ RSpec.describe ::SystemNotes::IssuablesService do
end
end
end
describe '#unrelate_issuable' do
let(:noteable) { epic }
let(:target) { create(:epic) }
it 'creates system notes when epic gets unrelated' do
result = service.unrelate_issuable(target)
expect(result.note).to eq("removed the relation with #{target.to_reference(noteable.group)}")
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Epics::RelatedEpicLinks::DestroyService do
describe '#execute' do
let_it_be(:user) { create(:user) }
let!(:issuable_link) { create(:related_epic_link) }
before do
stub_licensed_features(epics: true, related_epics: true)
end
subject { described_class.new(issuable_link, user).execute }
it_behaves_like 'a destroyable issuable link'
end
end
......@@ -4,65 +4,26 @@ require 'spec_helper'
RSpec.describe IssueLinks::DestroyService do
describe '#execute' do
let(:project) { create(:project_empty_repo) }
let(:user) { create(:user) }
let_it_be(:project) { create(:project_empty_repo, :private) }
let_it_be(:user) { create(:user) }
let_it_be(:issue_a) { create(:issue, project: project) }
let_it_be(:issue_b) { create(:issue, project: project) }
subject { described_class.new(issue_link, user).execute }
let!(:issuable_link) { create(:issue_link, source: issue_a, target: issue_b) }
context 'when successfully removes an issue link' do
let(:issue_a) { create(:issue, project: project) }
let(:issue_b) { create(:issue, project: project) }
subject { described_class.new(issuable_link, user).execute }
let!(:issue_link) { create(:issue_link, source: issue_a, target: issue_b) }
it_behaves_like 'a destroyable issuable link'
context 'when target is an incident' do
before do
project.add_reporter(user)
end
it 'removes related issue' do
expect { subject }.to change(IssueLink, :count).from(1).to(0)
end
it 'creates notes' do
# Two-way notes creation
expect(SystemNoteService).to receive(:unrelate_issue)
.with(issue_link.source, issue_link.target, user)
expect(SystemNoteService).to receive(:unrelate_issue)
.with(issue_link.target, issue_link.source, user)
subject
end
it 'returns success message' do
is_expected.to eq(message: 'Relation was removed', status: :success)
end
context 'target is an incident' do
let(:issue_b) { create(:incident, project: project) }
it_behaves_like 'an incident management tracked event', :incident_management_incident_unrelate do
let(:current_user) { user }
end
end
end
context 'when failing to remove an issue link' do
let(:unauthorized_project) { create(:project) }
let(:issue_a) { create(:issue, project: project) }
let(:issue_b) { create(:issue, project: unauthorized_project) }
let!(:issue_link) { create(:issue_link, source: issue_a, target: issue_b) }
it 'does not remove relation' do
expect { subject }.not_to change(IssueLink, :count).from(1)
end
it 'does not create notes' do
expect(SystemNoteService).not_to receive(:unrelate_issue)
end
let(:issue_b) { create(:incident, project: project) }
it 'returns error message' do
is_expected.to eq(message: 'No Issue Link found', status: :error, http_status: 404)
it_behaves_like 'an incident management tracked event', :incident_management_incident_unrelate do
let(:current_user) { user }
end
end
end
......
......@@ -117,7 +117,7 @@ RSpec.describe SystemNoteService do
end
end
describe '.unrelate_issue' do
describe '.unrelate_issuable' do
let(:noteable_ref) { double }
let(:noteable) { double }
......@@ -127,10 +127,10 @@ RSpec.describe SystemNoteService do
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:unrelate_issue).with(noteable_ref)
expect(service).to receive(:unrelate_issuable).with(noteable_ref)
end
described_class.unrelate_issue(noteable, noteable_ref, double)
described_class.unrelate_issuable(noteable, noteable_ref, double)
end
end
......
......@@ -30,10 +30,10 @@ RSpec.describe ::SystemNotes::IssuablesService do
end
end
describe '#unrelate_issue' do
describe '#unrelate_issuable' do
let(:noteable_ref) { create(:issue) }
subject { service.unrelate_issue(noteable_ref) }
subject { service.unrelate_issuable(noteable_ref) }
it_behaves_like 'a system note' do
let(:action) { 'unrelate' }
......
# frozen_string_literal: true
shared_examples 'a destroyable issuable link' do
context 'when successfully removes an issuable link' do
before do
issuable_link.source.resource_parent.add_reporter(user)
issuable_link.target.resource_parent.add_reporter(user)
end
it 'removes related issue' do
expect { subject }.to change(issuable_link.class, :count).by(-1)
end
it 'creates notes' do
# Two-way notes creation
expect(SystemNoteService).to receive(:unrelate_issuable)
.with(issuable_link.source, issuable_link.target, user)
expect(SystemNoteService).to receive(:unrelate_issuable)
.with(issuable_link.target, issuable_link.source, user)
subject
end
it 'returns success message' do
is_expected.to eq(message: 'Relation was removed', status: :success)
end
end
context 'when failing to remove an issuable link' do
it 'does not remove relation' do
expect { subject }.not_to change(issuable_link.class, :count).from(1)
end
it 'does not create notes' do
expect(SystemNoteService).not_to receive(:unrelate_issuable)
end
it 'returns error message' do
is_expected.to eq(message: "No #{issuable_link.class.model_name.human.titleize} found", status: :error, http_status: 404)
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