From f7ece62009cc9f765061a68bbc9e2cddd17635ea Mon Sep 17 00:00:00 2001 From: PaulDoyle-DEFRA <97455399+PaulDoyle-DEFRA@users.noreply.github.com> Date: Fri, 14 Apr 2023 14:53:04 +0100 Subject: [PATCH] Disable Airbrake remote_config (#14) https://eaflood.atlassian.net/browse/RUBY-2456 --- .rubocop.yml | 4 +++ .ruby-version | 2 +- Gemfile | 23 +++++++++++++ defra_ruby_alert.gemspec | 34 ++++--------------- lib/defra_ruby/alert/airbrake_runner.rb | 4 +++ spec/defra_ruby/alert/airbrake_runner_spec.rb | 29 +++++++++++----- spec/defra_ruby/alert/configuration_spec.rb | 18 +++++----- spec/defra_ruby/alert_spec.rb | 15 ++++---- 8 files changed, 74 insertions(+), 55 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0cd780f..0919b1b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,7 @@ inherit_gem: defra_ruby_style: - default.yml +require: + - rubocop-rails + - rubocop-rake + - rubocop-rspec diff --git a/.ruby-version b/.ruby-version index bff6ce5..7bde84d 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.7.1 +ruby-3.1.2 diff --git a/Gemfile b/Gemfile index dbb0720..9c5520e 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,26 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in defra_ruby_validators.gemspec gemspec + +group :development, :test do + gem "defra_ruby_style" + # Shim to load environment variables from a .env file into ENV + gem "dotenv" + # Allows us to automatically generate the change log from the tags, issues, + # labels and pull requests on GitHub. Added as a dependency so all dev's have + # access to it to generate a log, and so they are using the same version. + # New dev's should first create GitHub personal app token and add it to their + # ~/.bash_profile (or equivalent) + # https://github.com/github-changelog-generator/github-changelog-generator#github-token + gem "github_changelog_generator" + # Adds step-by-step debugging and stack navigation capabilities to pry using byebug + gem "pry-byebug" + gem "rake" + gem "rspec", "~> 3.0" + gem "rubocop" + gem "rubocop-rails" + gem "rubocop-rake" + gem "rubocop-rspec" + gem "simplecov", "~> 0.17.1" + gem "webmock", "~> 3.4" +end diff --git a/defra_ruby_alert.gemspec b/defra_ruby_alert.gemspec index 1097467..fd4d373 100644 --- a/defra_ruby_alert.gemspec +++ b/defra_ruby_alert.gemspec @@ -1,4 +1,3 @@ -# rubocop:disable Gemspec/RequiredRubyVersion # frozen_string_literal: true $LOAD_PATH.push File.expand_path("lib", __dir__) @@ -15,41 +14,20 @@ Gem::Specification.new do |spec| spec.homepage = "https://github.com/DEFRA/defra-ruby-alert" spec.summary = "Defra ruby on rails Alert gem" spec.description = "Provides a single source of functionality for initialising and managing Alert in our apps." + spec.required_ruby_version = ">= 3.1" spec.files = Dir["{bin,config,lib}/**/*", "LICENSE", "Rakefile", "README.md"] - spec.test_files = Dir["spec/**/*"] spec.require_paths = ["lib"] # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # to allow pushing to a single host or delete this section to allow pushing to any host. - if spec.respond_to?(:metadata) - spec.metadata["allowed_push_host"] = "https://rubygems.org" - else - raise "RubyGems 2.0 or newer is required to protect against " \ - "public gem pushes." - end + raise "RubyGems 2.0 or newer is required to protect against public gem pushes." unless spec.respond_to?(:metadata) + + spec.metadata["allowed_push_host"] = "https://rubygems.org" + + spec.metadata["rubygems_mfa_required"] = "true" # Alert catches exceptions, sends them to our Errbit instances spec.add_dependency "airbrake" - - spec.add_development_dependency "defra_ruby_style" - # Shim to load environment variables from a .env file into ENV - spec.add_development_dependency "dotenv" - # Allows us to automatically generate the change log from the tags, issues, - # labels and pull requests on GitHub. Added as a dependency so all dev's have - # access to it to generate a log, and so they are using the same version. - # New dev's should first create GitHub personal app token and add it to their - # ~/.bash_profile (or equivalent) - # https://github.com/github-changelog-generator/github-changelog-generator#github-token - spec.add_development_dependency "github_changelog_generator" - # Adds step-by-step debugging and stack navigation capabilities to pry using - # byebug - spec.add_development_dependency "pry-byebug" - spec.add_development_dependency "rake" - spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "rubocop" - spec.add_development_dependency "simplecov", "~> 0.17.1" - spec.add_development_dependency "webmock", "~> 3.4" end -# rubocop:enable Gemspec/RequiredRubyVersion diff --git a/lib/defra_ruby/alert/airbrake_runner.rb b/lib/defra_ruby/alert/airbrake_runner.rb index 4465a13..4e290cb 100644 --- a/lib/defra_ruby/alert/airbrake_runner.rb +++ b/lib/defra_ruby/alert/airbrake_runner.rb @@ -71,6 +71,10 @@ def self.invoke # errors in our logs. To prevent this we disable this feature. # https://github.com/airbrake/airbrake-ruby#performance_stats c.performance_stats = false + + # Prevent Airbrake from trying to auto-load remote config from Airbrake servers + # as this fails (our projects are not on Airbrake) and disables Airbrake in the app + c.remote_config = false end end end diff --git a/spec/defra_ruby/alert/airbrake_runner_spec.rb b/spec/defra_ruby/alert/airbrake_runner_spec.rb index d632a1a..fd3acd3 100644 --- a/spec/defra_ruby/alert/airbrake_runner_spec.rb +++ b/spec/defra_ruby/alert/airbrake_runner_spec.rb @@ -21,43 +21,54 @@ module Alert :environment= => nil, :ignore_environments= => nil, :blocklist_keys= => nil, - :performance_stats= => nil + :performance_stats= => nil, + :remote_config= => nil ) allow(configuration).to receive(:performance_stats) allow(Airbrake).to receive(:configure).and_yield(configuration) end - let(:configuration) { double("Configuration") } + let(:configuration) { instance_double("Configuration") } # rubocop:disable RSpec/VerifiedDoubleReference let(:enabled) { true } let(:host) { "http://localhost:8005" } let(:project_key) { "ABC123456789" } describe ".invoke" do - it "configures Airbrake" do - expect(configuration).to receive(:host=).with(host) - expect(configuration).to receive(:project_key=).with(project_key) + before { allow(Airbrake).to receive(:add_filter) } + it "configures Airbrake with host" do described_class.invoke + + expect(configuration).to have_received(:host=).with(host) + end + + it "configures Airbrake with project_key" do + described_class.invoke + + expect(configuration).to have_received(:project_key=).with(project_key) end context "when airbrake is enabled" do it "does not tell Airbrake to ignore everything" do - expect(Airbrake).not_to receive(:add_filter) - described_class.invoke + + expect(Airbrake).not_to have_received(:add_filter) end end context "when airbrake is not enabled" do let(:enabled) { false } + # rubocop:disable RSpec/MultipleExpectations it "tells Airbrake to ignore everything" do - expect(Airbrake).to receive(:add_filter) { |&block| expect(block).to be(&:ignore!) } - described_class.invoke + + expect(Airbrake).to have_received(:add_filter) { |&block| expect(block).to be(&:ignore!) } end + # rubocop:enable RSpec/MultipleExpectations + end end diff --git a/spec/defra_ruby/alert/configuration_spec.rb b/spec/defra_ruby/alert/configuration_spec.rb index df9ed0d..a7631c8 100644 --- a/spec/defra_ruby/alert/configuration_spec.rb +++ b/spec/defra_ruby/alert/configuration_spec.rb @@ -5,18 +5,16 @@ module DefraRuby module Alert RSpec.describe Configuration do - it "sets the appropriate default config settings" do - fresh_config = described_class.new - expect(fresh_config.root_directory).to be_nil - expect(fresh_config.logger).to be_nil - expect(fresh_config.environment).to be_nil + subject(:fresh_config) { described_class.new } - expect(fresh_config.host).to be_nil - expect(fresh_config.project_key).to be_nil - expect(fresh_config.blocklist).to eq([]) - expect(fresh_config.enabled).to eq(false) - end + it { expect(fresh_config.root_directory).to be_nil } + it { expect(fresh_config.logger).to be_nil } + it { expect(fresh_config.environment).to be_nil } + it { expect(fresh_config.host).to be_nil } + it { expect(fresh_config.project_key).to be_nil } + it { expect(fresh_config.blocklist).to eq([]) } + it { expect(fresh_config.enabled).to be(false) } end end end diff --git a/spec/defra_ruby/alert_spec.rb b/spec/defra_ruby/alert_spec.rb index e431fb7..26fc345 100644 --- a/spec/defra_ruby/alert_spec.rb +++ b/spec/defra_ruby/alert_spec.rb @@ -4,10 +4,8 @@ RSpec.describe DefraRuby::Alert do describe "VERSION" do - it "is a version string in the correct format" do - expect(DefraRuby::Alert::VERSION).to be_a(String) - expect(DefraRuby::Alert::VERSION).to match(/\d+\.\d+\.\d+/) - end + it { expect(DefraRuby::Alert::VERSION).to be_a(String) } + it { expect(DefraRuby::Alert::VERSION).to match(/\d+\.\d+\.\d+/) } end describe ".configuration" do @@ -30,16 +28,19 @@ describe ".start" do before do - DefraRuby::Alert.configure do |c| + allow(DefraRuby::Alert::AirbrakeRunner).to receive(:invoke) + + described_class.configure do |c| c.enabled = false c.host = "http://localhost:8005" c.project_key = "ABC123456789" end end - it "invokes 'AirbrakeRunner'" do - expect(DefraRuby::Alert::AirbrakeRunner).to receive(:invoke) + it "invokes 'AirbrakeRunner'" do described_class.start + + expect(DefraRuby::Alert::AirbrakeRunner).to have_received(:invoke) end end end