Skip to content

Commit c2de2cf

Browse files
authored
Don't use logger.error with capistrano 2 (#1043)
* fix: don't use logger.error with capistrano 2 * chore: pin rexml to ruby v2.0 compatible version for Rails 3 ci config * style: refactor for line length
1 parent 54ecd6f commit c2de2cf

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

gemfiles/rails30.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ platforms :rbx do
3030
end
3131

3232
gem 'capistrano', :require => false
33+
gem 'rexml', '<= 3.2.4'
3334
gem 'sucker_punch', '~> 2.0'
3435
gem 'shoryuken'
3536
gem 'codacy-coverage'

gemfiles/rails31.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ platforms :rbx do
2929
end
3030

3131
gem 'capistrano', :require => false
32+
gem 'rexml', '<= 3.2.4'
3233
gem 'sucker_punch'
3334
gem 'shoryuken'
3435
gem 'codacy-coverage'

gemfiles/rails32.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ platforms :rbx do
3131
end
3232

3333
gem 'capistrano', :require => false
34+
gem 'rexml', '<= 3.2.4'
3435
gem 'sucker_punch', '~> 2.0'
3536
gem 'shoryuken'
3637
gem 'codacy-coverage'

lib/rollbar/capistrano_tasks.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def deploy_started(capistrano, logger, dry_run)
1616
if result[:success] && (deploy_id = result[:data] && result[:data][:deploy_id])
1717
capistrano.set :rollbar_deploy_id, deploy_id
1818
else
19-
logger.error 'Unable to report deploy to Rollbar' + (result[:message] ? ': ' + result[:message] : '')
19+
message = format_message('Unable to report deploy to Rollbar',
20+
result[:message])
21+
log_error(logger, message)
2022
end
2123
end
2224
end
@@ -42,7 +44,7 @@ def deploy_task(logger, opts = {})
4244
yield
4345

4446
rescue StandardError => e
45-
logger.error "Error reporting to Rollbar: #{e.inspect}"
47+
log_error logger, "Error reporting to Rollbar: #{e.inspect}"
4648
end
4749

4850
def deploy_update(capistrano, logger, dry_run, opts = {})
@@ -56,7 +58,9 @@ def deploy_update(capistrano, logger, dry_run, opts = {})
5658
if result[:success]
5759
logger.info 'Updated deploy status in Rollbar'
5860
else
59-
logger.error 'Unable to update deploy status in Rollbar' + (result[:message] ? ': ' + result[:message] : '')
61+
message = format_message('Unable to update deploy status in Rollbar',
62+
result[:message])
63+
log_error(logger, message)
6064
end
6165
end
6266
end
@@ -117,7 +121,7 @@ def depend_on_deploy_id(capistrano, logger)
117121
if capistrano.fetch(:rollbar_deploy_id)
118122
yield
119123
else
120-
logger.error 'Failed to update the deploy in Rollbar. No deploy id available.'
124+
log_error logger, 'Failed to update the deploy in Rollbar. No deploy id available.'
121125
end
122126
end
123127

@@ -134,6 +138,20 @@ def debug_request_response(logger, result)
134138
logger.debug result[:request_info]
135139
logger.debug result[:response_info] if result[:response_info]
136140
end
141+
142+
def format_message(*args)
143+
args.compact.join(': ')
144+
end
145+
146+
def log_error(logger, message)
147+
# Capistrano 2.x doesn't have the #error method,
148+
# so we use #important if #error isn't present
149+
if logger.respond_to?(:error)
150+
logger.error message
151+
elsif logger.respond_to?(:important)
152+
logger.important message
153+
end
154+
end
137155
end
138156
end
139157
end

spec/rollbar/capistrano_tasks_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
let(:rollbar_revision) { 'sha123' }
1515
let(:dry_run) { false }
1616

17+
class Capistrano2LoggerStub
18+
def important(message, line_prefix=nil) end
19+
def info(message, line_prefix=nil) end
20+
def debug(message, line_prefix=nil) end
21+
end
22+
1723
before do
1824
allow(logger).to receive(:info)
1925
allow(logger).to receive(:error)
@@ -88,6 +94,19 @@
8894

8995
subject.deploy_started(capistrano, logger, dry_run)
9096
end
97+
98+
context 'when using Capistrano 2.x logger' do
99+
let(:logger2) { Capistrano2LoggerStub.new }
100+
101+
it "logs the error to the logger" do
102+
expect(::Rollbar::Deploy).to receive(:report)
103+
.and_raise('an API exception')
104+
105+
expect(logger2).to receive(:important).with(/an API exception/)
106+
107+
subject.deploy_started(capistrano, logger2, dry_run)
108+
end
109+
end
91110
end
92111

93112
context 'with --dry-run provided' do
@@ -174,6 +193,19 @@
174193

175194
subject.deploy_succeeded(capistrano, logger, dry_run)
176195
end
196+
197+
context 'when using Capistrano 2.x logger' do
198+
let(:logger2) { Capistrano2LoggerStub.new }
199+
200+
it "logs the error to the logger" do
201+
expect(::Rollbar::Deploy).to receive(:update)
202+
.and_raise('an API exception')
203+
204+
expect(logger2).to receive(:important).with(/an API exception/)
205+
206+
subject.deploy_succeeded(capistrano, logger2, dry_run)
207+
end
208+
end
177209
end
178210

179211
context 'with --dry-run provided' do
@@ -282,6 +314,19 @@
282314

283315
subject.deploy_failed(capistrano, logger, dry_run)
284316
end
317+
318+
context 'when using Capistrano 2.x logger' do
319+
let(:logger2) { Capistrano2LoggerStub.new }
320+
321+
it "logs the error to the logger" do
322+
expect(::Rollbar::Deploy).to receive(:update)
323+
.and_raise('an API exception')
324+
325+
expect(logger2).to receive(:important).with(/an API exception/)
326+
327+
subject.deploy_failed(capistrano, logger2, dry_run)
328+
end
329+
end
285330
end
286331

287332
context 'with --dry-run provided' do
@@ -324,6 +369,18 @@
324369

325370
subject.deploy_failed(capistrano, logger, dry_run)
326371
end
372+
373+
context 'when using Capistrano 2.x logger' do
374+
let(:logger2) { Capistrano2LoggerStub.new }
375+
376+
it 'displays an error message and exits' do
377+
expect(Rollbar::Deploy).to_not receive(:report)
378+
379+
expect(logger2).to receive(:important).with(/Failed(.*)No deploy id available/)
380+
381+
subject.deploy_failed(capistrano, logger2, dry_run)
382+
end
383+
end
327384
end
328385
end
329386

0 commit comments

Comments
 (0)