Skip to content

Commit

Permalink
Support deployment tracking in code (#397)
Browse files Browse the repository at this point in the history
Resolves #396
  • Loading branch information
danleyden authored Mar 8, 2021
1 parent db62713 commit 7666a9f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/honeybadger/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ def check_in(id)
response.success?
end

# Track a new deployment
#
# @example
# Honeybadger.track_deployment(revision: 'be2ceb6')
#
# @param [String] :env The environment name. Defaults to the current configured environment.
# @param [String] :revision The VCS revision being deployed. Defaults to the currently configured revision.
# @param [String] :local_username The name of the user who performed the deploy.
# @param [String] :repository The base URL of the VCS repository. It should be HTTPS-style.
#
# @return [Boolean] true if the deployment was successfully tracked and false
# otherwise.
def track_deployment(env: nil, revision: nil, local_username: nil, repository: nil)
opts = {
env: env || config[:env],
revision: revision || config[:revision],
local_username: local_username,
repository: repository
}
response = backend.track_deployment(opts)
response.success?
end

# Save global context for the current request.
#
# @example
Expand Down
9 changes: 9 additions & 0 deletions lib/honeybadger/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ def check_in(id)
raise NotImplementedError, 'must define #check_in on subclass.'
end

# Track a deployment
# @example
# backend.track_deployment({ revision: 'be2ceb6' })
#
# @param [#to_json] payload The JSON payload containing all deployment data.
def track_deployment(payload)
notify(:deploys, payload)
end

private

attr_reader :config
Expand Down
1 change: 1 addition & 0 deletions lib/honeybadger/singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module Honeybadger
def_delegator :'Honeybadger::Agent.instance', :add_breadcrumb
def_delegator :'Honeybadger::Agent.instance', :breadcrumbs
def_delegator :'Honeybadger::Agent.instance', :clear!
def_delegator :'Honeybadger::Agent.instance', :track_deployment

# @!macro [attach] def_delegator
# @!method $2(...)
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/honeybadger/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@
end
end

describe '#track_deployment' do
let(:config) { Honeybadger::Config.new(api_key:'fake api key', logger: NULL_LOGGER) }
subject(:instance) { described_class.new(config) }

it 'returns true for successful deployment tracking' do
stub_request(:post, "https://api.honeybadger.io/v1/deploys").
to_return(status: 200)

expect(instance.track_deployment).to eq(true)
end

it 'returns false for unsuccessful deployment tracking' do
stub_request(:post, "https://api.honeybadger.io/v1/deploys").
to_return(status: 400)

expect(instance.track_deployment).to eq(false)
end

it 'passes the revision to the servce' do
allow_any_instance_of(Honeybadger::Util::HTTP).to receive(:compress) { |_, body| body }
stub_request(:post, "https://api.honeybadger.io/v1/deploys").
with(body: { env: nil, revision: '1234', local_username: nil, repository: nil }).
to_return(status: 200)

expect(instance.track_deployment(revision: '1234')).to eq(true)
end
end

describe "#clear!" do
it 'clears all transactional data' do
config = Honeybadger::Config.new(api_key:'fake api key', logger: NULL_LOGGER)
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/honeybadger/backend/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@
expect { subject.check_in(10) }.to raise_error NotImplementedError
end
end

describe "#track_deployment" do
it "defers the request to notify with the feature set as deploys" do
opts = double(:opts)
expect(subject).to receive(:notify).with(:deploys, opts)
subject.track_deployment(opts)
end
end
end
1 change: 1 addition & 0 deletions spec/unit/honeybadger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
it { should be_a Module }
it { should respond_to :notify }
it { should respond_to :start }
it { should respond_to :track_deployment }

it { should define(:Rack) }

Expand Down

0 comments on commit 7666a9f

Please sign in to comment.