Commit 7b712d35 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make default config entry value configurable

Introduce `default:` configuration entry setting that makes it possible
to configure a default value of an entry, what overrides class-level
`def self.default` value.
parent 0cba9bef
...@@ -66,7 +66,8 @@ module Gitlab ...@@ -66,7 +66,8 @@ module Gitlab
description: 'Services that will be used to execute this job.' description: 'Services that will be used to execute this job.'
entry :only, Entry::Policy, entry :only, Entry::Policy,
description: 'Refs policy this job will be executed for.' description: 'Refs policy this job will be executed for.',
default: { refs: %w[branches tags] }
entry :except, Entry::Policy, entry :except, Entry::Policy,
description: 'Refs policy this job will be executed for.' description: 'Refs policy this job will be executed for.'
......
...@@ -14,7 +14,7 @@ module Gitlab ...@@ -14,7 +14,7 @@ module Gitlab
validates :config, key: true validates :config, key: true
end end
def self.default(**) def self.default
'default' 'default'
end end
end end
......
...@@ -65,15 +65,9 @@ module Gitlab ...@@ -65,15 +65,9 @@ module Gitlab
end end
def value def value
self.class.default(key: @subject.key).yield_self do |default|
default.to_h.deep_merge(@subject.value.to_h) default.to_h.deep_merge(@subject.value.to_h)
end end
end end
def self.default(**attributes)
{ refs: %w(branches tags) } if attributes[:key] == :only
end
end
end end
end end
end end
......
...@@ -14,7 +14,7 @@ module Gitlab ...@@ -14,7 +14,7 @@ module Gitlab
validates :config, type: String validates :config, type: String
end end
def self.default(**) def self.default
'test' 'test'
end end
end end
......
...@@ -14,7 +14,7 @@ module Gitlab ...@@ -14,7 +14,7 @@ module Gitlab
validates :config, array_of_strings: true validates :config, array_of_strings: true
end end
def self.default(**) def self.default
%w[build test deploy] %w[build test deploy]
end end
end end
......
...@@ -56,6 +56,7 @@ module Gitlab ...@@ -56,6 +56,7 @@ module Gitlab
def entry(key, entry, metadata) def entry(key, entry, metadata)
factory = ::Gitlab::Config::Entry::Factory.new(entry) factory = ::Gitlab::Config::Entry::Factory.new(entry)
.with(description: metadata[:description]) .with(description: metadata[:description])
.with(default: metadata[:default])
(@nodes ||= {}).merge!(key.to_sym => factory) (@nodes ||= {}).merge!(key.to_sym => factory)
end end
......
...@@ -12,7 +12,7 @@ module Gitlab ...@@ -12,7 +12,7 @@ module Gitlab
def initialize(entry) def initialize(entry)
@entry = entry @entry = entry
@metadata = {} @metadata = {}
@attributes = {} @attributes = { default: entry.default }
end end
def value(value) def value(value)
...@@ -21,12 +21,12 @@ module Gitlab ...@@ -21,12 +21,12 @@ module Gitlab
end end
def metadata(metadata) def metadata(metadata)
@metadata.merge!(metadata) @metadata.merge!(metadata.compact)
self self
end end
def with(attributes) def with(attributes)
@attributes.merge!(attributes) @attributes.merge!(attributes.compact)
self self
end end
...@@ -38,9 +38,7 @@ module Gitlab ...@@ -38,9 +38,7 @@ module Gitlab
# See issue #18775. # See issue #18775.
# #
if @value.nil? if @value.nil?
Entry::Unspecified.new( Entry::Unspecified.new(fabricate_unspecified)
fabricate_unspecified
)
else else
fabricate(@entry, @value) fabricate(@entry, @value)
end end
...@@ -53,19 +51,20 @@ module Gitlab ...@@ -53,19 +51,20 @@ module Gitlab
# If entry has a default value we fabricate concrete node # If entry has a default value we fabricate concrete node
# with default value. # with default value.
# #
@entry.default(@attributes).yield_self do |default| default = @attributes.fetch(:default)
if default.nil? if default.nil?
fabricate(Entry::Undefined) fabricate(Entry::Undefined)
else else
fabricate(@entry, default) fabricate(@entry, default)
end end
end end
end
def fabricate(entry, value = nil) def fabricate(entry, value = nil)
entry.new(value, @metadata).tap do |node| entry.new(value, @metadata).tap do |node|
node.key = @attributes[:key] node.key = @attributes[:key]
node.parent = @attributes[:parent] node.parent = @attributes[:parent]
node.default = @attributes[:default]
node.description = @attributes[:description] node.description = @attributes[:description]
end end
end end
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
InvalidError = Class.new(StandardError) InvalidError = Class.new(StandardError)
attr_reader :config, :metadata attr_reader :config, :metadata
attr_accessor :key, :parent, :description attr_accessor :key, :parent, :default, :description
def initialize(config, **metadata) def initialize(config, **metadata)
@config = config @config = config
......
...@@ -38,7 +38,7 @@ module Gitlab ...@@ -38,7 +38,7 @@ module Gitlab
end end
end end
def self.default(**) def self.default
end end
end end
end end
......
...@@ -169,9 +169,9 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -169,9 +169,9 @@ describe Gitlab::Ci::Config::Entry::Policy do
end end
describe '#value' do describe '#value' do
context 'when it is `only` policy' do context 'when default value has been provided' do
before do before do
entry.key = :only entry.default = { refs: %w[branches tags] }
end end
context 'when user overrides default values' do context 'when user overrides default values' do
...@@ -182,7 +182,7 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -182,7 +182,7 @@ describe Gitlab::Ci::Config::Entry::Policy do
end end
end end
context 'when user does not override default values' do context 'when default value has not been defined' do
let(:config) { { variables: %w[$VARIABLE] } } let(:config) { { variables: %w[$VARIABLE] } }
it 'includes default values' do it 'includes default values' do
...@@ -191,34 +191,11 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -191,34 +191,11 @@ describe Gitlab::Ci::Config::Entry::Policy do
end end
end end
end end
context 'when it is `except` policy' do
before do
entry.key = :except
end
context 'when user does not override default values' do
let(:config) { { variables: %w[$VARIABLE] } }
it 'does not include default values' do
expect(entry.value).to eq config
end
end
end
end end
describe '.default' do describe '.default' do
context 'when `only` policy is about to be fabricated' do it 'does not have default policy' do
it 'has a default value' do expect(described_class.default).to be_nil
expect(described_class.default(key: :only))
.to eq(refs: %w[branches tags])
end
end
context 'when `except` policy is about to be fabricated' do
it 'does not have a default value' do
expect(described_class.default(key: :except)).to be_nil
end
end end
end end
end end
...@@ -7,6 +7,10 @@ describe Gitlab::Config::Entry::Configurable do ...@@ -7,6 +7,10 @@ describe Gitlab::Config::Entry::Configurable do
end end
end end
before do
allow(entry).to receive(:default)
end
describe 'validations' do describe 'validations' do
context 'when entry is a hash' do context 'when entry is a hash' do
let(:instance) { entry.new(key: 'value') } let(:instance) { entry.new(key: 'value') }
...@@ -26,9 +30,11 @@ describe Gitlab::Config::Entry::Configurable do ...@@ -26,9 +30,11 @@ describe Gitlab::Config::Entry::Configurable do
end end
describe 'configured entries' do describe 'configured entries' do
let(:another) { double('another', default: nil) }
before do before do
entry.class_eval do entry.class_exec(another) do |another|
entry :object, Object, description: 'test object' entry :object, another, description: 'test object'
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment