From 42dd567e8e9ec7e71406be1efe36f6f944d5c57f Mon Sep 17 00:00:00 2001
From: Steve Mokris <smokris@softpixel.com>
Date: Sun, 3 Apr 2022 20:34:04 -0500
Subject: [PATCH] In email footers, explicitly link to the GitLab host

Fixes issue where some email clients (macOS/iOS Mail.app) would
automatically turn the GitLab hostname into a link with different
styling from other links.

Also factor out some of the links in various templates' email footers,
making them more consistent between templates, and improving the
cohesion of translatable strings.

Changelog: changed
---
 app/helpers/emails_helper.rb                  | 94 ++++++++++++++++---
 app/views/layouts/mailer.html.haml            |  4 +-
 app/views/layouts/mailer.text.erb             |  2 +-
 app/views/layouts/notify.html.haml            | 11 +--
 app/views/layouts/notify.text.erb             |  2 +-
 .../approved_merge_request_email.html.haml    |  4 +-
 ...rge_when_pipeline_succeeds_email.html.haml |  4 +-
 .../unapproved_merge_request_email.html.haml  |  4 +-
 ee/app/views/layouts/mailer/devise.text.erb   |  2 +-
 ee/spec/mailers/ee/emails/profile_spec.rb     |  2 +-
 locale/gitlab.pot                             | 23 ++++-
 spec/features/unsubscribe_links_spec.rb       |  2 +-
 spec/helpers/emails_helper_spec.rb            |  2 +-
 .../mailers/emails/admin_notification_spec.rb |  2 +-
 spec/mailers/emails/profile_spec.rb           |  8 +-
 spec/mailers/notify_spec.rb                   |  9 +-
 16 files changed, 119 insertions(+), 56 deletions(-)

diff --git a/app/helpers/emails_helper.rb b/app/helpers/emails_helper.rb
index c23d905a0089d..dca9cbd02ffb0 100644
--- a/app/helpers/emails_helper.rb
+++ b/app/helpers/emails_helper.rb
@@ -116,19 +116,16 @@ def closure_reason_text(closed_via, format: nil)
     end
   end
 
-  # "You are receiving this email because #{reason} on #{gitlab_host}."
-  def notification_reason_text(reason)
-    gitlab_host = Gitlab.config.gitlab.host
-
-    case reason
-    when NotificationReason::OWN_ACTIVITY
-      _("You're receiving this email because of your activity on %{host}.") % { host: gitlab_host }
-    when NotificationReason::ASSIGNED
-      _("You're receiving this email because you have been assigned an item on %{host}.") % { host: gitlab_host }
-    when NotificationReason::MENTIONED
-      _("You're receiving this email because you have been mentioned on %{host}.") % { host: gitlab_host }
+  # "You are receiving this email because ... on #{host}. ..."
+  def notification_reason_text(reason: nil, show_manage_notifications_link: false, show_help_link: false, manage_label_subscriptions_url: nil, unsubscribe_url: nil, format: :text)
+    if unsubscribe_url && show_manage_notifications_link && show_help_link
+      notification_reason_text_with_unsubscribe_and_manage_notifications_and_help_links(reason: reason, unsubscribe_url: unsubscribe_url, format: format)
+    elsif !reason && manage_label_subscriptions_url && show_help_link
+      notification_reason_text_with_manage_label_subscriptions_and_help_links(manage_label_subscriptions_url: manage_label_subscriptions_url, format: format)
+    elsif show_manage_notifications_link && show_help_link
+      notification_reason_text_with_manage_notifications_and_help_links(reason: reason, format: format)
     else
-      _("You're receiving this email because of your account on %{host}.") % { host: gitlab_host }
+      notification_reason_text_without_links(reason: reason, format: format)
     end
   end
 
@@ -259,9 +256,7 @@ def group_membership_expiration_changed_link(member, group, format: nil)
   end
 
   def instance_access_request_text(user, format: nil)
