Commit f294d93b authored by Robert Hunt's avatar Robert Hunt

Changing the compliance seeder to create violations

Reworked the seeder to create compliance violations rather than the
previous merge request approach. This also simplifies the logic and
makes it more full-proof against duplication errors.
parent 48512927
# frozen_string_literal: true
require './spec/support/sidekiq_middleware'
class Gitlab::Seeder::ComplianceDashboardMergeRequests
PIPELINE_STATUSES = %w[success failed].freeze
def initialize(project)
@project = project
end
def admin
@admin ||= FactoryBot.create(:user, :admin)
end
def seed!
used_statuses = []
merge_requests.each do |merge_request|
branch = merge_request.source_branch
commit = merge_request.diff_head_sha || @project.commit('HEAD')
status = PIPELINE_STATUSES.reject { |s| used_statuses.include?(s) }.sample
create_pipeline!(@project, branch, commit, status).tap do |pipeline|
merge_request.update!(head_pipeline_id: pipeline.id)
end
EventCreateService.new.merge_mr(merge_request, merge_request.author)
used_statuses.append(status)
print '.'
end
end
def merge_requests
maintainer = new_maintainer
developer = new_developer
Array.new(2).map do
amount = rand(1..2)
approvers = [{ user: maintainer }]
if amount === 2
approvers = [{ user: developer }, { user: maintainer }]
end
create_merge_request!(approvers)
end
end
def create_merge_request!(approvals)
opts = {
title: FFaker::Lorem.sentence(6),
description: FFaker::Lorem.sentence,
source_branch: "#{FFaker::Lorem.word}-#{SecureRandom.hex(5)}",
target_branch: 'master',
state: :opened
}
Sidekiq::Worker.skipping_transaction_check do
merge_request = MergeRequests::CreateService.new(project: @project, current_user: admin, params: opts).execute
merge_request.save!
merge_request.approvals.create(approvals)
merge_request.state = :merged
merge_request
end
rescue ::Gitlab::Access::AccessDeniedError
raise ::Gitlab::Access::AccessDeniedError, "If you are re-creating your GitLab database, you should also delete your old repositories located at $GDK/repositories/@hashed"
end
def create_pipeline!(project, ref, commit, status)
project.ci_pipelines.create!(sha: commit.id, ref: ref, source: :push, status: status)
end
def new_developer
developer = new_user
@project.add_developer(developer)
developer
end
def new_maintainer
maintainer = new_user
@project.add_maintainer(maintainer)
maintainer
end
def new_user
FactoryBot.create(:user)
end
end
Gitlab::Seeder.quiet do
projects = Project
.non_archived
.with_merge_requests_enabled
.not_mass_generated
.reject(&:empty_repo?)
projects.each do |project|
merge_requests = Gitlab::Seeder::ComplianceDashboardMergeRequests.new(project)
merge_requests.seed!
end
end
# frozen_string_literal: true
class Gitlab::Seeder::ComplianceReportViolations
include MergeRequestMetricsHelper
def initialize(project)
@project = project
end
attr_reader :project
def seed!
Array.new(rand(2..10)).each do
merge_request = create_merge_request([{ user: project.members.sample.user }])
::Enums::MergeRequests::ComplianceViolation.reasons.keys.each do |reason|
FactoryBot.create(
:compliance_violation,
reason,
merge_request: merge_request,
violating_user: merge_request.metrics.merged_by
)
print '.'
end
end
end
private
def create_merge_request(approvers)
merge_request = FactoryBot.create(
:merge_request,
state: :merged,
source_project: project,
target_project: project,
source_branch: "#{FFaker::Product.brand}-#{FFaker::Product.brand}-#{rand(1000)}",
target_branch: project.default_branch,
title: FFaker::Lorem.sentence(6),
description: FFaker::Lorem.sentence,
author: approvers.first[:user]
)
merge_request.approvals.create(approvers)
metrics = merge_request.build_metrics
metrics.merged_at = rand(1..30).days.ago
metrics.merged_by = approvers.first[:user]
metrics.save!
merge_request
end
end
Gitlab::Seeder.quiet do
FactoryBot.definition_file_paths << Rails.root.join('ee', 'spec', 'factories')
FactoryBot.reload # rubocop:disable Cop/ActiveRecordAssociationReload
projects = Project
.non_archived
.with_merge_requests_enabled
.not_mass_generated
.reject(&:empty_repo?)
projects.each do |project|
violations = Gitlab::Seeder::ComplianceReportViolations.new(project)
violations.seed!
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