From f5c624f1542f8310fe68b65519a75eb930cad73c Mon Sep 17 00:00:00 2001 From: Dan Leyden Date: Fri, 5 Mar 2021 09:33:02 +0000 Subject: [PATCH] Support deployment tracking in code Resolves #396 --- lib/honeybadger/agent.rb | 23 ++++++++++++++++++ lib/honeybadger/backend/base.rb | 9 +++++++ lib/honeybadger/singleton.rb | 1 + spec/unit/honeybadger/agent_spec.rb | 28 ++++++++++++++++++++++ spec/unit/honeybadger/backend/base_spec.rb | 8 +++++++ spec/unit/honeybadger_spec.rb | 1 + 6 files changed, 70 insertions(+) diff --git a/lib/honeybadger/agent.rb b/lib/honeybadger/agent.rb index de56f907f..d56dc765e 100644 --- a/lib/honeybadger/agent.rb +++ b/lib/honeybadger/agent.rb @@ -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 diff --git a/lib/honeybadger/backend/base.rb b/lib/honeybadger/backend/base.rb index 84729f476..de5180a61 100644 --- a/lib/honeybadger/backend/base.rb +++ b/lib/honeybadger/backend/base.rb @@ -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 diff --git a/lib/honeybadger/singleton.rb b/lib/honeybadger/singleton.rb index 768f015b5..53a57a607 100644 --- a/lib/honeybadger/singleton.rb +++ b/lib/honeybadger/singleton.rb @@ -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(...) diff --git a/spec/unit/honeybadger/agent_spec.rb b/spec/unit/honeybadger/agent_spec.rb index 1ab1c15fe..508f47f77 100644 --- a/spec/unit/honeybadger/agent_spec.rb +++ b/spec/unit/honeybadger/agent_spec.rb @@ -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) diff --git a/spec/unit/honeybadger/backend/base_spec.rb b/spec/unit/honeybadger/backend/base_spec.rb index c7d5a8edf..f1d671c8e 100644 --- a/spec/unit/honeybadger/backend/base_spec.rb +++ b/spec/unit/honeybadger/backend/base_spec.rb @@ -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 diff --git a/spec/unit/honeybadger_spec.rb b/spec/unit/honeybadger_spec.rb index 1b22f572b..996103f6e 100644 --- a/spec/unit/honeybadger_spec.rb +++ b/spec/unit/honeybadger_spec.rb @@ -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) }