Commit bf7ba36b authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Create notes for both issues when related

parent d43f2fdc
......@@ -2,8 +2,7 @@ class SystemNoteMetadata < ActiveRecord::Base
ICON_TYPES = %w[
commit description merge confidential visible label assignee cross_reference
title time_tracking branch milestone discussion task moved opened closed merged
outdated
approved unapproved
outdated approved unapproved relate
].freeze
validates :note, presence: true
......
......@@ -23,11 +23,21 @@ module RelatedIssues
def create_related_issues!
RelatedIssue.transaction do
referenced_issues.each do |referenced_issue|
RelatedIssue.create!(issue: @issue, related_issue: referenced_issue)
relate_issues!(referenced_issue)
create_notes!(referenced_issue)
end
end
end
def relate_issues!(referenced_issue)
RelatedIssue.create!(issue: @issue, related_issue: referenced_issue)
end
def create_notes!(referenced_issue)
SystemNoteService.relate_issue(@issue, referenced_issue, current_user)
SystemNoteService.relate_issue(referenced_issue, @issue, current_user)
end
def referenced_issues
@referenced_issues ||= begin
issue_references = params[:issue_references]
......
......@@ -552,6 +552,21 @@ module SystemNoteService
create_note(NoteSummary.new(noteable, project, author, body, action: 'moved'))
end
#
# noteable - Noteable object
# user - User performing approve
#
# Example Note text:
#
# "marked this issue as related to gitlab-ce#9001"
#
# Returns the created Note object
def relate_issue(noteable, noteable_ref, user)
body = "marked this issue as related to #{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
......
......@@ -72,6 +72,22 @@ describe RelatedIssues::CreateService, service: true do
it 'returns success message with Issue references' do
is_expected.to eq(message: "#{issue_a_ref} and #{another_project_issue_ref} were successfully related", status: :success)
end
it 'creates notes' do
# First two-way relation notes
expect(SystemNoteService).to receive(:relate_issue)
.with(issue, issue_a, user)
expect(SystemNoteService).to receive(:relate_issue)
.with(issue_a, issue, user)
# Second two-way relation notes
expect(SystemNoteService).to receive(:relate_issue)
.with(issue, another_project_issue, user)
expect(SystemNoteService).to receive(:relate_issue)
.with(another_project_issue, issue, user)
subject
end
end
context 'when relation already exists' do
......
......@@ -899,6 +899,22 @@ describe SystemNoteService, services: true do
end
end
describe '.relate_issue' do
let(:noteable_ref) { create(:issue) }
subject { described_class.relate_issue(noteable, noteable_ref, author) }
it_behaves_like 'a system note' do
let(:action) { 'relate' }
end
context 'when issue marks another as related' do
it 'sets the note text' do
expect(subject.note).to eq "marked this issue as related to #{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