diff --git a/ee/db/seeds/data_seeder/vishal_seed.rb b/ee/db/seeds/data_seeder/vishal_seed.rb new file mode 100644 index 0000000000000000000000000000000000000000..6c8d18e90d2dbe7a26471e4c000fcc1b4d995d7d --- /dev/null +++ b/ee/db/seeds/data_seeder/vishal_seed.rb @@ -0,0 +1,207 @@ +# frozen_string_literal: true + +class DataSeeder + def seed + puts "################### CLEANING UP ORPHANED WITH FACTORY ###################" + cleanup_orphaned_ci_mirrors + + puts "################### CREATING USER WITH FACTORY ###################" + @user = create_user_with_factory + + puts "################### CREATING THE GROUP WITH FACTORY ###################" + @group = create_group_with_factory + + puts "################### CREATING THE PROJECT WITH FACTORY ###################" + @project = create_project_with_factory + + puts "################### CREATING THE REPOSITORY WITH FACTORY ###################" + create_repository_for_project + + puts "################### CREATING THE GROUP LABELS WITH FACTORY ###################" + create_group_labels_with_factories + + puts "################### CREATING MERGE REQUEST WITH FACTORY ###################" + create_merge_request_with_factory + end + + private + + def cleanup_orphaned_ci_mirrors + # Remove CI namespace mirror for namespace_id=2 specifically if it exists + Ci::NamespaceMirror.where(namespace_id: 2).delete_all + + # Also clean up any truly orphaned mirrors + orphaned_mirrors = Ci::NamespaceMirror.joins("LEFT JOIN namespaces ON namespaces.id = ci_namespace_mirrors.namespace_id") + .where("namespaces.id IS NULL") + + if orphaned_mirrors.any? + puts "Cleaning up #{orphaned_mirrors.count} orphaned CI namespace mirrors..." + orphaned_mirrors.delete_all + end + end + + def create_user_with_factory + existing_user = User.find_by(username: 'vishal_dev') + return existing_user if existing_user + + create(:user, + name: 'Vishal Developer', + username: 'vishal_dev', + email: "vishal_dev_#{SecureRandom.hex(8)}@example.com" + ) + end + + def create_group_with_factory + group_path = "vishal-group-#{SecureRandom.hex(4)}" + + existing_group = Group.find_by(path: group_path) + return existing_group if existing_group + + # Build the group without triggering callbacks, then save + group = build(:group, + name: 'Vishal Group', + path: group_path, + description: 'A group for Vishal testing', + visibility_level: Gitlab::VisibilityLevel::PRIVATE + ) + + # Save the group first + group.save! + + # Manually create CI namespace mirror if it doesn't exist + unless Ci::NamespaceMirror.exists?(namespace_id: group.id) + Ci::NamespaceMirror.create!(namespace_id: group.id) + end + + group.add_owner(@user) + group + end + + def create_project_with_factory + project_path = "vishal-project-#{SecureRandom.hex(4)}" + + existing_project = @group.projects.find_by(path: project_path) + return existing_project if existing_project + + create(:project, + name: 'Vishal Project', + path: project_path, + namespace: @group, + description: 'A test project for Vishal with dummy repository', + visibility_level: Gitlab::VisibilityLevel::PRIVATE, + creator: @user + ) + end + + def create_repository_for_project + unless @project.repository_exists? + @project.create_repository + + @project.repository.create_file( + @user, + 'README.md', + "# #{@project.name}\n\nWelcome to Vishal's Project!", + message: 'Initial commit', + branch_name: @project.default_branch || 'main' + ) + end + end + + def create_group_labels_with_factories + labels_data = [ + { title: 'priority::1', color: '#FF0000' }, + { title: 'priority::2', color: '#DD0000' }, + { title: 'priority::3', color: '#CC0000' }, + { title: 'priority::4', color: '#CC1111' } + ] + + labels_data.each do |label_data| + create_group_label_with_factory(label_data[:title], label_data[:color]) + end + end + + def create_group_label_with_factory(title, color) + return if @group.labels.exists?(title: title) + + create(:group_label, + group: @group, + title: title, + color: color + ) + end + + def create_merge_request_with_factory + existing_mr = @project.merge_requests.find_by( + source_branch: 'feature/vishal-feature', + target_branch: @project.default_branch || 'main' + ) + return existing_mr if existing_mr + + create_feature_branch_with_changes + + create(:merge_request, + title: 'Add Vishal feature implementation', + description: 'This MR adds a new feature for Vishal with documentation and tests', + source_project: @project, + target_project: @project, + source_branch: 'feature/vishal-feature', + target_branch: @project.default_branch || 'main', + author: @user + ) + end + + def create_feature_branch_with_changes + return if @project.repository.branch_exists?('feature/vishal-feature') + + default_branch = @project.default_branch || 'main' + @project.repository.create_branch('feature/vishal-feature', default_branch) + + files_to_create = [ + { + path: 'lib/vishal_feature.rb', + content: <<~RUBY, + # frozen_string_literal: true + + class VishalFeature + def initialize(name) + @name = name + end + + def greet + "Hello, \#{@name}! This is Vishal's feature." + end + end + RUBY + message: 'Add VishalFeature class' + }, + { + path: 'spec/vishal_feature_spec.rb', + content: <<~RUBY, + # frozen_string_literal: true + + require 'spec_helper' + + RSpec.describe VishalFeature do + describe '#greet' do + it 'returns a greeting from Vishal' do + feature = VishalFeature.new('World') + expect(feature.greet).to eq('Hello, World! This is Vishal\'s feature.') + end + end + end + RUBY + message: 'Add tests for VishalFeature' + } + ] + + files_to_create.each do |file_data| + @project.repository.create_file( + @user, + file_data[:path], + file_data[:content], + message: file_data[:message], + branch_name: 'feature/vishal-feature' + ) + end + end +end