Commit d6a2d655 authored by Kamil Trzciński's avatar Kamil Trzciński

Simplify behaviour of rules

This makes the logic when to remove only/except
much simpler
parent de5dd327
...@@ -65,6 +65,7 @@ module EE ...@@ -65,6 +65,7 @@ module EE
entry :only, ::Gitlab::Ci::Config::Entry::Policy, entry :only, ::Gitlab::Ci::Config::Entry::Policy,
description: 'Refs policy this job will be executed for.', description: 'Refs policy this job will be executed for.',
default: ::Gitlab::Ci::Config::Entry::Policy::DEFAULT_ONLY,
inherit: false inherit: false
entry :except, ::Gitlab::Ci::Config::Entry::Policy, entry :except, ::Gitlab::Ci::Config::Entry::Policy,
...@@ -98,12 +99,18 @@ module EE ...@@ -98,12 +99,18 @@ module EE
def compose!(deps = nil) def compose!(deps = nil)
super do super do
# If workflow:rules:, rules: and only: are undefined has_workflow_rules = deps&.workflow&.has_rules?
# do create default `only:`
# If workflow:rules: or rules: are used
# they are considered not compatible
# with `only/except` defaults
# #
# This is to make `only:` to be backward compatible # Context: https://gitlab.com/gitlab-org/gitlab/merge_requests/21742
if !deps&.workflow&.has_rules? && !has_rules? && !only_defined? if has_rules? || has_workflow_rules
entry_create!(:only, ::Gitlab::Ci::Config::Entry::Policy::DEFAULT_ONLY) # Remove only/except defaults
# defaults are not considered as defined
@entries.delete(:only) unless only_defined?
@entries.delete(:except) unless except_defined?
end end
end end
end end
......
...@@ -120,6 +120,7 @@ module Gitlab ...@@ -120,6 +120,7 @@ module Gitlab
entry :only, Entry::Policy, entry :only, Entry::Policy,
description: 'Refs policy this job will be executed for.', description: 'Refs policy this job will be executed for.',
default: ::Gitlab::Ci::Config::Entry::Policy::DEFAULT_ONLY,
inherit: false inherit: false
entry :except, Entry::Policy, entry :except, Entry::Policy,
...@@ -176,12 +177,18 @@ module Gitlab ...@@ -176,12 +177,18 @@ module Gitlab
@entries.delete(:type) @entries.delete(:type)
# If workflow:rules:, rules: and only: are undefined has_workflow_rules = deps&.workflow&.has_rules?
# do create default `only:`
# If workflow:rules: or rules: are used
# they are considered not compatible
# with `only/except` defaults
# #
# This is to make `only:` to be backward compatible # Context: https://gitlab.com/gitlab-org/gitlab/merge_requests/21742
if !deps&.workflow&.has_rules? && !has_rules? && !only_defined? if has_rules? || has_workflow_rules
entry_create!(:only, Entry::Policy::DEFAULT_ONLY) # Remove only/except defaults
# defaults are not considered as defined
@entries.delete(:only) unless only_defined?
@entries.delete(:except) unless except_defined?
end end
end end
end end
......
...@@ -9,7 +9,13 @@ module Gitlab ...@@ -9,7 +9,13 @@ module Gitlab
include Chain::Helpers include Chain::Helpers
def perform! def perform!
return unless feature_enabled? unless feature_enabled?
if has_workflow_rules?
error("Workflow rules are disabled", config_error: true)
end
return
end
unless workflow_passed? unless workflow_passed?
error('Pipeline filtered out by workflow rules.') error('Pipeline filtered out by workflow rules.')
...@@ -17,15 +23,13 @@ module Gitlab ...@@ -17,15 +23,13 @@ module Gitlab
end end
def break? def break?
return false unless feature_enabled? @pipeline.errors.any? || @pipeline.persisted?
!workflow_passed?
end end
private private
def feature_enabled? def feature_enabled?
Feature.enabled?(:workflow_rules, default_enabled: true) Feature.enabled?(:workflow_rules, @pipeline.project, default_enabled: true)
end end
def workflow_passed? def workflow_passed?
...@@ -44,6 +48,10 @@ module Gitlab ...@@ -44,6 +48,10 @@ module Gitlab
@pipeline, yaml_variables: workflow_config[:yaml_variables]) @pipeline, yaml_variables: workflow_config[:yaml_variables])
end end
def has_workflow_rules?
workflow_config[:rules].present?
end
def workflow_config def workflow_config
@command.config_processor.workflow_attributes || {} @command.config_processor.workflow_attributes || {}
end end
......
...@@ -2366,6 +2366,7 @@ describe Ci::Build do ...@@ -2366,6 +2366,7 @@ describe Ci::Build do
{ key: 'CI_COMMIT_BEFORE_SHA', value: build.before_sha, public: true, masked: false }, { key: 'CI_COMMIT_BEFORE_SHA', value: build.before_sha, public: true, masked: false },
{ key: 'CI_COMMIT_REF_NAME', value: build.ref, public: true, masked: false }, { key: 'CI_COMMIT_REF_NAME', value: build.ref, public: true, masked: false },
{ key: 'CI_COMMIT_REF_SLUG', value: build.ref_slug, public: true, masked: false }, { key: 'CI_COMMIT_REF_SLUG', value: build.ref_slug, public: true, masked: false },
{ key: 'CI_COMMIT_BRANCH', value: build.ref, public: true, masked: false },
{ key: 'CI_COMMIT_MESSAGE', value: pipeline.git_commit_message, public: true, masked: false }, { key: 'CI_COMMIT_MESSAGE', value: pipeline.git_commit_message, public: true, masked: false },
{ key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true, masked: false }, { key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true, masked: false },
{ key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true, masked: false }, { key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true, masked: false },
......
...@@ -599,6 +599,7 @@ describe Ci::Pipeline, :mailer do ...@@ -599,6 +599,7 @@ describe Ci::Pipeline, :mailer do
CI_COMMIT_BEFORE_SHA CI_COMMIT_BEFORE_SHA
CI_COMMIT_REF_NAME CI_COMMIT_REF_NAME
CI_COMMIT_REF_SLUG CI_COMMIT_REF_SLUG
CI_COMMIT_BRANCH
CI_COMMIT_MESSAGE CI_COMMIT_MESSAGE
CI_COMMIT_TITLE CI_COMMIT_TITLE
CI_COMMIT_DESCRIPTION CI_COMMIT_DESCRIPTION
......
...@@ -100,6 +100,17 @@ describe Ci::CreatePipelineService do ...@@ -100,6 +100,17 @@ describe Ci::CreatePipelineService do
stub_ci_pipeline_yaml_file(config) stub_ci_pipeline_yaml_file(config)
end end
shared_examples 'workflow:rules feature disabled' do
before do
stub_feature_flags(workflow_rules: false)
end
it 'presents a message that rules are disabled' do
expect(pipeline.errors[:base]).to include('Workflow rules are disabled')
expect(pipeline).to be_persisted
end
end
context 'with a single regex-matching if: clause' do context 'with a single regex-matching if: clause' do
let(:config) do let(:config) do
<<-EOY <<-EOY
...@@ -231,16 +242,7 @@ describe Ci::CreatePipelineService do ...@@ -231,16 +242,7 @@ describe Ci::CreatePipelineService do
expect(pipeline).not_to be_persisted expect(pipeline).not_to be_persisted
end end
context 'with workflow:rules shut off' do it_behaves_like 'workflow:rules feature disabled'
before do
stub_feature_flags(workflow_rules: false)
end
it 'invalidates the pipeline with an empty jobs error' do
expect(pipeline.errors[:base]).to include('No stages / jobs for this pipeline.')
expect(pipeline).not_to be_persisted
end
end
end end
context 'where workflow passes and the job passes' do context 'where workflow passes and the job passes' do
...@@ -251,16 +253,7 @@ describe Ci::CreatePipelineService do ...@@ -251,16 +253,7 @@ describe Ci::CreatePipelineService do
expect(pipeline).to be_persisted expect(pipeline).to be_persisted
end end
context 'with workflow:rules shut off' do it_behaves_like 'workflow:rules feature disabled'
before do
stub_feature_flags(workflow_rules: false)
end
it 'saves a pending pipeline' do
expect(pipeline).to be_pending
expect(pipeline).to be_persisted
end
end
end end
context 'where workflow fails and the job fails' do context 'where workflow fails and the job fails' do
...@@ -271,16 +264,7 @@ describe Ci::CreatePipelineService do ...@@ -271,16 +264,7 @@ describe Ci::CreatePipelineService do
expect(pipeline).not_to be_persisted expect(pipeline).not_to be_persisted
end end
context 'with workflow:rules shut off' do it_behaves_like 'workflow:rules feature disabled'
before do
stub_feature_flags(workflow_rules: false)
end
it 'invalidates the pipeline with an empty jobs error' do
expect(pipeline.errors[:base]).to include('No stages / jobs for this pipeline.')
expect(pipeline).not_to be_persisted
end
end
end end
context 'where workflow fails and the job passes' do context 'where workflow fails and the job passes' do
...@@ -291,16 +275,7 @@ describe Ci::CreatePipelineService do ...@@ -291,16 +275,7 @@ describe Ci::CreatePipelineService do
expect(pipeline).not_to be_persisted expect(pipeline).not_to be_persisted
end end
context 'with workflow:rules shut off' do it_behaves_like 'workflow:rules feature disabled'
before do
stub_feature_flags(workflow_rules: false)
end
it 'saves a pending pipeline' do
expect(pipeline).to be_pending
expect(pipeline).to be_persisted
end
end
end end
end 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