2015-12-17 06:59:39 -05:00
|
|
|
class Gitlab::Seeder::Builds
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
2015-12-16 13:07:27 -05:00
|
|
|
|
2015-12-17 06:59:39 -05:00
|
|
|
def seed!
|
2015-12-16 13:07:27 -05:00
|
|
|
ci_commits.each do |ci_commit|
|
|
|
|
begin
|
2016-02-08 04:34:19 -05:00
|
|
|
build_create!(ci_commit, name: 'test build 1')
|
|
|
|
build_create!(ci_commit, status: 'success', name: 'test build 2')
|
2015-12-16 13:07:27 -05:00
|
|
|
print '.'
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
print 'F'
|
|
|
|
end
|
|
|
|
end
|
2015-12-16 09:13:34 -05:00
|
|
|
end
|
2015-12-17 06:59:39 -05:00
|
|
|
|
|
|
|
def ci_commits
|
|
|
|
commits = @project.repository.commits('master', nil, 5)
|
|
|
|
commits_sha = commits.map { |commit| commit.raw.id }
|
|
|
|
commits_sha.map do |sha|
|
|
|
|
@project.ensure_ci_commit(sha)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2016-02-08 04:34:19 -05:00
|
|
|
def build_create!(ci_commit, opts = {})
|
|
|
|
attributes = build_attributes_for(ci_commit).merge(opts)
|
|
|
|
build = Ci::Build.new(attributes)
|
|
|
|
|
|
|
|
if %w(success failed).include?(build.status)
|
|
|
|
artifacts_cache_file(artifacts_archive_path) do |file|
|
|
|
|
build.artifacts_file = file
|
|
|
|
end
|
|
|
|
|
|
|
|
artifacts_cache_file(artifacts_metadata_path) do |file|
|
|
|
|
build.artifacts_metadata = file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
build.save!
|
|
|
|
|
|
|
|
if %w(running success failed).include?(build.status)
|
|
|
|
# We need to set build trace after saving a build (id required)
|
|
|
|
build.trace = FFaker::Lorem.paragraphs(6).join("\n\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 06:59:39 -05:00
|
|
|
def build_attributes_for(ci_commit)
|
|
|
|
{ name: 'test build', commands: "$ build command",
|
|
|
|
stage: 'test', stage_idx: 1, ref: 'master',
|
|
|
|
user_id: build_user, gl_project_id: @project.id,
|
|
|
|
status: build_status, commit_id: ci_commit.id,
|
|
|
|
created_at: Time.now, updated_at: Time.now }
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_user
|
|
|
|
@project.team.users.sample
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_status
|
2016-02-08 04:34:19 -05:00
|
|
|
Ci::Build::AVAILABLE_STATUSES.sample
|
2015-12-17 06:59:39 -05:00
|
|
|
end
|
|
|
|
|
2015-12-30 09:21:05 -05:00
|
|
|
def artifacts_archive_path
|
2015-12-28 04:54:22 -05:00
|
|
|
Rails.root + 'spec/fixtures/ci_build_artifacts.zip'
|
2015-12-17 06:59:39 -05:00
|
|
|
end
|
|
|
|
|
2015-12-30 09:21:05 -05:00
|
|
|
def artifacts_metadata_path
|
|
|
|
Rails.root + 'spec/fixtures/ci_build_artifacts_metadata.gz'
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def artifacts_cache_file(file_path)
|
|
|
|
cache_path = file_path.to_s.gsub('ci_', "p#{@project.id}_")
|
|
|
|
|
|
|
|
FileUtils.copy(file_path, cache_path)
|
|
|
|
File.open(cache_path) do |file|
|
|
|
|
yield file
|
|
|
|
end
|
2015-12-17 06:59:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Seeder.quiet do
|
|
|
|
Project.all.sample(10).each do |project|
|
|
|
|
project_builds = Gitlab::Seeder::Builds.new(project)
|
|
|
|
project_builds.seed!
|
|
|
|
end
|
2015-12-16 09:13:34 -05:00
|
|
|
end
|