diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index c93f0e0cd55..aeee7f0a5d2 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -270,6 +270,8 @@ module Ci def ref_exists? project.repository.ref_exists?(git_ref) + rescue Gitlab::Git::Repository::NoRepository + false end ## diff --git a/spec/features/projects/pipelines/pipeline_spec.rb b/spec/features/projects/pipelines/pipeline_spec.rb index 5734c7e355e..cd6c37bf54d 100644 --- a/spec/features/projects/pipelines/pipeline_spec.rb +++ b/spec/features/projects/pipelines/pipeline_spec.rb @@ -241,9 +241,12 @@ describe 'Pipeline', :js do end end - context 'with deleted branch' do - before do - allow(pipeline).to receive(:ref_exists?).and_return(false) + context 'when pipeline ref does not exist in repository anymore' do + let(:pipeline) do + create(:ci_empty_pipeline, project: project, + ref: 'non-existent', + sha: project.commit.id, + user: user) end it 'does not render link to the pipeline ref' do diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 06f000a7118..153244b2159 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -780,24 +780,36 @@ describe Ci::Pipeline, :mailer do end describe 'ref_exists?' do - using RSpec::Parameterized::TableSyntax + context 'when repository exists' do + using RSpec::Parameterized::TableSyntax - let(:project) { create(:project, :repository) } + let(:project) { create(:project, :repository) } - where(:tag, :ref, :result) do - false | 'master' | true - false | 'non-existent-branch' | false - true | 'v1.1.0' | true - true | 'non-existent-tag' | false - end - - with_them do - let(:pipeline) do - create(:ci_empty_pipeline, project: project, tag: tag, ref: ref) + where(:tag, :ref, :result) do + false | 'master' | true + false | 'non-existent-branch' | false + true | 'v1.1.0' | true + true | 'non-existent-tag' | false end - it "correctly detects ref" do - expect(pipeline.ref_exists?).to be result + with_them do + let(:pipeline) do + create(:ci_empty_pipeline, project: project, tag: tag, ref: ref) + end + + it "correctly detects ref" do + expect(pipeline.ref_exists?).to be result + end + end + end + + context 'when repository does not exist' do + let(:pipeline) do + create(:ci_empty_pipeline, project: project, ref: 'master') + end + + it 'always returns false' do + expect(pipeline.ref_exists?).to eq false end end end