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
description: 'Services that will be used to execute this job.'
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,
description: 'Refs policy this job will be executed for.'
......
......@@ -14,7 +14,7 @@ module Gitlab
validates :config, key: true
end
def self.default(**)
def self.default
'default'
end
end
......
......@@ -65,15 +65,9 @@ module Gitlab
end
def value
self.class.default(key: @subject.key).yield_self do |default|
default.to_h.deep_merge(@subject.value.to_h)
end
end
def self.default(**attributes)
{ refs: %w(branches tags) } if attributes[:key] == :only
end
end
end
end
end
......
......@@ -14,7 +14,7 @@ module Gitlab
validates :config, type: String
end
def self.default(**)
def self.default
'test'
end
end
......
......@@ -14,7 +14,7 @@ module Gitlab
validates :config, array_of_strings: true
end
def self.default(**)
def self.default
%w[build test deploy]
end
end
......
......@@ -56,6 +56,7 @@ module Gitlab
def entry(key, entry, metadata)
factory = ::Gitlab::Config::Entry::Factory.new(entry)
.with(description: metadata[:description])
.with(default: metadata[:default])
(@nodes ||= {}).merge!(key.to_sym => factory)
end
......
......@@ -12,7 +12,7 @@ module Gitlab
def initialize(entry)
@entry = entry
@metadata = {}
@attributes = {}
@attributes = { default: entry.default }
end
def value(value)
......@@ -21,12 +21,12 @@ module Gitlab
end
def metadata(metadata)
@metadata.merge!(metadata)
@metadata.merge!(metadata.compact)
self
end
def with(attributes)
@attributes.merge!(attributes)
@attributes.merge!(attributes.compact)
self
end
......@@ -38,9 +38,7 @@ module Gitlab
# See issue #18775.
#
if @value.nil?
Entry::Unspecified.new(
fabricate_unspecified
)
Entry::Unspecified.new(fabricate_unspecified)
else
fabricate(@entry, @value)
end
......@@ -53,19 +51,20 @@ module Gitlab
# If entry has a default value we fabricate concrete node
# with default value.
#
@entry.default(@attributes).yield_self do |default|
default = @attributes.fetch(:default)
if default.nil?
fabricate(Entry::Undefined)
else
fabricate(@entry, default)
end
end
end
def fabricate(entry, value = nil)
entry.new(value, @metadata).tap do |node|
node.key = @attributes[:key]
node.parent = @attributes[:parent]
node.default = @attributes[:default]
node.description = @attributes[:description]
end
end
......
......@@ -10,7 +10,7 @@ module Gitlab
InvalidError = Class.new(StandardError)
attr_reader :config, :metadata
attr_accessor :key, :parent, :description
attr_accessor :key, :parent, :default, :description
def initialize(config, **metadata)
@config = config
......
......@@ -38,7 +38,7 @@ module Gitlab
end
end
def self.default(**)
def self.default
end
end
end
......
......@@ -169,9 +169,9 @@ describe Gitlab::Ci::Config::Entry::Policy do
end
describe '#value' do
context 'when it is `only` policy' do
context 'when default value has been provided' do
before do
entry.key = :only
entry.default = { refs: %w[branches tags] }
end
context 'when user overrides default values' do
......@@ -182,7 +182,7 @@ describe Gitlab::Ci::Config::Entry::Policy do
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] } }
it 'includes default values' do
......@@ -191,34 +191,11 @@ describe Gitlab::Ci::Config::Entry::Policy do
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
describe '.default' do
context 'when `only` policy is about to be fabricated' do
it 'has a default value' do
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
it 'does not have default policy' do
expect(described_class.default).to be_nil
end
end
end
......@@ -7,6 +7,10 @@ describe Gitlab::Config::Entry::Configurable do
end
end
before do
allow(entry).to receive(:default)
end
describe 'validations' do
context 'when entry is a hash' do
let(:instance) { entry.new(key: 'value') }
......@@ -26,9 +30,11 @@ describe Gitlab::Config::Entry::Configurable do
end
describe 'configured entries' do
let(:another) { double('another', default: nil) }
before do
entry.class_eval do
entry :object, Object, description: 'test object'
entry.class_exec(another) do |another|
entry :object, another, description: 'test object'
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