2018-12-10 13:25:49 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2020-01-10 18:07:43 +00:00
|
|
|
class Bridge < Ci::Processable
|
2019-03-02 14:29:04 +00:00
|
|
|
include Ci::Contextable
|
2020-02-06 18:08:54 +00:00
|
|
|
include Ci::Metadatable
|
2018-12-10 13:25:49 +00:00
|
|
|
include Importable
|
|
|
|
include AfterCommitQueue
|
2020-03-13 15:09:21 +00:00
|
|
|
include Ci::HasRef
|
2018-12-10 13:25:49 +00:00
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
InvalidBridgeTypeError = Class.new(StandardError)
|
2020-03-12 09:09:55 +00:00
|
|
|
InvalidTransitionError = Class.new(StandardError)
|
2020-02-06 18:08:54 +00:00
|
|
|
|
2018-12-12 12:49:42 +00:00
|
|
|
belongs_to :project
|
2019-01-14 12:07:46 +00:00
|
|
|
belongs_to :trigger_request
|
2020-02-06 18:08:54 +00:00
|
|
|
has_many :sourced_pipelines, class_name: "::Ci::Sources::Pipeline",
|
|
|
|
foreign_key: :source_job_id
|
|
|
|
|
2018-12-10 13:25:49 +00:00
|
|
|
validates :ref, presence: true
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
# rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :options
|
|
|
|
serialize :yaml_variables, ::Gitlab::Serializer::Ci::Variables
|
|
|
|
# rubocop:enable Cop/ActiveRecordSerialize
|
|
|
|
|
|
|
|
state_machine :status do
|
2020-02-10 12:08:59 +00:00
|
|
|
after_transition created: :pending do |bridge|
|
|
|
|
next unless bridge.downstream_project
|
|
|
|
|
|
|
|
bridge.run_after_commit do
|
|
|
|
bridge.schedule_downstream_pipeline!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
event :manual do
|
|
|
|
transition all => :manual
|
|
|
|
end
|
|
|
|
|
|
|
|
event :scheduled do
|
|
|
|
transition all => :scheduled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-10 13:25:49 +00:00
|
|
|
def self.retry(bridge, current_user)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2020-02-10 12:08:59 +00:00
|
|
|
def schedule_downstream_pipeline!
|
|
|
|
raise InvalidBridgeTypeError unless downstream_project
|
|
|
|
|
|
|
|
::Ci::CreateCrossProjectPipelineWorker.perform_async(self.id)
|
|
|
|
end
|
|
|
|
|
2020-02-06 18:08:54 +00: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 18:08:16 +00:00
|
|
|
def has_downstream_pipeline?
|
|
|
|
sourced_pipelines.exists?
|
|
|
|
end
|
|
|
|
|
2020-02-06 18:08:54 +00: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
|
|
|
|
options&.dig(:trigger, :project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-06 15:08:05 +00:00
|
|
|
def parent_pipeline
|
|
|
|
pipeline if triggers_child_pipeline?
|
|
|
|
end
|
|
|
|
|
2020-02-06 18:08:54 +00:00
|
|
|
def triggers_child_pipeline?
|
|
|
|
yaml_for_downstream.present?
|
|
|
|
end
|
|
|
|
|
2018-12-10 13:25:49 +00:00
|
|
|
def tags
|
|
|
|
[:bridge]
|
|
|
|
end
|
|
|
|
|
|
|
|
def detailed_status(current_user)
|
|
|
|
Gitlab::Ci::Status::Bridge::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
|
|
|
end
|
|
|
|
|
2019-01-22 12:56:06 +00:00
|
|
|
def schedulable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def action?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def artifacts?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-03-02 14:29:04 +00:00
|
|
|
def runnable?
|
|
|
|
false
|
2019-01-22 12:56:06 +00:00
|
|
|
end
|
|
|
|
|
2019-03-02 14:29:04 +00:00
|
|
|
def expanded_environment_name
|
2018-12-10 13:25:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute_hooks
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2019-01-22 12:56:06 +00:00
|
|
|
|
|
|
|
def to_partial_path
|
2019-01-22 13:21:48 +00:00
|
|
|
'projects/generic_commit_statuses/generic_commit_status'
|
2019-01-22 12:56:06 +00:00
|
|
|
end
|
2020-01-13 12:08:04 +00:00
|
|
|
|
|
|
|
def yaml_for_downstream
|
2020-02-06 18:08:54 +00: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
|
|
|
|
variables = scoped_variables.concat(pipeline.persisted_variables)
|
|
|
|
|
|
|
|
variables.to_runner_variables.yield_self do |all_variables|
|
|
|
|
yaml_variables.to_a.map do |hash|
|
|
|
|
{ key: hash[:key], value: ::ExpandVariables.expand(hash[:value], all_variables) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cross_project_params
|
|
|
|
{
|
|
|
|
project: downstream_project,
|
|
|
|
source: :pipeline,
|
|
|
|
target_revision: {
|
|
|
|
ref: target_ref || downstream_project.default_branch
|
|
|
|
},
|
2020-03-02 15:08:01 +00:00
|
|
|
execute_params: {
|
|
|
|
ignore_skip_ci: true,
|
|
|
|
bridge: self
|
|
|
|
}
|
2020-02-06 18:08:54 +00: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,
|
|
|
|
target_sha: parent_pipeline.target_sha
|
|
|
|
},
|
|
|
|
execute_params: {
|
|
|
|
ignore_skip_ci: true,
|
2020-02-13 12:08:49 +00:00
|
|
|
bridge: self,
|
|
|
|
merge_request: parent_pipeline.merge_request
|
2020-02-06 18:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 12:08:04 +00:00
|
|
|
end
|
2018-12-10 13:25:49 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
::Ci::Bridge.prepend_if_ee('::EE::Ci::Bridge')
|