From 64bf46af38190bf24569add6095a9a8bbced0a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Mon, 11 Mar 2019 16:16:58 +0100 Subject: Add labels to seeded issues and merge requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- db/fixtures/development/02_settings.rb | 8 ++ db/fixtures/development/03_project.rb | 137 ++++++++++++++++++++++ db/fixtures/development/03_settings.rb | 8 -- db/fixtures/development/04_labels.rb | 49 ++++++++ db/fixtures/development/04_project.rb | 137 ---------------------- db/fixtures/development/09_issues.rb | 6 +- db/fixtures/development/10_merge_requests.rb | 6 +- db/fixtures/development/22_labeled_issues_seed.rb | 103 ---------------- 8 files changed, 204 insertions(+), 250 deletions(-) create mode 100644 db/fixtures/development/02_settings.rb create mode 100644 db/fixtures/development/03_project.rb delete mode 100644 db/fixtures/development/03_settings.rb create mode 100644 db/fixtures/development/04_labels.rb delete mode 100644 db/fixtures/development/04_project.rb delete mode 100644 db/fixtures/development/22_labeled_issues_seed.rb diff --git a/db/fixtures/development/02_settings.rb b/db/fixtures/development/02_settings.rb new file mode 100644 index 00000000000..3a4a5d436bf --- /dev/null +++ b/db/fixtures/development/02_settings.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Enable hashed storage, in development mode, for all projects by default. +Gitlab::Seeder.quiet do + ApplicationSetting.create_from_defaults unless ApplicationSetting.current_without_cache + ApplicationSetting.current_without_cache.update!(hashed_storage_enabled: true) + print '.' +end diff --git a/db/fixtures/development/03_project.rb b/db/fixtures/development/03_project.rb new file mode 100644 index 00000000000..46018cf68aa --- /dev/null +++ b/db/fixtures/development/03_project.rb @@ -0,0 +1,137 @@ +require './spec/support/sidekiq' + +# rubocop:disable Rails/Output + +Sidekiq::Testing.inline! do + Gitlab::Seeder.quiet do + Gitlab::Seeder.without_gitaly_timeout do + project_urls = %w[ + https://gitlab.com/gitlab-org/gitlab-test.git + https://gitlab.com/gitlab-org/gitlab-shell.git + https://gitlab.com/gnuwget/wget2.git + https://gitlab.com/Commit451/LabCoat.git + https://github.com/jashkenas/underscore.git + https://github.com/flightjs/flight.git + https://github.com/twitter/typeahead.js.git + https://github.com/h5bp/html5-boilerplate.git + https://github.com/google/material-design-lite.git + https://github.com/jlevy/the-art-of-command-line.git + https://github.com/FreeCodeCamp/freecodecamp.git + https://github.com/google/deepdream.git + https://github.com/jtleek/datasharing.git + https://github.com/WebAssembly/design.git + https://github.com/airbnb/javascript.git + https://github.com/tessalt/echo-chamber-js.git + https://github.com/atom/atom.git + https://github.com/mattermost/mattermost-server.git + https://github.com/purifycss/purifycss.git + https://github.com/facebook/nuclide.git + https://github.com/wbkd/awesome-d3.git + https://github.com/kilimchoi/engineering-blogs.git + https://github.com/gilbarbara/logos.git + https://github.com/reduxjs/redux.git + https://github.com/awslabs/s2n.git + https://github.com/arkency/reactjs_koans.git + https://github.com/twbs/bootstrap.git + https://github.com/chjj/ttystudio.git + https://github.com/MostlyAdequate/mostly-adequate-guide.git + https://github.com/octocat/Spoon-Knife.git + https://github.com/opencontainers/runc.git + https://github.com/googlesamples/android-topeka.git + ] + + large_project_urls = %w[ + https://github.com/torvalds/linux.git + https://gitlab.gnome.org/GNOME/gimp.git + https://gitlab.gnome.org/GNOME/gnome-mud.git + https://gitlab.com/fdroid/fdroidclient.git + https://gitlab.com/inkscape/inkscape.git + https://github.com/gnachman/iTerm2.git + ] + + def create_project(url, force_latest_storage: false) + group_path, project_path = url.split('/')[-2..-1] + + group = Group.find_by(path: group_path) + + unless group + group = Group.new( + name: group_path.titleize, + path: group_path + ) + group.description = FFaker::Lorem.sentence + group.save! + + group.add_owner(User.first) + end + + project_path.gsub!(".git", "") + + params = { + import_url: url, + namespace_id: group.id, + name: project_path.titleize, + description: FFaker::Lorem.sentence, + visibility_level: Gitlab::VisibilityLevel.values.sample, + skip_disk_validation: true + } + + if force_latest_storage + params[:storage_version] = Project::LATEST_STORAGE_VERSION + end + + project = nil + + Sidekiq::Worker.skipping_transaction_check do + project = Projects::CreateService.new(User.first, params).execute + + # Seed-Fu runs this entire fixture in a transaction, so the `after_commit` + # hook won't run until after the fixture is loaded. That is too late + # since the Sidekiq::Testing block has already exited. Force clearing + # the `after_commit` queue to ensure the job is run now. + project.send(:_run_after_commit_queue) + project.import_state.send(:_run_after_commit_queue) + end + + if project.valid? && project.valid_repo? + print '.' + else + puts project.errors.full_messages + print 'F' + end + end + + # You can specify how many projects you need during seed execution + size = ENV['SIZE'].present? ? ENV['SIZE'].to_i : 8 + + project_urls.first(size).each_with_index do |url, i| + create_project(url, force_latest_storage: i.even?) + end + + if ENV['LARGE_PROJECTS'].present? + large_project_urls.each(&method(:create_project)) + + if ENV['FORK'].present? + puts "\nGenerating forks" + + project_name = ENV['FORK'] == 'true' ? 'torvalds/linux' : ENV['FORK'] + + project = Project.find_by_full_path(project_name) + + User.offset(1).first(5).each do |user| + new_project = Projects::ForkService.new(project, user).execute + + if new_project.valid? && (new_project.valid_repo? || new_project.import_state.scheduled?) + print '.' + else + new_project.errors.full_messages.each do |error| + puts "#{new_project.full_path}: #{error}" + end + print 'F' + end + end + end + end + end + end +end diff --git a/db/fixtures/development/03_settings.rb b/db/fixtures/development/03_settings.rb deleted file mode 100644 index 3a4a5d436bf..00000000000 --- a/db/fixtures/development/03_settings.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -# Enable hashed storage, in development mode, for all projects by default. -Gitlab::Seeder.quiet do - ApplicationSetting.create_from_defaults unless ApplicationSetting.current_without_cache - ApplicationSetting.current_without_cache.update!(hashed_storage_enabled: true) - print '.' -end diff --git a/db/fixtures/development/04_labels.rb b/db/fixtures/development/04_labels.rb new file mode 100644 index 00000000000..b9ae4098d76 --- /dev/null +++ b/db/fixtures/development/04_labels.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'digest/md5' + +class Gitlab::Seeder::GroupLabels + def initialize(group, label_per_group: 10) + @group = group + @label_per_group = label_per_group + end + + def seed! + @label_per_group.times do + label_title = FFaker::Product.brand + Labels::CreateService + .new(title: label_title, color: "##{Digest::MD5.hexdigest(label_title)[0..5]}") + .execute(group: @group) + print '.' + end + end +end + +class Gitlab::Seeder::ProjectLabels + def initialize(project, label_per_project: 5) + @project = project + @label_per_project = label_per_project + end + + def seed! + @label_per_project.times do + label_title = FFaker::Vehicle.model + Labels::CreateService + .new(title: label_title, color: "##{Digest::MD5.hexdigest(label_title)[0..5]}") + .execute(project: @project) + print '.' + end + end +end + +Gitlab::Seeder.quiet do + puts "\nGenerating group labels" + Group.all.find_each do |group| + Gitlab::Seeder::GroupLabels.new(group).seed! + end + + puts "\nGenerating project labels" + Project.all.find_each do |project| + Gitlab::Seeder::ProjectLabels.new(project).seed! + end +end diff --git a/db/fixtures/development/04_project.rb b/db/fixtures/development/04_project.rb deleted file mode 100644 index 9a5f7cf8175..00000000000 --- a/db/fixtures/development/04_project.rb +++ /dev/null @@ -1,137 +0,0 @@ -require './spec/support/sidekiq' - -# rubocop:disable Rails/Output - -Sidekiq::Testing.inline! do - Gitlab::Seeder.quiet do - Gitlab::Seeder.without_gitaly_timeout do - project_urls = %w[ - https://gitlab.com/gitlab-org/gitlab-test.git - https://gitlab.com/gitlab-org/gitlab-shell.git - https://gitlab.com/gnuwget/wget2.git - https://gitlab.com/Commit451/LabCoat.git - https://github.com/jashkenas/underscore.git - https://github.com/flightjs/flight.git - https://github.com/twitter/typeahead.js.git - https://github.com/h5bp/html5-boilerplate.git - https://github.com/google/material-design-lite.git - https://github.com/jlevy/the-art-of-command-line.git - https://github.com/FreeCodeCamp/freecodecamp.git - https://github.com/google/deepdream.git - https://github.com/jtleek/datasharing.git - https://github.com/WebAssembly/design.git - https://github.com/airbnb/javascript.git - https://github.com/tessalt/echo-chamber-js.git - https://github.com/atom/atom.git - https://github.com/mattermost/mattermost-server.git - https://github.com/purifycss/purifycss.git - https://github.com/facebook/nuclide.git - https://github.com/wbkd/awesome-d3.git - https://github.com/kilimchoi/engineering-blogs.git - https://github.com/gilbarbara/logos.git - https://github.com/reduxjs/redux.git - https://github.com/awslabs/s2n.git - https://github.com/arkency/reactjs_koans.git - https://github.com/twbs/bootstrap.git - https://github.com/chjj/ttystudio.git - https://github.com/MostlyAdequate/mostly-adequate-guide.git - https://github.com/octocat/Spoon-Knife.git - https://github.com/opencontainers/runc.git - https://github.com/googlesamples/android-topeka.git - ] - - large_project_urls = %w[ - https://github.com/torvalds/linux.git - https://gitlab.gnome.org/GNOME/gimp.git - https://gitlab.gnome.org/GNOME/gnome-mud.git - https://gitlab.com/fdroid/fdroidclient.git - https://gitlab.com/inkscape/inkscape.git - https://github.com/gnachman/iTerm2.git - ] - - def create_project(url, force_latest_storage: false) - group_path, project_path = url.split('/')[-2..-1] - - group = Group.find_by(path: group_path) - - unless group - group = Group.new( - name: group_path.titleize, - path: group_path - ) - group.description = FFaker::Lorem.sentence - group.save - - group.add_owner(User.first) - end - - project_path.gsub!(".git", "") - - params = { - import_url: url, - namespace_id: group.id, - name: project_path.titleize, - description: FFaker::Lorem.sentence, - visibility_level: Gitlab::VisibilityLevel.values.sample, - skip_disk_validation: true - } - - if force_latest_storage - params[:storage_version] = Project::LATEST_STORAGE_VERSION - end - - project = nil - - Sidekiq::Worker.skipping_transaction_check do - project = Projects::CreateService.new(User.first, params).execute - - # Seed-Fu runs this entire fixture in a transaction, so the `after_commit` - # hook won't run until after the fixture is loaded. That is too late - # since the Sidekiq::Testing block has already exited. Force clearing - # the `after_commit` queue to ensure the job is run now. - project.send(:_run_after_commit_queue) - project.import_state.send(:_run_after_commit_queue) - end - - if project.valid? && project.valid_repo? - print '.' - else - puts project.errors.full_messages - print 'F' - end - end - - # You can specify how many projects you need during seed execution - size = ENV['SIZE'].present? ? ENV['SIZE'].to_i : 8 - - project_urls.first(size).each_with_index do |url, i| - create_project(url, force_latest_storage: i.even?) - end - - if ENV['LARGE_PROJECTS'].present? - large_project_urls.each(&method(:create_project)) - - if ENV['FORK'].present? - puts "\nGenerating forks" - - project_name = ENV['FORK'] == 'true' ? 'torvalds/linux' : ENV['FORK'] - - project = Project.find_by_full_path(project_name) - - User.offset(1).first(5).each do |user| - new_project = Projects::ForkService.new(project, user).execute - - if new_project.valid? && (new_project.valid_repo? || new_project.import_state.scheduled?) - print '.' - else - new_project.errors.full_messages.each do |error| - puts "#{new_project.full_path}: #{error}" - end - print 'F' - end - end - end - end - end - end -end diff --git a/db/fixtures/development/09_issues.rb b/db/fixtures/development/09_issues.rb index 16243b72f9a..926401d8b9e 100644 --- a/db/fixtures/development/09_issues.rb +++ b/db/fixtures/development/09_issues.rb @@ -3,13 +3,17 @@ require './spec/support/sidekiq' Gitlab::Seeder.quiet do Project.all.each do |project| 10.times do + label_ids = project.labels.pluck(:id).sample(3) + label_ids += project.group.labels.sample(3) if project.group + issue_params = { title: FFaker::Lorem.sentence(6), description: FFaker::Lorem.sentence, state: ['opened', 'closed'].sample, milestone: project.milestones.sample, assignees: [project.team.users.sample], - created_at: rand(12).months.ago + created_at: rand(12).months.ago, + label_ids: label_ids } Issues::CreateService.new(project, project.team.users.sample, issue_params).execute diff --git a/db/fixtures/development/10_merge_requests.rb b/db/fixtures/development/10_merge_requests.rb index 2051bcff8f0..1952f84ed62 100644 --- a/db/fixtures/development/10_merge_requests.rb +++ b/db/fixtures/development/10_merge_requests.rb @@ -12,13 +12,17 @@ Gitlab::Seeder.quiet do source_branch = branches.pop target_branch = branches.pop + label_ids = project.labels.pluck(:id).sample(3) + label_ids += project.group.labels.sample(3) if project.group + params = { source_branch: source_branch, target_branch: target_branch, title: FFaker::Lorem.sentence(6), description: FFaker::Lorem.sentences(3).join(" "), milestone: project.milestones.sample, - assignee: project.team.users.sample + assignee: project.team.users.sample, + label_ids: label_ids } # Only create MRs with users that are allowed to create MRs diff --git a/db/fixtures/development/22_labeled_issues_seed.rb b/db/fixtures/development/22_labeled_issues_seed.rb deleted file mode 100644 index 3730e9c7958..00000000000 --- a/db/fixtures/development/22_labeled_issues_seed.rb +++ /dev/null @@ -1,103 +0,0 @@ -# Creates a project with labeled issues for an user. -# Run this single seed file using: rake db:seed_fu FILTER=labeled USER_ID=74. -# If an USER_ID is not provided it will use the last created user. -require './spec/support/sidekiq' - -class Gitlab::Seeder::LabeledIssues - include ::Gitlab::Utils - - def initialize(user) - @user = user - end - - def seed! - Sidekiq::Testing.inline! do - group = create_group - - create_projects(group) - create_labels(group) - create_issues(group) - end - - print '.' - end - - private - - def create_group - group_name = "group_of_#{@user.username}_#{SecureRandom.hex(4)}" - - group_params = { - name: group_name, - path: group_name, - description: FFaker::Lorem.sentence - } - - Groups::CreateService.new(@user, group_params).execute - end - - def create_projects(group) - 5.times do - project_name = "project_#{SecureRandom.hex(6)}" - - params = { - namespace_id: group.id, - name: project_name, - description: FFaker::Lorem.sentence, - visibility_level: Gitlab::VisibilityLevel.values.sample - } - - Projects::CreateService.new(@user, params).execute - end - end - - def create_labels(group) - group.projects.each do |project| - 5.times do - label_title = FFaker::Vehicle.model - Labels::CreateService.new(title: label_title, color: "#69D100").execute(project: project) - end - end - - 10.times do - label_title = FFaker::Product.brand - Labels::CreateService.new(title: label_title).execute(group: group) - end - end - - def create_issues(group) - # Get only group labels - group_labels = - LabelsFinder.new(@user, group_id: group.id).execute.where.not(group_id: nil) - - group.projects.each do |project| - label_ids = project.labels.pluck(:id).sample(5) - label_ids.push(*group.labels.sample(4)) - - 20.times do - issue_params = { - title: FFaker::Lorem.sentence(6), - description: FFaker::Lorem.sentence, - state: 'opened', - label_ids: label_ids - - } - - Issues::CreateService.new(project, @user, issue_params).execute if project.project_feature.present? - end - end - end -end - -Gitlab::Seeder.quiet do - user_id = ENV['USER_ID'] - - user = - if user_id.present? - User.find(user_id) - else - User.last - end - - Gitlab::Seeder::LabeledIssues.new(user).seed! -end -- cgit v1.2.1