Class: Ci::Bridge

Inherits:
Processable show all
Extended by:
Gitlab::Utils::Override
Includes:
AfterCommitQueue, Contextable, HasRef, Metadatable, Importable
Defined in:
app/models/ci/bridge.rb

Constant Summary collapse

InvalidBridgeTypeError =
Class.new(StandardError)
InvalidTransitionError =
Class.new(StandardError)

Constants included from HasStatus

HasStatus::ACTIVE_STATUSES, HasStatus::AVAILABLE_STATUSES, HasStatus::BLOCKED_STATUS, HasStatus::COMPLETED_STATUSES, HasStatus::DEFAULT_STATUS, HasStatus::EXCLUDE_IGNORED_STATUSES, HasStatus::ORDERED_STATUSES, HasStatus::PASSED_WITH_WARNINGS_STATUSES, HasStatus::STARTED_STATUSES, HasStatus::STATUSES_ENUM, HasStatus::UnknownStatusError

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from HasRef

#branch?, #git_ref, #ref_slug

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Metadatable

#degenerate!, #degenerated?, #ensure_metadata, #interruptible, #interruptible=, #options, #options=, #yaml_variables, #yaml_variables=

Methods included from Contextable

#deployment_variables, #predefined_variables, #scoped_variables, #scoped_variables_hash, #secret_group_variables, #secret_instance_variables, #secret_project_variables, #simple_variables, #simple_variables_without_dependencies, #user_variables

Methods inherited from Processable

#aggregated_needs_names, #all_dependencies, #ensure_scheduling_type!, #find_legacy_scheduling_type, #needs_attributes, populate_scheduling_type!, #scheduling_type_dag?, #scoped_variables_hash, select_with_aggregated_needs, #when

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from CommitStatus

#all_met_to_become_pending?, #any_unmet_prerequisites?, #archived?, #auto_canceled?, #cancelable?, #duration, #failed_but_allowed?, #group_name, #has_trace?, #importing?, #latest?, #locking_enabled?, locking_enabled?, names, #recoverable?, #requires_resource?, #retryable?, #sortable_name, #stuck?, update_as_processed!

Methods included from BulkInsertableAssociations

#bulk_insert_associations!, bulk_inserts_enabled?, with_bulk_insert

Methods included from Presentable

#present

Methods included from HasStatus

#active?, #blocked?, #complete?, #started?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, with_fast_statement_timeout, without_order

Class Method Details

.retry(bridge, current_user) ⇒ Object

Raises:

  • (NotImplementedError)

56
57
58
# File 'app/models/ci/bridge.rb', line 56

def self.retry(bridge, current_user)
  raise NotImplementedError
end

.with_preloadsObject


60
61
62
63
64
65
66
# File 'app/models/ci/bridge.rb', line 60

def self.with_preloads
  preload(
    :metadata,
    downstream_pipeline: [project: [:route, { namespace: :route }]],
    project: [:namespace]
  )
end

Instance Method Details

#action?Boolean

Returns:

  • (Boolean)

140
141
142
143
144
# File 'app/models/ci/bridge.rb', line 140

def action?
  return false unless ::Gitlab::Ci::Features.manual_bridges_enabled?(project)

  %w[manual].include?(self.when)
end

#artifacts?Boolean

rubocop: enable CodeReuse/ServiceClass

Returns:

  • (Boolean)

156
157
158
# File 'app/models/ci/bridge.rb', line 156

def artifacts?
  false
end

#dependency_variablesObject


208
209
210
211
212
# File 'app/models/ci/bridge.rb', line 208

def dependency_variables
  return [] unless ::Feature.enabled?(:ci_bridge_dependency_variables, project, default_enabled: true)

  super
end

#dependent?Boolean

Returns:

  • (Boolean)

191
192
193
194
195
# File 'app/models/ci/bridge.rb', line 191

def dependent?
  strong_memoize(:dependent) do
    options&.dig(:trigger, :strategy) == 'depend'
  end
end

#detailed_status(current_user) ⇒ Object


124
125
126
127
128
# File 'app/models/ci/bridge.rb', line 124

def detailed_status(current_user)
  Gitlab::Ci::Status::Bridge::Factory
    .new(self, current_user)
    .fabricate!
