Skip to content

Commit 8b4b7ca

Browse files
committed
Merge branch 'backstage/gb/refactor-only-except-policies-config' into 'master'
Refactor only/except configuration policies See merge request gitlab-org/gitlab-ce!24359
2 parents 11d6c7e + 3c5846c commit 8b4b7ca

File tree

13 files changed

+64
-39
lines changed

13 files changed

+64
-39
lines changed

lib/gitlab/ci/config/entry/job.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ class Job < ::Gitlab::Config::Entry::Node
1616
dependencies before_script after_script variables
1717
environment coverage retry parallel extends].freeze
1818

19-
DEFAULT_ONLY_POLICY = {
20-
refs: %w(branches tags)
21-
}.freeze
22-
23-
DEFAULT_EXCEPT_POLICY = {
24-
}.freeze
25-
2619
validations do
2720
validates :config, allowed_keys: ALLOWED_KEYS
2821
validates :config, presence: true
@@ -73,7 +66,8 @@ class Job < ::Gitlab::Config::Entry::Node
7366
description: 'Services that will be used to execute this job.'
7467

7568
entry :only, Entry::Policy,
76-
description: 'Refs policy this job will be executed for.'
69+
description: 'Refs policy this job will be executed for.',
70+
default: { refs: %w[branches tags] }
7771

7872
entry :except, Entry::Policy,
7973
description: 'Refs policy this job will be executed for.'
@@ -156,8 +150,8 @@ def to_hash
156150
services: services_value,
157151
stage: stage_value,
158152
cache: cache_value,
159-
only: DEFAULT_ONLY_POLICY.deep_merge(only_value.to_h),
160-
except: DEFAULT_EXCEPT_POLICY.deep_merge(except_value.to_h),
153+
only: only_value,
154+
except: except_value,
161155
variables: variables_defined? ? variables_value : nil,
162156
environment: environment_defined? ? environment_value : nil,
163157
environment_name: environment_defined? ? environment_value[:name] : nil,

lib/gitlab/ci/config/entry/policy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def errors
6464
end
6565
end
6666

67-
def self.default
67+
def value
68+
default.to_h.deep_merge(subject.value.to_h)
6869
end
6970
end
7071
end

lib/gitlab/ci/config/entry/retry.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ def location
8282
'retry config'
8383
end
8484
end
85-
86-
def self.default
87-
end
8885
end
8986
end
9087
end

lib/gitlab/ci/config/entry/variables.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Variables < ::Gitlab::Config::Entry::Node
1414
validates :config, variables: true
1515
end
1616

17-
def self.default
17+
def self.default(**)
1818
{}
1919
end
2020

lib/gitlab/config/entry/configurable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def nodes
5656
def entry(key, entry, metadata)
5757
factory = ::Gitlab::Config::Entry::Factory.new(entry)
5858
.with(description: metadata[:description])
59+
.with(default: metadata[:default])
5960

6061
(@nodes ||= {}).merge!(key.to_sym => factory)
6162
end

lib/gitlab/config/entry/factory.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Factory
1212
def initialize(entry)
1313
@entry = entry
1414
@metadata = {}
15-
@attributes = {}
15+
@attributes = { default: entry.default }
1616
end
1717

1818
def value(value)
@@ -21,12 +21,12 @@ def value(value)
2121
end
2222

2323
def metadata(metadata)
24-
@metadata.merge!(metadata)
24+
@metadata.merge!(metadata.compact)
2525
self
2626
end
2727

2828
def with(attributes)
29-
@attributes.merge!(attributes)
29+
@attributes.merge!(attributes.compact)
3030
self
3131
end
3232

@@ -38,9 +38,7 @@ def create!
3838
# See issue #18775.
3939
#
4040
if @value.nil?
41-
Entry::Unspecified.new(
42-
fabricate_unspecified
43-
)
41+
Entry::Unspecified.new(fabricate_unspecified)
4442
else
4543
fabricate(@entry, @value)
4644
end
@@ -53,17 +51,20 @@ def fabricate_unspecified
5351
# If entry has a default value we fabricate concrete node
5452
# with default value.
5553
#
56-
if @entry.default.nil?
54+
default = @attributes.fetch(:default)
55+
56+
if default.nil?
5757
fabricate(Entry::Undefined)
5858
else
59-
fabricate(@entry, @entry.default)
59+
fabricate(@entry, default)
6060
end
6161
end
6262

6363
def fabricate(entry, value = nil)
6464
entry.new(value, @metadata).tap do |node|
6565
node.key = @attributes[:key]
6666
node.parent = @attributes[:parent]
67+
node.default = @attributes[:default]
6768
node.description = @attributes[:description]
6869
end
6970
end

lib/gitlab/config/entry/node.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Node
1010
InvalidError = Class.new(StandardError)
1111

1212
attr_reader :config, :metadata
13-
attr_accessor :key, :parent, :description
13+
attr_accessor :key, :parent, :default, :description
1414

1515
def initialize(config, **metadata)
1616
@config = config
@@ -85,7 +85,7 @@ def inspect
8585
"#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
8686
end
8787

88-
def self.default
88+
def self.default(**)
8989
end
9090

9191
def self.aspects

lib/gitlab/config/entry/simplifiable.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Entry
66
class Simplifiable < SimpleDelegator
77
EntryStrategy = Struct.new(:name, :condition)
88

9+
attr_reader :subject
10+
911
def initialize(config, **metadata)
1012
unless self.class.const_defined?(:UnknownStrategy)
1113
raise ArgumentError, 'UndefinedStrategy not available!'
@@ -17,7 +19,7 @@ def initialize(config, **metadata)
1719

1820
entry = self.class.entry_class(strategy)
1921

20-
super(entry.new(config, metadata))
22+
super(@subject = entry.new(config, metadata))
2123
end
2224

2325
def self.strategy(name, **opts)
@@ -37,6 +39,9 @@ def self.entry_class(strategy)
3739
self::UnknownStrategy
3840
end
3941
end
42+
43+
def self.default
44+
end
4045
end
4146
end
4247
end

spec/lib/gitlab/ci/config/entry/global_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@
160160
variables: { 'VAR' => 'value' },
161161
ignore: false,
162162
after_script: ['make clean'],
163-
only: { refs: %w[branches tags] },
164-
except: {} },
163+
only: { refs: %w[branches tags] } },
165164
spinach: { name: :spinach,
166165
before_script: [],
167166
script: %w[spinach],
@@ -172,8 +171,7 @@
172171
variables: {},
173172
ignore: false,
174173
after_script: ['make clean'],
175-
only: { refs: %w[branches tags] },
176-
except: {} }
174+
only: { refs: %w[branches tags] } }
177175
)
178176
end
179177
end

spec/lib/gitlab/ci/config/entry/job_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@
258258
stage: 'test',
259259
ignore: false,
260260
after_script: %w[cleanup],
261-
only: { refs: %w[branches tags] },
262-
except: {})
261+
only: { refs: %w[branches tags] })
263262
end
264263
end
265264
end

0 commit comments

Comments
 (0)