-    gitlab_host = Gitlab.config.gitlab.host
-
-    _('%{username} has asked for a GitLab account on your instance %{host}:') % { username: sanitize_name(user.name), host: gitlab_host }
+    _('%{username} has asked for a GitLab account on your instance %{host}:').html_safe % { username: sanitize_name(user.name), host: gitlab_host_link(format) }
   end
 
   def instance_access_request_link(user, format: nil)
@@ -317,6 +312,75 @@ def show_header?
   def email_header_and_footer_enabled?
     current_appearance&.email_header_and_footer_enabled?
   end
+
+  def gitlab_host_link(format)
+    case format
+    when :html
+      generate_link(Gitlab.config.gitlab.host, Gitlab.config.gitlab.url)
+    when :text
+      Gitlab.config.gitlab.host
+    end
+  end
+
+  def notification_reason_text_with_unsubscribe_and_manage_notifications_and_help_links(reason:, unsubscribe_url:, format:)
+    unsubscribe_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: unsubscribe_url }
+    unsubscribe_link_end = '</a>'.html_safe
+
+    manage_notifications_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="mng-notif-link">'.html_safe % { url: profile_notifications_url }
+    manage_notifications_link_end = '</a>'.html_safe
+
+    help_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="help-link">'.html_safe % { url: help_url }
+    help_link_end = '</a>'.html_safe
+
+    case reason
+    when NotificationReason::OWN_ACTIVITY
+      _("You're receiving this email because of your activity on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), unsubscribe_link_start: unsubscribe_link_start, unsubscribe_link_end: unsubscribe_link_end, manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    when NotificationReason::ASSIGNED
+      _("You're receiving this email because you have been assigned an item on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), unsubscribe_link_start: unsubscribe_link_start, unsubscribe_link_end: unsubscribe_link_end, manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    when NotificationReason::MENTIONED
+      _("You're receiving this email because you have been mentioned on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), unsubscribe_link_start: unsubscribe_link_start, unsubscribe_link_end: unsubscribe_link_end, manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    else
+      _("You're receiving this email because of your account on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), unsubscribe_link_start: unsubscribe_link_start, unsubscribe_link_end: unsubscribe_link_end, manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    end
+  end
+
+  def notification_reason_text_with_manage_label_subscriptions_and_help_links(manage_label_subscriptions_url:, format:)
+    manage_label_subscriptions_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="mng-notif-link">'.html_safe % { url: manage_label_subscriptions_url }
+    manage_label_subscriptions_link_end = '</a>'.html_safe
+
+    help_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="help-link">'.html_safe % { url: help_url }
+    help_link_end = '</a>'.html_safe
+
+    _("You're receiving this email because of your account on %{host}. %{manage_label_subscriptions_link_start}Manage label subscriptions%{manage_label_subscriptions_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), manage_label_subscriptions_link_start: manage_label_subscriptions_link_start, manage_label_subscriptions_link_end: manage_label_subscriptions_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+  end
+
+  def notification_reason_text_with_manage_notifications_and_help_links(reason:, format:)
+    manage_notifications_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="mng-notif-link">'.html_safe % { url: profile_notifications_url }
+    manage_notifications_link_end = '</a>'.html_safe
+
+    help_link_start = '<a href="%{url}" target="_blank" rel="noopener noreferrer" class="help-link">'.html_safe % { url: help_url }
+    help_link_end = '</a>'.html_safe
+
+    case reason
+    when NotificationReason::MENTIONED
+      _("You're receiving this email because you have been mentioned on %{host}. %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    else
+      _("You're receiving this email because of your account on %{host}. %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}").html_safe % { host: gitlab_host_link(format), manage_notifications_link_start: manage_notifications_link_start, manage_notifications_link_end: manage_notifications_link_end, help_link_start: help_link_start, help_link_end: help_link_end }
+    end
+  end
+
+  def notification_reason_text_without_links(reason:, format:)
+    case reason
+    when NotificationReason::OWN_ACTIVITY
+      _("You're receiving this email because of your activity on %{host}.").html_safe % { host: gitlab_host_link(format) }
+    when NotificationReason::ASSIGNED
+      _("You're receiving this email because you have been assigned an item on %{host}.").html_safe % { host: gitlab_host_link(format) }
+    when NotificationReason::MENTIONED
+      _("You're receiving this email because you have been mentioned on %{host}.").html_safe % { host: gitlab_host_link(format) }
+    else
+      _("You're receiving this email because of your account on %{host}.").html_safe % { host: gitlab_host_link(format) }
+    end
+  end
 end
 
 EmailsHelper.prepend_mod_with('EmailsHelper')
diff --git a/app/views/layouts/mailer.html.haml b/app/views/layouts/mailer.html.haml
index 580b8e67a3c0f..8452f0d99766a 100644
--- a/app/views/layouts/mailer.html.haml
+++ b/app/views/layouts/mailer.html.haml
@@ -3,8 +3,6 @@
     %td
       %img.footer-logo{ alt: "GitLab", src: image_url('mailers/gitlab_logo_black_text.png') }
       %div
-        - manage_notifications_link = link_to(_("Manage all notifications"), profile_notifications_url, class: 'mng-notif-link')
-        - help_link = link_to(_("Help"), help_url, class: 'help-link')
-        = _("You're receiving this email because of your account on %{host}. %{manage_notifications_link} &middot; %{help_link}").html_safe % { host: Gitlab.config.gitlab.host, manage_notifications_link: manage_notifications_link, help_link: help_link }
+        = notification_reason_text(show_manage_notifications_link: true, show_help_link: true, format: :html)
 
 = render 'layouts/mailer'
diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb
index 1a06ea68bcd70..24553734e494a 100644
--- a/app/views/layouts/mailer.text.erb
+++ b/app/views/layouts/mailer.text.erb
@@ -3,7 +3,7 @@
 <%= yield -%>
 
 -- <%# signature marker %>
-<%= _("You're receiving this email because of your account on %{host}.") % { host: Gitlab.config.gitlab.host } %>
+<%= notification_reason_text %>
 <%= render_if_exists 'layouts/mailer/additional_text' %>
 
 <%= text_footer_message %>
diff --git a/app/views/layouts/notify.html.haml b/app/views/layouts/notify.html.haml
index d9f16a89fbc2a..d05b6951fbf0e 100644
--- a/app/views/layouts/notify.html.haml
+++ b/app/views/layouts/notify.html.haml
@@ -26,16 +26,7 @@
           - else
             #{link_to _("View it on GitLab"), @target_url}.
           %br
-          -# Don't link the host in the line below, one link in the email is easier to quickly click than two.
-          = notification_reason_text(@reason)
-          If you'd like to receive fewer emails, you can
-          - if @labels_url
-            adjust your #{link_to 'label subscriptions', @labels_url}.
-          - else
-            - if @unsubscribe_url
-              = link_to "unsubscribe", @unsubscribe_url
-              from this thread or
-            adjust your notification settings.
+          = notification_reason_text(reason: @reason, show_manage_notifications_link: !@labels_url, show_help_link: true, manage_label_subscriptions_url: @labels_url, unsubscribe_url: @unsubscribe_url, format: :html)
 
           = email_action @target_url
 
diff --git a/app/views/layouts/notify.text.erb b/app/views/layouts/notify.text.erb
index 49ad0b5abc583..4eae96dc3767a 100644
--- a/app/views/layouts/notify.text.erb
+++ b/app/views/layouts/notify.text.erb
@@ -11,7 +11,7 @@
 <%    end -%>
 <%  end -%>
 
-<%= notification_reason_text(@reason) %>
+<%= notification_reason_text(reason: @reason) %>
 <%= render_if_exists 'layouts/mailer/additional_text' %>
 
 <%= text_footer_message -%>
diff --git a/app/views/notify/approved_merge_request_email.html.haml b/app/views/notify/approved_merge_request_email.html.haml
index c51fe02370de4..28da1182d4916 100644
--- a/app/views/notify/approved_merge_request_email.html.haml
+++ b/app/views/notify/approved_merge_request_email.html.haml
@@ -152,6 +152,4 @@
           %td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;padding:25px 0;font-size:13px;line-height:1.6;color:#5c5c5c;" }
             %img{ alt: "GitLab", src: image_url('mailers/gitlab_logo_black_text.png'), style: "display:block;margin:0 auto 1em;", width: "90" }/
             %div
-              - manage_notifications_link = link_to(_("Manage all notifications"), profile_notifications_url, style: "color:#3777b0;text-decoration:none;")
-              - help_link = link_to(_("Help"), help_url, style: "color:#3777b0;text-decoration:none;")
-              = _("You're receiving this email because of your account on %{host}. %{manage_notifications_link} &middot; %{help_link}").html_safe % { host: Gitlab.config.gitlab.host, manage_notifications_link: manage_notifications_link, help_link: help_link }
+              = notification_reason_text(show_manage_notifications_link: true, show_help_link: true, format: :html)
diff --git a/app/views/notify/merge_when_pipeline_succeeds_email.html.haml b/app/views/notify/merge_when_pipeline_succeeds_email.html.haml
index 550d386c84330..f6b517d6e34dc 100644
--- a/app/views/notify/merge_when_pipeline_succeeds_email.html.haml
+++ b/app/views/notify/merge_when_pipeline_succeeds_email.html.haml
@@ -148,6 +148,4 @@
           %td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;padding:25px 0;font-size:13px;line-height:1.6;color:#5c5c5c;" }
             %img{ alt: "GitLab", src: image_url('mailers/gitlab_logo_black_text.png'), style: "display:block;margin:0 auto 1em;", width: "90" }
             %div
-              - manage_notifications_link = link_to(_("Manage all notifications"), profile_notifications_url, style: "color:#3777b0;text-decoration:none;")
-              - help_link = link_to(_("Help"), help_url, style: "color:#3777b0;text-decoration:none;")
-              = _("You're receiving this email because of your account on %{host}. %{manage_notifications_link} &middot; %{help_link}").html_safe % { host: Gitlab.config.gitlab.host, manage_notifications_link: manage_notifications_link, help_link: help_link }
+              = notification_reason_text(show_manage_notifications_link: true, show_help_link: true, format: :html)
diff --git a/app/views/notify/unapproved_merge_request_email.html.haml b/app/views/notify/unapproved_merge_request_email.html.haml
index ae58ccd399501..0b8fbe1422836 100644
--- a/app/views/notify/unapproved_merge_request_email.html.haml
+++ b/app/views/notify/unapproved_merge_request_email.html.haml
@@ -151,6 +151,4 @@
           %td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;padding:25px 0;font-size:13px;line-height:1.6;color:#5c5c5c;" }
             %img{ alt: "GitLab", src: image_url('mailers/gitlab_logo_black_text.png'), style: "display:block;margin:0 auto 1em;", width: "90" }/
             %div
-              - manage_notifications_link = link_to(_("Manage all notifications"), profile_notifications_url, style: "color:#3777b0;text-decoration:none;")
-              - help_link = link_to(_("Help"), help_url, style: "color:#3777b0;text-decoration:none;")
-              = _("You're receiving this email because of your account on %{host}. %{manage_notifications_link} &middot; %{help_link}").html_safe % { host: Gitlab.config.gitlab.host, manage_notifications_link: manage_notifications_link, help_link: help_link }
+              = notification_reason_text(show_manage_notifications_link: true, show_help_link: true, format: :html)
diff --git a/ee/app/views/layouts/mailer/devise.text.erb b/ee/app/views/layouts/mailer/devise.text.erb
index 9ccd3082bf03e..75df7023b5d54 100644
--- a/ee/app/views/layouts/mailer/devise.text.erb
+++ b/ee/app/views/layouts/mailer/devise.text.erb
@@ -1,5 +1,5 @@
 <%= yield -%>
 
 ---
-You're receiving this email because of your account on <%= Gitlab.config.gitlab.host %>.
+<%= notification_reason_text %>
 <%# EE-specific start %><%= render 'layouts/mailer/additional_text' %><%# EE-specific end %>
diff --git a/ee/spec/mailers/ee/emails/profile_spec.rb b/ee/spec/mailers/ee/emails/profile_spec.rb
index 4e5ddc73e8a8a..ba8f7a44c5821 100644
--- a/ee/spec/mailers/ee/emails/profile_spec.rb
+++ b/ee/spec/mailers/ee/emails/profile_spec.rb
@@ -30,7 +30,7 @@
     end
 
     it 'includes the email reason' do
-      is_expected.to have_body_text /You're receiving this email because of your account on localhost/
+      is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>}
     end
   end
 end
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index ecea30cb802a9..dd3b695e4b7c3 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -23441,9 +23441,6 @@ msgstr ""
 msgid "Manage access"
 msgstr ""
 
-msgid "Manage all notifications"
-msgstr ""
-
 msgid "Manage applications that can use GitLab as an OAuth provider, and applications that you've authorized to use your account."
 msgstr ""
 
@@ -44284,18 +44281,36 @@ msgstr ""
 msgid "You're receiving this email because of your account on %{host}."
 msgstr ""
 
-msgid "You're receiving this email because of your account on %{host}. %{manage_notifications_link} &middot; %{help_link}"
+msgid "You're receiving this email because of your account on %{host}. %{manage_label_subscriptions_link_start}Manage label subscriptions%{manage_label_subscriptions_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
+msgid "You're receiving this email because of your account on %{host}. %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
+msgid "You're receiving this email because of your account on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
 msgstr ""
 
 msgid "You're receiving this email because of your activity on %{host}."
 msgstr ""
 
+msgid "You're receiving this email because of your activity on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
 msgid "You're receiving this email because you have been assigned an item on %{host}."
 msgstr ""
 
+msgid "You're receiving this email because you have been assigned an item on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
 msgid "You're receiving this email because you have been mentioned on %{host}."
 msgstr ""
 
+msgid "You're receiving this email because you have been mentioned on %{host}. %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
+msgid "You're receiving this email because you have been mentioned on %{host}. %{unsubscribe_link_start}Unsubscribe%{unsubscribe_link_end} from this thread &middot; %{manage_notifications_link_start}Manage all notifications%{manage_notifications_link_end} &middot; %{help_link_start}Help%{help_link_end}"
+msgstr ""
+
 msgid "You've already enabled two-factor authentication using one time password authenticators. In order to register a different device, you must first disable two-factor authentication."
 msgstr ""
 
diff --git a/spec/features/unsubscribe_links_spec.rb b/spec/features/unsubscribe_links_spec.rb
index 3fe276ce1621f..5317f5863901f 100644
--- a/spec/features/unsubscribe_links_spec.rb
+++ b/spec/features/unsubscribe_links_spec.rb
@@ -14,7 +14,7 @@
   let(:mail) { ActionMailer::Base.deliveries.last }
   let(:body) { Capybara::Node::Simple.new(mail.default_part_body.to_s) }
   let(:header_link) { mail.header['List-Unsubscribe'].to_s[1..-2] } # Strip angle brackets
-  let(:body_link) { body.find_link('unsubscribe')['href'] }
+  let(:body_link) { body.find_link('Unsubscribe')['href'] }
 
   before do
     perform_enqueued_jobs { issue }
diff --git a/spec/helpers/emails_helper_spec.rb b/spec/helpers/emails_helper_spec.rb
index 220e154aad85b..04653d9ff033d 100644
--- a/spec/helpers/emails_helper_spec.rb
+++ b/spec/helpers/emails_helper_spec.rb
@@ -77,7 +77,7 @@
   end
 
   describe 'notification_reason_text' do
-    subject { helper.notification_reason_text(reason_code) }
+    subject { helper.notification_reason_text(reason: reason_code) }
 
     using RSpec::Parameterized::TableSyntax
 
diff --git a/spec/mailers/emails/admin_notification_spec.rb b/spec/mailers/emails/admin_notification_spec.rb
index a233be86a83dc..02ccbeb6c51a1 100644
--- a/spec/mailers/emails/admin_notification_spec.rb
+++ b/spec/mailers/emails/admin_notification_spec.rb
@@ -58,7 +58,7 @@
     end
 
     it 'includes the email reason' do
-      is_expected.to have_body_text "You're receiving this email because of your account on localhost"
+      is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>}
     end
   end
 end
diff --git a/spec/mailers/emails/profile_spec.rb b/spec/mailers/emails/profile_spec.rb
index f4483f7e8f5c7..09ed27eb90fe9 100644
--- a/spec/mailers/emails/profile_spec.rb
+++ b/spec/mailers/emails/profile_spec.rb
@@ -151,7 +151,7 @@
       end
 
       it 'includes the email reason' do
-        is_expected.to have_body_text /You're receiving this email because of your account on localhost/
+        is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>}
       end
     end
   end
@@ -187,7 +187,7 @@
     end
 
     it 'includes the email reason' do
-      is_expected.to have_body_text /You're receiving this email because of your account on localhost/
+      is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>}
     end
 
     context 'with User does not exist' do
@@ -222,7 +222,7 @@
       end
 
       it 'includes the email reason' do
-        is_expected.to have_body_text /You're receiving this email because of your account on localhost/
+        is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>}
       end
     end
 
@@ -266,7 +266,7 @@
     end
 
     shared_examples 'includes the email reason' do
-      it { is_expected.to have_body_text /You're receiving this email because of your account on localhost/ }
+      it { is_expected.to have_body_text %r{You're receiving this email because of your account on <a .*>localhost<\/a>} }
     end
 
     shared_examples 'valid use case' do
diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb
index a9796c28870b6..7f0bb70db7df8 100644
--- a/spec/mailers/notify_spec.rb
+++ b/spec/mailers/notify_spec.rb
@@ -7,6 +7,7 @@
   include EmailSpec::Helpers
   include EmailSpec::Matchers
   include EmailHelpers
+  include EmailsHelper
   include RepoHelpers
   include MembersHelper
 
@@ -396,7 +397,7 @@
           end
         end
 
-        context 'when sent with a reason' do
+        context 'when sent with a reason', type: :helper do
           subject { described_class.reassigned_merge_request_email(recipient.id, merge_request.id, [previous_assignee.id], current_user.id, NotificationReason::ASSIGNED) }
 
           it_behaves_like 'appearance header and footer enabled'
@@ -407,15 +408,15 @@
           end
 
           it 'includes the reason in the footer' do
-            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(NotificationReason::ASSIGNED)
+            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(reason: NotificationReason::ASSIGNED, format: :html)
             is_expected.to have_body_text(text)
 
             new_subject = described_class.reassigned_merge_request_email(recipient.id, merge_request.id, [previous_assignee.id], current_user.id, NotificationReason::MENTIONED)
-            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(NotificationReason::MENTIONED)
+            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(reason: NotificationReason::MENTIONED, format: :html)
             expect(new_subject).to have_body_text(text)
 
             new_subject = described_class.reassigned_merge_request_email(recipient.id, merge_request.id, [previous_assignee.id], current_user.id, nil)
-            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(nil)
+            text = EmailsHelper.instance_method(:notification_reason_text).bind(self).call(format: :html)
             expect(new_subject).to have_body_text(text)
           end
         end
-- 
GitLab