Commit a3cdaeef6654edac27a07fac8189c581977827e5

Authored by Dmitriy Zaporozhets
1 parent 52d3fa19

refactor emails a bit. Add email on ssh key creation

app/mailers/emails/issues.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +module Emails
  2 + module Issues
  3 + def new_issue_email(issue_id)
  4 + @issue = Issue.find(issue_id)
  5 + @project = @issue.project
  6 + mail(to: @issue.assignee_email, subject: subject("new issue ##{@issue.id}", @issue.title))
  7 + end
  8 +
  9 + def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id)
  10 + @issue = Issue.find(issue_id)
  11 + @previous_assignee ||= User.find(previous_assignee_id)
  12 + @project = @issue.project
  13 + mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title))
  14 + end
  15 +
  16 + def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id)
  17 + @issue = Issue.find issue_id
  18 + @issue_status = status
  19 + @project = @issue.project
  20 + @updated_by = User.find updated_by_user_id
  21 + mail(to: recipient(recipient_id),
  22 + subject: subject("changed issue ##{@issue.id}", @issue.title))
  23 + end
  24 + end
  25 +end
... ...
app/mailers/emails/merge_requests.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +module Emails
  2 + module MergeRequests
  3 + def new_merge_request_email(merge_request_id)
  4 + @merge_request = MergeRequest.find(merge_request_id)
  5 + @project = @merge_request.project
  6 + mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title))
  7 + end
  8 +
  9 + def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id)
  10 + @merge_request = MergeRequest.find(merge_request_id)
  11 + @previous_assignee ||= User.find(previous_assignee_id)
  12 + @project = @merge_request.project
  13 + mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title))
  14 + end
  15 + end
  16 +end
... ...
app/mailers/emails/notes.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +module Emails
  2 + module Notes
  3 + def note_commit_email(recipient_id, note_id)
  4 + @note = Note.find(note_id)
  5 + @commit = @note.noteable
  6 + @commit = CommitDecorator.decorate(@commit)
  7 + @project = @note.project
  8 + mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title))
  9 + end
  10 +
  11 + def note_issue_email(recipient_id, note_id)
  12 + @note = Note.find(note_id)
  13 + @issue = @note.noteable
  14 + @project = @note.project
  15 + mail(to: recipient(recipient_id), subject: subject("note for issue ##{@issue.id}"))
  16 + end
  17 +
  18 + def note_merge_request_email(recipient_id, note_id)
  19 + @note = Note.find(note_id)
  20 + @merge_request = @note.noteable
  21 + @project = @note.project
  22 + mail(to: recipient(recipient_id), subject: subject("note for merge request !#{@merge_request.id}"))
  23 + end
  24 +
  25 + def note_wall_email(recipient_id, note_id)
  26 + @note = Note.find(note_id)
  27 + @project = @note.project
  28 + mail(to: recipient(recipient_id), subject: subject("note on wall"))
  29 + end
  30 + end
  31 +end
... ...
app/mailers/emails/projects.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +module Emails
  2 + module Projects
  3 + def project_access_granted_email(user_project_id)
  4 + @users_project = UsersProject.find user_project_id
  5 + @project = @users_project.project
  6 + mail(to: @users_project.user.email,
  7 + subject: subject("access to project was granted"))
  8 + end
  9 +
  10 +
  11 + def project_was_moved_email(user_project_id)
  12 + @users_project = UsersProject.find user_project_id
  13 + @project = @users_project.project
  14 + mail(to: @users_project.user.email,
  15 + subject: subject("project was moved"))
  16 + end
  17 + end
  18 +end
... ...
app/mailers/notify.rb
1 1 class Notify < ActionMailer::Base
  2 + include Emails::Issues
  3 + include Emails::MergeRequests
  4 + include Emails::Notes
  5 + include Emails::Projects
