2020-01-01 13:55:28 +05:30
|
|
|
require './spec/support/sidekiq_middleware'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
class Gitlab::Seeder::Environments
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def seed!
|
|
|
|
@project.create_mock_deployment_service!(active: true)
|
|
|
|
@project.create_mock_monitoring_service!(active: true)
|
|
|
|
|
|
|
|
create_master_deployments!('production')
|
|
|
|
create_master_deployments!('staging')
|
|
|
|
create_merge_request_review_deployments!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_master_deployments!(name)
|
|
|
|
@project.repository.commits('master', limit: 4).map do |commit|
|
|
|
|
create_deployment!(
|
|
|
|
@project,
|
|
|
|
name,
|
|
|
|
'master',
|
|
|
|
commit.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_merge_request_review_deployments!
|
2018-10-15 14:42:47 +05:30
|
|
|
@project
|
|
|
|
.merge_requests
|
2018-11-18 11:00:15 +05:30
|
|
|
.select { |mr| mr.source_branch.match?(/[a-zA-Z0-9]+/) }
|
2018-10-15 14:42:47 +05:30
|
|
|
.sample(4)
|
|
|
|
.each do |merge_request|
|
2017-08-17 22:00:37 +05:30
|
|
|
next unless merge_request.diff_head_sha
|
|
|
|
|
|
|
|
create_deployment!(
|
|
|
|
merge_request.source_project,
|
2018-11-18 11:00:15 +05:30
|
|
|
"review/#{merge_request.source_branch.gsub(/[^a-zA-Z0-9]+/, '')}",
|
2017-08-17 22:00:37 +05:30
|
|
|
merge_request.source_branch,
|
|
|
|
merge_request.diff_head_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_deployment!(project, name, ref, sha)
|
|
|
|
environment = find_or_create_environment!(project, name)
|
|
|
|
environment.deployments.create!(
|
|
|
|
project: project,
|
|
|
|
ref: ref,
|
|
|
|
sha: sha,
|
|
|
|
tag: false,
|
|
|
|
deployable: find_deployable(project, name)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_environment!(project, name)
|
|
|
|
project.environments.find_or_create_by!(name: name).tap do |environment|
|
|
|
|
environment.update(external_url: "https://google.com/#{name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_deployable(project, environment)
|
|
|
|
project.builds.where(environment: environment).sample
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Seeder.quiet do
|
2019-12-26 22:10:19 +05:30
|
|
|
Project.not_mass_generated.sample(5).each do |project|
|
2017-08-17 22:00:37 +05:30
|
|
|
project_environments = Gitlab::Seeder::Environments.new(project)
|
|
|
|
project_environments.seed!
|
|
|
|
end
|
|
|
|
end
|