From 45f652aa48e82419d85578100d40ef5735bdaff4 Mon Sep 17 00:00:00 2001
From: Alex Pooley <apooley@gitlab.com>
Date: Fri, 23 Aug 2024 02:52:17 +0000
Subject: [PATCH] Extend AvoidCurrentOrganization cop so it will also detect
 `Current.organization_id`

---
 .../gitlab/avoid_current_organization.yml     |  7 +++++++
 .../cop/gitlab/avoid_current_organization.rb  |  5 +----
 .../gitlab/avoid_current_organization_spec.rb | 20 +++++++++++++++----
 3 files changed, 24 insertions(+), 8 deletions(-)
 create mode 100644 .rubocop_todo/gitlab/avoid_current_organization.yml

diff --git a/.rubocop_todo/gitlab/avoid_current_organization.yml b/.rubocop_todo/gitlab/avoid_current_organization.yml
new file mode 100644
index 0000000000000..59158f4ddf96e
--- /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 c355c823d87b4..57d64d0d05ac1 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 5d86e48b030d5..dadbaac352309 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
-- 
GitLab