Commit f49a2ac0 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add close issue/mr methods to Notify. Refactored Notificationservice

parent 9e616459
...@@ -13,6 +13,14 @@ module Emails ...@@ -13,6 +13,14 @@ module Emails
mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title)) mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title))
end end
def close_issue_email(recipient_id, issue_id, updated_by_user_id)
@issue = Issue.find issue_id
@project = @issue.project
@updated_by = User.find updated_by_user_id
mail(to: recipient(recipient_id),
subject: subject("Closed issue ##{@issue.id}", @issue.title))
end
def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id) def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id)
@issue = Issue.find issue_id @issue = Issue.find issue_id
@issue_status = status @issue_status = status
......
...@@ -12,5 +12,18 @@ module Emails ...@@ -12,5 +12,18 @@ module Emails
@project = @merge_request.project @project = @merge_request.project
mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title)) mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title))
end end
def closed_merge_request_email(recipient_id, merge_request_id, updated_by_user_id)
@merge_request = MergeRequest.find(merge_request_id)
@project = @merge_request.project
@updated_by = User.find updated_by_user_id
mail(to: recipient(recipient_id), subject: subject("Closed merge request !#{@merge_request.id}", @merge_request.title))
end
def merged_merge_request_email(recipient_id, merge_request_id)
@merge_request = MergeRequest.find(merge_request_id)
@project = @merge_request.project
mail(to: recipient(recipient_id), subject: subject("Accepted merge request !#{@merge_request.id}", @merge_request.title))
end
end end
end end
...@@ -12,7 +12,7 @@ class MergeRequestObserver < BaseObserver ...@@ -12,7 +12,7 @@ class MergeRequestObserver < BaseObserver
end end
def after_merge(merge_request, transition) def after_merge(merge_request, transition)
notification.merge_mr(merge_request, current_user) notification.merge_mr(merge_request)
end end
def after_reopen(merge_request, transition) def after_reopen(merge_request, transition)
......
...@@ -23,12 +23,7 @@ class NotificationService ...@@ -23,12 +23,7 @@ class NotificationService
# * project team members with notification level higher then Participating # * project team members with notification level higher then Participating
# #
def new_issue(issue, current_user) def new_issue(issue, current_user)
recipients = reject_muted_users([issue.assignee]) new_resource_email(issue, 'new_issue_email')
recipients = recipients.concat(project_watchers(issue.project)).uniq
recipients.each do |recipient|
Notify.delay.new_issue_email(recipient.id, issue.id)
end
end end
# When we close an issue we should send next emails: # When we close an issue we should send next emails:
...@@ -38,17 +33,7 @@ class NotificationService ...@@ -38,17 +33,7 @@ class NotificationService
# * project team members with notification level higher then Participating # * project team members with notification level higher then Participating
# #
def close_issue(issue, current_user) def close_issue(issue, current_user)
recipients = reject_muted_users([issue.author, issue.assignee]) close_resource_email(issue, current_user, 'close_issue_email')
# Add watchers to email list
recipients = recipients.concat(project_watchers(issue.project)).uniq
# Dont send email to me when I close an issue
recipients.delete(current_user)
recipients.each do |recipient|
Notify.delay.issue_status_changed_email(recipient.id, issue.id, issue.state, current_user.id)
end
end end
# When we reassign an issue we should send next emails: # When we reassign an issue we should send next emails:
...@@ -57,7 +42,7 @@ class NotificationService ...@@ -57,7 +42,7 @@ class NotificationService
# * issue new assignee if his notification level is not Disabled # * issue new assignee if his notification level is not Disabled
# #
def reassigned_issue(issue, current_user) def reassigned_issue(issue, current_user)
reassign_email(issue, current_user, 'reassigned_issue_email') reassign_resource_email(issue, current_user, 'reassigned_issue_email')
end end
...@@ -66,12 +51,7 @@ class NotificationService ...@@ -66,12 +51,7 @@ class NotificationService
# * mr assignee if his notification level is not Disabled # * mr assignee if his notification level is not Disabled
# #
def new_merge_request(merge_request, current_user) def new_merge_request(merge_request, current_user)
recipients = reject_muted_users([merge_request.assignee]) new_resource_email(merge_request, 'new_merge_request_email')
recipients = recipients.concat(project_watchers(merge_request.project)).uniq
recipients.each do |recipient|
Notify.delay.new_merge_request_email(recipient.id, merge_request.id)
end
end end
# When we reassign a merge_request we should send next emails: # When we reassign a merge_request we should send next emails:
...@@ -80,7 +60,7 @@ class NotificationService ...@@ -80,7 +60,7 @@ class NotificationService
# * merge_request assignee if his notification level is not Disabled # * merge_request assignee if his notification level is not Disabled
# #
def reassigned_merge_request(merge_request, current_user) def reassigned_merge_request(merge_request, current_user)
reassign_email(merge_request, current_user, 'reassigned_merge_request_email') reassign_resource_email(merge_request, current_user, 'reassigned_merge_request_email')
end end
# When we close a merge request we should send next emails: # When we close a merge request we should send next emails:
...@@ -89,13 +69,8 @@ class NotificationService ...@@ -89,13 +69,8 @@ class NotificationService
# * merge_request assignee if his notification level is not Disabled # * merge_request assignee if his notification level is not Disabled
# * project team members with notification level higher then Participating # * project team members with notification level higher then Participating
# #
def close_mr(merge_request) def close_mr(merge_request, current_user)
recipients = reject_muted_users([merge_request.author, merge_request.assignee]) close_resource_email(merge_request, current_user, 'closed_merge_request_email')
recipients = recipients.concat(project_watchers(merge_request.project)).uniq
recipients.each do |recipient|
Notify.delay.closed_merge_request_email(recipient.id, merge_request.id)
end
end end
# When we merge a merge request we should send next emails: # When we merge a merge request we should send next emails:
...@@ -166,7 +141,27 @@ class NotificationService ...@@ -166,7 +141,27 @@ class NotificationService
end end
end end
def reassign_email(target, current_user, method) def new_resource_email(target, method)
recipients = reject_muted_users([target.assignee])
recipients = recipients.concat(project_watchers(target.project)).uniq
recipients.delete(target.author)
recipients.each do |recipient|
Notify.delay.send(method, recipient.id, target.id)
end
end
def close_resource_email(target, current_user, method)
recipients = reject_muted_users([target.author, target.assignee])
recipients = recipients.concat(project_watchers(target.project)).uniq
recipients.delete(current_user)
recipients.each do |recipient|
Notify.delay.send(method, recipient.id, target.id, current_user.id)
end
end
def reassign_resource_email(target, current_user, method)
recipients = User.where(id: [target.assignee_id, target.assignee_id_was]) recipients = User.where(id: [target.assignee_id, target.assignee_id_was])
# Add watchers to email list # Add watchers to email list
......
%p
= "Issue was closed by #{@updated_by.name}"
%p
= "Issue ##{@issue.id}"
= link_to_gfm truncate(@issue.title, length: 45), project_issue_url(@issue.project, @issue), title: @issue.title
%p %p
= "Merge Request #{@merge_request.id} was closed" = "Merge Request #{@merge_request.id} was closed by #{@updated_by.name}"
%p %p
= link_to_gfm truncate(@merge_request.title, length: 40), project_merge_request_url(@merge_request.project, @merge_request) = link_to_gfm truncate(@merge_request.title, length: 40), project_merge_request_url(@merge_request.project, @merge_request)
%p %p
......
...@@ -65,11 +65,11 @@ describe NotificationService do ...@@ -65,11 +65,11 @@ describe NotificationService do
end end
def should_email(user_id) def should_email(user_id)
Notify.should_receive(:issue_status_changed_email).with(user_id, issue.id, issue.assignee_id) Notify.should_receive(:closed_issue_email).with(user_id, issue.id, issue.assignee_id)
end end
def should_not_email(user_id) def should_not_email(user_id)
Notify.should_not_receive(:issue_status_changed_email).with(user_id, issue.id, issue.assignee_id) Notify.should_not_receive(:closed_issue_email).with(user_id, issue.id, issue.assignee_id)
end end
end end
end end
...@@ -123,7 +123,7 @@ describe NotificationService do ...@@ -123,7 +123,7 @@ describe NotificationService do
should_email(@u_watcher.id) should_email(@u_watcher.id)
should_not_email(@u_participating.id) should_not_email(@u_participating.id)
should_not_email(@u_disabled.id) should_not_email(@u_disabled.id)
notification.close_mr(merge_request) notification.close_mr(merge_request, @u_disabled)
end end
def should_email(user_id) def should_email(user_id)
......
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