Commit 897d0b13 authored by Sean McGivern's avatar Sean McGivern

Merge branch '4261-moving-issue-note' into 'master'

Add system notes when moving issues between epics

Closes #4261

See merge request gitlab-org/gitlab-ee!3972
parents 6f3f8bf5 a9ebacb2
......@@ -17,6 +17,7 @@ class SystemNoteMetadata < ActiveRecord::Base
outdated
approved unapproved relate unrelate
epic_issue_added issue_added_to_epic epic_issue_removed issue_removed_from_epic
epic_issue_moved issue_changed_epic
].freeze
validates :note, presence: true
......
......@@ -576,6 +576,20 @@ module SystemNoteService
create_note(NoteSummary.new(epic, nil, user, body, action: action))
end
def epic_issue_moved(from_epic, issue, to_epic, user)
epic_issue_moved_act(from_epic, issue, to_epic, user, verb: 'added', direction: 'from')
epic_issue_moved_act(to_epic, issue, from_epic, user, verb: 'moved', direction: 'to')
end
def epic_issue_moved_act(subject_epic, issue, object_epic, user, verb:, direction:)
action = 'epic_issue_moved'
body = "#{verb} issue #{issue.to_reference(subject_epic.group)} #{direction}" \
" epic #{subject_epic.to_reference(object_epic.group)}"
create_note(NoteSummary.new(object_epic, nil, user, body, action: action))
end
def issue_on_epic(issue, epic, user, type)
return unless validate_epic_issue_action_type(type)
......@@ -592,6 +606,13 @@ module SystemNoteService
create_note(NoteSummary.new(issue, issue.project, user, body, action: action))
end
def issue_epic_change(issue, epic, user)
body = "changed epic to #{epic.to_reference(issue.project)}"
action = 'issue_changed_epic'
create_note(NoteSummary.new(issue, issue.project, user, body, action: action))
end
def validate_epic_issue_action_type(type)
[:added, :removed].include?(type)
end
......
---
title: Add system notes when moving issues between epics
merge_request:
author:
type: added
......@@ -4,16 +4,30 @@ module EpicIssues
def relate_issues(referenced_issue)
link = EpicIssue.find_or_initialize_by(issue: referenced_issue)
params = if link.persisted?
{ issue_moved: true, original_epic: link.epic }
else
{}
end
link.epic = issuable
link.move_to_start
link.save!
link
yield params
end
def create_notes(referenced_issue)
SystemNoteService.epic_issue(issuable, referenced_issue, current_user, :added)
SystemNoteService.issue_on_epic(referenced_issue, issuable, current_user, :added)
def create_notes(referenced_issue, params)
if params[:issue_moved]
SystemNoteService.epic_issue_moved(
params[:original_epic], referenced_issue, issuable, current_user
)
SystemNoteService.issue_epic_change(referenced_issue, issuable, current_user)
else
SystemNoteService.epic_issue(issuable, referenced_issue, current_user, :added)
SystemNoteService.issue_on_epic(referenced_issue, issuable, current_user, :added)
end
end
def extractor_context
......
......@@ -19,11 +19,9 @@ module IssuableLinks
def create_issue_links
referenced_issues.each do |referenced_issue|
link = relate_issues(referenced_issue)
next unless link.persisted?
create_notes(referenced_issue)
relate_issues(referenced_issue) do |params|
create_notes(referenced_issue, params)
end
end
end
......
module IssueLinks
class CreateService < IssuableLinks::CreateService
def relate_issues(referenced_issue)
IssueLink.create(source: issuable, target: referenced_issue)
link = IssueLink.create(source: issuable, target: referenced_issue)
yield if link.persisted?
end
def linkable_issues(issues)
issues.select { |issue| can?(current_user, :admin_issue_link, issue) }
end
def create_notes(referenced_issue)
def create_notes(referenced_issue, params)
SystemNoteService.relate_issue(issuable, referenced_issue, current_user)
SystemNoteService.relate_issue(referenced_issue, issuable, current_user)
end
......
......@@ -223,6 +223,37 @@ describe EpicIssues::CreateService do
it 'returns success status' do
is_expected.to eq(status: :success)
end
it 'creates 3 system notes' do
expect { subject }.to change { Note.count }.from(0).to(3)
end
it 'creates a note correctly for the original epic' do
subject
note = Note.find_by(system: true, noteable_type: 'Epic', noteable_id: epic.id)
expect(note.note).to eq("moved issue #{issue.to_reference(epic.group)} to epic #{another_epic.to_reference(epic.group)}")
expect(note.system_note_metadata.action).to eq('epic_issue_moved')
end
it 'creates a note correctly for the new epic' do
subject
note = Note.find_by(system: true, noteable_type: 'Epic', noteable_id: another_epic.id)
expect(note.note).to eq("added issue #{issue.to_reference(epic.group)} from epic #{epic.to_reference(epic.group)}")
expect(note.system_note_metadata.action).to eq('epic_issue_moved')
end
it 'creates a note correctly for the issue' do
subject
note = Note.find_by(system: true, noteable_type: 'Issue', noteable_id: issue.id)
expect(note.note).to eq("changed epic to #{another_epic.to_reference(issue.project)}")
expect(note.system_note_metadata.action).to eq('issue_changed_epic')
end
end
context 'when issue from non group project is given' do
......
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