Commit 5b1ab398 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '7272-epics-notifications' into 'master'

Send notifications for epic status change

Closes #7272

See merge request gitlab-org/gitlab-ee!8247
parents 82527d84 7abe61ab
......@@ -11,6 +11,18 @@ module Emails
mail_new_thread(@epic, epic_thread_options(@epic.author_id, recipient_id, reason))
end
def epic_status_changed_email(recipient_id, epic_id, status, updated_by_user_id, reason = nil)
@epic = Epic.find_by_id(epic_id)
return unless @epic
setup_epic_mail(recipient_id)
@status = status
@updated_by = User.find(updated_by_user_id)
mail_answer_thread(@epic, epic_thread_options(updated_by_user_id, recipient_id, reason))
end
private
def setup_epic_mail(recipient_id)
......
......@@ -42,6 +42,14 @@ module EE
new_resource_email(epic, :new_epic_email)
end
def close_epic(epic, current_user)
epic_status_change_email(epic, current_user, 'closed')
end
def reopen_epic(epic, current_user)
epic_status_change_email(epic, current_user, 'reopened')
end
def project_mirror_user_changed(new_mirror_user, deleted_user_name, project)
mailer.project_mirror_user_changed_email(new_mirror_user.id, deleted_user_name, project.id).deliver_later
end
......@@ -98,5 +106,21 @@ module EE
mailer.service_desk_new_note_email(issue.id, note.id).deliver_later
end
def epic_status_change_email(target, current_user, status)
action = status == 'reopened' ? 'reopen' : 'close'
recipients = NotificationRecipientService.build_recipients(
target,
current_user,
action: action
)
recipients.each do |recipient|
mailer.epic_status_changed_email(
recipient.user.id, target.id, status, current_user.id, recipient.reason)
.deliver_later
end
end
end
end
......@@ -14,6 +14,7 @@ module Epics
if epic.close
epic.update(closed_by: current_user)
SystemNoteService.change_status(epic, nil, current_user, epic.state)
notification_service.close_epic(epic, current_user)
end
end
end
......
......@@ -13,6 +13,7 @@ module Epics
def reopen_epic(epic)
if epic.reopen
SystemNoteService.change_status(epic, nil, current_user, epic.state)
notification_service.reopen_epic(epic, current_user)
end
end
end
......
%p
Epic was #{@status} by #{@updated_by.name}
%p
Epic
= link_to @epic.to_reference, group_epic_url(@epic.group, @epic)
Epic was #{@status} by #{@updated_by.name}
Epic #{@epic.to_reference}: #{group_epic_url(@epic.group, @epic)}
---
title: Send notifications for epic status change
merge_request: 8247
author:
type: added
......@@ -255,25 +255,25 @@ describe EE::NotificationService, :mailer do
end
end
describe 'Notes' do
describe 'epics' do
set(:group) { create(:group, :private) }
set(:epic) { create(:epic, group: group) }
around do |example|
perform_enqueued_jobs do
example.run
end
end
before do
stub_licensed_features(epics: true)
group.add_developer(epic.author)
end
context 'epic notes' do
set(:group) { create(:group, :private) }
set(:epic) { create(:epic, group: group) }
set(:note) { create(:note, project: nil, noteable: epic, note: '@mention referenced, @unsubscribed_mentioned and @outsider also') }
before(:all) do
create(:group_member, group: group, user: epic.author)
create(:group_member, group: group, user: note.author)
end
before do
stub_licensed_features(epics: true)
build_group_members(group)
@u_custom_off = create_user_with_notification(:custom, 'custom_off', group)
......@@ -289,11 +289,11 @@ describe EE::NotificationService, :mailer do
update_custom_notification(:new_note, @u_guest_custom, resource: group)
update_custom_notification(:new_note, @u_custom_global)
add_users_with_subscription(group, epic)
end
describe '#new_note' do
it do
add_users_with_subscription(group, epic)
reset_delivered_emails!
expect(SentNotification).to receive(:record).with(epic, any_args).exactly(9).times
......@@ -320,6 +320,59 @@ describe EE::NotificationService, :mailer do
end
end
end
shared_examples 'epic notifications' do
let(:watcher) { create(:user) }
let(:participating) { create(:user) }
let(:other_user) { create(:user) }
before do
create_global_setting_for(watcher, :watch)
create_global_setting_for(participating, :participating)
group.add_developer(watcher)
group.add_developer(participating)
group.add_developer(other_user)
reset_delivered_emails!
end
it 'sends notification to watcher when no user participates' do
execute
should_email(watcher)
should_not_email(participating)
should_not_email(other_user)
end
it 'sends notification to watcher and a participator' do
epic.subscriptions.create(user: participating, subscribed: true)
execute
should_email(watcher)
should_email(participating)
should_not_email(other_user)
end
end
context 'close epic' do
let(:execute) { subject.close_epic(epic, epic.author) }
include_examples 'epic notifications'
end
context 'reopen epic' do
let(:execute) { subject.reopen_epic(epic, epic.author) }
include_examples 'epic notifications'
end
context 'new epic' do
let(:execute) { subject.new_epic(epic) }
include_examples 'epic notifications'
end
end
def build_group_members(group)
......
......@@ -50,6 +50,15 @@ describe Epics::CloseService do
expect(note.note).to eq('closed')
expect(note.system_note_metadata.action).to eq('closed')
end
it 'notifies the subscribers' do
notification_service = double
expect(NotificationService).to receive(:new).and_return(notification_service)
expect(notification_service).to receive(:close_epic).with(epic, user)
subject.execute(epic)
end
end
context 'when trying to close a closed epic' do
......@@ -72,6 +81,12 @@ describe Epics::CloseService do
it 'does not create a system note' do
expect { subject.execute(epic) }.not_to change { epic.notes.count }
end
it 'does not send any emails' do
expect(NotificationService).not_to receive(:new)
subject.execute(epic)
end
end
end
......
......@@ -50,6 +50,15 @@ describe Epics::ReopenService do
expect(note.note).to eq('opened')
expect(note.system_note_metadata.action).to eq('opened')
end
it 'notifies the subscribers' do
notification_service = double
expect(NotificationService).to receive(:new).and_return(notification_service)
expect(notification_service).to receive(:reopen_epic).with(epic, user)
subject.execute(epic)
end
end
context 'when trying to reopen an opened epic' do
......@@ -72,6 +81,12 @@ describe Epics::ReopenService do
it 'does not create a system note' do
expect { subject.execute(epic) }.not_to change { epic.notes.count }
end
it 'does not send any emails' do
expect(NotificationService).not_to receive(:new)
subject.execute(epic)
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