Commit c2f246e2 authored by Rémy Coutable's avatar Rémy Coutable

Provide new gitlab:seed:issues and gitlab:seed:insights:issues tasks

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent f6451879
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,
label_ids: label_ids
}
Issues::CreateService.new(project, project.team.users.sample, issue_params).execute
print '.'
end
end
Rake::Task["gitlab:seed:issues"].invoke
end
......@@ -12,6 +12,40 @@ The `setup` task is an alias for `gitlab:setup`.
This tasks calls `db:reset` to create the database, calls `add_limits_mysql` that adds limits to the database schema in case of a MySQL database and finally it calls `db:seed_fu` to seed the database.
Note: `db:setup` calls `db:seed` but this does nothing.
### Seeding issues for all or a given project
You can seed issues for all or a given project with the `gitlab:seed:issues`
task:
```shell
# All projects
bin/rake gitlab:seed:issues
# A specific project
bin/rake "gitlab:seed:issues[group-path/project-path]"
```
By default, this seeds an average of 2 issues per week for the last 5 weeks per
project.
#### Seeding issues for Insights charts **[ULTIMATE]**
You can seed issues specifically for working with the
[Insights charts](../user/group/insights/index.md) with the
`gitlab:seed:insights:issues` task:
```shell
# All projects
bin/rake gitlab:seed:insights:issues
# A specific project
bin/rake "gitlab:seed:insights:issues[group-path/project-path]"
```
By default, this seeds an average of 10 issues per week for the last 52 weeks
per project. All issues will also be randomly labeled with team, type, severity,
and priority.
### Automation
If you're very sure that you want to **wipe the current database** and refill
......
# frozen_string_literal: true
module Quality
module Seeders
module Insights
class Issues < Seeders::Issues
TEAM_LABELS = %w[Plan Create Manage Verify Secure].freeze
TYPE_LABELS = %w[bug feature].freeze
SEVERITY_LABELS = %w[severity::1 severity::2 severity::3 severity::4].freeze
PRIORITY_LABELS = %w[priority::1 priority::2 priority::3 priority::4].freeze
private
def labels
super + [
TEAM_LABELS.sample,
TYPE_LABELS.sample,
SEVERITY_LABELS.sample,
PRIORITY_LABELS.sample
]
end
end
end
end
end
namespace :gitlab do
namespace :seed do
namespace :insights do
desc "GitLab | Seed | Insights | Seeds issues for Insights charts"
task :issues, [:project_full_path] => :environment do |t, args|
projects =
if args.project_full_path
project = Project.find_by_full_path(args.project_full_path)
unless project
error_message = "Project '#{args.project_full_path}' does not exist!"
potential_projects = Project.search(args.project_full_path)
if potential_projects.present?
error_message += " Did you mean '#{potential_projects.first.full_path}'?"
end
puts error_message.color(:red)
exit 1
end
[project]
else
Project.find_each
end
projects.each do |project|
puts "\nSeeding issues for the Insights charts of the '#{project.full_path}' project"
seeder = Quality::Seeders::Insights::Issues.new(project: project)
issues_created = seeder.seed
puts "\n#{issues_created} issues created!"
end
end
end
end
end
# frozen_string_literal: true
# rubocop:disable CodeReuse/ActiveRecord
module Quality
module Seeders
class Issues
DEFAULT_BACKFILL_WEEKS = 52
DEFAULT_AVERAGE_ISSUES_PER_WEEK = 10
attr_reader :project, :user
def initialize(project:)
@project = project
end
def seed(backfill_weeks: DEFAULT_BACKFILL_WEEKS, average_issues_per_week: DEFAULT_AVERAGE_ISSUES_PER_WEEK)
created_at = backfill_weeks.to_i.weeks.ago
team = project.team.users
created_issues_count = 0
loop do
rand(average_issues_per_week * 2).times do
params = {
title: FFaker::Lorem.sentence(6),
description: FFaker::Lorem.sentence,
created_at: created_at + rand(6).days,
state: %w[opened closed].sample,
milestone: project.milestones.sample,
assignee_ids: Array(team.pluck(:id).sample(3)),
labels: labels.join(',')
}
issue = ::Issues::CreateService.new(project, team.sample, params).execute
if issue.persisted?
created_issues_count += 1
print '.' # rubocop:disable Rails/Output
end
end
created_at += 1.week
break if created_at > Time.now
end
created_issues_count
end
private
def labels
@labels_pool ||= project.labels.limit(rand(3)).pluck(:title).tap do |labels_array|
labels_array.concat(project.group.labels.limit(rand(3)).pluck(:title)) if project.group
end
end
end
end
end
# rubocop:enable CodeReuse/ActiveRecord
namespace :gitlab do
namespace :seed do
desc "GitLab | Seed | Seeds issues"
task :issues, [:project_full_path] => :environment do |t, args|
projects =
if args.project_full_path
project = Project.find_by_full_path(args.project_full_path)
unless project
error_message = "Project '#{args.project_full_path}' does not exist!"
potential_projects = Project.search(args.project_full_path)
if potential_projects.present?
error_message += " Did you mean '#{potential_projects.first.full_path}'?"
end
puts error_message.color(:red)
exit 1
end
[project]
else
Project.find_each
end
projects.each do |project|
puts "\nSeeding issues for the '#{project.full_path}' project"
seeder = Quality::Seeders::Issues.new(project: project)
issues_created = seeder.seed(backfill_weeks: 5, average_issues_per_week: 2)
puts "\n#{issues_created} issues created!"
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Quality::Seeders::Issues do
let(:project) { create(:project) }
subject { described_class.new(project: project) }
describe '#seed' do
it 'seeds issues' do
issues_created = subject.seed(backfill_weeks: 1, average_issues_per_week: 1)
expect(issues_created).to be_between(0, 2)
expect(project.issues.count).to eq(issues_created)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment