2018-12-10 08:25:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2020-01-10 13:07:43 -05:00
|
|
|
class Bridge < Ci::Processable
|
2019-03-02 09:29:04 -05:00
|
|
|
include Ci::Contextable
|
2020-02-06 13:08:54 -05:00
|
|
|
include Ci::Metadatable
|
2018-12-10 08:25:49 -05:00
|
|
|
include Importable
|
|
|
|
include AfterCommitQueue
|
2020-03-13 11:09:21 -04:00
|
|
|
include Ci::HasRef
|
2018-12-10 08:25:49 -05:00
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
InvalidBridgeTypeError = Class.new(StandardError)
|
2020-03-12 05:09:55 -04:00
|
|
|
InvalidTransitionError = Class.new(StandardError)
|
2020-02-06 13:08:54 -05:00
|
|
|
|
2022-03-17 17:08:35 -04:00
|
|
|
FORWARD_DEFAULTS = {
|
|
|
|
yaml_variables: true,
|
|
|
|
pipeline_variables: false
|
|
|
|
}.freeze
|
|
|
|
|
2018-12-12 07:49:42 -05:00
|
|
|
belongs_to :project
|
2019-01-14 07:07:46 -05:00
|
|
|
belongs_to :trigger_request
|
2020-02-06 13:08:54 -05:00
|
|
|
has_many :sourced_pipelines, class_name: "::Ci::Sources::Pipeline",
|
2022-08-01 11:11:13 -04:00
|
|
|
foreign_key: :source_job_id
|
2020-02-06 13:08:54 -05:00
|
|
|
|
2020-06-16 08:09:00 -04:00
|
|
|
has_one :sourced_pipeline, class_name: "::Ci::Sources::Pipeline", foreign_key: :source_job_id
|
|
|
|
has_one :downstream_pipeline, through: :sourced_pipeline, source: :pipeline
|
|
|
|
|
2018-12-10 08:25:49 -05:00
|
|
|
validates :ref, presence: true
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
# rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :options
|
|
|
|
serialize :yaml_variables, ::Gitlab::Serializer::Ci::Variables
|
|
|
|
# rubocop:enable Cop/ActiveRecordSerialize
|
|
|
|
|
|
|
|
state_machine :status do
|
2021-02-05 04:09:10 -05:00
|
|
|
after_transition [:created, :manual, :waiting_for_resource] => :pending do |bridge|
|
2021-08-26 14:10:35 -04:00
|
|
|
next unless bridge.triggers_downstream_pipeline?
|
2020-02-10 07:08:59 -05:00
|
|
|
|
|
|
|
bridge.run_after_commit do
|
2021-10-03 11:09:32 -04:00
|
|
|
::Ci::CreateDownstreamPipelineWorker.perform_async(bridge.id)
|
2020-02-10 07:08:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-17 08:09:37 -04:00
|
|
|
event :pending do
|
|
|
|
transition all => :pending
|
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
event :manual do
|
|
|
|
transition all => :manual
|
|
|
|
end
|
|
|
|
|
|
|
|
event :scheduled do
|
|
|
|
transition all => :scheduled
|
|
|
|
end
|
2020-10-13 05:08:27 -04:00
|
|
|
|
|
|
|
event :actionize do
|
|
|
|
transition created: :manual
|
|
|
|
end
|
2020-02-06 13:08:54 -05:00
|
|
|
end
|
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
def retryable?
|
2022-05-06 11:09:03 -04:00
|
|
|
return false unless Feature.enabled?(:ci_recreate_downstream_pipeline, project)
|
2022-04-26 11:10:32 -04:00
|
|
|
|
|
|
|
return false if failed? && (pipeline_loop_detected? || reached_max_descendant_pipelines_depth?)
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
def self.with_preloads
|
|
|
|
preload(
|
|
|
|
:metadata,
|
|
|
|
downstream_pipeline: [project: [:route, { namespace: :route }]],
|
|
|
|
project: [:namespace]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
def self.clone_accessors
|
|
|
|
%i[pipeline project ref tag options name
|
|
|
|
allow_failure stage stage_id stage_idx
|
|
|
|
yaml_variables when description needs_attributes
|
|
|
|
scheduling_type].freeze
|
2022-03-23 14:08:47 -04:00
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
def inherit_status_from_downstream!(pipeline)
|
|
|
|
case pipeline.status
|
|
|
|
when 'success'
|
|
|
|
self.success!
|
|
|
|
when 'failed', 'canceled', 'skipped'
|
|
|
|
self.drop!
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-03 13:08:16 -05:00
|
|
|
def has_downstream_pipeline?
|
|
|
|
sourced_pipelines.exists?
|
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
def downstream_pipeline_params
|
|
|
|
return child_params if triggers_child_pipeline?
|
|
|
|
return cross_project_params if downstream_project.present?
|
|
|
|
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_project
|
|
|
|
strong_memoize(:downstream_project) do
|
|
|
|
if downstream_project_path
|
|
|
|
::Project.find_by_full_path(downstream_project_path)
|
|
|
|
elsif triggers_child_pipeline?
|
|
|
|
project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_project_path
|
|
|
|
strong_memoize(:downstream_project_path) do
|
2022-08-01 17:09:22 -04:00
|
|
|
project = options&.dig(:trigger, :project)
|
|
|
|
next unless project
|
|
|
|
|
|
|
|
scoped_variables.to_runner_variables.yield_self do |all_variables|
|
|
|
|
::ExpandVariables.expand(project, all_variables)
|
|
|
|
end
|
2020-02-06 13:08:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-06 10:08:05 -05:00
|
|
|
def parent_pipeline
|
|
|
|
pipeline if triggers_child_pipeline?
|
|
|
|
end
|
|
|
|
|
2021-08-26 14:10:35 -04:00
|
|
|
def triggers_downstream_pipeline?
|
|
|
|
triggers_child_pipeline? || triggers_cross_project_pipeline?
|
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
def triggers_child_pipeline?
|
|
|
|
yaml_for_downstream.present?
|
|
|
|
end
|
|
|
|
|
2021-08-26 14:10:35 -04:00
|
|
|
def triggers_cross_project_pipeline?
|
|
|
|
downstream_project_path.present?
|
|
|
|
end
|
|
|
|
|
2018-12-10 08:25:49 -05:00
|
|
|
def tags
|
|
|
|
[:bridge]
|
|
|
|
end
|
|
|
|
|
|
|
|
def detailed_status(current_user)
|
|
|
|
Gitlab::Ci::Status::Bridge::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
|
|
|
end
|
|
|
|
|
2019-01-22 07:56:06 -05:00
|
|
|
def schedulable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-10-13 05:08:27 -04:00
|
|
|
def playable?
|
|
|
|
action? && !archived? && manual?
|
|
|
|
end
|
|
|
|
|
2019-01-22 07:56:06 -05:00
|
|
|
def action?
|
2020-10-13 05:08:27 -04:00
|
|
|
%w[manual].include?(self.when)
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
|
|
|
# We don't need it but we are taking `job_variables_attributes` parameter
|
|
|
|
# to make it consistent with `Ci::Build#play` method.
|
|
|
|
def play(current_user, job_variables_attributes = nil)
|
|
|
|
Ci::PlayBridgeService
|
|
|
|
.new(project, current_user)
|
|
|
|
.execute(self)
|
2019-01-22 07:56:06 -05:00
|
|
|
end
|
2020-10-13 05:08:27 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2019-01-22 07:56:06 -05:00
|
|
|
|
|
|
|
def artifacts?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-03-02 09:29:04 -05:00
|
|
|
def runnable?
|
|
|
|
false
|
2019-01-22 07:56:06 -05:00
|
|
|
end
|
|
|
|
|
2021-02-05 04:09:10 -05:00
|
|
|
def any_unmet_prerequisites?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-03-02 09:29:04 -05:00
|
|
|
def expanded_environment_name
|
2018-12-10 08:25:49 -05:00
|
|
|
end
|
|
|
|
|
2021-05-20 23:10:24 -04:00
|
|
|
def persisted_environment
|
2021-05-18 05:10:37 -04:00
|
|
|
end
|
|
|
|
|
2018-12-10 08:25:49 -05:00
|
|
|
def execute_hooks
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2019-01-22 07:56:06 -05:00
|
|
|
|
|
|
|
def to_partial_path
|
2019-01-22 08:21:48 -05:00
|
|
|
'projects/generic_commit_statuses/generic_commit_status'
|
2019-01-22 07:56:06 -05:00
|
|
|
end
|
2020-01-13 07:08:04 -05:00
|
|
|
|
|
|
|
def yaml_for_downstream
|
2020-02-06 13:08:54 -05:00
|
|
|
strong_memoize(:yaml_for_downstream) do
|
|
|
|
includes = options&.dig(:trigger, :include)
|
|
|
|
YAML.dump('include' => includes) if includes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_ref
|
|
|
|
branch = options&.dig(:trigger, :branch)
|
|
|
|
return unless branch
|
|
|
|
|
|
|
|
scoped_variables.to_runner_variables.yield_self do |all_variables|
|
|
|
|
::ExpandVariables.expand(branch, all_variables)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependent?
|
|
|
|
strong_memoize(:dependent) do
|
|
|
|
options&.dig(:trigger, :strategy) == 'depend'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_variables
|
2022-05-26 02:08:37 -04:00
|
|
|
calculate_downstream_variables
|
|
|
|
.reverse # variables priority
|
|
|
|
.uniq { |var| var[:key] } # only one variable key to pass
|
|
|
|
.reverse
|
2020-02-06 13:08:54 -05:00
|
|
|
end
|
|
|
|
|
2020-10-13 05:08:27 -04:00
|
|
|
def target_revision_ref
|
|
|
|
downstream_pipeline_params.dig(:target_revision, :ref)
|
|
|
|
end
|
|
|
|
|
2020-02-06 13:08:54 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def cross_project_params
|
|
|
|
{
|
|
|
|
project: downstream_project,
|
|
|
|
source: :pipeline,
|
|
|
|
target_revision: {
|
2021-01-14 07:10:54 -05:00
|
|
|
ref: target_ref || downstream_project.default_branch,
|
|
|
|
variables_attributes: downstream_variables
|
2020-02-06 13:08:54 -05:00
|
|
|
},
|
2020-03-02 10:08:01 -05:00
|
|
|
execute_params: {
|
|
|
|
ignore_skip_ci: true,
|
|
|
|
bridge: self
|
|
|
|
}
|
2020-02-06 13:08:54 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def child_params
|
|
|
|
parent_pipeline = pipeline
|
|
|
|
|
|
|
|
{
|
|
|
|
project: project,
|
|
|
|
source: :parent_pipeline,
|
|
|
|
target_revision: {
|
|
|
|
ref: parent_pipeline.ref,
|
|
|
|
checkout_sha: parent_pipeline.sha,
|
|
|
|
before: parent_pipeline.before_sha,
|
|
|
|
source_sha: parent_pipeline.source_sha,
|
2021-01-14 07:10:54 -05:00
|
|
|
target_sha: parent_pipeline.target_sha,
|
|
|
|
variables_attributes: downstream_variables
|
2020-02-06 13:08:54 -05:00
|
|
|
},
|
|
|
|
execute_params: {
|
|
|
|
ignore_skip_ci: true,
|
2020-02-13 07:08:49 -05:00
|
|
|
bridge: self,
|
|
|
|
merge_request: parent_pipeline.merge_request
|
2020-02-06 13:08:54 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
2022-03-17 17:08:35 -04:00
|
|
|
|
|
|
|
def calculate_downstream_variables
|
|
|
|
expand_variables = scoped_variables
|
|
|
|
.concat(pipeline.persisted_variables)
|
|
|
|
.to_runner_variables
|
|
|
|
|
|
|
|
# The order of this list refers to the priority of the variables
|
|
|
|
downstream_yaml_variables(expand_variables) +
|
2022-04-12 08:08:50 -04:00
|
|
|
downstream_pipeline_variables(expand_variables) +
|
|
|
|
downstream_pipeline_schedule_variables(expand_variables)
|
2022-03-17 17:08:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_yaml_variables(expand_variables)
|
|
|
|
return [] unless forward_yaml_variables?
|
|
|
|
|
|
|
|
yaml_variables.to_a.map do |hash|
|
|
|
|
{ key: hash[:key], value: ::ExpandVariables.expand(hash[:value], expand_variables) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def downstream_pipeline_variables(expand_variables)
|
|
|
|
return [] unless forward_pipeline_variables?
|
|
|
|
|
|
|
|
pipeline.variables.to_a.map do |variable|
|
|
|
|
{ key: variable.key, value: ::ExpandVariables.expand(variable.value, expand_variables) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-12 08:08:50 -04:00
|
|
|
def downstream_pipeline_schedule_variables(expand_variables)
|
|
|
|
return [] unless forward_pipeline_variables?
|
|
|
|
return [] unless pipeline.pipeline_schedule
|
|
|
|
|
|
|
|
pipeline.pipeline_schedule.variables.to_a.map do |variable|
|
|
|
|
{ key: variable.key, value: ::ExpandVariables.expand(variable.value, expand_variables) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-17 17:08:35 -04:00
|
|
|
def forward_yaml_variables?
|
|
|
|
strong_memoize(:forward_yaml_variables) do
|
|
|
|
result = options&.dig(:trigger, :forward, :yaml_variables)
|
|
|
|
|
|
|
|
result.nil? ? FORWARD_DEFAULTS[:yaml_variables] : result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def forward_pipeline_variables?
|
|
|
|
strong_memoize(:forward_pipeline_variables) do
|
|
|
|
result = options&.dig(:trigger, :forward, :pipeline_variables)
|
|
|
|
|
|
|
|
result.nil? ? FORWARD_DEFAULTS[:pipeline_variables] : result
|
|
|
|
end
|
|
|
|
end
|
2018-12-10 08:25:49 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
::Ci::Bridge.prepend_mod_with('Ci::Bridge')
|