Commit d5993408 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Create RelatedIssues::DestroyService

parent bf7ba36b
module RelatedIssues
class DestroyService < BaseService
def initialize(related_issue, user)
@related_issue = related_issue
@current_user = user
@issue = related_issue.issue
@referenced_issue = related_issue.related_issue
end
def execute
remove_relation!
create_notes!
success(message: 'Relation was removed')
end
private
def remove_relation!
@related_issue.destroy!
end
def create_notes!
SystemNoteService.unrelate_issue(@issue, @referenced_issue, current_user)
SystemNoteService.unrelate_issue(@referenced_issue, @issue, current_user)
end
end
end
......@@ -553,8 +553,9 @@ module SystemNoteService
end
#
# noteable - Noteable object
# user - User performing approve
# noteable - Noteable object
# noteable_ref - Referenced noteable object
# user - User performing reference
#
# Example Note text:
#
......@@ -567,6 +568,22 @@ module SystemNoteService
create_note(NoteSummary.new(noteable, noteable.project, user, body, action: 'relate'))
end
#
# noteable - Noteable object
# noteable_ref - Referenced noteable object
# user - User performing reference
#
# Example Note text:
#
# "removed the relation with gitlab-ce#9001"
#
# Returns the created Note object
def unrelate_issue(noteable, noteable_ref, user)
body = "removed the relation with #{noteable_ref.to_reference(noteable.project)}"
create_note(NoteSummary.new(noteable, noteable.project, user, body, action: 'relate'))
end
# Called when the merge request is approved by user
#
# noteable - Noteable object
......
require 'spec_helper'
describe RelatedIssues::DestroyService, service: true do
describe '#execute' do
let(:user) { create :user }
let!(:related_issue) { create :related_issue }
subject { described_class.new(related_issue, user).execute }
it 'remove related issue' do
expect { subject }.to change(RelatedIssue, :count).from(1).to(0)
end
it 'create notes' do
# Two-way notes creation
expect(SystemNoteService).to receive(:unrelate_issue)
.with(related_issue.issue, related_issue.related_issue, user)
expect(SystemNoteService).to receive(:unrelate_issue)
.with(related_issue.related_issue, related_issue.issue, user)
subject
end
it 'returns success message' do
is_expected.to eq(message: 'Relation was removed', status: :success)
end
end
end
......@@ -915,6 +915,22 @@ describe SystemNoteService, services: true do
end
end
describe '.unrelate_issue' do
let(:noteable_ref) { create(:issue) }
subject { described_class.unrelate_issue(noteable, noteable_ref, author) }
it_behaves_like 'a system note' do
let(:action) { 'unrelate' }
end
context 'when issue relation is removed' do
it 'sets the note text' do
expect(subject.note).to eq "removed the relation with #{noteable_ref.to_reference(project)}"
end
end
end
describe '.approve_mr' do
let(:noteable) { create(:merge_request, source_project: project) }
subject { described_class.approve_mr(noteable, author) }
......
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