From 24d682254a0ae68dfc605fc077c6a7610b97994a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 20 Nov 2018 15:07:25 +0100 Subject: Move Project#resolve_ref to Repository --- app/models/project.rb | 17 +------- app/models/repository.rb | 15 +++++++ .../ci/pipeline/chain/validate/repository.rb | 2 +- spec/models/project_spec.rb | 51 +--------------------- spec/models/repository_spec.rb | 47 ++++++++++++++++++++ 5 files changed, 65 insertions(+), 67 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index e9fa9cfabc9..2ba273c6f98 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1160,21 +1160,6 @@ class Project < ActiveRecord::Base end end - def resolve_ref(ref) - tag_exists = repository.tag_exists?(ref) - branch_exists = repository.branch_exists?(ref) - - if tag_exists && branch_exists - nil - elsif tag_exists - Gitlab::Git::TAG_REF_PREFIX + ref - elsif branch_exists - Gitlab::Git::BRANCH_REF_PREFIX + ref - else - ref - end - end - def root_ref?(branch) repository.root_ref == branch end @@ -1752,7 +1737,7 @@ class Project < ActiveRecord::Base end def protected_for?(ref) - resolved_ref = resolve_ref(ref) + resolved_ref = repository.resolve_ref(ref) return false unless resolved_ref ref_name = Gitlab::Git.ref_name(resolved_ref) diff --git a/app/models/repository.rb b/app/models/repository.rb index 35dd120856d..221d01c5b44 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -182,6 +182,21 @@ class Repository tags.find { |tag| tag.name == name } end + def resolve_ref(ref) + tag_exists = tag_exists?(ref) + branch_exists = branch_exists?(ref) + + if tag_exists && branch_exists + nil + elsif tag_exists + Gitlab::Git::TAG_REF_PREFIX + ref + elsif branch_exists + Gitlab::Git::BRANCH_REF_PREFIX + ref + else + ref + end + end + def add_branch(user, branch_name, ref) branch = raw_repository.add_branch(branch_name, user: user, target: ref) diff --git a/lib/gitlab/ci/pipeline/chain/validate/repository.rb b/lib/gitlab/ci/pipeline/chain/validate/repository.rb index 0b411422264..6e95c0717c6 100644 --- a/lib/gitlab/ci/pipeline/chain/validate/repository.rb +++ b/lib/gitlab/ci/pipeline/chain/validate/repository.rb @@ -17,7 +17,7 @@ module Gitlab return error('Commit not found') end - unless @command.project.resolve_ref(@command.origin_ref) + unless @command.project.repository.resolve_ref(@command.origin_ref) return error('Ref is ambiguous') end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index cbc242308e8..25560ddea8d 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2568,7 +2568,7 @@ describe Project do subject { project.protected_for?('ref') } before do - allow(project).to receive(:resolve_ref).and_return(ref) + allow(project.repository).to receive(:resolve_ref).and_return(ref) end context 'when ref is ambiguous' do @@ -2796,55 +2796,6 @@ describe Project do end end - describe '#resolve_ref' do - let(:project) { create(:project, :repository) } - - subject { project.resolve_ref(ref) } - - context 'when ref is full ref' do - let(:ref) { 'refs/heads/master' } - - it 'returns the ref' do - is_expected.to eq(ref) - end - end - - context 'when ref is a tag or branch name' do - let(:ref) { 'ref' } - - context 'when ref is ambiguous' do - before do - project.repository.add_tag(project.creator, ref, 'master') - project.repository.add_branch(project.creator, ref, 'master') - end - - it 'returns nil' do - is_expected.to eq(nil) - end - end - - context 'when ref is tag name' do - before do - project.repository.add_tag(project.creator, ref, 'master') - end - - it 'returns the tag ref' do - is_expected.to eq("refs/tags/#{ref}") - end - end - - context 'when ref is branch name' do - before do - project.repository.add_branch(project.creator, ref, 'master') - end - - it 'returns the branch ref' do - is_expected.to eq("refs/heads/#{ref}") - end - end - end - end - describe '#http_url_to_repo' do let(:project) { create(:project) } diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index f09b4b67061..3d6cf2cbc19 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1005,6 +1005,53 @@ describe Repository do end end + describe '#resolve_ref' do + subject { repository.resolve_ref(ref) } + + context 'when ref is full ref' do + let(:ref) { 'refs/heads/master' } + + it 'returns the ref' do + is_expected.to eq(ref) + end + end + + context 'when ref is a tag or branch name' do + let(:ref) { 'ref' } + + context 'when ref is ambiguous' do + before do + repository.add_tag(project.creator, ref, 'master') + repository.add_branch(project.creator, ref, 'master') + end + + it 'returns nil' do + is_expected.to eq(nil) + end + end + + context 'when ref is tag name' do + before do + repository.add_tag(project.creator, ref, 'master') + end + + it 'returns the tag ref' do + is_expected.to eq("refs/tags/#{ref}") + end + end + + context 'when ref is branch name' do + before do + repository.add_branch(project.creator, ref, 'master') + end + + it 'returns the branch ref' do + is_expected.to eq("refs/heads/#{ref}") + end + end + end + end + describe '#add_branch' do let(:branch_name) { 'new_feature' } let(:target) { 'master' } -- cgit v1.2.1