Class: Ci::Bridge

Inherits:
Processable show all
Includes:
AfterCommitQueue, Contextable, Deployable, HasRef, Importable
Defined in:
app/models/ci/bridge.rb

Constant Summary collapse

InvalidBridgeTypeError =
Class.new(StandardError)
InvalidTransitionError =
Class.new(StandardError)
FORWARD_DEFAULTS =
{
  yaml_variables: true,
  pipeline_variables: false
}.freeze

Constants included from TaggableQueries

TaggableQueries::MAX_TAGS_IDS, TaggableQueries::TooManyTagsError

Constants included from HasStatus

HasStatus::ACTIVE_STATUSES, HasStatus::ALIVE_STATUSES, HasStatus::AVAILABLE_STATUSES, HasStatus::BLOCKED_STATUS, HasStatus::CANCELABLE_STATUSES, HasStatus::COMPLETED_STATUSES, HasStatus::DEFAULT_STATUS, HasStatus::IGNORED_STATUSES, HasStatus::ORDERED_STATUSES, HasStatus::PASSED_WITH_WARNINGS_STATUSES, HasStatus::STARTED_STATUSES, HasStatus::STATUSES_ENUM, HasStatus::STOPPED_STATUSES, HasStatus::UnknownStatusError

Constants included from Partitionable

Partitionable::MUTEX

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasRef

#branch?, #git_ref, #ref_slug

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Deployable

#actual_persisted_environment, #deployment_job?, #deployment_status, #environment_action, #environment_slug, #environment_status, #environment_tier, #environment_tier_from_options, #environment_url, #expanded_environment_name, #expanded_kubernetes_namespace, #has_environment_keyword?, #on_stop, #outdated_deployment?, #persisted_environment, #persisted_environment=, #stop_action_successful?, #stops_environment?, #successful_deployment_status

Methods included from Contextable

#scoped_variables, #simple_variables, #simple_variables_without_dependencies, #track_duration

Methods inherited from Processable

#aggregated_needs_names, #all_dependencies, #all_met_to_become_pending?, #clone, #dependency_variables, #ensure_scheduling_type!, #expanded_environment_name, #find_legacy_scheduling_type, #needs_attributes, #other_manual_actions, #persisted_environment, populate_scheduling_type!, #scheduling_type_dag?, select_with_aggregated_needs, #when, #with_resource_group?

Methods included from Gitlab::Utils::Override

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

Methods included from Metadatable

#cancel_gracefully?, #degenerate!, #degenerated?, #enqueue_immediately?, #ensure_metadata, #has_exposed_artifacts?, #id_tokens=, #id_tokens?, #interruptible, #interruptible=, #options, #options=, #set_enqueue_immediately!, #yaml_variables, #yaml_variables=

Methods inherited from CommitStatus

#all_met_to_become_pending?, #archived?, #auto_canceled?, #cancelable?, #duration, #expire_etag_cache!, #group_name, #has_trace?, #importing?, #latest?, #locking_enabled?, locking_enabled?, names, #queued_duration, #recoverable?, #resource_parent, #sortable_name, #stage_name, #stuck?, switch_table_names, #to_ability_name, update_as_processed!, #update_older_statuses_retried!

Methods included from TaggableQueries

#tags_ids

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?, #incomplete?, #started?

Methods inherited from ApplicationRecord

model_name, table_name_prefix

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.clone_accessorsObject



71
72
73
74
75
76
# File 'app/models/ci/bridge.rb', line 71

def self.clone_accessors
  %i[pipeline project ref tag options name
     allow_failure stage stage_idx
     yaml_variables when environment description needs_attributes
     scheduling_type ci_stage partition_id].freeze
end

.with_preloadsObject



63
64
65
66
67
68
69
# File 'app/models/ci/bridge.rb', line 63

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

Instance Method Details

#action?Boolean

Returns:

  • (Boolean)


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

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

#any_unmet_prerequisites?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'app/models/ci/bridge.rb', line 179

def any_unmet_prerequisites?
  false
end

#artifacts?Boolean

rubocop: enable CodeReuse/ServiceClass

Returns:

  • (Boolean)


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

def artifacts?
  false
end

#dependent?Boolean

