diff --git a/config/default.yml b/config/default.yml index 900180b..61cc65d 100644 --- a/config/default.yml +++ b/config/default.yml @@ -76,3 +76,9 @@ BugcrowdCops/PreventReindexFullESDocumentCop: Enabled: true Include: - 'app/**/*.rb' + +BugcrowdCops/PreventBugsnagUsage: + Enabled: true + Include: + - 'app/**/*.rb' + - 'lib/**/*.rb' diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb new file mode 100644 index 0000000..ad67f51 --- /dev/null +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Bugcrowd + class PreventBugsnagUsage < RuboCop::Cop::Base + MSG = 'Do not use Bugsnag in the codebase, as its integration has been removed. ' \ + 'Use ErrorTrackingService for reporting errors.' + + def on_send(node) + add_offense(node, message: MSG) if node.receiver&.const_name == 'Bugsnag' + end + end + end + end +end diff --git a/lib/rubocop/cop/bugcrowd_cops.rb b/lib/rubocop/cop/bugcrowd_cops.rb index 8816f52..881f741 100644 --- a/lib/rubocop/cop/bugcrowd_cops.rb +++ b/lib/rubocop/cop/bugcrowd_cops.rb @@ -38,3 +38,4 @@ require_relative 'bugcrowd/no_event_deprecated_publish' require_relative 'bugcrowd/sidekiq_testing_inline' require_relative 'bugcrowd/prevent_reindex_full_es_document_cop' +require_relative 'bugcrowd/prevent_bugsnag_usage' diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb new file mode 100644 index 0000000..115383c --- /dev/null +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage, :config do + subject(:cop) { described_class.new(config) } + + let(:message) do + 'Do not use Bugsnag in the codebase, as its integration has been removed. ' \ + 'Use ErrorTrackingService for reporting errors.' + end + + it 'registers an offense when Bugsnag is used' do + expect_offense(<<~RUBY) + Bugsnag.error('Error') + ^^^^^^^^^^^^^^^^^^^^^^ #{message} + RUBY + end + + it 'registers an offense when Bugsnag.notify is used' do + expect_offense(<<~RUBY) + Bugsnag.notify('Error') + ^^^^^^^^^^^^^^^^^^^^^^^ #{message} + RUBY + end + + it 'does not register an offense for ErrorTrackingService' do + expect_no_offenses(<<~RUBY) + ErrorTrackingService.notify('Error') + RUBY + end + + it 'does not register an offense for unrelated constants' do + expect_no_offenses(<<~RUBY) + SomeOtherService.notify('Error') + RUBY + end + + it 'does not register an offense for lowercase bugsnag' do + expect_no_offenses(<<~RUBY) + bugsnag.error('Error') + RUBY + end + + it 'does not register an offense for local variable named Bugsnag' do + expect_no_offenses(<<~RUBY) + bugsnag = SomeService.new + bugsnag.error('Error') + RUBY + end +end