From f4ea767810aa0d6e96b5a24cee6183024732ff49 Mon Sep 17 00:00:00 2001 From: Jessie Young Date: Mon, 8 Sep 2025 14:27:13 -0700 Subject: [PATCH] Fix Duo seed script for SM * This rake task is used for seeding data/licenses for Duo features locally * Previously, in self-managed mode this would fail if the group already existed. * https://gitlab.com/gitlab-org/gitlab-development-kit/-/issues/2767 --- doc/development/ai_features/_index.md | 4 +++ doc/development/development_seed_files.md | 6 ++-- ee/lib/gitlab/duo/developments/setup.rb | 8 +++-- .../lib/gitlab/duo/developments/setup_spec.rb | 33 ++++++++++++++----- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/doc/development/ai_features/_index.md b/doc/development/ai_features/_index.md index e88186d0c1a32e..50b1b62430bf0c 100644 --- a/doc/development/ai_features/_index.md +++ b/doc/development/ai_features/_index.md @@ -70,6 +70,10 @@ After following the steps in the [setup](https://gitlab.com/gitlab-org/gitlab-de ### Run `gitlab:duo:setup` script +Note: this task is idempotent and skips reseeding if the `gitlab-duo` group +already exists. To force reseeding from this task, set `GITLAB_DUO_RESEED=1`. +For details on the seeds used, see [Development seed files](../development_seed_files.md#seed-project-and-group-resources-for-gitlab-duo). + This ensures that your instance or group has the correct licenses, settings, and feature flags to test Duo features locally. Below are several options. If you are unsure, use option 1. 1. GitLab.com (SaaS) mode diff --git a/doc/development/development_seed_files.md b/doc/development/development_seed_files.md index cc17ab3755a355..2ca50572571bb5 100644 --- a/doc/development/development_seed_files.md +++ b/doc/development/development_seed_files.md @@ -30,9 +30,9 @@ data for features. ### Seed project and group resources for GitLab Duo -The [`gitlab:duo:setup` setup script](ai_features/_index.md#run-gitlabduosetup-script) will execute -the development seed file for GitLab Duo project and group resources. -However, if you would like to re-create the resources, you can re-run the seed task using the command: +The [`gitlab:duo:setup` setup script](ai_features/_index.md#run-gitlabduosetup-script) executes the development seed file for GitLab Duo project and group resources. In self-managed mode, the task is idempotent and skips reseeding if the `gitlab-duo` group already exists. To force reseeding from the setup task, set `GITLAB_DUO_RESEED=1`. + +To run the seed directly (outside the setup task) and recreate all resources: ```shell SEED_GITLAB_DUO=1 FILTER=gitlab_duo bundle exec rake db:seed_fu diff --git a/ee/lib/gitlab/duo/developments/setup.rb b/ee/lib/gitlab/duo/developments/setup.rb index 77f46998bd543d..0a276e6735cada 100644 --- a/ee/lib/gitlab/duo/developments/setup.rb +++ b/ee/lib/gitlab/duo/developments/setup.rb @@ -5,12 +5,15 @@ module Gitlab module Duo module Developments def self.seed_data(namespace) - if Group.find_by_full_path(namespace) + return if namespace.blank? + + if Group.find_by_full_path(namespace) && !::Gitlab::Utils.to_boolean(ENV['GITLAB_DUO_RESEED']) puts <<~TXT.strip ================================================================================ ## Gitlab Duo test group and project already seeded ## If you want to destroy and re-create them, you can re-run the seed task ## SEED_GITLAB_DUO=1 FILTER=gitlab_duo bundle exec rake db:seed_fu + ## Or set GITLAB_DUO_RESEED=1 to force reseeding via this setup task ## See https://docs.gitlab.com/development/development_seed_files/#seed-project-and-group-resources-for-gitlab-duo ================================================================================ TXT @@ -19,6 +22,7 @@ def self.seed_data(namespace) puts "Seeding GitLab Duo data..." ENV['FILTER'] = 'gitlab_duo' ENV['SEED_GITLAB_DUO'] = '1' + Rake::Task['db:seed_fu'].reenable Rake::Task['db:seed_fu'].invoke end end @@ -195,7 +199,7 @@ def execute setup_strategy = if ::Gitlab::Utils.to_boolean(ENV['GITLAB_SIMULATE_SAAS']) GitlabComStrategy.new(@namespace, @args) else - SelfManagedStrategy.new(nil, @args) + SelfManagedStrategy.new(@namespace, @args) end ensure_dev_mode! diff --git a/ee/spec/lib/gitlab/duo/developments/setup_spec.rb b/ee/spec/lib/gitlab/duo/developments/setup_spec.rb index 354ae358549e30..a33f089595c5e7 100644 --- a/ee/spec/lib/gitlab/duo/developments/setup_spec.rb +++ b/ee/spec/lib/gitlab/duo/developments/setup_spec.rb @@ -10,6 +10,7 @@ let!(:user) { create(:user, maintainer_of: project, username: 'root') } let(:task) { described_class.new(args) } + let(:namespace) { 'gitlab-duo' } let(:feature_flags) do [ @@ -187,26 +188,32 @@ before do allow(Rake::Task).to receive(:[]).with(any_args).and_return(rake_task) allow(rake_task).to receive(:invoke) + allow(rake_task).to receive(:reenable) allow($stdout).to receive(:puts) end context 'when Gitlab Duo data is not seeded' do + before do + allow(Group).to receive(:find_by_full_path).with(namespace).and_return(nil) + end + it 'prints a message indicating seeding is happening' do expect($stdout).to receive(:puts).with('Seeding GitLab Duo data...') - ::Gitlab::Duo::Developments.seed_data(nil) + ::Gitlab::Duo::Developments.seed_data(namespace) end - it 'invokes the db:seed_fu rake task' do - ::Gitlab::Duo::Developments.seed_data(nil) + it 'reenables and invokes the db:seed_fu rake task' do + ::Gitlab::Duo::Developments.seed_data(namespace) + expect(rake_task).to have_received(:reenable) expect(rake_task).to have_received(:invoke) end end context 'when Gitlab Duo data is already seeded' do before do - allow(Group).to receive(:find_by_full_path).with(nil).and_return(group) + allow(Group).to receive(:find_by_full_path).with(namespace).and_return(group) end let(:expected_already_seeded_message) do @@ -215,21 +222,29 @@ ## Gitlab Duo test group and project already seeded ## If you want to destroy and re-create them, you can re-run the seed task ## SEED_GITLAB_DUO=1 FILTER=gitlab_duo bundle exec rake db:seed_fu + ## Or set GITLAB_DUO_RESEED=1 to force reseeding via this setup task ## See https://docs.gitlab.com/development/development_seed_files/#seed-project-and-group-resources-for-gitlab-duo ================================================================================ TXT end - it 'prints a message indicating data is already seeded' do + it 'prints a message indicating data is already seeded and does not run seeds' do expect($stdout).to receive(:puts).with(expected_already_seeded_message) - ::Gitlab::Duo::Developments.seed_data(nil) + ::Gitlab::Duo::Developments.seed_data(namespace) + + expect(rake_task).not_to have_received(:invoke) end - it 'does not invoke the db:seed_fu rake task' do - ::Gitlab::Duo::Developments.seed_data(nil) + context 'when GITLAB_DUO_RESEED=1 is set' do + it 'forces reseeding' do + stub_env('GITLAB_DUO_RESEED', '1') - expect(rake_task).not_to have_received(:invoke) + ::Gitlab::Duo::Developments.seed_data(namespace) + + expect(rake_task).to have_received(:reenable) + expect(rake_task).to have_received(:invoke) + end end end end -- GitLab