Commit 39ec62ea authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'fix/improve-naming-convention-in-ci-config' into 'master'

Improve naming convention in ci configuration module

## What does this MR do?

This MR improves the naming convention in CI configuration module to reflect the domain design better.

## What are the relevant issue numbers?

Related to #15060

See merge request !7448
parents 763a89a3 b3f38797
......@@ -2,7 +2,7 @@ module Ci
class GitlabCiYamlProcessor
class ValidationError < StandardError; end
include Gitlab::Ci::Config::Node::LegacyValidationHelpers
include Gitlab::Ci::Config::Entry::LegacyValidationHelpers
attr_reader :path, :cache, :stages, :jobs
......
......@@ -13,7 +13,7 @@ module Gitlab
def initialize(config)
@config = Loader.new(config).load!
@global = Node::Global.new(@config)
@global = Entry::Global.new(@config)
@global.compose!
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a configuration of job artifacts.
#
class Artifacts < Entry
class Artifacts < Node
include Validatable
include Attributable
......
module Gitlab
module Ci
class Config
module Node
module Entry
module Attributable
extend ActiveSupport::Concern
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a boolean value.
#
class Boolean < Entry
class Boolean < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a cache configuration
#
class Cache < Entry
class Cache < Node
include Configurable
ALLOWED_KEYS = %i[key untracked paths]
......@@ -14,13 +14,13 @@ module Gitlab
validates :config, allowed_keys: ALLOWED_KEYS
end
node :key, Node::Key,
entry :key, Entry::Key,
description: 'Cache key used to define a cache affinity.'
node :untracked, Node::Boolean,
entry :untracked, Entry::Boolean,
description: 'Cache all untracked files.'
node :paths, Node::Paths,
entry :paths, Entry::Paths,
description: 'Specify which paths should be cached across builds.'
end
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a job script.
#
class Commands < Entry
class Commands < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# This mixin is responsible for adding DSL, which purpose is to
# simplifly process of adding child nodes.
......@@ -48,8 +48,8 @@ module Gitlab
private # rubocop:disable Lint/UselessAccessModifier
def node(key, node, metadata)
factory = Node::Factory.new(node)
def entry(key, entry, metadata)
factory = Entry::Factory.new(entry)
.with(description: metadata[:description])
(@nodes ||= {}).merge!(key.to_sym => factory)
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents an environment.
#
class Environment < Entry
class Environment < Node
include Validatable
ALLOWED_KEYS = %i[name url action on_stop]
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Factory class responsible for fabricating node entry objects.
# Factory class responsible for fabricating entry objects.
#
class Factory
class InvalidFactory < StandardError; end
def initialize(node)
@node = node
def initialize(entry)
@entry = entry
@metadata = {}
@attributes = {}
end
......@@ -37,11 +37,11 @@ module Gitlab
# See issue #18775.
#
if @value.nil?
Node::Unspecified.new(
Entry::Unspecified.new(
fabricate_unspecified
)
else
fabricate(@node, @value)
fabricate(@entry, @value)
end
end
......@@ -49,21 +49,21 @@ module Gitlab
def fabricate_unspecified
##
# If node has a default value we fabricate concrete node
# If entry has a default value we fabricate concrete node
# with default value.
#
if @node.default.nil?
fabricate(Node::Undefined)
if @entry.default.nil?
fabricate(Entry::Undefined)
else
fabricate(@node, @node.default)
fabricate(@entry, @entry.default)
end
end
def fabricate(node, value = nil)
node.new(value, @metadata).tap do |entry|
entry.key = @attributes[:key]
entry.parent = @attributes[:parent]
entry.description = @attributes[:description]
def fabricate(entry, value = nil)
entry.new(value, @metadata).tap do |node|
node.key = @attributes[:key]
node.parent = @attributes[:parent]
node.description = @attributes[:description]
end
end
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# This class represents a global entry - root node for entire
# This class represents a global entry - root Entry for entire
# GitLab CI Configuration file.
#
class Global < Entry
class Global < Node
include Configurable
node :before_script, Node::Script,
entry :before_script, Entry::Script,
description: 'Script that will be executed before each job.'
node :image, Node::Image,
entry :image, Entry::Image,
description: 'Docker image that will be used to execute jobs.'
node :services, Node::Services,
entry :services, Entry::Services,
description: 'Docker images that will be linked to the container.'
node :after_script, Node::Script,
entry :after_script, Entry::Script,
description: 'Script that will be executed after each job.'
node :variables, Node::Variables,
entry :variables, Entry::Variables,
description: 'Environment variables that will be used.'
node :stages, Node::Stages,
entry :stages, Entry::Stages,
description: 'Configuration of stages for this pipeline.'
node :types, Node::Stages,
entry :types, Entry::Stages,
description: 'Deprecated: stages for this pipeline.'
node :cache, Node::Cache,
entry :cache, Entry::Cache,
description: 'Configure caching between build jobs.'
helpers :before_script, :image, :services, :after_script,
......@@ -46,7 +46,7 @@ module Gitlab
private
def compose_jobs!
factory = Node::Factory.new(Node::Jobs)
factory = Entry::Factory.new(Entry::Jobs)
.value(@config.except(*self.class.nodes.keys))
.with(key: :jobs, parent: self,
description: 'Jobs definition for this pipeline')
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a hidden CI/CD job.
# Entry that represents a hidden CI/CD key.
#
class Hidden < Entry
class Hidden < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a Docker image.
#
class Image < Entry
class Image < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a concrete CI/CD job.
#
class Job < Entry
class Job < Node
include Configurable
include Attributable
......@@ -34,43 +34,43 @@ module Gitlab
end
end
node :before_script, Node::Script,
entry :before_script, Entry::Script,
description: 'Global before script overridden in this job.'
node :script, Node::Commands,
entry :script, Entry::Commands,
description: 'Commands that will be executed in this job.'
node :stage, Node::Stage,
entry :stage, Entry::Stage,
description: 'Pipeline stage this job will be executed into.'
node :type, Node::Stage,
entry :type, Entry::Stage,
description: 'Deprecated: stage this job will be executed into.'
node :after_script, Node::Script,
entry :after_script, Entry::Script,
description: 'Commands that will be executed when finishing job.'
node :cache, Node::Cache,
entry :cache, Entry::Cache,
description: 'Cache definition for this job.'
node :image, Node::Image,
entry :image, Entry::Image,
description: 'Image that will be used to execute this job.'
node :services, Node::Services,
entry :services, Entry::Services,
description: 'Services that will be used to execute this job.'
node :only, Node::Trigger,
entry :only, Entry::Trigger,
description: 'Refs policy this job will be executed for.'
node :except, Node::Trigger,
entry :except, Entry::Trigger,
description: 'Refs policy this job will be executed for.'
node :variables, Node::Variables,
entry :variables, Entry::Variables,
description: 'Environment variables available for this job.'
node :artifacts, Node::Artifacts,
entry :artifacts, Entry::Artifacts,
description: 'Artifacts configuration for this job.'
node :environment, Node::Environment,
entry :environment, Entry::Environment,
description: 'Environment configuration for this job.'
helpers :before_script, :script, :stage, :type, :after_script,
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a set of jobs.
#
class Jobs < Entry
class Jobs < Node
include Validatable
validations do
......@@ -29,9 +29,9 @@ module Gitlab
def compose!(deps = nil)
super do
@config.each do |name, config|
node = hidden?(name) ? Node::Hidden : Node::Job
node = hidden?(name) ? Entry::Hidden : Entry::Job
factory = Node::Factory.new(node)
factory = Entry::Factory.new(node)
.value(config || {})
.metadata(name: name)
.with(key: name, parent: self,
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a key.
#
class Key < Entry
class Key < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
module LegacyValidationHelpers
private
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Base abstract class for each configuration entry node.
#
class Entry
class Node
class InvalidError < StandardError; end
attr_reader :config, :metadata
......@@ -21,7 +21,7 @@ module Gitlab
end
def [](key)
@entries[key] || Node::Undefined.new
@entries[key] || Entry::Undefined.new
end
def compose!(deps = nil)
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents an array of paths.
#
class Paths < Entry
class Paths < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a script.
#
class Script < Entry
class Script < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a configuration of Docker services.
#
class Services < Entry
class Services < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a stage for a job.
#
class Stage < Entry
class Stage < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a configuration for pipeline stages.
#
class Stages < Entry
class Stages < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents a trigger policy for the job.
#
class Trigger < Entry
class Trigger < Node
include Validatable
validations do
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# This class represents an undefined node.
# This class represents an undefined entry.
#
# Implements the Null Object pattern.
#
class Undefined < Entry
class Undefined < Node
def initialize(*)
super(nil)
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# This class represents an unspecified entry node.
# This class represents an unspecified entry.
#
# It decorates original entry adding method that indicates it is
# unspecified.
......
module Gitlab
module Ci
class Config
module Node
module Entry
module Validatable
extend ActiveSupport::Concern
class_methods do
def validator
@validator ||= Class.new(Node::Validator).tap do |validator|
@validator ||= Class.new(Entry::Validator).tap do |validator|
if defined?(@validations)
@validations.each { |rules| validator.class_eval(&rules) }
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
class Validator < SimpleDelegator
include ActiveModel::Validations
include Node::Validators
include Entry::Validators
def initialize(node)
super(node)
@node = node
def initialize(entry)
super(entry)
@entry = entry
end
def messages
......@@ -30,7 +30,7 @@ module Gitlab
def key_name
if key.blank?
@node.class.name.demodulize.underscore.humanize
@entry.class.name.demodulize.underscore.humanize
else
key
end
......
module Gitlab
module Ci
class Config
module Node
module Entry
module Validators
class AllowedKeysValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
......
module Gitlab
module Ci
class Config
module Node
module Entry
##
# Entry that represents environment variables.
#
class Variables < Entry
class Variables < Node
include Validatable
validations do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Artifacts do
describe Gitlab::Ci::Config::Entry::Artifacts do
let(:entry) { described_class.new(config) }
describe 'validation' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Attributable do
describe Gitlab::Ci::Config::Entry::Attributable do
let(:node) { Class.new }
let(:instance) { node.new }
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Boolean do
describe Gitlab::Ci::Config::Entry::Boolean do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Cache do
describe Gitlab::Ci::Config::Entry::Cache do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Commands do
describe Gitlab::Ci::Config::Entry::Commands do
let(:entry) { described_class.new(config) }
context 'when entry config value is an array' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Configurable do
let(:node) { Class.new }
describe Gitlab::Ci::Config::Entry::Configurable do
let(:entry) { Class.new }
before do
node.include(described_class)
entry.include(described_class)
end
describe 'validations' do
let(:validator) { node.validator.new(instance) }
let(:validator) { entry.validator.new(instance) }
before do
node.class_eval do
entry.class_eval do
attr_reader :config
def initialize(config)
......@@ -22,16 +22,16 @@ describe Gitlab::Ci::Config::Node::Configurable do
validator.validate
end
context 'when node validator is invalid' do
let(:instance) { node.new('ls') }
context 'when entry validator is invalid' do
let(:instance) { entry.new('ls') }
it 'returns invalid validator' do
expect(validator).to be_invalid
end
end
context 'when node instance is valid' do
let(:instance) { node.new(key: 'value') }
context 'when entry instance is valid' do
let(:instance) { entry.new(key: 'value') }
it 'returns valid validator' do
expect(validator).to be_valid
......@@ -39,26 +39,26 @@ describe Gitlab::Ci::Config::Node::Configurable do
end
end
describe 'configured nodes' do
describe 'configured entries' do
before do
node.class_eval do
node :object, Object, description: 'test object'
entry.class_eval do
entry :object, Object, description: 'test object'
end
end
describe '.nodes' do
it 'has valid nodes' do
expect(node.nodes).to include :object
expect(entry.nodes).to include :object
end
it 'creates a node factory' do
expect(node.nodes[:object])
.to be_an_instance_of Gitlab::Ci::Config::Node::Factory
expect(entry.nodes[:object])
.to be_an_instance_of Gitlab::Ci::Config::Entry::Factory
end
it 'returns a duplicated factory object' do
first_factory = node.nodes[:object]
second_factory = node.nodes[:object]
first_factory = entry.nodes[:object]
second_factory = entry.nodes[:object]
expect(first_factory).not_to be_equal(second_factory)
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Environment do
describe Gitlab::Ci::Config::Entry::Environment do
let(:entry) { described_class.new(config) }
before { entry.compose! }
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Factory do
describe Gitlab::Ci::Config::Entry::Factory do
describe '#create!' do
let(:factory) { described_class.new(node) }
let(:node) { Gitlab::Ci::Config::Node::Script }
let(:factory) { described_class.new(entry) }
let(:entry) { Gitlab::Ci::Config::Entry::Script }
context 'when setting a concrete value' do
it 'creates entry with valid value' do
......@@ -54,7 +54,7 @@ describe Gitlab::Ci::Config::Node::Factory do
context 'when not setting a value' do
it 'raises error' do
expect { factory.create! }.to raise_error(
Gitlab::Ci::Config::Node::Factory::InvalidFactory
Gitlab::Ci::Config::Entry::Factory::InvalidFactory
)
end
end
......@@ -66,12 +66,12 @@ describe Gitlab::Ci::Config::Node::Factory do
.create!
expect(entry)
.to be_an_instance_of Gitlab::Ci::Config::Node::Unspecified
.to be_an_instance_of Gitlab::Ci::Config::Entry::Unspecified
end
end
context 'when passing metadata' do
let(:node) { spy('node') }
let(:entry) { spy('entry') }
it 'passes metadata as a parameter' do
factory
......@@ -79,7 +79,7 @@ describe Gitlab::Ci::Config::Node::Factory do
.metadata(some: 'hash')
.create!
expect(node).to have_received(:new)
expect(entry).to have_received(:new)
.with('some value', { some: 'hash' })
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Global do
describe Gitlab::Ci::Config::Entry::Global do
let(:global) { described_class.new(hash) }
describe '.nodes' do
......@@ -40,9 +40,9 @@ describe Gitlab::Ci::Config::Node::Global do
it 'creates node object using valid class' do
expect(global.descendants.first)
.to be_an_instance_of Gitlab::Ci::Config::Node::Script
.to be_an_instance_of Gitlab::Ci::Config::Entry::Script
expect(global.descendants.second)
.to be_an_instance_of Gitlab::Ci::Config::Node::Image
.to be_an_instance_of Gitlab::Ci::Config::Entry::Image
end
it 'sets correct description for nodes' do
......@@ -181,7 +181,7 @@ describe Gitlab::Ci::Config::Node::Global do
it 'contains unspecified nodes' do
expect(global.descendants.first)
.to be_an_instance_of Gitlab::Ci::Config::Node::Unspecified
.to be_an_instance_of Gitlab::Ci::Config::Entry::Unspecified
end
end
......@@ -284,7 +284,7 @@ describe Gitlab::Ci::Config::Node::Global do
context 'when node exists' do
it 'returns correct entry' do
expect(global[:cache])
.to be_an_instance_of Gitlab::Ci::Config::Node::Cache
.to be_an_instance_of Gitlab::Ci::Config::Entry::Cache
expect(global[:jobs][:rspec][:script].value).to eq ['ls']
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Hidden do
describe Gitlab::Ci::Config::Entry::Hidden do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Image do
describe Gitlab::Ci::Config::Entry::Image do
let(:entry) { described_class.new(config) }
describe 'validation' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Job do
describe Gitlab::Ci::Config::Entry::Job do
let(:entry) { described_class.new(config, name: :rspec) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Jobs do
describe Gitlab::Ci::Config::Entry::Jobs do
let(:entry) { described_class.new(config) }
describe 'validations' do
......@@ -74,9 +74,9 @@ describe Gitlab::Ci::Config::Node::Jobs do
it 'creates valid descendant nodes' do
expect(entry.descendants.count).to eq 3
expect(entry.descendants.first(2))
.to all(be_an_instance_of(Gitlab::Ci::Config::Node::Job))
.to all(be_an_instance_of(Gitlab::Ci::Config::Entry::Job))
expect(entry.descendants.last)
.to be_an_instance_of(Gitlab::Ci::Config::Node::Hidden)
.to be_an_instance_of(Gitlab::Ci::Config::Entry::Hidden)
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Key do
describe Gitlab::Ci::Config::Entry::Key do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Paths do
describe Gitlab::Ci::Config::Entry::Paths do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Script do
describe Gitlab::Ci::Config::Entry::Script do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Services do
describe Gitlab::Ci::Config::Entry::Services do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Stage do
describe Gitlab::Ci::Config::Entry::Stage do
let(:stage) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Stages do
describe Gitlab::Ci::Config::Entry::Stages do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Trigger do
describe Gitlab::Ci::Config::Entry::Trigger do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Undefined do
describe Gitlab::Ci::Config::Entry::Undefined do
let(:entry) { described_class.new }
describe '#leaf?' do
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Unspecified do
describe Gitlab::Ci::Config::Entry::Unspecified do
let(:unspecified) { described_class.new(entry) }
let(:entry) { spy('Entry') }
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Validatable do
let(:node) { Class.new }
describe Gitlab::Ci::Config::Entry::Validatable do
let(:entry) { Class.new }
before do
node.include(described_class)
entry.include(described_class)
end
describe '.validator' do
before do
node.class_eval do
entry.class_eval do
attr_accessor :test_attribute
validations do
......@@ -19,34 +19,34 @@ describe Gitlab::Ci::Config::Node::Validatable do
end
it 'returns validator' do
expect(node.validator.superclass)
.to be Gitlab::Ci::Config::Node::Validator
expect(entry.validator.superclass)
.to be Gitlab::Ci::Config::Entry::Validator
end
it 'returns only one validator to mitigate leaks' do
expect { node.validator }.not_to change { node.validator }
expect { entry.validator }.not_to change { entry.validator }
end
context 'when validating node instance' do
let(:node_instance) { node.new }
context 'when validating entry instance' do
let(:entry_instance) { entry.new }
context 'when attribute is valid' do
before do
node_instance.test_attribute = 'valid'
entry_instance.test_attribute = 'valid'
end
it 'instance of validator is valid' do
expect(node.validator.new(node_instance)).to be_valid
expect(entry.validator.new(entry_instance)).to be_valid
end
end
context 'when attribute is not valid' do
before do
node_instance.test_attribute = nil
entry_instance.test_attribute = nil
end
it 'instance of validator is invalid' do
expect(node.validator.new(node_instance)).to be_invalid
expect(entry.validator.new(entry_instance)).to be_invalid
end
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Validator do
describe Gitlab::Ci::Config::Entry::Validator do
let(:validator) { Class.new(described_class) }
let(:validator_instance) { validator.new(node) }
let(:node) { spy('node') }
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Variables do
describe Gitlab::Ci::Config::Entry::Variables do
let(:entry) { described_class.new(config) }
describe 'validations' do
......
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