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 ...@@ -9,8 +9,10 @@ module Ci
Gitlab::Ci::Pipeline::Chain::Validate::Config, Gitlab::Ci::Pipeline::Chain::Validate::Config,
Gitlab::Ci::Pipeline::Chain::Skip, Gitlab::Ci::Pipeline::Chain::Skip,
EE::Gitlab::Ci::Pipeline::Chain::Limit::Size, EE::Gitlab::Ci::Pipeline::Chain::Limit::Size,
Gitlab::Ci::Pipeline::Chain::Populate,
Gitlab::Ci::Pipeline::Chain::Create, Gitlab::Ci::Pipeline::Chain::Create,
EE::Gitlab::Ci::Pipeline::Chain::Limit::Activity].freeze 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) def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, mirror_update: false, &block)
@pipeline = Ci::Pipeline.new @pipeline = Ci::Pipeline.new
......
...@@ -18,8 +18,8 @@ module Ci ...@@ -18,8 +18,8 @@ module Ci
pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: params[:ref]) pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: params[:ref])
.execute(:trigger, ignore_skip_ci: true) do |pipeline| .execute(:trigger, ignore_skip_ci: true) do |pipeline|
pipeline.trigger_requests.create!(trigger: trigger) pipeline.trigger_requests.build(trigger: trigger)
create_pipeline_variables!(pipeline) pipeline.variables.build(variables)
end end
if pipeline.persisted? if pipeline.persisted?
...@@ -37,13 +37,15 @@ module Ci ...@@ -37,13 +37,15 @@ module Ci
pipeline = Ci::CreatePipelineService.new(project, job.user, ref: params[:ref]) pipeline = Ci::CreatePipelineService.new(project, job.user, ref: params[:ref])
.execute(:pipeline, ignore_skip_ci: true) do |pipeline| .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_pipeline: job.pipeline,
source_project: job.project, source_project: job.project,
pipeline: pipeline, pipeline: pipeline,
project: project) project: project)
create_pipeline_variables!(pipeline) pipeline.variables.build(variables)
end end
if pipeline.persisted? if pipeline.persisted?
...@@ -60,19 +62,15 @@ module Ci ...@@ -60,19 +62,15 @@ module Ci
end end
def job_from_token def job_from_token
return @job if defined?(@job) strong_memoize(:job) do
Ci::Build.find_by_token(params[:token].to_s)
@job = Ci::Build.find_by_token(params[:token].to_s) end
end end
def create_pipeline_variables!(pipeline) def variables
return unless params[:variables] params[:variables].to_h.map do |key, value|
variables = params[:variables].map do |key, value|
{ key: key, value: value } { key: key, value: value }
end end
pipeline.variables.create!(variables)
end end
end end
end end
...@@ -18,20 +18,21 @@ module EE ...@@ -18,20 +18,21 @@ module EE
def exceeded? def exceeded?
return false unless enabled? return false unless enabled?
excessive_seeds_count > 0 excessive_pipeline_size > 0
end end
def message def message
return unless exceeded? return unless exceeded?
'Pipeline size limit exceeded by ' \ 'Pipeline size limit exceeded by ' \
"#{pluralize(excessive_seeds_count, 'job')}!" "#{pluralize(excessive_pipeline_size, 'job')}!"
end end
private private
def excessive_seeds_count def excessive_pipeline_size
@excessive ||= @pipeline.seeds_size - @namespace.max_pipeline_size TODO fix size quota
@excessive ||= @pipeline.builds.size - @namespace.max_pipeline_size
end end
end end
end end
......
...@@ -9,13 +9,6 @@ module Gitlab ...@@ -9,13 +9,6 @@ module Gitlab
::Ci::Pipeline.transaction do ::Ci::Pipeline.transaction do
pipeline.save! 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. # 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 ...@@ -37,8 +37,6 @@ module Gitlab
stage.builds << build stage.builds << build
end end
end end
@pipeline.stages << stage
end end
end end
end end
......
...@@ -79,5 +79,15 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do ...@@ -79,5 +79,15 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
expect(pipeline.stages) expect(pipeline.stages)
.to all(satisfy { |stage| stage.project.present? }) .to all(satisfy { |stage| stage.project.present? })
end 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
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