Commit 937d770a authored by Robert Speicher's avatar Robert Speicher

Merge branch 'fix/pipeline-seeds-in-dev-env' into 'master'

Improve pipeline fixtures in development environment

### What does this MR do?

This MR adds further improvements to seeds for development environment:

* fixes build artifacts fixtures
* fixes build log fixtures
* moves unsuccessful jobs to the last triggered stage

See merge request !5838
parents 3750a92d c15dc455
class Gitlab::Seeder::Builds
class Gitlab::Seeder::Pipelines
STAGES = %w[build test deploy notify]
BUILDS = [
{ name: 'build:linux', stage: 'build', status: :success },
......@@ -7,11 +7,12 @@ class Gitlab::Seeder::Builds
{ name: 'rspec:windows', stage: 'test', status: :success },
{ name: 'rspec:windows', stage: 'test', status: :success },
{ name: 'rspec:osx', stage: 'test', status_event: :success },
{ name: 'spinach:linux', stage: 'test', status: :pending },
{ name: 'spinach:osx', stage: 'test', status: :canceled },
{ name: 'cucumber:linux', stage: 'test', status: :running },
{ name: 'cucumber:osx', stage: 'test', status: :failed },
{ name: 'staging', stage: 'deploy', environment: 'staging', status: :success },
{ name: 'spinach:linux', stage: 'test', status: :success },
{ name: 'spinach:osx', stage: 'test', status: :failed, allow_failure: true},
{ name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending },
{ name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running },
{ name: 'env:gamma', stage: 'deploy', environment: 'gamma', status: :canceled },
{ name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success },
{ name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
{ name: 'slack', stage: 'notify', when: 'manual', status: :created },
]
......@@ -34,40 +35,58 @@ class Gitlab::Seeder::Builds
end
end
private
def pipelines
master_pipelines + merge_request_pipelines
create_master_pipelines + create_merge_request_pipelines
end
def master_pipelines
create_pipelines_for(@project, 'master')
def create_master_pipelines
@project.repository.commits('master', limit: 4).map do |commit|
create_pipeline!(@project, 'master', commit)
end
rescue
[]
end
def merge_request_pipelines
@project.merge_requests.last(5).map do |merge_request|
create_pipelines(merge_request.source_project, merge_request.source_branch, merge_request.commits.last(5))
end.flatten
def create_merge_request_pipelines
pipelines = @project.merge_requests.first(3).map do |merge_request|
project = merge_request.source_project
branch = merge_request.source_branch
merge_request.commits.last(4).map do |commit|
create_pipeline!(project, branch, commit)
end
end
pipelines.flatten
rescue
[]
end
def create_pipelines_for(project, ref)
commits = project.repository.commits(ref, limit: 5)
create_pipelines(project, ref, commits)
end
def create_pipelines(project, ref, commits)
commits.map do |commit|
def create_pipeline!(project, ref, commit)
project.pipelines.create(sha: commit.id, ref: ref)
end
end
def build_create!(pipeline, opts = {})
attributes = build_attributes_for(pipeline, opts)
attributes = job_attributes(pipeline, opts)
.merge(commands: '$ build command')
Ci::Build.create!(attributes).tap do |build|
# We need to set build trace and artifacts after saving a build
# (id required), that is why we need `#tap` method instead of passing
# block directly to `Ci::Build#create!`.
setup_artifacts(build)
setup_build_log(build)
build.save
end
end
def setup_artifacts(build)
return unless %w[build test].include?(build.stage)
Ci::Build.create!(attributes) do |build|
if opts[:name].start_with?('build')
artifacts_cache_file(artifacts_archive_path) do |file|
build.artifacts_file = file
end
......@@ -77,29 +96,25 @@ class Gitlab::Seeder::Builds
end
end
def setup_build_log(build)
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
end
def commit_status_create!(pipeline, opts = {})
attributes = commit_status_attributes_for(pipeline, opts)
attributes = job_attributes(pipeline, opts)
GenericCommitStatus.create!(attributes)
end
def commit_status_attributes_for(pipeline, opts)
def job_attributes(pipeline, opts)
{ name: 'test build', stage: 'test', stage_idx: stage_index(opts[:stage]),
ref: 'master', tag: false, user: build_user, project: @project, pipeline: pipeline,
created_at: Time.now, updated_at: Time.now
}.merge(opts)
end
def build_attributes_for(pipeline, opts)
commit_status_attributes_for(pipeline, opts).merge(commands: '$ build command')
end
def build_user
@project.team.users.sample
end
......@@ -131,8 +146,8 @@ class Gitlab::Seeder::Builds
end
Gitlab::Seeder.quiet do
Project.all.sample(10).each do |project|
project_builds = Gitlab::Seeder::Builds.new(project)
Project.all.sample(5).each do |project|
project_builds = Gitlab::Seeder::Pipelines.new(project)
project_builds.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