From 0b8c845143e95a892da1a6b875d8da3bcb6590a4 Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Fri, 14 Nov 2025 13:13:52 -0500 Subject: [PATCH 1/3] Pass organization_id to SnippetsFinder in controllers Add organization_id parameter to SnippetsFinder initialization across all snippet controllers, positioning it immediately after current_user to prepare for it becoming required. Uses Current.organization.id and updates relevant specs with :with_current_organization metadata. --- app/controllers/dashboard/snippets_controller.rb | 3 ++- app/controllers/explore/snippets_controller.rb | 2 +- app/controllers/projects/snippets_controller.rb | 3 ++- app/controllers/users_controller.rb | 3 ++- spec/controllers/explore/snippets_controller_spec.rb | 2 +- spec/controllers/projects/snippets_controller_spec.rb | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/dashboard/snippets_controller.rb b/app/controllers/dashboard/snippets_controller.rb index 3632deb7d3c994..6c1ae6565a00ab 100644 --- a/app/controllers/dashboard/snippets_controller.rb +++ b/app/controllers/dashboard/snippets_controller.rb @@ -14,7 +14,8 @@ def index .new(current_user, author: current_user) .execute - @snippets = SnippetsFinder.new(current_user, author: current_user, scope: params[:scope], sort: sort_param) + @snippets = SnippetsFinder.new(current_user, organization_id: Current.organization.id, author: current_user, + scope: params[:scope], sort: sort_param) .execute .page(pagination_params[:page]) .inc_author diff --git a/app/controllers/explore/snippets_controller.rb b/app/controllers/explore/snippets_controller.rb index c788909ab498e8..86cdcfe8f2dad6 100644 --- a/app/controllers/explore/snippets_controller.rb +++ b/app/controllers/explore/snippets_controller.rb @@ -6,7 +6,7 @@ class Explore::SnippetsController < Explore::ApplicationController feature_category :source_code_management def index - @snippets = SnippetsFinder.new(current_user, explore: true) + @snippets = SnippetsFinder.new(current_user, organization_id: Current.organization.id, explore: true) .execute .page(pagination_params[:page]) .without_count diff --git a/app/controllers/projects/snippets_controller.rb b/app/controllers/projects/snippets_controller.rb index 76e2da6eb57d03..f77b69081eb99b 100644 --- a/app/controllers/projects/snippets_controller.rb +++ b/app/controllers/projects/snippets_controller.rb @@ -21,7 +21,8 @@ def index .new(current_user, project: @project) .execute - @snippets = SnippetsFinder.new(current_user, project: @project, scope: params[:scope], sort: sort_param) + @snippets = SnippetsFinder.new(current_user, organization_id: Current.organization.id, project: @project, + scope: params[:scope], sort: sort_param) .execute .page(params[:page]) .inc_author diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 38de86416be16a..cdcd1404b24023 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -305,7 +305,8 @@ def load_groups end def load_snippets - @snippets = SnippetsFinder.new(current_user, author: user, scope: params[:scope]) + @snippets = SnippetsFinder.new(current_user, organization_id: Current.organization.id, author: user, + scope: params[:scope]) .execute .page(params[:page]) .inc_author diff --git a/spec/controllers/explore/snippets_controller_spec.rb b/spec/controllers/explore/snippets_controller_spec.rb index e93e8502dfcc34..9f47833caf1b8f 100644 --- a/spec/controllers/explore/snippets_controller_spec.rb +++ b/spec/controllers/explore/snippets_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Explore::SnippetsController do +RSpec.describe Explore::SnippetsController, :with_current_organization do describe 'GET #index' do let!(:project_snippet) { create_list(:project_snippet, 3, :public) } let!(:personal_snippet) { create_list(:personal_snippet, 3, :public) } diff --git a/spec/controllers/projects/snippets_controller_spec.rb b/spec/controllers/projects/snippets_controller_spec.rb index 3b52f4dcf8ad62..ca2923f4ec13b2 100644 --- a/spec/controllers/projects/snippets_controller_spec.rb +++ b/spec/controllers/projects/snippets_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Projects::SnippetsController do +RSpec.describe Projects::SnippetsController, :with_current_organization do include Gitlab::Routing let_it_be(:user) { create(:user) } -- GitLab From 24d35107cc559068412580a8824be2398db60b6b Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Mon, 17 Nov 2025 22:29:10 -0500 Subject: [PATCH 2/3] Rename Snippet scope for_user_organization to in_organization Rename the scope from for_user_organization to in_organization for better clarity. The new name more accurately describes what the scope does: it returns snippets within the organization scope (all project snippets plus personal snippets from the given organization). The old name 'for_user_organization' was misleading as it takes an organization_id parameter, not a user object, and implies a user context that doesn't exist at the method signature level. Also adds missing feature_category metadata to two controller specs that were flagged during test runs. --- app/finders/snippets_finder.rb | 2 +- app/models/snippet.rb | 2 +- spec/controllers/explore/snippets_controller_spec.rb | 2 +- spec/controllers/projects/snippets_controller_spec.rb | 2 +- spec/models/snippet_spec.rb | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb index 6e46d67ed3f51d..b67ba3cd1b0c73 100644 --- a/app/finders/snippets_finder.rb +++ b/app/finders/snippets_finder.rb @@ -224,7 +224,7 @@ def by_ids(snippets) def by_organization(snippets) return snippets unless should_apply_organization_filter? - snippets.for_user_organization(organization_id) + snippets.in_organization(organization_id) end def should_apply_organization_filter? diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 9f193f47cfac0b..696ca2ccb6e7b4 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -82,7 +82,7 @@ def content_html_invalidated? scope :with_repository_storage_moves, -> { joins(:repository_storage_moves) } scope :inc_projects_namespace_route, -> { includes(project: [:route, :namespace]) } - scope :for_user_organization, ->(organization_id) do + scope :in_organization, ->(organization_id) do where.not(project_id: nil).or(where(project_id: nil, organization_id: organization_id)) end diff --git a/spec/controllers/explore/snippets_controller_spec.rb b/spec/controllers/explore/snippets_controller_spec.rb index 9f47833caf1b8f..d1577bc7fb4bdf 100644 --- a/spec/controllers/explore/snippets_controller_spec.rb +++ b/spec/controllers/explore/snippets_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Explore::SnippetsController, :with_current_organization do +RSpec.describe Explore::SnippetsController, :with_current_organization, feature_category: :source_code_management do describe 'GET #index' do let!(:project_snippet) { create_list(:project_snippet, 3, :public) } let!(:personal_snippet) { create_list(:personal_snippet, 3, :public) } diff --git a/spec/controllers/projects/snippets_controller_spec.rb b/spec/controllers/projects/snippets_controller_spec.rb index ca2923f4ec13b2..6f2afa27f660bf 100644 --- a/spec/controllers/projects/snippets_controller_spec.rb +++ b/spec/controllers/projects/snippets_controller_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Projects::SnippetsController, :with_current_organization do +RSpec.describe Projects::SnippetsController, :with_current_organization, feature_category: :source_code_management do include Gitlab::Routing let_it_be(:user) { create(:user) } diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 48d0a15e17a54d..543890c79a79ff 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -42,7 +42,7 @@ end end - describe '.for_user_organization' do + describe '.in_organization' do let_it_be(:organization) { create(:organization) } let_it_be(:other_organization) { create(:organization) } let_it_be(:project) { create(:project, organization: organization) } @@ -56,7 +56,7 @@ let_it_be(:project_snippet) { create(:project_snippet, project: project) } let_it_be(:project_snippet_other_org) { create(:project_snippet, project: other_project) } - subject { described_class.for_user_organization(organization.id) } + subject { described_class.in_organization(organization.id) } it 'returns personal snippets for the specified organization' do expect(subject).to include(personal_snippet_in_org) -- GitLab From f39a180b472b9f48698f1fb97d0999f765119d63 Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Wed, 19 Nov 2025 02:54:47 -0500 Subject: [PATCH 3/3] Fix specs by adding :with_current_organization --- ee/spec/features/search/elastic/snippet_search_spec.rb | 2 +- spec/features/dashboard/shortcuts_spec.rb | 2 +- spec/features/snippets/explore_spec.rb | 2 +- spec/features/users/snippets_spec.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ee/spec/features/search/elastic/snippet_search_spec.rb b/ee/spec/features/search/elastic/snippet_search_spec.rb index 6a7302e1a32197..e4fc321d61ba37 100644 --- a/ee/spec/features/search/elastic/snippet_search_spec.rb +++ b/ee/spec/features/search/elastic/snippet_search_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Snippet elastic search', :js, :elastic_delete_by_query, :aggregate_failures, feature_category: :global_search do +RSpec.describe 'Snippet elastic search', :js, :elastic_delete_by_query, :aggregate_failures, :with_current_organization, feature_category: :global_search do let_it_be(:public_project) { create(:project, :public) } let_it_be(:regular_user) { create(:user) } let_it_be(:authorized_user) { create(:user) } diff --git a/spec/features/dashboard/shortcuts_spec.rb b/spec/features/dashboard/shortcuts_spec.rb index 6066bd57d01806..a9fc319c1234ec 100644 --- a/spec/features/dashboard/shortcuts_spec.rb +++ b/spec/features/dashboard/shortcuts_spec.rb @@ -58,7 +58,7 @@ end end - context 'logged out' do + context 'logged out', :with_current_organization do before do visit explore_root_path end diff --git a/spec/features/snippets/explore_spec.rb b/spec/features/snippets/explore_spec.rb index 139866b5b0b04f..a38641c9c671d1 100644 --- a/spec/features/snippets/explore_spec.rb +++ b/spec/features/snippets/explore_spec.rb @@ -58,7 +58,7 @@ end end - context 'when user is not authenticated' do + context 'when user is not authenticated', :with_current_organization do before do visit explore_snippets_path end diff --git a/spec/features/users/snippets_spec.rb b/spec/features/users/snippets_spec.rb index 7f73027ede364e..67a093cae2a534 100644 --- a/spec/features/users/snippets_spec.rb +++ b/spec/features/users/snippets_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe 'Snippets tab on a user profile', :js, feature_category: :source_code_management do +RSpec.describe 'Snippets tab on a user profile', :with_current_organization, :js, feature_category: :source_code_management do context 'when the user has snippets' do let(:user) { create(:user) } -- GitLab