summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-08-25 10:27:00 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-08-25 10:27:00 +0200
commitfcb4d1f80945cbcdfbdb73e102d046447f55e5b5 (patch)
tree575e03a5ca43bf8af583d958fccc817315dd5400
parent8c409fc40ba4bf2e7fe0c8458fd2b59c09bd123a (diff)
downloadgitlab-ce-fcb4d1f80945cbcdfbdb73e102d046447f55e5b5.tar.gz
Implement complex only/except policy CI/CD config
-rw-r--r--lib/gitlab/ci/config/entry/policy.rb20
-rw-r--r--lib/gitlab/ci/config/entry/simplifiable.rb12
-rw-r--r--spec/lib/gitlab/ci/config/entry/policy_spec.rb26
3 files changed, 54 insertions, 4 deletions
diff --git a/lib/gitlab/ci/config/entry/policy.rb b/lib/gitlab/ci/config/entry/policy.rb
index ac564a6c7d0..decacebee55 100644
--- a/lib/gitlab/ci/config/entry/policy.rb
+++ b/lib/gitlab/ci/config/entry/policy.rb
@@ -3,7 +3,7 @@ module Gitlab
class Config
module Entry
##
- # Entry that represents a trigger policy for the job.
+ # Entry that represents an only/except trigger policy for the job.
#
class Policy < Simplifiable
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
@@ -23,9 +23,25 @@ module Gitlab
class ExpressionsPolicy < Entry::Node
include Entry::Validatable
+ include Entry::Attributable
+
+ attributes :refs, :expressions
validations do
- validates :config, type: Hash
+ validates :config, presence: true
+ validates :config, allowed_keys: %i[refs expressions]
+
+ with_options allow_nil: true do
+ validates :refs, array_of_strings_or_regexps: true
+ validates :expressions, type: Array
+ validates :expressions, presence: true
+ end
+ end
+ end
+
+ class UnknownStrategy < Entry::Node
+ def errors
+ ['policy has to be either an array of conditions or a hash']
end
end
end
diff --git a/lib/gitlab/ci/config/entry/simplifiable.rb b/lib/gitlab/ci/config/entry/simplifiable.rb
index 5319065b846..c26576fcbcd 100644
--- a/lib/gitlab/ci/config/entry/simplifiable.rb
+++ b/lib/gitlab/ci/config/entry/simplifiable.rb
@@ -10,7 +10,7 @@ module Gitlab
variant.condition.call(config)
end
- entry = self.class.const_get(strategy.name)
+ entry = self.class.entry_class(strategy)
super(entry.new(config, metadata))
end
@@ -22,7 +22,15 @@ module Gitlab
end
def self.strategies
- @strategies || []
+ @strategies.to_a
+ end
+
+ def self.entry_class(strategy)
+ if strategy.present?
+ self.const_get(strategy.name)
+ else
+ self::UnknownStrategy
+ end
end
end
end
diff --git a/spec/lib/gitlab/ci/config/entry/policy_spec.rb b/spec/lib/gitlab/ci/config/entry/policy_spec.rb
index fe9a053c46c..69095144daa 100644
--- a/spec/lib/gitlab/ci/config/entry/policy_spec.rb
+++ b/spec/lib/gitlab/ci/config/entry/policy_spec.rb
@@ -57,5 +57,31 @@ describe Gitlab::Ci::Config::Entry::Policy do
end
context 'when using complex policy' do
+ context 'when it is an empty hash' do
+ let(:config) { { } }
+
+ it 'reports an error about configuration not being present' do
+ expect(entry.errors).to include /can't be blank/
+ end
+ end
+
+ context 'when it contains unknown keys' do
+ let(:config) { { refs: ['something'], invalid: 'master' } }
+
+ it 'is not valid entry' do
+ expect(entry).not_to be_valid
+ expect(entry.errors)
+ .to include /policy config contains unknown keys: invalid/
+ end
+ end
+ end
+
+ context 'when policy strategy does not match' do
+ let(:config) { 'string strategy' }
+
+ it 'returns information about errors' do
+ expect(entry.errors)
+ .to include 'policy has to be either an array of conditions or a hash'
+ end
end
end