From 15d32b6a11380c257180dbc8e2691bc12e2792bc Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 15 Mar 2016 11:35:40 +0100 Subject: [PATCH] Add new notifications for issue move action [ci skip] --- app/mailers/emails/issues.rb | 8 ++++++ app/services/issues/move_service.rb | 9 +++++-- app/services/notification_service.rb | 10 ++++++++ app/views/notify/issue_moved_email.html.haml | 6 +++++ app/views/notify/issue_moved_email.text.erb | 4 +++ spec/mailers/notify_spec.rb | 27 ++++++++++++++++++++ spec/services/issues/move_service_spec.rb | 2 +- 7 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 app/views/notify/issue_moved_email.html.haml create mode 100644 app/views/notify/issue_moved_email.text.erb diff --git a/app/mailers/emails/issues.rb b/app/mailers/emails/issues.rb index 5f9adb32e00..6f54c42146c 100644 --- a/app/mailers/emails/issues.rb +++ b/app/mailers/emails/issues.rb @@ -36,6 +36,14 @@ module Emails mail_answer_thread(@issue, issue_thread_options(updated_by_user_id, recipient_id)) end + def issue_moved_email(recipient, issue, new_issue, updated_by_user) + setup_issue_mail(issue.id, recipient.id) + + @new_issue = new_issue + @new_project = new_issue.project + mail_answer_thread(issue, issue_thread_options(updated_by_user.id, recipient.id)) + end + private def setup_issue_mail(issue_id, recipient_id) diff --git a/app/services/issues/move_service.rb b/app/services/issues/move_service.rb index e2089e22bcb..4abcd203407 100644 --- a/app/services/issues/move_service.rb +++ b/app/services/issues/move_service.rb @@ -10,13 +10,17 @@ module Issues if new_project_id @project_new = Project.find(new_project_id) end + + if @project_new == @project_old + raise StandardError, 'Cannot move issue to project it originates from!' + end end def execute return unless move? - # Using trasaction because of a high footprint on - # rewriting notes (unfolding references) + # Using trasaction because of a high resources footprint + # on rewriting notes (unfolding references) # ActiveRecord::Base.transaction do # New issue tasks @@ -99,6 +103,7 @@ module Issues end def notify_participants + notification_service.issue_moved(@issue_old, @issue_new, @current_user) end end end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 19a6779dea9..3bdf00a8291 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -236,6 +236,16 @@ class NotificationService end end + def issue_moved(issue, new_issue, current_user) + recipients = build_recipients(issue, issue.project, current_user) + + recipients.map do |recipient| + email = mailer.issue_moved_email(recipient, issue, new_issue, current_user) + email.deliver_later + email + end + end + protected # Get project users with WATCH notification level diff --git a/app/views/notify/issue_moved_email.html.haml b/app/views/notify/issue_moved_email.html.haml new file mode 100644 index 00000000000..40f7d61fe19 --- /dev/null +++ b/app/views/notify/issue_moved_email.html.haml @@ -0,0 +1,6 @@ +%p + Issue was moved to another project. +%p + New issue: + = link_to namespace_project_issue_url(@new_project.namespace, @new_project, @new_issue) do + = @new_issue.title diff --git a/app/views/notify/issue_moved_email.text.erb b/app/views/notify/issue_moved_email.text.erb new file mode 100644 index 00000000000..b3bd43c2055 --- /dev/null +++ b/app/views/notify/issue_moved_email.text.erb @@ -0,0 +1,4 @@ +Issue was moved to another project. + +New issue location: +<%= namespace_project_issue_url(@new_project.namespace, @new_project, @new_issue) %> diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index f910424d85b..9b47acfe0cd 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -158,6 +158,33 @@ describe Notify do is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/ end end + + describe 'moved to another project' do + let(:new_issue) { create(:issue) } + subject { Notify.issue_moved_email(recipient, issue, new_issue, current_user) } + + it_behaves_like 'an answer to an existing thread', 'issue' + it_behaves_like 'it should show Gmail Actions View Issue link' + it_behaves_like 'an unsubscribeable thread' + + it 'contains description about action taken' do + is_expected.to have_body_text 'Issue was moved to another project' + end + + it 'has the correct subject' do + is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i + end + + it 'contains link to new issue' do + new_issue_url = namespace_project_issue_path(new_issue.project.namespace, + new_issue.project, new_issue) + is_expected.to have_body_text new_issue_url + end + + it 'contains a link to the original issue' do + is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/ + end + end end context 'for merge requests' do diff --git a/spec/services/issues/move_service_spec.rb b/spec/services/issues/move_service_spec.rb index 69f4748ae67..b71ce311afd 100644 --- a/spec/services/issues/move_service_spec.rb +++ b/spec/services/issues/move_service_spec.rb @@ -55,7 +55,7 @@ describe Issues::MoveService, services: true do end it 'rewrites issue description' do - expect(new_issue.description).to include description + expect(new_issue.description).to eq description end it 'adds system note to old issue at the end' do