Commit 8c409fc4 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it possible to define CI/CD config strategies

parent 0d7d7c10
...@@ -5,12 +5,29 @@ module Gitlab ...@@ -5,12 +5,29 @@ module Gitlab
## ##
# Entry that represents a trigger policy for the job. # Entry that represents a trigger policy for the job.
# #
class Policy < Node class Policy < Simplifiable
include Validatable strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
strategy :ExpressionsPolicy, if: -> (config) { config.is_a?(Hash) }
class RefsPolicy < Entry::Node
include Entry::Validatable
validations do validations do
validates :config, array_of_strings_or_regexps: true validates :config, array_of_strings_or_regexps: true
end end
def value
{ refs: @config }
end
end
class ExpressionsPolicy < Entry::Node
include Entry::Validatable
validations do
validates :config, type: Hash
end
end
end end
end end
end end
......
module Gitlab
module Ci
class Config
module Entry
class Simplifiable < SimpleDelegator
EntryStrategy = Struct.new(:name, :condition)
def initialize(config, **metadata)
strategy = self.class.strategies.find do |variant|
variant.condition.call(config)
end
entry = self.class.const_get(strategy.name)
super(entry.new(config, metadata))
end
def self.strategy(name, **opts)
EntryStrategy.new(name, opts.fetch(:if)).tap do |strategy|
(@strategies ||= []).append(strategy)
end
end
def self.strategies
@strategies || []
end
end
end
end
end
end
...@@ -3,6 +3,7 @@ require 'spec_helper' ...@@ -3,6 +3,7 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Policy do describe Gitlab::Ci::Config::Entry::Policy do
let(:entry) { described_class.new(config) } let(:entry) { described_class.new(config) }
context 'when using simplified policy' do
describe 'validations' do describe 'validations' do
context 'when entry config value is valid' do context 'when entry config value is valid' do
context 'when config is a branch or tag name' do context 'when config is a branch or tag name' do
...@@ -16,7 +17,7 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -16,7 +17,7 @@ describe Gitlab::Ci::Config::Entry::Policy do
describe '#value' do describe '#value' do
it 'returns key value' do it 'returns key value' do
expect(entry.value).to eq config expect(entry.value).to eq(refs: config)
end end
end end
end end
...@@ -48,9 +49,13 @@ describe Gitlab::Ci::Config::Entry::Policy do ...@@ -48,9 +49,13 @@ describe Gitlab::Ci::Config::Entry::Policy do
describe '#errors' do describe '#errors' do
it 'saves errors' do it 'saves errors' do
expect(entry.errors) expect(entry.errors)
.to include 'policy config should be an array of strings or regexps' .to include /policy config should be an array of strings or regexps/
end end
end end
end end
end end
end
context 'when using complex policy' do
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