Commit 61dc0b7d authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract pipeline persistence to a separate chain class

parent 7cfaccd6
...@@ -3,9 +3,10 @@ module Ci ...@@ -3,9 +3,10 @@ module Ci
attr_reader :pipeline attr_reader :pipeline
SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Validate, SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Validate,
Gitlab::Ci::Pipeline::Chain::Skip].freeze Gitlab::Ci::Pipeline::Chain::Skip,
Gitlab::Ci::Pipeline::Chain::Create].freeze
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil) def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, &block)
@pipeline = Ci::Pipeline.new( @pipeline = Ci::Pipeline.new(
source: source, source: source,
project: project, project: project,
...@@ -19,43 +20,31 @@ module Ci ...@@ -19,43 +20,31 @@ module Ci
protected: project.protected_for?(ref) protected: project.protected_for?(ref)
) )
@pipeline.tap do |pipeline|
command = OpenStruct.new(ignore_skip_ci: ignore_skip_ci, command = OpenStruct.new(ignore_skip_ci: ignore_skip_ci,
save_incompleted: save_on_errors, save_incompleted: save_on_errors,
trigger_request: trigger_request, trigger_request: trigger_request,
schedule: schedule, schedule: schedule,
seeds_block: block,
project: project, project: project,
current_user: current_user) current_user: current_user)
sequence = SEQUENCE.map { |chain| chain.new(pipeline, command) } sequence = SEQUENCE.map { |chain| chain.new(pipeline, command) }
done = sequence.any? do |chain| sequence_complete = sequence.none? do |chain|
chain.perform! chain.perform!
chain.break? chain.break?
end end
update_merge_requests_head_pipeline if pipeline.persisted? update_merge_requests_head_pipeline if pipeline.persisted?
return pipeline if done if sequence_complete
begin
Ci::Pipeline.transaction do
pipeline.save!
yield(pipeline) if block_given?
Ci::CreatePipelineStagesService
.new(project, current_user)
.execute(pipeline)
end
rescue ActiveRecord::RecordInvalid => e
return error("Failed to persist the pipeline: #{e}")
end
update_merge_requests_head_pipeline if pipeline.persisted?
cancel_pending_pipelines if project.auto_cancel_pending_pipelines? cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
pipeline_created_counter.increment(source: source) pipeline_created_counter.increment(source: source)
pipeline.tap(&:process!) pipeline.process!
end
end
end end
private private
......
module Gitlab
module Ci
module Pipeline
module Chain
class Create < Chain::Base
def perform!
::Ci::Pipeline.transaction do
pipeline.save!
if @command.seeds_block
@command.seeds_block.call(pipeline)
end
::Ci::CreatePipelineStagesService
.new(project, current_user)
.execute(pipeline)
end
rescue ActiveRecord::RecordInvalid => e
pipeline.erros.add(:base, "Failed to persist the pipeline: #{e}")
end
def break?
!pipeline.persisted?
end
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