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

Make it possible to define CI/CD config strategies

parent 0d7d7c10
......@@ -5,11 +5,28 @@ module Gitlab
##
# Entry that represents a trigger policy for the job.
#
class Policy < Node
include Validatable
class Policy < Simplifiable
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
strategy :ExpressionsPolicy, if: -> (config) { config.is_a?(Hash) }
validations do
validates :config, array_of_strings_or_regexps: true
class RefsPolicy < Entry::Node
include Entry::Validatable
validations do
validates :config, array_of_strings_or_regexps: true
end
def value
{ refs: @config }
end
end
class ExpressionsPolicy < Entry::Node
include Entry::Validatable
validations do
validates :config, type: Hash
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,54 +3,59 @@ require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Policy do
let(:entry) { described_class.new(config) }
describe 'validations' do
context 'when entry config value is valid' do
context 'when config is a branch or tag name' do
let(:config) { %w[master feature/branch] }
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
context 'when using simplified policy' do
describe 'validations' do
context 'when entry config value is valid' do
context 'when config is a branch or tag name' do
let(:config) { %w[master feature/branch] }
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq config
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq(refs: config)
end
end
end
end
context 'when config is a regexp' do
let(:config) { ['/^issue-.*$/'] }
context 'when config is a regexp' do
let(:config) { ['/^issue-.*$/'] }
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
end
context 'when config is a special keyword' do
let(:config) { %w[tags triggers branches] }
context 'when config is a special keyword' do
let(:config) { %w[tags triggers branches] }
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
describe '#valid?' do
it 'is valid' do
expect(entry).to be_valid
end
end
end
end
end
context 'when entry value is not valid' do
let(:config) { [1] }
context 'when entry value is not valid' do
let(:config) { [1] }
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include 'policy config should be an array of strings or regexps'
describe '#errors' do
it 'saves errors' do
expect(entry.errors)
.to include /policy config should be an array of strings or regexps/
end
end
end
end
end
context 'when using complex policy' do
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