diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-08-18 13:50:51 +0300 |
commit | db384e6b19af03b4c3c82a5760d83a3fd79f7982 (patch) | |
tree | 34beaef37df5f47ccbcf5729d7583aae093cffa0 /db/fixtures/development/38_ci_cd_analytics.rb | |
parent | 54fd7b1bad233e3944434da91d257fa7f63c3996 (diff) |
Add latest changes from gitlab-org/gitlab@16-3-stable-eev16.3.0-rc42
Diffstat (limited to 'db/fixtures/development/38_ci_cd_analytics.rb')
-rw-r--r-- | db/fixtures/development/38_ci_cd_analytics.rb | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/db/fixtures/development/38_ci_cd_analytics.rb b/db/fixtures/development/38_ci_cd_analytics.rb new file mode 100644 index 00000000000..12aab18c63b --- /dev/null +++ b/db/fixtures/development/38_ci_cd_analytics.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +require './ee/db/seeds/shared/dora_metrics' if Gitlab.ee? + +# Usage: +# +# Simple invocation always creates a new project within a group: +# +# FILTER=ci_cd_analytics SEED_CI_CD_ANALYTICS=1 bundle exec rake db:seed_fu + +# rubocop:disable Rails/Output +class Gitlab::Seeder::CiAnalytics # rubocop:disable Style/ClassAndModuleChildren + FLAG = 'SEED_CI_CD_ANALYTICS' + + def initialize + @project = create_new_project + end + + def seed! + seed_data! + end + + private + + attr_reader :project + + def seed_data! + Sidekiq::Worker.skipping_transaction_check do + create_dora_metrics! if Gitlab.ee? + create_pipelines! + create_releases! + + puts "Successfully seeded '#{project.full_path}' for CI/CD analytics!" + puts "URL: #{Rails.application.routes.url_helpers.project_url(project)}" + end + end + + def create_new_project + namespace = FactoryBot.create( + :group, + name: "CICD analytics Group #{suffix}", + path: "cicd-#{suffix}" + ) + project = FactoryBot.create( + :project, + :repository, + name: "CICD analytics Project #{suffix}", + path: "cicd-#{suffix}", + creator: admin, + namespace: namespace + ) + + namespace.add_owner(admin) + project.create_repository + project + end + + def create_dora_metrics! + Gitlab::Seeder::DoraMetrics.new(project: project).execute + end + + def create_pipelines! + branches = project.repository.branches.sample(15) + pipeline_statuses = [:success, :failed] + + branches.each do |branch| + FactoryBot.create( + :ci_pipeline, + pipeline_statuses.sample, + project: project, + ref: branch.name, + sha: branch.target, + created_at: random_past_date, + duration: rand(10).hours + ) + end + end + + def create_releases! + FactoryBot.create_list(:release, 3, project: project, author: admin) + end + + def admin + @admin ||= User.admins.first + end + + def suffix + @suffix ||= Time.now.to_i + end + + def random_past_date + rand(120).days.ago + end +end + +Gitlab::Seeder.quiet do + if ENV[Gitlab::Seeder::CiAnalytics::FLAG] + Gitlab::Seeder::CiAnalytics.new.seed! + else + puts "Skipped. Use the `#{Gitlab::Seeder::CiAnalytics::FLAG}` environment variable to enable." + end +end +# rubocop:enable Rails/Output |