2 6  
3 7 add_template_helper ApplicationHelper
4 8 add_template_helper GitlabMarkdownHelper
... ... @@ -15,116 +19,17 @@ class Notify &lt; ActionMailer::Base
15 19 delay_for(2.seconds)
16 20 end
17 21  
18   -
19   - #
20   - # Issue
21   - #
22   -
23   - def new_issue_email(issue_id)
24   - @issue = Issue.find(issue_id)
25   - @project = @issue.project
26   - mail(to: @issue.assignee_email, subject: subject("new issue ##{@issue.id}", @issue.title))
27   - end
28   -
29   - def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id)
30   - @issue = Issue.find(issue_id)
31   - @previous_assignee ||= User.find(previous_assignee_id)
32   - @project = @issue.project
33   - mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title))
34   - end
35   -
36   - def issue_status_changed_email(recipient_id, issue_id, status, updated_by_user_id)
37   - @issue = Issue.find issue_id
38   - @issue_status = status
39   - @project = @issue.project
40   - @updated_by = User.find updated_by_user_id
41   - mail(to: recipient(recipient_id),
42   - subject: subject("changed issue ##{@issue.id}", @issue.title))
43   - end
44   -
45   -
46   -
47   - #
48   - # Merge Request
49   - #
50   -
51   - def new_merge_request_email(merge_request_id)
52   - @merge_request = MergeRequest.find(merge_request_id)
53   - @project = @merge_request.project
54   - mail(to: @merge_request.assignee_email, subject: subject("new merge request !#{@merge_request.id}", @merge_request.title))
55   - end
56   -
57   - def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id)
58   - @merge_request = MergeRequest.find(merge_request_id)
59   - @previous_assignee ||= User.find(previous_assignee_id)
60   - @project = @merge_request.project
61   - mail(to: recipient(recipient_id), subject: subject("changed merge request !#{@merge_request.id}", @merge_request.title))
62   - end
63   -
64   -
65   -
66   - #
67   - # Note
68   - #
69   -
70   - def note_commit_email(recipient_id, note_id)
71   - @note = Note.find(note_id)
72   - @commit = @note.noteable
73   - @commit = CommitDecorator.decorate(@commit)
74   - @project = @note.project
75   - mail(to: recipient(recipient_id), subject: subject("note for commit #{@commit.short_id}", @commit.title))
76   - end
77   -
78   - def note_issue_email(recipient_id, note_id)
79   - @note = Note.find(note_id)
80   - @issue = @note.noteable
81   - @project = @note.project
82   - mail(to: recipient(recipient_id), subject: subject("note for issue ##{@issue.id}"))
83   - end
84   -
85   - def note_merge_request_email(recipient_id, note_id)
86   - @note = Note.find(note_id)
87   - @merge_request = @note.noteable
88   - @project = @note.project
89   - mail(to: recipient(recipient_id), subject: subject("note for merge request !#{@merge_request.id}"))
90   - end
91   -
92   - def note_wall_email(recipient_id, note_id)
93   - @note = Note.find(note_id)
94   - @project = @note.project
95   - mail(to: recipient(recipient_id), subject: subject("note on wall"))
96   - end
97   -
98   -
99   - #
100   - # Project
101   - #
102   -
103   - def project_access_granted_email(user_project_id)
104   - @users_project = UsersProject.find user_project_id
105   - @project = @users_project.project
106   - mail(to: @users_project.user.email,
107   - subject: subject("access to project was granted"))
108   - end
109   -
110   -
111   - def project_was_moved_email(user_project_id)
112   - @users_project = UsersProject.find user_project_id
113   - @project = @users_project.project
114   - mail(to: @users_project.user.email,
115   - subject: subject("project was moved"))
116   - end
117   -
118   - #
119   - # User
120   - #
121   -
122 22 def new_user_email(user_id, password)
123 23 @user = User.find(user_id)
124 24 @password = password
125 25 mail(to: @user.email, subject: subject("Account was created for you"))
126 26 end
127 27  
  28 + def new_ssh_key_email(key_id)
  29 + @key = Key.find(key_id)
  30 + @user = @key.user
  31 + mail(to: @user.email, subject: subject("SSH key was added to your account"))
  32 + end
128 33  
129 34 private
130 35  
... ...
app/observers/key_observer.rb
... ... @@ -7,6 +7,9 @@ class KeyObserver &lt; ActiveRecord::Observer
7 7 key.shell_id,
8 8 key.key
9 9 )
  10 +
  11 + # Notify about ssh key being added
  12 + Notify.delay.new_ssh_key_email(key.id) if key.user
10 13 end
11 14  
12 15 def after_destroy(key)
... ...
app/views/notify/new_ssh_key_email.html.haml 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +%p
  2 + Hi #{@user.name}!
  3 +%p
  4 + A new public key was added to your account:
  5 +%p
  6 + title:
  7 + %code= @key.title
  8 +%p
  9 + If this key was added in error, you can remove here:
  10 + = link_to "SSH Keys", keys_url
... ...
app/views/notify/new_ssh_key_email.text.erb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +Hi <%= @user.name %>!
  2 +
  3 +A new public key was added to your account:
  4 +
  5 +title.................. <%= @key.title %>
  6 +
  7 +If this key was added in error, you can remove here: <%= keys_url %>
... ...
spec/mailers/notify_spec.rb
... ... @@ -70,6 +70,28 @@ describe Notify do
70 70 end
71 71 end
72 72  
  73 + describe 'user added ssh key' do
  74 + let(:key) { create(:personal_key) }
  75 +
  76 + subject { Notify.new_ssh_key_email(key.id) }
  77 +
  78 + it 'is sent to the new user' do
  79 + should deliver_to key.user.email
  80 + end
  81 +
  82 + it 'has the correct subject' do
  83 + should have_subject /^gitlab \| SSH key was added to your account$/i
  84 + end
  85 +
  86 + it 'contains the new ssh key title' do
  87 + should have_body_text /#{key.title}/
  88 + end
  89 +
  90 + it 'includes a link to ssh keys page' do
  91 + should have_body_text /#{keys_path}/
  92 + end
  93 + end
  94 +
73 95 context 'for a project' do
74 96 describe 'items that are assignable, the email' do
75 97 let(:assignee) { create(:user, email: 'assignee@example.com') }
... ...