From b30e0e0691716c2ef0b858b81dd5cef8d4728c59 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 8 Dec 2017 10:50:10 +0100 Subject: Fix invalid pipeline build chain tag evaluation --- lib/gitlab/ci/pipeline/chain/build.rb | 6 ++-- lib/gitlab/ci/pipeline/chain/helpers.rb | 14 ++++++--- spec/lib/gitlab/ci/pipeline/chain/build_spec.rb | 37 ++++++++++++++++++++++-- spec/services/ci/create_pipeline_service_spec.rb | 20 +++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/lib/gitlab/ci/pipeline/chain/build.rb b/lib/gitlab/ci/pipeline/chain/build.rb index a126dded1ae..d47ebf70068 100644 --- a/lib/gitlab/ci/pipeline/chain/build.rb +++ b/lib/gitlab/ci/pipeline/chain/build.rb @@ -3,8 +3,6 @@ module Gitlab module Pipeline module Chain class Build < Chain::Base - include Chain::Helpers - def perform! @pipeline.assign_attributes( source: @command.source, @@ -51,6 +49,10 @@ module Gitlab def protected_ref? @project.protected_for?(ref) end + + def tag_exists? + project.repository.tag_exists?(ref) + end end end end diff --git a/lib/gitlab/ci/pipeline/chain/helpers.rb b/lib/gitlab/ci/pipeline/chain/helpers.rb index 02d81286f21..f6708a849f9 100644 --- a/lib/gitlab/ci/pipeline/chain/helpers.rb +++ b/lib/gitlab/ci/pipeline/chain/helpers.rb @@ -3,16 +3,22 @@ module Gitlab module Pipeline module Chain module Helpers + include Gitlab::Utils::StrongMemoize + def branch_exists? - return @is_branch if defined?(@is_branch) + strong_memoize(:is_branch) do + raise ArgumentError unless pipeline.ref - @is_branch = project.repository.branch_exists?(pipeline.ref) + project.repository.branch_exists?(pipeline.ref) + end end def tag_exists? - return @is_tag if defined?(@is_tag) + strong_memoize(:is_tag) do + raise ArgumentError unless pipeline.ref - @is_tag = project.repository.tag_exists?(pipeline.ref) + project.repository.tag_exists?(pipeline.ref) + end end def error(message) diff --git a/spec/lib/gitlab/ci/pipeline/chain/build_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/build_spec.rb index 0f1d72080c5..ff90d3f10a4 100644 --- a/spec/lib/gitlab/ci/pipeline/chain/build_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/chain/build_spec.rb @@ -21,31 +21,64 @@ describe Gitlab::Ci::Pipeline::Chain::Build do before do stub_repository_ci_yaml_file(sha: anything) - - step.perform! end it 'never breaks the chain' do + step.perform! + expect(step.break?).to be false end it 'fills pipeline object with data' do + step.perform! + expect(pipeline.sha).not_to be_empty expect(pipeline.sha).to eq project.commit.id expect(pipeline.ref).to eq 'master' + expect(pipeline.tag).to be false expect(pipeline.user).to eq user expect(pipeline.project).to eq project end it 'sets a valid config source' do + step.perform! + expect(pipeline.repository_source?).to be true end it 'returns a valid pipeline' do + step.perform! + expect(pipeline).to be_valid end it 'does not persist a pipeline' do + step.perform! + expect(pipeline).not_to be_persisted end + + context 'when pipeline is running for a tag' do + let(:command) do + double('command', source: :push, + origin_ref: 'mytag', + checkout_sha: project.commit.id, + after_sha: nil, + before_sha: nil, + trigger_request: nil, + schedule: nil, + project: project, + current_user: user) + end + + before do + allow(step).to receive(:tag_exists?).and_return(true) + + step.perform! + end + + it 'correctly indicated that this is a tagged pipeline' do + expect(pipeline).to be_tag + end + end end diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index b0de8d447a2..be4abf11cf4 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -518,5 +518,25 @@ describe Ci::CreatePipelineService do end end end + + context 'when pipelie is running for a tag' do + before do + allow_any_instance_of(Repository) + .to receive(:tag_exists?).and_return(false) + allow_any_instance_of(Repository) + .to receive(:tag_exists?).with('refs/tags/mytag').and_return(true) + + config = YAML.dump(test: { script: 'test', only: ['branches'] }, + deploy: { script: 'deploy', only: ['tags'] }) + + stub_ci_pipeline_yaml_file(config) + end + + it 'creates a tagged pipeline' do + pipeline = execute_service(ref: 'mytag') + + expect(pipeline.tag?).to be true + end + end end end -- cgit v1.2.1