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] STAGES = %w[build test deploy notify]
BUILDS = [ BUILDS = [
{ name: 'build:linux', stage: 'build', status: :success }, { name: 'build:linux', stage: 'build', status: :success },
...@@ -7,11 +7,12 @@ class Gitlab::Seeder::Builds ...@@ -7,11 +7,12 @@ class Gitlab::Seeder::Builds
{ name: 'rspec:windows', stage: 'test', status: :success }, { name: 'rspec:windows', stage: 'test', status: :success },
{ name: 'rspec:windows', stage: 'test', status: :success }, { name: 'rspec:windows', stage: 'test', status: :success },
{ name: 'rspec:osx', stage: 'test', status_event: :success }, { name: 'rspec:osx', stage: 'test', status_event: :success },
{ name: 'spinach:linux', stage: 'test', status: :pending }, { name: 'spinach:linux', stage: 'test', status: :success },
{ name: 'spinach:osx', stage: 'test', status: :canceled }, { name: 'spinach:osx', stage: 'test', status: :failed, allow_failure: true},
{ name: 'cucumber:linux', stage: 'test', status: :running }, { name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending },
{ name: 'cucumber:osx', stage: 'test', status: :failed }, { name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running },
{ name: 'staging', stage: 'deploy', environment: 'staging', status: :success }, { 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: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
{ name: 'slack', stage: 'notify', when: 'manual', status: :created }, { name: 'slack', stage: 'notify', when: 'manual', status: :created },
] ]
...@@ -34,72 +35,86 @@ class Gitlab::Seeder::Builds ...@@ -34,72 +35,86 @@ class Gitlab::Seeder::Builds
end end
end end
private
def pipelines def pipelines
master_pipelines + merge_request_pipelines create_master_pipelines + create_merge_request_pipelines
end end
def master_pipelines def create_master_pipelines
create_pipelines_for(@project, 'master') @project.repository.commits('master', limit: 4).map do |commit|
create_pipeline!(@project, 'master', commit)
end
rescue rescue
[] []
end end
def merge_request_pipelines def create_merge_request_pipelines
@project.merge_requests.last(5).map do |merge_request| pipelines = @project.merge_requests.first(3).map do |merge_request|
create_pipelines(merge_request.source_project, merge_request.source_branch, merge_request.commits.last(5)) project = merge_request.source_project
end.flatten branch = merge_request.source_branch
merge_request.commits.last(4).map do |commit|
create_pipeline!(project, branch, commit)
end
end
pipelines.flatten
rescue rescue
[] []
end end
def create_pipelines_for(project, ref)
commits = project.repository.commits(ref, limit: 5) def create_pipeline!(project, ref, commit)
create_pipelines(project, ref, commits) project.pipelines.create(sha: commit.id, ref: ref)
end end
def create_pipelines(project, ref, commits) def build_create!(pipeline, opts = {})
commits.map do |commit| attributes = job_attributes(pipeline, opts)
project.pipelines.create(sha: commit.id, ref: ref) .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
end end
def build_create!(pipeline, opts = {}) def setup_artifacts(build)
attributes = build_attributes_for(pipeline, opts) return unless %w[build test].include?(build.stage)
Ci::Build.create!(attributes) do |build| artifacts_cache_file(artifacts_archive_path) do |file|
if opts[:name].start_with?('build') build.artifacts_file = file
artifacts_cache_file(artifacts_archive_path) do |file| end
build.artifacts_file = file
end
artifacts_cache_file(artifacts_metadata_path) do |file| artifacts_cache_file(artifacts_metadata_path) do |file|
build.artifacts_metadata = file build.artifacts_metadata = file
end end
end end
if %w(running success failed).include?(build.status) def setup_build_log(build)
# We need to set build trace after saving a build (id required) if %w(running success failed).include?(build.status)
build.trace = FFaker::Lorem.paragraphs(6).join("\n\n") build.trace = FFaker::Lorem.paragraphs(6).join("\n\n")
end
end end
end end
def commit_status_create!(pipeline, opts = {}) def commit_status_create!(pipeline, opts = {})
attributes = commit_status_attributes_for(pipeline, opts) attributes = job_attributes(pipeline, opts)
GenericCommitStatus.create!(attributes) GenericCommitStatus.create!(attributes)
end end
def commit_status_attributes_for(pipeline, opts) def job_attributes(pipeline, opts)
{ name: 'test build', stage: 'test', stage_idx: stage_index(opts[:stage]), { name: 'test build', stage: 'test', stage_idx: stage_index(opts[:stage]),
ref: 'master', tag: false, user: build_user, project: @project, pipeline: pipeline, ref: 'master', tag: false, user: build_user, project: @project, pipeline: pipeline,
created_at: Time.now, updated_at: Time.now created_at: Time.now, updated_at: Time.now
}.merge(opts) }.merge(opts)
end end
def build_attributes_for(pipeline, opts)
commit_status_attributes_for(pipeline, opts).merge(commands: '$ build command')
end
def build_user def build_user
@project.team.users.sample @project.team.users.sample
end end
...@@ -131,8 +146,8 @@ class Gitlab::Seeder::Builds ...@@ -131,8 +146,8 @@ class Gitlab::Seeder::Builds
end end
Gitlab::Seeder.quiet do Gitlab::Seeder.quiet do
Project.all.sample(10).each do |project| Project.all.sample(5).each do |project|
project_builds = Gitlab::Seeder::Builds.new(project) project_builds = Gitlab::Seeder::Pipelines.new(project)
project_builds.seed! project_builds.seed!
end 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