2020-02-10 07:08:59 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2020-09-03 08:08:47 -04:00
|
|
|
# Takes in input a Ci::Bridge job and creates a downstream pipeline
|
|
|
|
# (either multi-project or child pipeline) according to the Ci::Bridge
|
|
|
|
# specifications.
|
|
|
|
class CreateDownstreamPipelineService < ::BaseService
|
2020-02-10 07:08:59 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2022-04-21 08:10:16 -04:00
|
|
|
include Ci::DownstreamPipelineHelpers
|
2020-02-10 07:08:59 -05:00
|
|
|
|
2020-03-03 13:08:16 -05:00
|
|
|
DuplicateDownstreamPipelineError = Class.new(StandardError)
|
|
|
|
|
2022-03-24 08:07:26 -04:00
|
|
|
MAX_NESTED_CHILDREN = 2
|
2022-09-15 17:12:27 -04:00
|
|
|
MAX_HIERARCHY_SIZE = 1000
|
2020-09-08 11:08:41 -04:00
|
|
|
|
2020-02-10 07:08:59 -05:00
|
|
|
def execute(bridge)
|
|
|
|
@bridge = bridge
|
|
|
|
|
2020-03-03 13:08:16 -05:00
|
|
|
if bridge.has_downstream_pipeline?
|
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
DuplicateDownstreamPipelineError.new,
|
|
|
|
bridge_id: @bridge.id, project_id: @bridge.project_id
|
|
|
|
)
|
2021-06-02 14:10:01 -04:00
|
|
|
|
|
|
|
return error('Already has a downstream pipeline')
|
2020-03-03 13:08:16 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 07:08:59 -05:00
|
|
|
pipeline_params = @bridge.downstream_pipeline_params
|
|
|
|
target_ref = pipeline_params.dig(:target_revision, :ref)
|
|
|
|
|
2021-06-02 14:10:01 -04:00
|
|
|
return error('Pre-conditions not met') unless ensure_preconditions!(target_ref)
|
2020-02-10 07:08:59 -05:00
|
|
|
|
|
|
|
service = ::Ci::CreatePipelineService.new(
|
|
|
|
pipeline_params.fetch(:project),
|
|
|
|
current_user,
|
|
|
|
pipeline_params.fetch(:target_revision))
|
|
|
|
|
2021-07-20 08:10:29 -04:00
|
|
|
downstream_pipeline = service
|
|
|
|
.execute(pipeline_params.fetch(:source), **pipeline_params[:execute_params])
|
|
|
|
.payload
|
2020-03-03 13:08:16 -05:00
|
|
|
|
2022-04-21 08:10:16 -04:00
|
|
|
log_downstream_pipeline_creation(downstream_pipeline)
|
|
|
|
|
2020-03-03 13:08:16 -05:00
|
|
|
downstream_pipeline.tap do |pipeline|
|
|
|
|
update_bridge_status!(@bridge, pipeline)
|
|
|
|
end
|
2020-02-10 07:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-03 13:08:16 -05:00
|
|
|
def update_bridge_status!(bridge, pipeline)
|
2021-03-02 10:10:57 -05:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(bridge, name: 'create_downstream_pipeline_update_bridge_status') do |subject|
|
2020-03-03 13:08:16 -05:00
|
|
|
if pipeline.created_successfully?
|
|
|
|
# If bridge uses `strategy:depend` we leave it running
|
|
|
|
# and update the status when the downstream pipeline completes.
|
|
|
|
subject.success! unless subject.dependent?
|
|
|
|
else
|
2020-06-15 08:08:44 -04:00
|
|
|
subject.options[:downstream_errors] = pipeline.errors.full_messages
|
2020-03-03 13:08:16 -05:00
|
|
|
subject.drop!(:downstream_pipeline_creation_failed)
|
|
|
|
end
|
|
|
|
end
|
2020-03-12 05:09:55 -04:00
|
|
|
rescue StateMachines::InvalidTransition => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
Ci::Bridge::InvalidTransitionError.new(e.message),
|
|
|
|
bridge_id: bridge.id,
|
|
|
|
downstream_pipeline_id: pipeline.id)
|
2020-03-03 13:08:16 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 07:08:59 -05:00
|
|
|
def ensure_preconditions!(target_ref)
|
|
|
|
unless downstream_project_accessible?
|
|
|
|
@bridge.drop!(:downstream_bridge_project_not_found)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Remove this condition if favour of model validation
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/38338
|
|
|
|
if downstream_project == project && !@bridge.triggers_child_pipeline?
|
|
|
|
@bridge.drop!(:invalid_bridge_trigger)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Remove this condition if favour of model validation
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/38338
|
2022-03-24 08:07:26 -04:00
|
|
|
# only applies to parent-child pipelines not multi-project
|
|
|
|
if has_max_nested_children?
|
2020-10-16 11:08:46 -04:00
|
|
|
@bridge.drop!(:reached_max_descendant_pipelines_depth)
|
|
|
|
return false
|
2020-02-10 07:08:59 -05:00
|
|
|
end
|
|
|
|
|
2022-09-15 17:12:27 -04:00
|
|
|
if Feature.enabled?(:ci_limit_complete_hierarchy_size) && pipeline_tree_too_large?
|
|
|
|
@bridge.drop!(:reached_max_pipeline_hierarchy_size)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-02-10 07:08:59 -05:00
|
|
|
unless can_create_downstream_pipeline?(target_ref)
|
|
|
|
@bridge.drop!(:insufficient_bridge_permissions)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-05-17 14:10:42 -04:00
|
|
|
if has_cyclic_dependency?
|
|
|
|
@bridge.drop!(:pipeline_loop_detected)
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-02-10 07:08:59 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_project_accessible?
|
|
|
|
downstream_project.present? &&
|
|
|
|
can?(current_user, :read_project, downstream_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_downstream_pipeline?(target_ref)
|
|
|
|
can?(current_user, :update_pipeline, project) &&
|
|
|
|
can?(current_user, :create_pipeline, downstream_project) &&
|
|
|
|
can_update_branch?(target_ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_update_branch?(target_ref)
|
2020-07-21 14:09:45 -04:00
|
|
|
::Gitlab::UserAccess.new(current_user, container: downstream_project).can_update_branch?(target_ref)
|
2020-02-10 07:08:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_project
|
|
|
|
strong_memoize(:downstream_project) do
|
|
|
|
@bridge.downstream_project
|
|
|
|
end
|
|
|
|
end
|
2020-09-08 11:08:41 -04:00
|
|
|
|
2021-05-17 14:10:42 -04:00
|
|
|
def has_cyclic_dependency?
|
|
|
|
return false if @bridge.triggers_child_pipeline?
|
|
|
|
|
2022-02-22 13:17:57 -05:00
|
|
|
pipeline_checksums = @bridge.pipeline.self_and_upstreams.filter_map do |pipeline|
|
|
|
|
config_checksum(pipeline) unless pipeline.child?
|
2021-05-17 14:10:42 -04:00
|
|
|
end
|
2022-02-22 13:17:57 -05:00
|
|
|
|
|
|
|
# To avoid false positives we allow 1 cycle in the ancestry and
|
|
|
|
# fail when 2 cycles are detected: A -> B -> A -> B -> A
|
|
|
|
pipeline_checksums.tally.any? { |_checksum, occurrences| occurrences > 2 }
|
2021-05-17 14:10:42 -04:00
|
|
|
end
|
|
|
|
|
2022-03-24 08:07:26 -04:00
|
|
|
def has_max_nested_children?
|
2020-09-08 11:08:41 -04:00
|
|
|
return false unless @bridge.triggers_child_pipeline?
|
|
|
|
|
2022-03-24 08:07:26 -04:00
|
|
|
# only applies to parent-child pipelines not multi-project
|
2022-09-08 17:13:09 -04:00
|
|
|
ancestors_of_new_child = @bridge.pipeline.self_and_project_ancestors
|
2022-03-24 08:07:26 -04:00
|
|
|
ancestors_of_new_child.count > MAX_NESTED_CHILDREN
|
2020-09-08 11:08:41 -04:00
|
|
|
end
|
2021-05-17 14:10:42 -04:00
|
|
|
|
2022-09-15 17:12:27 -04:00
|
|
|
def pipeline_tree_too_large?
|
|
|
|
return false unless @bridge.triggers_downstream_pipeline?
|
|
|
|
|
|
|
|
# Applies to the entire pipeline tree across all projects
|
|
|
|
@bridge.pipeline.complete_hierarchy_count >= MAX_HIERARCHY_SIZE
|
|
|
|
end
|
|
|
|
|
2021-05-17 14:10:42 -04:00
|
|
|
def config_checksum(pipeline)
|
2022-01-20 10:11:58 -05:00
|
|
|
[pipeline.project_id, pipeline.ref, pipeline.source].hash
|
2021-05-17 14:10:42 -04:00
|
|
|
end
|
2020-02-10 07:08:59 -05:00
|
|
|
end
|
|
|
|
end
|