debian-mirror-gitlab/lib/gitlab/ci/yaml_processor/result.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.4 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
# A data object that wraps `Ci::Config` and any messages
# (errors, warnings) generated by the YamlProcessor.
module Gitlab
module Ci
class YamlProcessor
class Result
2023-01-13 00:05:48 +05:30
attr_reader :errors, :warnings,
:root_variables, :root_variables_with_prefill_data,
:stages, :jobs,
:workflow_rules, :workflow_name
2020-11-24 15:15:51 +05:30
def initialize(ci_config: nil, errors: [], warnings: [])
@ci_config = ci_config
@errors = errors || []
@warnings = warnings || []
2023-01-13 00:05:48 +05:30
assign_valid_attributes if valid?
2020-11-24 15:15:51 +05:30
end
def valid?
errors.empty?
end
def stages_attributes
stages.uniq.map do |stage|
seeds = stage_builds_attributes(stage)
{ name: stage, index: stages.index(stage), builds: seeds }
end
end
def builds
jobs.map do |name, _|
build_attributes(name)
end
end
2021-03-08 18:12:59 +05:30
def included_templates
@included_templates ||= @ci_config.included_templates
end
2022-11-25 23:54:43 +05:30
def yaml_variables_for(job_name)
job = jobs[job_name]
return [] unless job
Gitlab::Ci::Variables::Helpers.inherit_yaml_variables(
from: root_variables,
to: job[:job_variables],
inheritance: job.fetch(:root_variables_inheritance, true)
)
end
def stage_for(job_name)
jobs.dig(job_name, :stage)
end
def config_metadata
@ci_config&.metadata || {}
end
private
2023-01-13 00:05:48 +05:30
def assign_valid_attributes
2023-03-17 16:20:25 +05:30
@root_variables = transform_to_array(@ci_config.variables_with_data)
2023-01-13 00:05:48 +05:30
@root_variables_with_prefill_data = @ci_config.variables_with_prefill_data
@stages = @ci_config.stages
@jobs = @ci_config.normalized_jobs
@workflow_rules = @ci_config.workflow_rules
@workflow_name = @ci_config.workflow_name&.strip
end
2022-11-25 23:54:43 +05:30
def stage_builds_attributes(stage)
jobs.values
.select { |job| job[:stage] == stage }
.map { |job| build_attributes(job[:name]) }
end
2020-11-24 15:15:51 +05:30
def build_attributes(name)
job = jobs.fetch(name.to_sym, {})
{ stage_idx: stages.index(job[:stage]),
stage: job[:stage],
tag_list: job[:tags],
name: job[:name].to_s,
allow_failure: job[:ignore],
when: job[:when] || 'on_success',
environment: job[:environment_name],
coverage_regex: job[:coverage],
2021-10-27 15:23:28 +05:30
# yaml_variables is calculated with using job_variables in Seed::Build
2022-10-11 01:57:18 +05:30
job_variables: transform_to_array(job[:job_variables]),
2021-04-29 21:17:54 +05:30
root_variables_inheritance: job[:root_variables_inheritance],
2020-11-24 15:15:51 +05:30
needs_attributes: job.dig(:needs, :job),
interruptible: job[:interruptible],
only: job[:only],
except: job[:except],
rules: job[:rules],
cache: job[:cache],
resource_group_key: job[:resource_group],
scheduling_type: job[:scheduling_type],
2023-03-04 22:38:38 +05:30
id_tokens: job[:id_tokens],
2020-11-24 15:15:51 +05:30
options: {
image: job[:image],
services: job[:services],
2021-02-22 17:27:13 +05:30
allow_failure_criteria: job[:allow_failure_criteria],
2020-11-24 15:15:51 +05:30
artifacts: job[:artifacts],
dependencies: job[:dependencies],
cross_dependencies: job.dig(:needs, :cross_dependency),
job_timeout: job[:timeout],
before_script: job[:before_script],
script: job[:script],
after_script: job[:after_script],
2023-03-04 22:38:38 +05:30
hooks: job[:hooks],
2020-11-24 15:15:51 +05:30
environment: job[:environment],
2022-01-26 12:08:38 +05:30
resource_group_key: job[:resource_group],
2020-11-24 15:15:51 +05:30
retry: job[:retry],
parallel: job[:parallel],
instance: job[:instance],
start_in: job[:start_in],
trigger: job[:trigger],
bridge_needs: job.dig(:needs, :bridge)&.first,
2023-06-20 00:43:36 +05:30
release: job[:release],
publish: job[:publish]
2020-11-24 15:15:51 +05:30
}.compact }.compact
end
2022-10-11 01:57:18 +05:30
def transform_to_array(variables)
::Gitlab::Ci::Variables::Helpers.transform_to_array(variables)
2020-11-24 15:15:51 +05:30
end
end
end
end
end
2021-09-04 01:27:46 +05:30
Gitlab::Ci::YamlProcessor::Result.prepend_mod_with('Gitlab::Ci::YamlProcessor::Result')