Commit 9458192f authored by drew's avatar drew Committed by Kamil Trzciński

Passing job:rules downstream and E2E specs for job:rules configuration

parent ee5bd244
---
title: Passing job rules downstream and E2E specs for job:rules configuration
merge_request: 32609
author:
type: fixed
......@@ -6,7 +6,14 @@ module Gitlab
class Rules
include ::Gitlab::Utils::StrongMemoize
Result = Struct.new(:when, :start_in)
Result = Struct.new(:when, :start_in) do
def build_attributes
{
when: self.when,
options: { start_in: start_in }.compact
}.compact
end
end
def initialize(rule_hashes, default_when = 'on_success')
@rule_list = Rule.fabricate_list(rule_hashes)
......
......@@ -13,9 +13,7 @@ module Gitlab
UnknownClauseError = Class.new(StandardError)
def self.fabricate(type, value)
type = type.to_s.camelize
self.const_get(type).new(value) if self.const_defined?(type)
"#{self}::#{type.to_s.camelize}".safe_constantize&.new(value)
end
def initialize(spec)
......
......@@ -122,7 +122,7 @@ module Gitlab
helpers :before_script, :script, :stage, :type, :after_script,
:cache, :image, :services, :only, :except, :variables,
:artifacts, :environment, :coverage, :retry,
:artifacts, :environment, :coverage, :retry, :rules,
:parallel, :needs, :interruptible
attributes :script, :tags, :allow_failure, :when, :dependencies,
......@@ -145,6 +145,13 @@ module Gitlab
end
@entries.delete(:type)
# This is something of a hack, see issue for details:
# https://gitlab.com/gitlab-org/gitlab-ce/issues/67150
if !only_defined? && has_rules?
@entries.delete(:only)
@entries.delete(:except)
end
end
inherit!(deps)
......@@ -203,6 +210,7 @@ module Gitlab
cache: cache_value,
only: only_value,
except: except_value,
rules: has_rules? ? rules_value : nil,
variables: variables_defined? ? variables_value : {},
environment: environment_defined? ? environment_value : nil,
environment_name: environment_defined? ? environment_value[:name] : nil,
......
......@@ -26,6 +26,10 @@ module Gitlab
end
end
end
def value
@config
end
end
end
end
......
......@@ -145,7 +145,7 @@ module Gitlab
def rules_attributes
strong_memoize(:rules_attributes) do
@using_rules ? @rules.evaluate(@pipeline, self).to_h.compact : {}
@using_rules ? @rules.evaluate(@pipeline, self).build_attributes : {}
end
end
end
......
......@@ -42,6 +42,7 @@ module Gitlab
yaml_variables: yaml_variables(name),
needs_attributes: job[:needs]&.map { |need| { name: need } },
interruptible: job[:interruptible],
rules: job[:rules],
options: {
image: job[:image],
services: job[:services],
......
......@@ -46,7 +46,7 @@ describe Gitlab::Ci::Pipeline::Seed::Build do
context 'is matched' do
let(:attributes) { { name: 'rspec', ref: 'master', rules: [{ if: '$VAR == null', when: 'delayed', start_in: '3 hours' }] } }
it { is_expected.to include(when: 'delayed', start_in: '3 hours') }
it { is_expected.to include(when: 'delayed', options: { start_in: '3 hours' }) }
end
context 'is not matched' do
......@@ -541,7 +541,7 @@ describe Gitlab::Ci::Pipeline::Seed::Build do
it { is_expected.to be_included }
it 'correctly populates when:' do
expect(seed_build.attributes).to include(when: 'delayed', start_in: '1 day')
expect(seed_build.attributes).to include(when: 'delayed', options: { start_in: '1 day' })
end
end
end
......
......@@ -16,7 +16,10 @@ module Gitlab
let(:config) do
YAML.dump(
before_script: ['pwd'],
rspec: { script: 'rspec' }
rspec: {
script: 'rspec',
interruptible: true
}
)
end
......@@ -29,6 +32,7 @@ module Gitlab
before_script: ["pwd"],
script: ["rspec"]
},
interruptible: true,
allow_failure: false,
when: "on_success",
yaml_variables: []
......@@ -36,6 +40,36 @@ module Gitlab
end
end
context 'with job rules' do
let(:config) do
YAML.dump(
rspec: {
script: 'rspec',
rules: [
{ if: '$CI_COMMIT_REF_NAME == "master"' },
{ changes: %w[README.md] }
]
}
)
end
it 'returns valid build attributes' do
expect(subject).to eq({
stage: 'test',
stage_idx: 1,
name: 'rspec',
options: { script: ['rspec'] },
rules: [
{ if: '$CI_COMMIT_REF_NAME == "master"' },
{ changes: %w[README.md] }
],
allow_failure: false,
when: 'on_success',
yaml_variables: []
})
end
end
describe 'coverage entry' do
describe 'code coverage regexp' do
let(:config) do
......@@ -1252,7 +1286,7 @@ module Gitlab
end
end
describe 'rules' do
context 'with when/rules conflict' do
subject { Gitlab::Ci::YamlProcessor.new(YAML.dump(config)) }
let(:config) 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