Commit 271f13f6 authored by Sean McGivern's avatar Sean McGivern

Merge branch '30217-refactor-email-notification-code' into 'master'

Replace the Notify#recipient method

Closes #30217

See merge request gitlab-org/gitlab!17741
parents 504e6929 f7a2f35c
...@@ -85,7 +85,7 @@ module Emails ...@@ -85,7 +85,7 @@ module Emails
@project = Project.find(project_id) @project = Project.find(project_id)
@results = results @results = results
mail(to: recipient(@user.id, @project.group), subject: subject('Imported issues')) do |format| mail(to: @user.notification_email_for(@project.group), subject: subject('Imported issues')) do |format|
format.html { render layout: 'mailer' } format.html { render layout: 'mailer' }
format.text { render layout: 'mailer' } format.text { render layout: 'mailer' }
end end
...@@ -105,7 +105,7 @@ module Emails ...@@ -105,7 +105,7 @@ module Emails
def issue_thread_options(sender_id, recipient_id, reason) def issue_thread_options(sender_id, recipient_id, reason)
{ {
from: sender(sender_id), from: sender(sender_id),
to: recipient(recipient_id, @project.group), to: User.find(recipient_id).notification_email_for(@project.group),
subject: subject("#{@issue.title} (##{@issue.iid})"), subject: subject("#{@issue.title} (##{@issue.iid})"),
'X-GitLab-NotificationReason' => reason 'X-GitLab-NotificationReason' => reason
} }
......
...@@ -13,7 +13,9 @@ module Emails ...@@ -13,7 +13,9 @@ module Emails
@member_source_type = member_source_type @member_source_type = member_source_type
@member_id = member_id @member_id = member_id
mail(to: recipient(recipient_id, notification_group), user = User.find(recipient_id)
mail(to: user.notification_email_for(notification_group),
subject: subject("Request to join the #{member_source.human_name} #{member_source.model_name.singular}")) subject: subject("Request to join the #{member_source.human_name} #{member_source.model_name.singular}"))
end end
...@@ -21,7 +23,7 @@ module Emails ...@@ -21,7 +23,7 @@ module Emails
@member_source_type = member_source_type @member_source_type = member_source_type
@member_id = member_id @member_id = member_id
mail(to: recipient(member.user, notification_group), mail(to: member.user.notification_email_for(notification_group),
subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was granted")) subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was granted"))
end end
...@@ -29,7 +31,9 @@ module Emails ...@@ -29,7 +31,9 @@ module Emails
@member_source_type = member_source_type @member_source_type = member_source_type
@member_source = member_source_class.find(source_id) @member_source = member_source_class.find(source_id)
mail(to: recipient(user_id, notification_group), user = User.find(user_id)
mail(to: user.notification_email_for(notification_group),
subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was denied")) subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was denied"))
end end
...@@ -47,7 +51,7 @@ module Emails ...@@ -47,7 +51,7 @@ module Emails
@member_id = member_id @member_id = member_id
return unless member.created_by return unless member.created_by
mail(to: recipient(member.created_by, notification_group), mail(to: member.created_by.notification_email_for(notification_group),
subject: subject('Invitation accepted')) subject: subject('Invitation accepted'))
end end
...@@ -58,7 +62,9 @@ module Emails ...@@ -58,7 +62,9 @@ module Emails
@member_source = member_source_class.find(source_id) @member_source = member_source_class.find(source_id)
@invite_email = invite_email @invite_email = invite_email
mail(to: recipient(created_by_id, notification_group), user = User.find(created_by_id)
mail(to: user.notification_email_for(notification_group),
subject: subject('Invitation declined')) subject: subject('Invitation declined'))
end end
......
...@@ -110,7 +110,7 @@ module Emails ...@@ -110,7 +110,7 @@ module Emails
def merge_request_thread_options(sender_id, recipient_id, reason = nil) def merge_request_thread_options(sender_id, recipient_id, reason = nil)
{ {
from: sender(sender_id), from: sender(sender_id),
to: recipient(recipient_id, @project.group), to: User.find(recipient_id).notification_email_for(@project.group),
subject: subject("#{@merge_request.title} (#{@merge_request.to_reference})"), subject: subject("#{@merge_request.title} (#{@merge_request.to_reference})"),
'X-GitLab-NotificationReason' => reason 'X-GitLab-NotificationReason' => reason
} }
......
...@@ -55,7 +55,7 @@ module Emails ...@@ -55,7 +55,7 @@ module Emails
def note_thread_options(recipient_id, reason) def note_thread_options(recipient_id, reason)
{ {
from: sender(@note.author_id), from: sender(@note.author_id),
to: recipient(recipient_id, @project&.group || @group), to: User.find(recipient_id).notification_email_for(@project&.group || @group),
subject: subject("#{@note.noteable.title} (#{@note.noteable.reference_link_text})"), subject: subject("#{@note.noteable.title} (#{@note.noteable.reference_link_text})"),
'X-GitLab-NotificationReason' => reason 'X-GitLab-NotificationReason' => reason
} }
......
...@@ -7,7 +7,7 @@ module Emails ...@@ -7,7 +7,7 @@ module Emails
@project = domain.project @project = domain.project
mail( mail(
to: recipient(recipient.id, @project.group), to: recipient.notification_email_for(@project.group),
subject: subject("GitLab Pages domain '#{domain.domain}' has been enabled") subject: subject("GitLab Pages domain '#{domain.domain}' has been enabled")
) )
end end
...@@ -17,7 +17,7 @@ module Emails ...@@ -17,7 +17,7 @@ module Emails
@project = domain.project @project = domain.project
mail( mail(
to: recipient(recipient.id, @project.group), to: recipient.notification_email_for(@project.group),
subject: subject("GitLab Pages domain '#{domain.domain}' has been disabled") subject: subject("GitLab Pages domain '#{domain.domain}' has been disabled")
) )
end end
...@@ -27,7 +27,7 @@ module Emails ...@@ -27,7 +27,7 @@ module Emails
@project = domain.project @project = domain.project
mail( mail(
to: recipient(recipient.id, @project.group), to: recipient.notification_email_for(@project.group),
subject: subject("Verification succeeded for GitLab Pages domain '#{domain.domain}'") subject: subject("Verification succeeded for GitLab Pages domain '#{domain.domain}'")
) )
end end
...@@ -37,7 +37,7 @@ module Emails ...@@ -37,7 +37,7 @@ module Emails
@project = domain.project @project = domain.project
mail( mail(
to: recipient(recipient.id, @project.group), to: recipient.notification_email_for(@project.group),
subject: subject("ACTION REQUIRED: Verification failed for GitLab Pages domain '#{domain.domain}'") subject: subject("ACTION REQUIRED: Verification failed for GitLab Pages domain '#{domain.domain}'")
) )
end end
......
...@@ -7,20 +7,20 @@ module Emails ...@@ -7,20 +7,20 @@ module Emails
@project = Project.find project_id @project = Project.find project_id
@target_url = project_url(@project) @target_url = project_url(@project)
@old_path_with_namespace = old_path_with_namespace @old_path_with_namespace = old_path_with_namespace
mail(to: recipient(user_id, @project.group), mail(to: @user.notification_email_for(@project.group),
subject: subject("Project was moved")) subject: subject("Project was moved"))
end end
def project_was_exported_email(current_user, project) def project_was_exported_email(current_user, project)
@project = project @project = project
mail(to: recipient(current_user.id, project.group), mail(to: current_user.notification_email_for(project.group),
subject: subject("Project was exported")) subject: subject("Project was exported"))
end end
def project_was_not_exported_email(current_user, project, errors) def project_was_not_exported_email(current_user, project, errors)
@project = project @project = project
@errors = errors @errors = errors
mail(to: recipient(current_user.id, @project.group), mail(to: current_user.notification_email_for(@project.group),
subject: subject("Project export error")) subject: subject("Project export error"))
end end
...@@ -28,7 +28,7 @@ module Emails ...@@ -28,7 +28,7 @@ module Emails
@project = project @project = project
@user = user @user = user
mail(to: recipient(user.id, project.group), subject: subject("Project cleanup has completed")) mail(to: user.notification_email_for(project.group), subject: subject("Project cleanup has completed"))
end end
def repository_cleanup_failure_email(project, user, error) def repository_cleanup_failure_email(project, user, error)
...@@ -36,7 +36,7 @@ module Emails ...@@ -36,7 +36,7 @@ module Emails
@user = user @user = user
@error = error @error = error
mail(to: recipient(user.id, project.group), subject: subject("Project cleanup failure")) mail(to: user.notification_email_for(project.group), subject: subject("Project cleanup failure"))
end end
def repository_push_email(project_id, opts = {}) def repository_push_email(project_id, opts = {})
......
...@@ -5,8 +5,9 @@ module Emails ...@@ -5,8 +5,9 @@ module Emails
def remote_mirror_update_failed_email(remote_mirror_id, recipient_id) def remote_mirror_update_failed_email(remote_mirror_id, recipient_id)
@remote_mirror = RemoteMirror.find_by_id(remote_mirror_id) @remote_mirror = RemoteMirror.find_by_id(remote_mirror_id)
@project = @remote_mirror.project @project = @remote_mirror.project
user = User.find(recipient_id)
mail(to: recipient(recipient_id, @project.group), subject: subject('Remote mirror update failed')) mail(to: user.notification_email_for(@project.group), subject: subject('Remote mirror update failed'))
end end
end end
end end
...@@ -71,20 +71,6 @@ class Notify < BaseMailer ...@@ -71,20 +71,6 @@ class Notify < BaseMailer
address.format address.format
end end
# Look up a User's notification email for a particular context.
# Can look up by their ID or can accept a User object.
#
# recipient - User object OR a User ID
# notification_group - The parent group of the notification
#
# Returns a String containing the User's email address.
def recipient(recipient, notification_group = nil)
user = recipient if recipient.is_a?(User)
user ||= User.find(recipient)
user.notification_email_for(notification_group)
end
# Formats arguments into a String suitable for use as an email subject # Formats arguments into a String suitable for use as an email subject
# #
# extra - Extra Strings to be inserted into the subject # extra - Extra Strings to be inserted into the subject
......
---
title: Refactor email notification code
merge_request: 17741
author: briankabiro
type: other
...@@ -5,21 +5,24 @@ module EE ...@@ -5,21 +5,24 @@ module EE
module Projects module Projects
def mirror_was_hard_failed_email(project_id, user_id) def mirror_was_hard_failed_email(project_id, user_id)
@project = ::Project.find(project_id) @project = ::Project.find(project_id)
user = ::User.find(user_id)
mail(to: recipient(user_id, @project.group), mail(to: user.notification_email_for(@project.group),
subject: subject('Repository mirroring paused')) subject: subject('Repository mirroring paused'))
end end
def project_mirror_user_changed_email(new_mirror_user_id, deleted_user_name, project_id) def project_mirror_user_changed_email(new_mirror_user_id, deleted_user_name, project_id)
@project = ::Project.find(project_id) @project = ::Project.find(project_id)
@deleted_user_name = deleted_user_name @deleted_user_name = deleted_user_name
user = ::User.find(new_mirror_user_id)
mail(to: recipient(new_mirror_user_id, @project.group), mail(to: user.notification_email_for(@project.group),
subject: subject('Mirror user changed')) subject: subject('Mirror user changed'))
end end
def prometheus_alert_fired_email(project_id, user_id, alert_payload) def prometheus_alert_fired_email(project_id, user_id, alert_payload)
@project = ::Project.find(project_id) @project = ::Project.find(project_id)
user = ::User.find(user_id)
@alert = ::Gitlab::Alerting::Alert @alert = ::Gitlab::Alerting::Alert
.new(project: @project, payload: alert_payload) .new(project: @project, payload: alert_payload)
...@@ -27,7 +30,7 @@ module EE ...@@ -27,7 +30,7 @@ module EE
return unless @alert.valid? return unless @alert.valid?
subject_text = "Alert: #{@alert.full_title}" subject_text = "Alert: #{@alert.full_title}"
mail(to: recipient(user_id, @project.group), subject: subject(subject_text)) mail(to: user.notification_email_for(@project.group), subject: subject(subject_text))
end end
end end
end end
......
...@@ -3,14 +3,16 @@ ...@@ -3,14 +3,16 @@
module Emails module Emails
module AdminNotification module AdminNotification
def send_admin_notification(user_id, subject, body) def send_admin_notification(user_id, subject, body)
email = recipient(user_id) user = User.find(user_id)
email = user.notification_email
@unsubscribe_url = unsubscribe_url(email: Base64.urlsafe_encode64(email)) @unsubscribe_url = unsubscribe_url(email: Base64.urlsafe_encode64(email))
@body = body @body = body
mail to: email, subject: subject mail to: email, subject: subject
end end
def send_unsubscribed_notification(user_id) def send_unsubscribed_notification(user_id)
email = recipient(user_id) user = User.find(user_id)
email = user.notification_email
mail to: email, subject: "Unsubscribed from GitLab administrator notifications" mail to: email, subject: "Unsubscribed from GitLab administrator notifications"
end end
end end
......
...@@ -10,7 +10,7 @@ module Emails ...@@ -10,7 +10,7 @@ module Emails
filename = "#{project.full_path.parameterize}_issues_#{Date.today.iso8601}.csv" filename = "#{project.full_path.parameterize}_issues_#{Date.today.iso8601}.csv"
attachments[filename] = { content: csv_data, mime_type: 'text/csv' } attachments[filename] = { content: csv_data, mime_type: 'text/csv' }
mail(to: recipient(user.id, @project.group), subject: subject("Exported issues")) do |format| mail(to: user.notification_email_for(@project.group), subject: subject("Exported issues")) do |format|
format.html { render layout: 'mailer' } format.html { render layout: 'mailer' }
format.text { render layout: 'mailer' } format.text { render layout: 'mailer' }
end end
......
...@@ -35,7 +35,7 @@ module Emails ...@@ -35,7 +35,7 @@ module Emails
def epic_thread_options(sender_id, recipient_id, reason) def epic_thread_options(sender_id, recipient_id, reason)
{ {
from: sender(sender_id), from: sender(sender_id),
to: recipient(recipient_id, @epic.group), to: User.find(recipient_id).notification_email_for(@epic.group),
subject: subject("#{@epic.title} (#{@epic.to_reference})"), subject: subject("#{@epic.title} (#{@epic.to_reference})"),
'X-GitLab-NotificationReason' => reason 'X-GitLab-NotificationReason' => reason
} }
......
...@@ -13,7 +13,7 @@ module Emails ...@@ -13,7 +13,7 @@ module Emails
def review_thread_options(recipient_id) def review_thread_options(recipient_id)
{ {
from: sender(@author.id), from: sender(@author.id),
to: recipient(recipient_id, @merge_request.target_project.group), to: User.find(recipient_id).notification_email_for(@merge_request.target_project.group),
subject: subject("#{@merge_request.title} (#{@merge_request.to_reference})") subject: subject("#{@merge_request.title} (#{@merge_request.to_reference})")
} }
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