From 798873ca75e656ed0fbd2a3080022eb55a6f3106 Mon Sep 17 00:00:00 2001
From: adamliesko <adamliesko@gmail.com>
Date: Mon, 9 Nov 2015 17:26:01 +0100
Subject: [PATCH] Add notification to the former assignee upon unassignment

---
 CHANGELOG                                   |  1 +
 app/services/notification_service.rb        |  6 ++++--
 spec/services/issues/update_service_spec.rb | 10 ++++++----
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 217dc4e0043..c8db4da1d74 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -26,6 +26,7 @@ v 8.2.0 (unreleased)
   - Add "added", "modified" and "removed" properties to commit object in webhook
   - Rename "Back to" links to "Go to" because its not always a case it point to place user come from
   - Allow groups to appear in the search results if the group owner allows it
+  - Add email notification to former assignee upon unassignment
 
 v 8.1.3
   - Spread out runner contacted_at updates
diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb
index a6b22348650..16c84f4a055 100644
--- a/app/services/notification_service.rb
+++ b/app/services/notification_service.rb
@@ -362,7 +362,8 @@ class NotificationService
 
   def reassign_resource_email(target, project, current_user, method)
     assignee_id_was = previous_record(target, "assignee_id")
-    recipients = build_recipients(target, project, current_user)
+    previous_assignee = User.find(assignee_id_was)
+    recipients = build_recipients(target, project, current_user, [previous_assignee])
 
     recipients.each do |recipient|
       mailer.send(method, recipient.id, target.id, assignee_id_was, current_user.id)
@@ -377,8 +378,9 @@ class NotificationService
     end
   end
 
-  def build_recipients(target, project, current_user)
+  def build_recipients(target, project, current_user, previous_records = nil )
     recipients = target.participants(current_user)
+    recipients.concat(previous_records).compact.uniq if previous_records
 
     recipients = add_project_watchers(recipients, project)
     recipients = reject_mention_users(recipients, project)
diff --git a/spec/services/issues/update_service_spec.rb b/spec/services/issues/update_service_spec.rb
index a91be3b4472..4e79484f26a 100644
--- a/spec/services/issues/update_service_spec.rb
+++ b/spec/services/issues/update_service_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
 describe Issues::UpdateService do
   let(:user) { create(:user) }
   let(:user2) { create(:user) }
-  let(:issue) { create(:issue, title: 'Old title') }
+  let(:issue) { create(:issue, title: 'Old title', assignee_id: user.id) }
   let(:label) { create(:label) }
   let(:project) { issue.project }
 
@@ -34,9 +34,11 @@ describe Issues::UpdateService do
       it { expect(@issue.labels.count).to eq(1) }
       it { expect(@issue.labels.first.title).to eq('Bug') }
 
-      it 'should send email to user2 about assign of new issue' do
-        email = ActionMailer::Base.deliveries.last
-        expect(email.to.first).to eq(user2.email)
+      it 'should send email to user2 about assign of new issue and email to user about issue unassignment' do
+        deliveries =  ActionMailer::Base.deliveries
+        email = deliveries.last
+        recipients = deliveries.map(&:to).uniq.flatten
+        expect(recipients.last(2)).to include(user.email,user2.email)
         expect(email.subject).to include(issue.title)
       end
 
-- 
GitLab