Returns:

  • (Boolean)


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

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

#detailed_status(current_user) ⇒ Object



143
144
145
146
147
# File 'app/models/ci/bridge.rb', line 143

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

#downstream_pipeline_paramsObject



95
96
97
98
99
100
# File 'app/models/ci/bridge.rb', line 95

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

  {}
end

#downstream_projectObject



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

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



112
113
114
115
116
117
118
119
120
121
# File 'app/models/ci/bridge.rb', line 112

def downstream_project_path
  strong_memoize(:downstream_project_path) do
    project = options&.dig(:trigger, :project)
    next unless project

    scoped_variables.to_runner_variables.yield_self do |all_variables|
      ::ExpandVariables.expand(project, all_variables)
    end
  end
end

#downstream_variablesObject



217
218
219
# File 'app/models/ci/bridge.rb', line 217

def downstream_variables
  Gitlab::Ci::Variables::Downstream::Generator.new(self).calculate
end

#execute_hooksObject

Raises:

  • (NotImplementedError)


183
184
185
# File 'app/models/ci/bridge.rb', line 183

def execute_hooks
  raise NotImplementedError
end

#expand_file_refs?Boolean

Returns:

  • (Boolean)


255
256
257
258
259
# File 'app/models/ci/bridge.rb', line 255

def expand_file_refs?
  strong_memoize(:expand_file_refs) do
    !Feature.enabled?(:ci_prevent_file_var_expansion_downstream_pipeline, project)
  end
end

#forward_pipeline_variables?Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
253
# File 'app/models/ci/bridge.rb', line 247

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

#forward_yaml_variables?Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
# File 'app/models/ci/bridge.rb', line 239

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

#has_downstream_pipeline?Boolean

Returns:

  • (Boolean)


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

def has_downstream_pipeline?
  sourced_pipeline.present?
end

#inherit_status_from_downstream!(pipeline) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/ci/bridge.rb', line 78

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

#parent_pipelineObject



123
124
125
# File 'app/models/ci/bridge.rb', line 123

def parent_pipeline
  pipeline if triggers_child_pipeline?
end

#pipeline_schedule_variablesObject



233
234
235
236
237
# File 'app/models/ci/bridge.rb', line 233

def pipeline_schedule_variables
  return [] unless pipeline.pipeline_schedule

  pipeline.pipeline_schedule.variables.to_a
end

#pipeline_variablesObject



229
230
231
# File 'app/models/ci/bridge.rb', line 229

def pipeline_variables
  pipeline.variables
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.



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

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

#playable?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/ci/bridge.rb', line 153

def playable?
  action? && !archived? && manual?
end

#retryable?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'app/models/ci/bridge.rb', line 57

def retryable?
  return false if failed? && (pipeline_loop_detected? || reached_max_descendant_pipelines_depth?)

  super
end

#runnable?Boolean

Returns:

  • (Boolean)


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

def runnable?
  false
end

#schedulable?Boolean

Returns:

  • (Boolean)


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

def schedulable?
  false
end

#tagsObject



139
140
141
# File 'app/models/ci/bridge.rb', line 139

def tags
  [:bridge]
end

#target_refObject



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

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



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

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

#to_partial_pathObject



187
188
189
# File 'app/models/ci/bridge.rb', line 187

def to_partial_path
  'projects/generic_commit_statuses/generic_commit_status'
end

#triggers_child_pipeline?Boolean

Returns:

  • (Boolean)


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

def triggers_child_pipeline?
  yaml_for_downstream.present?
end

#triggers_cross_project_pipeline?Boolean

Returns:

  • (Boolean)


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

def triggers_cross_project_pipeline?
  downstream_project_path.present?
end

#triggers_downstream_pipeline?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/ci/bridge.rb', line 127

def triggers_downstream_pipeline?
  triggers_child_pipeline? || triggers_cross_project_pipeline?
end

#variablesObject



221
222
223
224
225
226
227
# File 'app/models/ci/bridge.rb', line 221

def variables
  strong_memoize(:variables) do
    Gitlab::Ci::Variables::Collection.new
     .concat(scoped_variables)
     .concat(pipeline.persisted_variables)
  end
end

#yaml_for_downstreamObject



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

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