Commit 1ea1812b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Populate pipeline with objects before creating it

Conflicts:
	app/services/ci/create_pipeline_service.rb
	app/services/ci/pipeline_trigger_service.rb
parent c2523a5d
......@@ -9,8 +9,10 @@ module Ci
Gitlab::Ci::Pipeline::Chain::Validate::Config,
Gitlab::Ci::Pipeline::Chain::Skip,
EE::Gitlab::Ci::Pipeline::Chain::Limit::Size,
Gitlab::Ci::Pipeline::Chain::Populate,
Gitlab::Ci::Pipeline::Chain::Create,
EE::Gitlab::Ci::Pipeline::Chain::Limit::Activity].freeze
Gitlab::Ci::Pipeline::Chain::Create].freeze
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, mirror_update: false, &block)
@pipeline = Ci::Pipeline.new
......
......@@ -18,8 +18,8 @@ module Ci
pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: params[:ref])
.execute(:trigger, ignore_skip_ci: true) do |pipeline|
pipeline.trigger_requests.create!(trigger: trigger)
create_pipeline_variables!(pipeline)
pipeline.trigger_requests.build(trigger: trigger)
pipeline.variables.build(variables)
end
if pipeline.persisted?
......@@ -37,13 +37,15 @@ module Ci
pipeline = Ci::CreatePipelineService.new(project, job.user, ref: params[:ref])
.execute(:pipeline, ignore_skip_ci: true) do |pipeline|
job.sourced_pipelines.create!(
# TODO this needs a solution and specs
job.sourced_pipelines.build(
source_pipeline: job.pipeline,
source_project: job.project,
pipeline: pipeline,
project: project)
create_pipeline_variables!(pipeline)
pipeline.variables.build(variables)
end
if pipeline.persisted?
......@@ -60,19 +62,15 @@ module Ci
end
def job_from_token
return @job if defined?(@job)
@job = Ci::Build.find_by_token(params[:token].to_s)
strong_memoize(:job) do
Ci::Build.find_by_token(params[:token].to_s)
end
end
def create_pipeline_variables!(pipeline)
return unless params[:variables]
variables = params[:variables].map do |key, value|
def variables
params[:variables].to_h.map do |key, value|
{ key: key, value: value }
end
pipeline.variables.create!(variables)
end
end
end
......@@ -18,20 +18,21 @@ module EE
def exceeded?
return false unless enabled?
excessive_seeds_count > 0
excessive_pipeline_size > 0
end
def message
return unless exceeded?
'Pipeline size limit exceeded by ' \
"#{pluralize(excessive_seeds_count, 'job')}!"
"#{pluralize(excessive_pipeline_size, 'job')}!"
end
private
def excessive_seeds_count
@excessive ||= @pipeline.seeds_size - @namespace.max_pipeline_size
def excessive_pipeline_size
TODO fix size quota
@excessive ||= @pipeline.builds.size - @namespace.max_pipeline_size
end
end
end
......
......@@ -9,13 +9,6 @@ module Gitlab
::Ci::Pipeline.transaction do
pipeline.save!
@command.seeds_block&.call(pipeline)
pipeline.stage_seeds.each do |seed|
seed.user = current_user
seed.to_resource.save!
end
# TODO populate environments with find_or_initialize_by in the chain too.
##
......
module Gitlab
module Ci
module Pipeline
module Chain
class Populate < Chain::Base
PopulateError = Class.new(StandardError)
def perform!
##
# Populate pipeline with seeds block.
#
# It comes from a block argument to CreatePipelineService#execute.
#
@command.seeds_block&.call(pipeline)
pipeline.stage_seeds.each do |seed|
seed.user = current_user
pipeline.stages << seed.to_resource
end
raise Populate::PopulateError if pipeline.persisted?
end
def break?
pipeline.persisted?
end
end
end
end
end
end
......@@ -37,8 +37,6 @@ module Gitlab
stage.builds << build
end
end
@pipeline.stages << stage
end
end
end
......
......@@ -79,5 +79,15 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
expect(pipeline.stages)
.to all(satisfy { |stage| stage.project.present? })
end
it 'can not be persisted without explicit pipeline assignment' do
stage = subject.to_resource
pipeline.save!
expect(stage).not_to be_persisted
expect(pipeline.reload.stages.count).to eq 0
expect(pipeline.reload.builds.count).to eq 0
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