end

#downstream_pipeline_paramsObject


89
90
91
92
93
94
# File 'app/models/ci/bridge.rb', line 89

def downstream_pipeline_params
  return child_params if triggers_child_pipeline?
  return cross_project_params if downstream_project.present?

  {}
end

#downstream_projectObject


96
97
98
99
100
101
102
103
104
# File 'app/models/ci/bridge.rb', line 96

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

#downstream_project_pathObject


106
107
108
109
110
# File 'app/models/ci/bridge.rb', line 106

def downstream_project_path
  strong_memoize(:downstream_project_path) do
    options&.dig(:trigger, :project)
  end
end

#downstream_variablesObject


197
198
199
200
201
202
203
204
205
# File 'app/models/ci/bridge.rb', line 197

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

#execute_hooksObject

Raises:

  • (NotImplementedError)

167
168
169
# File 'app/models/ci/bridge.rb', line 167

def execute_hooks
  raise NotImplementedError
end

#expanded_environment_nameObject


164
165
# File 'app/models/ci/bridge.rb', line 164

def expanded_environment_name
end

#has_downstream_pipeline?Boolean

Returns:

  • (Boolean)

85
86
87
# File 'app/models/ci/bridge.rb', line 85

def has_downstream_pipeline?
  sourced_pipelines.exists?
end

#inherit_status_from_downstream!(pipeline) ⇒ Object


74
75
76
77
78
79
80
81
82
83
# File 'app/models/ci/bridge.rb', line 74

def inherit_status_from_downstream!(pipeline)
  case pipeline.status
  when 'success'
    self.success!
  when 'failed', 'canceled', 'skipped'
    self.drop!
  else
    false
  end
end

#parent_pipelineObject


112
113
114
# File 'app/models/ci/bridge.rb', line 112

def parent_pipeline
  pipeline if triggers_child_pipeline?
end

#play(current_user, job_variables_attributes = nil) ⇒ Object

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.


149
150
151
152
153
# File 'app/models/ci/bridge.rb', line 149

def play(current_user, job_variables_attributes = nil)
  Ci::PlayBridgeService
    .new(project, current_user)
    .execute(self)
end

#playable?Boolean

Returns:

  • (Boolean)

134
135
136
137
138
# File 'app/models/ci/bridge.rb', line 134

def playable?
  return false unless ::Gitlab::Ci::Features.manual_bridges_enabled?(project)

  action? && !archived? && manual?
end

#runnable?Boolean

Returns:

  • (Boolean)

160
161
162
# File 'app/models/ci/bridge.rb', line 160

def runnable?
  false
end

#schedulable?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'app/models/ci/bridge.rb', line 130

def schedulable?
  false
end

#schedule_downstream_pipeline!Object


68
69
70
71
72
# File 'app/models/ci/bridge.rb', line 68

def schedule_downstream_pipeline!
  raise InvalidBridgeTypeError unless downstream_project

  ::Ci::CreateCrossProjectPipelineWorker.perform_async(self.id)
end

#tagsObject


120
121
122
# File 'app/models/ci/bridge.rb', line 120

def tags
  [:bridge]
end

#target_refObject


182
183
184
185
186
187
188
189
# File 'app/models/ci/bridge.rb', line 182

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

#target_revision_refObject


214
215
216
# File 'app/models/ci/bridge.rb', line 214

def target_revision_ref
  downstream_pipeline_params.dig(:target_revision, :ref)
end

#to_partial_pathObject


171
172
173
# File 'app/models/ci/bridge.rb', line 171

def to_partial_path
  'projects/generic_commit_statuses/generic_commit_status'
end

#triggers_child_pipeline?Boolean

Returns:

  • (Boolean)

116
117
118
# File 'app/models/ci/bridge.rb', line 116

def triggers_child_pipeline?
  yaml_for_downstream.present?
end

#yaml_for_downstreamObject


175
176
177
178
179
180
# File 'app/models/ci/bridge.rb', line 175

def yaml_for_downstream
  strong_memoize(:yaml_for_downstream) do
    includes = options&.dig(:trigger, :include)
    YAML.dump('include' => includes) if includes
  end
end