Commit cb517cecf4d82b698dd2b4da4bfc0a342c13c55e

Authored by Nathan Broadbent
1 parent 1135aa99
Exists in master and in 1 other branch production

Give the option to either send notifications to all users, or to a configured list of watchers.

Adding users one by one could be a bit tedious for large teams.
app/mailers/mailer.rb
1 class Mailer < ActionMailer::Base 1 class Mailer < ActionMailer::Base
2 default :from => Errbit::Config.email_from 2 default :from => Errbit::Config.email_from
3 - 3 +
4 def err_notification(notice) 4 def err_notification(notice)
5 @notice = notice 5 @notice = notice
6 @app = notice.err.app 6 @app = notice.err.app
7 - 7 +
8 mail({ 8 mail({
9 - :to => @app.watchers.map(&:address), 9 + :to => @app.notification_recipients,
10 :subject => "[#{@app.name}][#{@notice.err.environment}] #{@notice.err.message}" 10 :subject => "[#{@app.name}][#{@notice.err.environment}] #{@notice.err.message}"
11 }) 11 })
12 end 12 end
13 - 13 +
14 def deploy_notification(deploy) 14 def deploy_notification(deploy)
15 @deploy = deploy 15 @deploy = deploy
16 @app = deploy.app 16 @app = deploy.app
17 - 17 +
18 mail({ 18 mail({
19 - :to => @app.watchers.map(&:address), 19 + :to => @app.notification_recipients,
20 :subject => "[#{@app.name}] Deployed to #{@deploy.environment} by #{@deploy.username}" 20 :subject => "[#{@app.name}] Deployed to #{@deploy.environment} by #{@deploy.username}"
21 }) 21 })
22 end 22 end
23 - 23 +
24 end 24 end
  25 +
app/models/app.rb
@@ -6,6 +6,7 @@ class App @@ -6,6 +6,7 @@ class App
6 field :api_key 6 field :api_key
7 field :github_url 7 field :github_url
8 field :resolve_errs_on_deploy, :type => Boolean, :default => false 8 field :resolve_errs_on_deploy, :type => Boolean, :default => false
  9 + field :notify_all_users, :type => Boolean, :default => false
9 field :notify_on_errs, :type => Boolean, :default => true 10 field :notify_on_errs, :type => Boolean, :default => true
10 field :notify_on_deploys, :type => Boolean, :default => true 11 field :notify_on_deploys, :type => Boolean, :default => true
11 field :email_at_notices, :type => Array, :default => Errbit::Config.email_at_notices 12 field :email_at_notices, :type => Array, :default => Errbit::Config.email_at_notices
@@ -74,6 +75,10 @@ class App @@ -74,6 +75,10 @@ class App
74 issue_tracker && issue_tracker.issue_tracker_type != "none" && !issue_tracker.project_id.blank? 75 issue_tracker && issue_tracker.issue_tracker_type != "none" && !issue_tracker.project_id.blank?
75 end 76 end
76 77
  78 + def notification_recipients
  79 + notify_all_users ? User.all.map(&:email) : watchers.map(&:address)
  80 + end
  81 +
77 protected 82 protected
78 83
79 def generate_api_key 84 def generate_api_key
app/views/apps/_fields.html.haml
@@ -23,11 +23,11 @@ @@ -23,11 +23,11 @@
23 = f.label :notify_on_deploys, 'Notify on deploys' 23 = f.label :notify_on_deploys, 'Notify on deploys'
24 24
25 %div.checkbox 25 %div.checkbox
26 - = f.check_box :resolve_errs_on_deploy  
27 - = f.label :resolve_errs_on_deploy, 'Resolve errs on deploy' 26 + = f.check_box :notify_all_users, 'data-hide-when-checked' => '.watchers.nested-wrapper'
  27 + = f.label :notify_all_users, 'Send notifications to all users'
28 28
29 29
30 -%fieldset.nested-wrapper 30 +%fieldset.watchers.nested-wrapper{:style => f.object.notify_all_users ? 'display: none;' : ''}
31 %legend Watchers 31 %legend Watchers
32 = f.fields_for :watchers do |w| 32 = f.fields_for :watchers do |w|
33 %div.watcher.nested 33 %div.watcher.nested
@@ -41,6 +41,10 @@ @@ -41,6 +41,10 @@
41 %div.watcher_params.email{:class => w.object.email.present? ? 'chosen' : nil} 41 %div.watcher_params.email{:class => w.object.email.present? ? 'chosen' : nil}
42 = w.text_field :email 42 = w.text_field :email
43 43
  44 +%div.checkbox
  45 + = f.check_box :resolve_errs_on_deploy
  46 + = f.label :resolve_errs_on_deploy, 'Resolve errs on deploy'
  47 +
