Class: Ci::CreateDownstreamPipelineService

Inherits:
BaseService
  • Object
show all
Includes:
DownstreamPipelineHelpers, Gitlab::Utils::StrongMemoize
Defined in:
app/services/ci/create_downstream_pipeline_service.rb

Overview

Takes in input a Ci::Bridge job and creates a downstream pipeline (either multi-project or child pipeline) according to the Ci::Bridge specifications.

Constant Summary collapse

DuplicateDownstreamPipelineError =
Class.new(StandardError)
MAX_NESTED_CHILDREN =
2

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from DownstreamPipelineHelpers

#log_downstream_pipeline_creation

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#execute(bridge) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/ci/create_downstream_pipeline_service.rb', line 15

def execute(bridge)
  @bridge = bridge

  return ServiceResponse.error(message: 'Can not run a failed bridge') if @bridge.failed?

  if @bridge.has_downstream_pipeline?
    Gitlab::ErrorTracking.track_exception(
      DuplicateDownstreamPipelineError.new,
      bridge_id: @bridge.id, project_id: @bridge.project_id
    )

    return ServiceResponse.error(message: 'Already has a downstream pipeline')
  end

  pipeline_params = @bridge.downstream_pipeline_params
  target_ref = pipeline_params.dig(:target_revision, :ref)

  return ServiceResponse.error(message: 'Pre-conditions not met') unless ensure_preconditions!(target_ref)

  # Allow retrying if the bridge is already running but has no downstream pipeline.
  # This handles the case where a previous worker was terminated mid-execution.
  unless @bridge.running? || @bridge.run
    return ServiceResponse.error(message: "Cannot run the bridge, status: #{@bridge.status}")
  end

  service = ::Ci::CreatePipelineService.new(
    pipeline_params.fetch(:project),
    current_user,
    pipeline_params.fetch(:target_revision))

  downstream_pipeline = service
    .execute(pipeline_params.fetch(:source), **pipeline_params[:execute_params])
    .payload

  log_downstream_pipeline_creation(downstream_pipeline)
  log_audit_event(downstream_pipeline)
  update_bridge_status!(@bridge, downstream_pipeline)
rescue StandardError => e
  @bridge.reset.drop!(:data_integrity_failure)
  raise e
end

#log_audit_event(downstream_pipeline) ⇒ Object



57
58
59
# File 'app/services/ci/create_downstream_pipeline_service.rb', line 57

def log_audit_event(downstream_pipeline)
  # defined in EE
end