From 8baee987beaea8197d28ee9715ef23f5813566e5 Mon Sep 17 00:00:00 2001
From: Grzegorz Bizon <grzesiek.bizon@gmail.com>
Date: Fri, 8 Jul 2016 11:27:36 +0200
Subject: [PATCH] Extract internal attributes validator for CI entry

---
 lib/gitlab/ci/config/node/stage.rb           | 8 +-------
 lib/gitlab/ci/config/node/validators.rb      | 9 +++++++++
 spec/lib/gitlab/ci/config/node/stage_spec.rb | 6 ++++--
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/lib/gitlab/ci/config/node/stage.rb b/lib/gitlab/ci/config/node/stage.rb
index c7e12f291d7..9c76cf7c0b7 100644
--- a/lib/gitlab/ci/config/node/stage.rb
+++ b/lib/gitlab/ci/config/node/stage.rb
@@ -10,13 +10,7 @@ module Gitlab
 
           validations do
             validates :config, key: true
-
-            validate do |entry|
-              unless entry.global
-                raise Entry::InvalidError,
-                  'This entry needs reference to global configuration'
-              end
-            end
+            validates :global, required_attribute: true
           end
 
           def self.default
diff --git a/lib/gitlab/ci/config/node/validators.rb b/lib/gitlab/ci/config/node/validators.rb
index 7b2f57990b5..6f0e14e2f0a 100644
--- a/lib/gitlab/ci/config/node/validators.rb
+++ b/lib/gitlab/ci/config/node/validators.rb
@@ -33,6 +33,15 @@ module Gitlab
             end
           end
 
+          class RequiredAttributeValidator < ActiveModel::EachValidator
+            def validate_each(record, attribute, value)
+              if value.nil?
+                raise Entry::InvalidError,
+                  "Entry needs #{attribute} attribute set internally."
+              end
+            end
+          end
+
           class KeyValidator < ActiveModel::EachValidator
             include LegacyValidationHelpers
 
diff --git a/spec/lib/gitlab/ci/config/node/stage_spec.rb b/spec/lib/gitlab/ci/config/node/stage_spec.rb
index 92150ea5337..4047d46c80f 100644
--- a/spec/lib/gitlab/ci/config/node/stage_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/stage_spec.rb
@@ -27,8 +27,10 @@ describe Gitlab::Ci::Config::Node::Stage do
           let(:entry) { described_class.new(config) }
 
           it 'raises error' do
-            expect { entry }
-              .to raise_error Gitlab::Ci::Config::Node::Entry::InvalidError
+            expect { entry }.to raise_error(
+              Gitlab::Ci::Config::Node::Entry::InvalidError,
+              /Entry needs global attribute set internally./
+            )
           end
         end
 
-- 
GitLab