diff --git a/.rubocop_todo/gitlab/avoid_current_organization.yml b/.rubocop_todo/gitlab/avoid_current_organization.yml new file mode 100644 index 0000000000000000000000000000000000000000..59158f4ddf96e18b79f4dd1cc15d88d6bc456847 --- /dev/null +++ b/.rubocop_todo/gitlab/avoid_current_organization.yml @@ -0,0 +1,7 @@ +--- +Gitlab/AvoidCurrentOrganization: + Details: grace period + Exclude: + - 'app/services/snippets/create_service.rb' + - 'lib/gitlab/current_settings.rb' + - 'lib/gitlab/github_gists_import/importer/gist_importer.rb' diff --git a/rubocop/cop/gitlab/avoid_current_organization.rb b/rubocop/cop/gitlab/avoid_current_organization.rb index c355c823d87b4f4f7e4b454c5f5f801619ad3d89..57d64d0d05ac1a92cde04d6a592b1dc20eff1e8a 100644 --- a/rubocop/cop/gitlab/avoid_current_organization.rb +++ b/rubocop/cop/gitlab/avoid_current_organization.rb @@ -38,15 +38,12 @@ class AvoidCurrentOrganization < RuboCop::Cop::Base MSG = 'Avoid the use of `%{name}` outside of approved application layers. ' \ 'Instead, pass the value down to those layers. ' \ 'See https://gitlab.com/gitlab-org/gitlab/-/issues/442751.' - RESTRICT_ON_SEND = %i[ - organization organization= - ].freeze # @!method current_organization?(node) def_node_matcher :current_organization?, <<~PATTERN (send (const - {nil? (cbase)} :Current) {:organization | :organization=} ...) + {nil? (cbase)} :Current) {:organization | :organization_id | :organization=} ...) PATTERN def on_send(node) diff --git a/spec/rubocop/cop/gitlab/avoid_current_organization_spec.rb b/spec/rubocop/cop/gitlab/avoid_current_organization_spec.rb index 5d86e48b030d5ab0ee7dd746000fac7e021ff98c..dadbaac35230912ee5767dc80364fd1b91f7c32e 100644 --- a/spec/rubocop/cop/gitlab/avoid_current_organization_spec.rb +++ b/spec/rubocop/cop/gitlab/avoid_current_organization_spec.rb @@ -6,9 +6,7 @@ RSpec.describe RuboCop::Cop::Gitlab::AvoidCurrentOrganization, feature_category: :cell do describe 'bad examples' do - let(:node_value) { 'Current.organization' } - - context 'when referencing' do + shared_examples 'reference offense' do it 'registers an offense' do expect_offense(<<~CODE, node: node_value) return if %{node} @@ -17,7 +15,21 @@ end end - context 'when assigning' do + context 'when referencing Current.organization' do + let(:node_value) { 'Current.organization' } + + include_examples 'reference offense' + end + + context 'when referencing Current.organization_id' do + let(:node_value) { 'Current.organization_id' } + + include_examples 'reference offense' + end + + context 'when assigning Current.organization=' do + let(:node_value) { 'Current.organization' } + it 'registers an offense' do expect_offense(<<~CODE, keyword: node_value) %{keyword} = some_value