Skip to content
This repository was archived by the owner on May 12, 2018. It is now read-only.

Commit a0126cf

Browse files
committed
Rewrite notifications to work without observers
Signed-off-by: Dmitriy Zaporozhets <[email protected]>
1 parent 36d537e commit a0126cf

File tree

10 files changed

+35
-50
lines changed

10 files changed

+35
-50
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ gem 'actionpack-page_caching'
1414
gem 'actionpack-action_caching'
1515
gem 'activerecord-deprecated_finders'
1616
gem 'activerecord-session_store'
17-
gem "rails-observers"
1817

1918
# DB
2019
gem 'mysql2', group: :mysql
@@ -71,6 +70,7 @@ gem "font-awesome-sass-rails", '~> 3.0.2'
7170
group :development do
7271
gem 'annotate'
7372
gem 'quiet_assets'
73+
gem "letter_opener"
7474
end
7575

7676

Gemfile.lock

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ GEM
142142
kaminari (0.15.0)
143143
actionpack (>= 3.0.0)
144144
activesupport (>= 3.0.0)
145+
launchy (2.4.2)
146+
addressable (~> 2.3)
147+
letter_opener (1.1.2)
148+
launchy (~> 2.2)
145149
libv8 (3.16.14.3)
146150
listen (2.4.0)
147151
celluloid (>= 0.15.2)
@@ -194,8 +198,6 @@ GEM
194198
bundler (>= 1.3.0, < 2.0)
195199
railties (= 4.0.2)
196200
sprockets-rails (~> 2.0.0)
197-
rails-observers (0.1.2)
198-
activemodel (~> 4.0)
199201
railties (4.0.2)
200202
actionpack (= 4.0.2)
201203
activesupport (= 4.0.2)
@@ -325,6 +327,7 @@ DEPENDENCIES
325327
httparty (= 0.11.0)
326328
jquery-rails
327329
kaminari
330+
letter_opener
328331
minitest (= 4.3.2)
329332
mysql2
330333
pg
@@ -334,7 +337,6 @@ DEPENDENCIES
334337
puma (~> 2.7.1)
335338
quiet_assets
336339
rails (= 4.0.2)
337-
rails-observers
338340
rake
339341
rb-fsevent
340342
rb-inotify

app/models/build.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def self.create_from(build)
7575

7676
after_transition any => [:success, :failed, :canceled] do |build, transition|
7777
build.update_attributes finished_at: Time.now
78+
project = build.project
79+
80+
if project.email_notification?
81+
if build.status.to_sym == :failed || project.email_all_broken_builds
82+
NotificationService.new.build_ended(build)
83+
end
84+
end
7885
end
7986

8087
state :pending, value: 'pending'
@@ -166,11 +173,10 @@ def allow_git_fetch
166173
def project_name
167174
project.name
168175
end
169-
176+
170177
def project_recipients
171178
recipients = project.email_recipients.split(' ')
172179
recipients << git_author_email if project.email_add_committer?
173180
recipients.uniq
174181
end
175-
176182
end

app/models/project.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
# gitlab_id :integer
1919
# allow_git_fetch :boolean default(TRUE), not null
2020
# email_recipients :string(255)
21-
# email_add_committer :boolean default(TRUE), not null
22-
# email_all_broken_builds :boolean default(TRUE), not null
21+
# email_add_committer :boolean default(TRUE), not null
22+
# email_all_broken_builds :boolean default(TRUE), not null
2323
#
2424

2525
class Project < ActiveRecord::Base
2626
attr_accessible :name, :path, :scripts, :timeout, :token,
2727
:default_ref, :gitlab_url, :always_build, :polling_interval,
28-
:public, :ssh_url_to_repo, :gitlab_id, :allow_git_fetch,
28+
:public, :ssh_url_to_repo, :gitlab_id, :allow_git_fetch,
2929
:email_recipients, :email_add_committer, :email_all_broken_builds
3030

3131
has_many :builds, dependent: :destroy
@@ -127,11 +127,11 @@ def broken?
127127
def success?
128128
last_build.success? if last_build
129129
end
130-
130+
131131
def broken_or_success?
132-
broken? || success?
132+
broken? || success?
133133
end
134-
134+
135135
def last_build
136136
builds.last
137137
end
@@ -184,17 +184,16 @@ def no_running_builds?
184184
# Get running builds not later than 3 days ago to ignore hangs
185185
builds.running.where("updated_at > ?", 3.days.ago).empty?
186186
end
187-
187+
188188
def email_notification?
189-
(email_add_committer || !email_recipients.blank?) && broken_or_success?
189+
email_add_committer || email_recipients.present?
190190
end
191-
191+
192192
# onlu check for toggling build status within same ref.
193193
def last_build_changed_status?
194-
ref = last_build.ref
194+
ref = last_build.ref
195195
last_builds = builds.where(ref: ref).order('id DESC').limit(2)
196-
return false if last_builds.size < 2
196+
return false if last_builds.size < 2
197197
return last_builds[0].status != last_builds[1].status
198198
end
199-
200199
end

app/observers/base_observer.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/observers/build_observer.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/services/notification_service.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@
66
# NotificationService.new.build_ended(build)
77
#
88
class NotificationService
9-
109
def build_ended(build)
1110
build.project_recipients.each do |recipient|
12-
if build.status == :success
13-
mailer.build_success_email(build.id, recipient)
14-
else
11+
case build.status.to_sym
12+
when :success
13+
mailer.build_success_email(build.id, recipient)
14+
when :failed
1515
mailer.build_fail_email(build.id, recipient)
1616
end
17-
end
17+
end
1818
end
1919

2020
protected
21-
22-
# Do we need to delay these emails?
21+
2322
def mailer
24-
# Notify.delay
25-
Notify
23+
Notify.delay
2624
end
27-
2825
end

config/application.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class Application < Rails::Application
1717
# :all can be used as a placeholder for all plugins not explicitly named.
1818
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
1919

20-
# Activate observers that should always be running.
21-
config.active_record.observers = :build_observer
22-
2320
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
2421
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
2522
# config.time_zone = 'Central Time (US & Canada)'

config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
config.assets.debug = true
3030

3131
config.eager_load = false
32+
33+
config.action_mailer.delivery_method = :letter_opener
3234
end

spec/services/notification_service_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def should_email(email)
3737
describe 'successfull build and project has email_recipients' do
3838
let(:project) { FactoryGirl.create(:project, :email_recipients => "[email protected]")}
3939
let(:build) { FactoryGirl.create(:build, :status => :success, :project => project) }
40-
40+
4141
it do
4242
should_email(build.git_author_email)
4343
should_email("[email protected]")
@@ -50,5 +50,4 @@ def should_email(email)
5050
end
5151
end
5252
end
53-
5453
end

0 commit comments

Comments
 (0)