44 %fieldset 48 %fieldset
45 %legend Issue tracker 49 %legend Issue tracker
46 = f.fields_for :issue_tracker do |w| 50 = f.fields_for :issue_tracker do |w|
spec/models/app_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe App do 3 describe App do
4 - 4 +
5 context 'validations' do 5 context 'validations' do
6 it 'requires a name' do 6 it 'requires a name' do
7 app = Factory.build(:app, :name => nil) 7 app = Factory.build(:app, :name => nil)
8 app.should_not be_valid 8 app.should_not be_valid
9 app.errors[:name].should include("can't be blank") 9 app.errors[:name].should include("can't be blank")
10 end 10 end
11 - 11 +
12 it 'requires unique names' do 12 it 'requires unique names' do
13 Factory(:app, :name => 'Errbit') 13 Factory(:app, :name => 'Errbit')
14 app = Factory.build(:app, :name => 'Errbit') 14 app = Factory.build(:app, :name => 'Errbit')
15 app.should_not be_valid 15 app.should_not be_valid
16 app.errors[:name].should include('is already taken') 16 app.errors[:name].should include('is already taken')
17 end 17 end
18 - 18 +
19 it 'requires unique api_keys' do 19 it 'requires unique api_keys' do
20 Factory(:app, :api_key => 'APIKEY') 20 Factory(:app, :api_key => 'APIKEY')
21 app = Factory.build(:app, :api_key => 'APIKEY') 21 app = Factory.build(:app, :api_key => 'APIKEY')
@@ -23,7 +23,7 @@ describe App do @@ -23,7 +23,7 @@ describe App do
23 app.errors[:api_key].should include('is already taken') 23 app.errors[:api_key].should include('is already taken')
24 end 24 end
25 end 25 end
26 - 26 +
27 context 'being created' do 27 context 'being created' do
28 it 'generates a new api-key' do 28 it 'generates a new api-key' do
29 app = Factory.build(:app) 29 app = Factory.build(:app)
@@ -31,18 +31,18 @@ describe App do @@ -31,18 +31,18 @@ describe App do
31 app.save 31 app.save
32 app.api_key.should_not be_nil 32 app.api_key.should_not be_nil
33 end 33 end
34 - 34 +
35 it 'generates a correct api-key' do 35 it 'generates a correct api-key' do
36 app = Factory(:app) 36 app = Factory(:app)
37 app.api_key.should match(/^[a-f0-9]{32}$/) 37 app.api_key.should match(/^[a-f0-9]{32}$/)
38 end 38 end
39 - 39 +
40 it 'is fine with blank github urls' do 40 it 'is fine with blank github urls' do
41 app = Factory.build(:app, :github_url => "") 41 app = Factory.build(:app, :github_url => "")
42 app.save 42 app.save
43 app.github_url.should == "" 43 app.github_url.should == ""
44 end 44 end
45 - 45 +
46 it 'does not touch https github urls' do 46 it 'does not touch https github urls' do
47 app = Factory.build(:app, :github_url => "https://github.com/jdpace/errbit") 47 app = Factory.build(:app, :github_url => "https://github.com/jdpace/errbit")
48 app.save 48 app.save
@@ -67,14 +67,14 @@ describe App do @@ -67,14 +67,14 @@ describe App do
67 app.github_url.should == "https://github.com/jdpace/errbit" 67 app.github_url.should == "https://github.com/jdpace/errbit"
68 end 68 end
69 end 69 end
70 - 70 +
71 context '#github_url_to_file' do 71 context '#github_url_to_file' do
72 it 'resolves to full path to file' do 72 it 'resolves to full path to file' do
73 app = Factory(:app, :github_url => "https://github.com/jdpace/errbit") 73 app = Factory(:app, :github_url => "https://github.com/jdpace/errbit")
74 app.github_url_to_file('/path/to/file').should == "https://github.com/jdpace/errbit/blob/master/path/to/file" 74 app.github_url_to_file('/path/to/file').should == "https://github.com/jdpace/errbit/blob/master/path/to/file"
75 end 75 end
76 end 76 end
77 - 77 +
78 context '#github_url?' do 78 context '#github_url?' do
79 it 'is true when there is a github_url' do 79 it 'is true when there is a github_url' do
80 app = Factory(:app, :github_url => "https://github.com/jdpace/errbit") 80 app = Factory(:app, :github_url => "https://github.com/jdpace/errbit")
@@ -86,5 +86,18 @@ describe App do @@ -86,5 +86,18 @@ describe App do
86 app.github_url?.should be_false 86 app.github_url?.should be_false
87 end 87 end
88 end 88 end
89 - 89 +
  90 + context "notification recipients" do
  91 + it "should send notices to either all users, or the configured watchers" do
  92 + @app = Factory(:app)
  93 + 3.times { Factory(:user) }
  94 + 5.times { Factory(:watcher, :app => @app) }
  95 + @app.notify_all_users = true
  96 + @app.notification_recipients.size.should == 3
  97 + @app.notify_all_users = false
  98 + @app.notification_recipients.size.should == 5
  99 + end
  100 + end
  101 +
90 end 102 end
  103 +