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 ...@@ -5,11 +5,28 @@ 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) }
validations do class RefsPolicy < Entry::Node
validates :config, array_of_strings_or_regexps: true 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 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' ...@@ -3,54 +3,59 @@ 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) }
describe 'validations' do context 'when using simplified policy' do
context 'when entry config value is valid' do describe 'validations' do
context 'when config is a branch or tag name' do context 'when entry config value is valid' do
let(:config) { %w[master feature/branch] } context 'when config is a branch or tag name' do
let(:config) { %w[master feature/branch] }
describe '#valid?' do
it 'is valid' do describe '#valid?' do
expect(entry).to be_valid it 'is valid' do
expect(entry).to be_valid
end
end end
end
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
context 'when config is a regexp' do context 'when config is a regexp' do
let(:config) { ['/^issue-.*$/'] } let(:config) { ['/^issue-.*$/'] }
describe '#valid?' do describe '#valid?' do
it 'is valid' do it 'is valid' do
expect(entry).to be_valid expect(entry).to be_valid
end
end end
end end
end
context 'when config is a special keyword' do context 'when config is a special keyword' do
let(:config) { %w[tags triggers branches] } let(:config) { %w[tags triggers branches] }
describe '#valid?' do describe '#valid?' do
it 'is valid' do it 'is valid' do
expect(entry).to be_valid expect(entry).to be_valid
end
end end
end end
end end
end
context 'when entry value is not valid' do context 'when entry value is not valid' do
let(:config) { [1] } let(:config) { [1] }
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