debian-mirror-gitlab/lib/gitlab/ci/pipeline/seed/build.rb

117 lines
3.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module Ci
module Pipeline
module Seed
class Build < Seed::Base
include Gitlab::Utils::StrongMemoize
delegate :dig, to: :@attributes
2019-10-12 21:52:04 +05:30
# When the `ci_dag_limit_needs` is enabled it uses the lower limit
LOW_NEEDS_LIMIT = 5
HARD_NEEDS_LIMIT = 50
def initialize(pipeline, attributes, previous_stages)
2018-05-09 12:01:36 +05:30
@pipeline = pipeline
@attributes = attributes
2019-10-12 21:52:04 +05:30
@previous_stages = previous_stages
@needs_attributes = dig(:needs_attributes)
2018-05-09 12:01:36 +05:30
@only = Gitlab::Ci::Build::Policy
.fabricate(attributes.delete(:only))
@except = Gitlab::Ci::Build::Policy
.fabricate(attributes.delete(:except))
end
2019-10-12 21:52:04 +05:30
def name
dig(:name)
end
2018-05-09 12:01:36 +05:30
def included?
strong_memoize(:inclusion) do
2019-10-12 21:52:04 +05:30
all_of_only? &&
none_of_except?
end
end
def errors
return unless included?
strong_memoize(:errors) do
needs_errors
2018-05-09 12:01:36 +05:30
end
end
def attributes
@attributes.merge(
pipeline: @pipeline,
project: @pipeline.project,
user: @pipeline.user,
ref: @pipeline.ref,
tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref?
)
end
2019-03-02 22:35:43 +05:30
def bridge?
2019-10-12 21:52:04 +05:30
attributes_hash = @attributes.to_h
attributes_hash.dig(:options, :trigger).present? ||
(attributes_hash.dig(:options, :bridge_needs).instance_of?(Hash) &&
attributes_hash.dig(:options, :bridge_needs, :pipeline).present?)
2019-03-02 22:35:43 +05:30
end
2018-05-09 12:01:36 +05:30
def to_resource
strong_memoize(:resource) do
2019-03-02 22:35:43 +05:30
if bridge?
::Ci::Bridge.new(attributes)
else
::Ci::Build.new(attributes)
end
2018-05-09 12:01:36 +05:30
end
end
2019-10-12 21:52:04 +05:30
private
def all_of_only?
@only.all? { |spec| spec.satisfied_by?(@pipeline, self) }
end
def none_of_except?
@except.none? { |spec| spec.satisfied_by?(@pipeline, self) }
end
def needs_errors
return if @needs_attributes.nil?
if @needs_attributes.size > max_needs_allowed
return [
"#{name}: one job can only need #{max_needs_allowed} others, but you have listed #{@needs_attributes.size}. " \
"See needs keyword documentation for more details"
]
end
@needs_attributes.flat_map do |need|
result = @previous_stages.any? do |stage|
stage.seeds_names.include?(need[:name])
end
"#{name}: needs '#{need[:name]}'" unless result
end.compact
end
def max_needs_allowed
if Feature.enabled?(:ci_dag_limit_needs, @project, default_enabled: true)
LOW_NEEDS_LIMIT
else
HARD_NEEDS_LIMIT
end
end
2018-05-09 12:01:36 +05:30
end
end
end
end
end