From cf4f277b1508f5502de0d779c058ac4e62657932 Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Tue, 18 Mar 2025 11:06:33 +0530 Subject: [PATCH 1/8] [BC-26568] Added rubocop warning for the use of Bugsnag --- config/default.yml | 6 ++++ .../cop/bugcrowd/prevent_bugsnag_usage.rb | 35 +++++++++++++++++++ lib/rubocop/cop/bugcrowd_cops.rb | 1 + .../bugcrowd/prevent_bugsnag_usage_spec.rb | 33 +++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb create mode 100644 spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb 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..eb8709d --- /dev/null +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Bugcrowd + class PreventBugsnagUsage < Cop + MSG = 'Avoid using Bugsnag in the codebase. ' \ + 'It has been replaced with ErrorNotifierService for error ' \ + 'notification handling. Please use ErrorNotifierService ' \ + 'instead.' + + def_node_matcher :bugsnag_usage?, <<-PATTERN + (send + (const nil? :Bugsnag) :notify ...) + PATTERN + + def_node_matcher :bugsnag_constant?, <<-PATTERN + (const nil? :Bugsnag) + PATTERN + + def on_send(node) + if bugsnag_usage?(node) + add_offense(node, message: MSG) + end + end + + def on_const(node) + if bugsnag_constant?(node) + add_offense(node, message: MSG) + end + 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..0214e54 --- /dev/null +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage do + subject(:cop) { described_class.new(config) } + + let(:config) { RuboCop::Config.new } + + it 'registers an offense when Bugsnag.notify is used' do + expect_offense(<<~RUBY) + Bugsnag.notify('Error') + ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + RUBY + end + + it 'registers an offense when Bugsnag is used as a constant' do + expect_offense(<<~RUBY) + Bugsnag.error('Error') + ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + RUBY + end + + it 'does not register an offense when ErrorNotifierService is used' do + expect_no_offenses(<<~RUBY) + ErrorNotifierService.notify('Error') + RUBY + end + + it 'does not register an offense when no Bugsnag is used' do + expect_no_offenses(<<~RUBY) + SomeOtherService.notify('Error') + RUBY + end +end From 9b39efbe840b4878d782393932cd2b3e7c773a7b Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Tue, 18 Mar 2025 12:10:31 +0530 Subject: [PATCH 2/8] [BC-26568] Added rubocop warning for the use of Bugsnag --- lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb index eb8709d..30d2466 100644 --- a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -6,7 +6,7 @@ module Bugcrowd class PreventBugsnagUsage < Cop MSG = 'Avoid using Bugsnag in the codebase. ' \ 'It has been replaced with ErrorNotifierService for error ' \ - 'notification handling. Please use ErrorNotifierService ' \ + 'notification handling. Please use ErrorNotifierService' \ 'instead.' def_node_matcher :bugsnag_usage?, <<-PATTERN From 38a2f0138348dac96a130b7a4928b96ca224f125 Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Tue, 18 Mar 2025 12:36:44 +0530 Subject: [PATCH 3/8] [BC-26568] Added rubocop warning for the use of Bugsnag --- spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index 0214e54..9f39fcf 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -2,20 +2,20 @@ RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage do subject(:cop) { described_class.new(config) } - let(:config) { RuboCop::Config.new } it 'registers an offense when Bugsnag.notify is used' do expect_offense(<<~RUBY) Bugsnag.notify('Error') - ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + ^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. + ^^^^^^^^^^^^^^^^^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. RUBY end it 'registers an offense when Bugsnag is used as a constant' do expect_offense(<<~RUBY) Bugsnag.error('Error') - ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + ^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. RUBY end From 43c7f8deeafc6a9a599ba0dc9fca6434227f8dc7 Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Thu, 20 Mar 2025 16:24:30 +0530 Subject: [PATCH 4/8] Updated rspec --- .../cop/bugcrowd/prevent_bugsnag_usage.rb | 26 ++++++------------- .../bugcrowd/prevent_bugsnag_usage_spec.rb | 22 +++++----------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb index 30d2466..3a7bfb5 100644 --- a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -3,31 +3,21 @@ module RuboCop module Cop module Bugcrowd - class PreventBugsnagUsage < Cop + class PreventBugsnagUsage < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MSG = 'Avoid using Bugsnag in the codebase. ' \ 'It has been replaced with ErrorNotifierService for error ' \ - 'notification handling. Please use ErrorNotifierService' \ - 'instead.' - - def_node_matcher :bugsnag_usage?, <<-PATTERN - (send - (const nil? :Bugsnag) :notify ...) - PATTERN + 'notification handling. Please use ErrorNotifierService instead.' - def_node_matcher :bugsnag_constant?, <<-PATTERN - (const nil? :Bugsnag) + def_node_matcher :bugsnag_usage?, <<~PATTERN + (send (const nil? :Bugsnag) ...) PATTERN def on_send(node) - if bugsnag_usage?(node) - add_offense(node, message: MSG) - end - end + return unless bugsnag_usage?(node) - def on_const(node) - if bugsnag_constant?(node) - add_offense(node, message: MSG) - end + add_offense(node, message: MSG) end end end diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index 9f39fcf..ed70cbb 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -1,21 +1,19 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage do +RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage, :config do subject(:cop) { described_class.new(config) } - let(:config) { RuboCop::Config.new } - it 'registers an offense when Bugsnag.notify is used' do + it 'registers an offense when Bugsnag is used as a constant' do expect_offense(<<~RUBY) - Bugsnag.notify('Error') - ^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. - ^^^^^^^^^^^^^^^^^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. + Bugsnag.error('Error') + ^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. RUBY end - it 'registers an offense when Bugsnag is used as a constant' do + it 'registers an offense when Bugsnag.notify is used' do expect_offense(<<~RUBY) - Bugsnag.error('Error') - ^^^^^^^ Bugcrowd/PreventBugsnagUsage: Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierServiceinstead. + Bugsnag.notify('Error') + ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. RUBY end @@ -24,10 +22,4 @@ ErrorNotifierService.notify('Error') RUBY end - - it 'does not register an offense when no Bugsnag is used' do - expect_no_offenses(<<~RUBY) - SomeOtherService.notify('Error') - RUBY - end end From 3303775031786020ae68fc5d8159d72b89753758 Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Thu, 20 Mar 2025 16:37:53 +0530 Subject: [PATCH 5/8] Updated rspec --- .../cop/bugcrowd/prevent_bugsnag_usage.rb | 10 +---- .../bugcrowd/prevent_bugsnag_usage_spec.rb | 37 ++++++++++++++++--- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb index 3a7bfb5..21010ed 100644 --- a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -4,20 +4,12 @@ module RuboCop module Cop module Bugcrowd class PreventBugsnagUsage < RuboCop::Cop::Base - extend RuboCop::Cop::AutoCorrector - MSG = 'Avoid using Bugsnag in the codebase. ' \ 'It has been replaced with ErrorNotifierService for error ' \ 'notification handling. Please use ErrorNotifierService instead.' - def_node_matcher :bugsnag_usage?, <<~PATTERN - (send (const nil? :Bugsnag) ...) - PATTERN - def on_send(node) - return unless bugsnag_usage?(node) - - add_offense(node, message: MSG) + add_offense(node, message: MSG) if node.receiver&.const_name == 'Bugsnag' end end end diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index ed70cbb..45b7793 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -3,23 +3,48 @@ RSpec.describe RuboCop::Cop::Bugcrowd::PreventBugsnagUsage, :config do subject(:cop) { described_class.new(config) } - it 'registers an offense when Bugsnag is used as a constant' do - expect_offense(<<~RUBY) + let(:message) do + 'Avoid using Bugsnag in the codebase. ' \ + 'It has been replaced with ErrorNotifierService for error ' \ + 'notification handling. Please use ErrorNotifierService instead.' + end + + it 'registers an offense when Bugsnag is used' do + expect_offense(<<~RUBY, message:) Bugsnag.error('Error') - ^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + ^^^^^^^^^^^^^^^^^^^^^^ %{message} RUBY end it 'registers an offense when Bugsnag.notify is used' do - expect_offense(<<~RUBY) + expect_offense(<<~RUBY, message:) Bugsnag.notify('Error') - ^^^^^^^^^^^^^^^^^^^^^^^ Avoid using Bugsnag in the codebase. It has been replaced with ErrorNotifierService for error notification handling. Please use ErrorNotifierService instead. + ^^^^^^^^^^^^^^^^^^^^^^^ %{message} RUBY end - it 'does not register an offense when ErrorNotifierService is used' do + it 'does not register an offense for ErrorNotifierService' do expect_no_offenses(<<~RUBY) ErrorNotifierService.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 From 55b66393316d8506af7090e516c213286f5d866d Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Thu, 20 Mar 2025 16:41:34 +0530 Subject: [PATCH 6/8] Updated rspec --- spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index 45b7793..f7358ec 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -10,16 +10,16 @@ end it 'registers an offense when Bugsnag is used' do - expect_offense(<<~RUBY, message:) + expect_offense(<<~RUBY) Bugsnag.error('Error') - ^^^^^^^^^^^^^^^^^^^^^^ %{message} + ^^^^^^^^^^^^^^^^^^^^^^ #{message} RUBY end it 'registers an offense when Bugsnag.notify is used' do - expect_offense(<<~RUBY, message:) + expect_offense(<<~RUBY) Bugsnag.notify('Error') - ^^^^^^^^^^^^^^^^^^^^^^^ %{message} + ^^^^^^^^^^^^^^^^^^^^^^^ #{message} RUBY end From 585858acaad9643a7c351f0c7d378f91c8a6da31 Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Fri, 21 Mar 2025 10:54:27 +0530 Subject: [PATCH 7/8] Updated rspec --- lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb | 4 ++-- spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb index 21010ed..6bf4f1f 100644 --- a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -5,8 +5,8 @@ module Cop module Bugcrowd class PreventBugsnagUsage < RuboCop::Cop::Base MSG = 'Avoid using Bugsnag in the codebase. ' \ - 'It has been replaced with ErrorNotifierService for error ' \ - 'notification handling. Please use ErrorNotifierService instead.' + 'It has been replaced with ErrorTrackingService for error ' \ + 'notification handling. Please use ErrorTrackingService instead.' def on_send(node) add_offense(node, message: MSG) if node.receiver&.const_name == 'Bugsnag' diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index f7358ec..4221063 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -5,8 +5,8 @@ let(:message) do 'Avoid using Bugsnag in the codebase. ' \ - 'It has been replaced with ErrorNotifierService for error ' \ - 'notification handling. Please use ErrorNotifierService instead.' + 'It has been replaced with ErrorTrackingService for error ' \ + 'notification handling. Please use ErrorTrackingService instead.' end it 'registers an offense when Bugsnag is used' do @@ -23,9 +23,9 @@ RUBY end - it 'does not register an offense for ErrorNotifierService' do + it 'does not register an offense for ErrorTrackingService' do expect_no_offenses(<<~RUBY) - ErrorNotifierService.notify('Error') + ErrorTrackingService.notify('Error') RUBY end From 5dff8bcadff5db7e349f897f6c94d368afb419cb Mon Sep 17 00:00:00 2001 From: Pramod Mahadev bhat Date: Fri, 21 Mar 2025 13:25:18 +0530 Subject: [PATCH 8/8] Updating rspec --- lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb | 5 ++--- spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb index 6bf4f1f..ad67f51 100644 --- a/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb +++ b/lib/rubocop/cop/bugcrowd/prevent_bugsnag_usage.rb @@ -4,9 +4,8 @@ module RuboCop module Cop module Bugcrowd class PreventBugsnagUsage < RuboCop::Cop::Base - MSG = 'Avoid using Bugsnag in the codebase. ' \ - 'It has been replaced with ErrorTrackingService for error ' \ - 'notification handling. Please use ErrorTrackingService instead.' + 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' diff --git a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb index 4221063..115383c 100644 --- a/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb +++ b/spec/rubocop/cop/bugcrowd/prevent_bugsnag_usage_spec.rb @@ -4,9 +4,8 @@ subject(:cop) { described_class.new(config) } let(:message) do - 'Avoid using Bugsnag in the codebase. ' \ - 'It has been replaced with ErrorTrackingService for error ' \ - 'notification handling. Please use ErrorTrackingService instead.' + '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