notify_preview.rb 6.08 KB
Newer Older
1 2
# frozen_string_literal: true

3
class NotifyPreview < ActionMailer::Preview
4 5 6 7 8 9 10 11
  def note_merge_request_email_for_individual_note
    note_email(:note_merge_request_email) do
      note = <<-MD.strip_heredoc
        This is an individual note on a merge request :smiley:

        In this notification email, we expect to see:

        - The note contents (that's what you're looking at)
12
        - A link to view this note on GitLab
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        - An explanation for why the user is receiving this notification
      MD

      create_note(noteable_type: 'merge_request', noteable_id: merge_request.id, note: note)
    end
  end

  def note_merge_request_email_for_discussion
    note_email(:note_merge_request_email) do
      note = <<-MD.strip_heredoc
        This is a new discussion on a merge request :smiley:

        In this notification email, we expect to see:

        - A line saying who started this discussion
        - The note contents (that's what you're looking at)
29
        - A link to view this discussion on GitLab
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        - An explanation for why the user is receiving this notification
      MD

      create_note(noteable_type: 'merge_request', noteable_id: merge_request.id, type: 'DiscussionNote', note: note)
    end
  end

  def note_merge_request_email_for_diff_discussion
    note_email(:note_merge_request_email) do
      note = <<-MD.strip_heredoc
        This is a new discussion on a merge request :smiley:

        In this notification email, we expect to see:

        - A line saying who started this discussion and on what file
        - The diff
        - The note contents (that's what you're looking at)
47
        - A link to view this discussion on GitLab
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        - An explanation for why the user is receiving this notification
      MD

      position = Gitlab::Diff::Position.new(
        old_path: "files/ruby/popen.rb",
        new_path: "files/ruby/popen.rb",
        old_line: nil,
        new_line: 14,
        diff_refs: merge_request.diff_refs
      )

      create_note(noteable_type: 'merge_request', noteable_id: merge_request.id, type: 'DiffNote', position: position, note: note)
    end
  end

63 64 65 66 67 68 69 70
  def closed_issue_email
    Notify.closed_issue_email(user.id, issue.id, user.id).message
  end

  def issue_status_changed_email
    Notify.issue_status_changed_email(user.id, issue.id, 'closed', user.id).message
  end

71 72 73 74 75 76 77 78
  def removed_milestone_issue_email
    Notify.removed_milestone_issue_email(user.id, issue.id, user.id)
  end

  def changed_milestone_issue_email
    Notify.changed_milestone_issue_email(user.id, issue.id, milestone, user.id)
  end

79
  def import_issues_csv_email
80
    Notify.import_issues_csv_email(user.id, project.id, { success: 3, errors: [5, 6, 7], valid_file: true })
81 82
  end

83 84 85 86
  def issues_csv_email
    Notify.issues_csv_email(user, project, '1997,Ford,E350', { truncated: false, rows_expected: 3, rows_written: 3 }).message
  end

87 88 89 90 91 92 93 94 95 96 97 98
  def closed_merge_request_email
    Notify.closed_merge_request_email(user.id, issue.id, user.id).message
  end

  def merge_request_status_email
    Notify.merge_request_status_email(user.id, merge_request.id, 'closed', user.id).message
  end

  def merged_merge_request_email
    Notify.merged_merge_request_email(user.id, merge_request.id, user.id).message
  end

99 100 101 102 103 104 105 106
  def removed_milestone_merge_request_email
    Notify.removed_milestone_merge_request_email(user.id, merge_request.id, user.id)
  end

  def changed_milestone_merge_request_email
    Notify.changed_milestone_merge_request_email(user.id, merge_request.id, milestone, user.id)
  end

107 108 109 110 111
  def member_access_denied_email
    Notify.member_access_denied_email('project', project.id, user.id).message
  end

  def member_access_granted_email
112
    Notify.member_access_granted_email(member.source_type, member.id).message
113 114 115
  end

  def member_access_requested_email
116
    Notify.member_access_requested_email(member.source_type, member.id, user.id).message
117 118 119
  end

  def member_invite_accepted_email
120
    Notify.member_invite_accepted_email(member.source_type, member.id).message
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  end

  def member_invite_declined_email
    Notify.member_invite_declined_email(
      'project',
      project.id,
      'invite@example.com',
      user.id
    ).message
  end

  def member_invited_email
    Notify.member_invited_email('project', user.id, '1234').message
  end

  def pages_domain_enabled_email
    cleanup do
      pages_domain = PagesDomain.new(domain: 'my.example.com', project: project, verified_at: Time.now, enabled_until: 1.week.from_now)

      Notify.pages_domain_enabled_email(pages_domain, user).message
    end
  end

  def pipeline_success_email
    Notify.pipeline_success_email(pipeline, pipeline.user.try(:email))
  end

  def pipeline_failed_email
    Notify.pipeline_failed_email(pipeline, pipeline.user.try(:email))
  end

152 153 154 155
  def pipeline_fixed_email
    Notify.pipeline_fixed_email(pipeline, pipeline.user.try(:email))
  end

156 157 158 159
  def autodevops_disabled_email
    Notify.autodevops_disabled_email(pipeline, user.email).message
  end

160 161 162 163
  def remote_mirror_update_failed_email
    Notify.remote_mirror_update_failed_email(remote_mirror.id, user.id).message
  end

164
  def unknown_sign_in_email
165
    Notify.unknown_sign_in_email(user, '127.0.0.1', Time.current).message
166 167
  end

168 169 170 171 172 173
  private

  def project
    @project ||= Project.find_by_full_path('gitlab-org/gitlab-test')
  end

174 175 176 177
  def issue
    @merge_request ||= project.issues.first
  end

178
  def merge_request
179
    @merge_request ||= project.merge_requests.first
180 181
  end

182 183 184 185
  def milestone
    @milestone ||= issue.milestone
  end

186 187 188 189
  def pipeline
    @pipeline = Ci::Pipeline.last
  end

190 191 192 193
  def remote_mirror
    @remote_mirror ||= RemoteMirror.last
  end

194 195 196 197
  def user
    @user ||= User.last
  end

198 199 200 201
  def member
    @member ||= Member.last
  end

202 203 204 205 206 207 208 209
  def create_note(params)
    Notes::CreateService.new(project, user, params).execute
  end

  def note_email(method)
    cleanup do
      note = yield

210
      Notify.public_send(method, user.id, note) # rubocop:disable GitlabSecurity/PublicSend
211 212 213 214 215 216 217 218 219 220 221 222 223
    end
  end

  def cleanup
    email = nil

    ActiveRecord::Base.transaction do
      email = yield
      raise ActiveRecord::Rollback
    end

    email
  end
224
end
225

226
NotifyPreview.prepend_if_ee('EE::Preview::NotifyPreview')