BigW Consortium Gitlab

Commit 61dc0b7d by 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)
) )
command = OpenStruct.new(ignore_skip_ci: ignore_skip_ci, @pipeline.tap do |pipeline|
save_incompleted: save_on_errors, command = OpenStruct.new(ignore_skip_ci: ignore_skip_ci,
trigger_request: trigger_request, save_incompleted: save_on_errors,
schedule: schedule, trigger_request: trigger_request,
project: project, schedule: schedule,
current_user: current_user) seeds_block: block,
project: project,
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?
return pipeline if done
begin update_merge_requests_head_pipeline if pipeline.persisted?
Ci::Pipeline.transaction do
pipeline.save!
yield(pipeline) if block_given? if sequence_complete
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
pipeline_created_counter.increment(source: source)
Ci::CreatePipelineStagesService pipeline.process!
.new(project, current_user)
.execute(pipeline)
end end
rescue ActiveRecord::RecordInvalid => e
return error("Failed to persist the pipeline: #{e}")
end end
update_merge_requests_head_pipeline if pipeline.persisted?
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
pipeline_created_counter.increment(source: source)
pipeline.tap(&:process!)
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