Commit b30e0e06 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix invalid pipeline build chain tag evaluation

parent fe62860e
...@@ -3,8 +3,6 @@ module Gitlab ...@@ -3,8 +3,6 @@ module Gitlab
module Pipeline module Pipeline
module Chain module Chain
class Build < Chain::Base class Build < Chain::Base
include Chain::Helpers
def perform! def perform!
@pipeline.assign_attributes( @pipeline.assign_attributes(
source: @command.source, source: @command.source,
...@@ -51,6 +49,10 @@ module Gitlab ...@@ -51,6 +49,10 @@ module Gitlab
def protected_ref? def protected_ref?
@project.protected_for?(ref) @project.protected_for?(ref)
end end
def tag_exists?
project.repository.tag_exists?(ref)
end
end end
end end
end end
......
...@@ -3,16 +3,22 @@ module Gitlab ...@@ -3,16 +3,22 @@ module Gitlab
module Pipeline module Pipeline
module Chain module Chain
module Helpers module Helpers
include Gitlab::Utils::StrongMemoize
def branch_exists? 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 end
def tag_exists? 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 end
def error(message) def error(message)
......
...@@ -21,31 +21,64 @@ describe Gitlab::Ci::Pipeline::Chain::Build do ...@@ -21,31 +21,64 @@ describe Gitlab::Ci::Pipeline::Chain::Build do
before do before do
stub_repository_ci_yaml_file(sha: anything) stub_repository_ci_yaml_file(sha: anything)
step.perform!
end end
it 'never breaks the chain' do it 'never breaks the chain' do
step.perform!
expect(step.break?).to be false expect(step.break?).to be false
end end
it 'fills pipeline object with data' do it 'fills pipeline object with data' do
step.perform!
expect(pipeline.sha).not_to be_empty expect(pipeline.sha).not_to be_empty
expect(pipeline.sha).to eq project.commit.id expect(pipeline.sha).to eq project.commit.id
expect(pipeline.ref).to eq 'master' expect(pipeline.ref).to eq 'master'
expect(pipeline.tag).to be false
expect(pipeline.user).to eq user expect(pipeline.user).to eq user
expect(pipeline.project).to eq project expect(pipeline.project).to eq project
end end
it 'sets a valid config source' do it 'sets a valid config source' do
step.perform!
expect(pipeline.repository_source?).to be true expect(pipeline.repository_source?).to be true
end end
it 'returns a valid pipeline' do it 'returns a valid pipeline' do
step.perform!
expect(pipeline).to be_valid expect(pipeline).to be_valid
end end
it 'does not persist a pipeline' do it 'does not persist a pipeline' do
step.perform!
expect(pipeline).not_to be_persisted expect(pipeline).not_to be_persisted
end 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 end
...@@ -518,5 +518,25 @@ describe Ci::CreatePipelineService do ...@@ -518,5 +518,25 @@ describe Ci::CreatePipelineService do
end end
end 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
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