summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-11-20 15:07:25 +0100
committerMatija Čupić <matteeyah@gmail.com>2018-12-08 19:28:56 +0100
commit24d682254a0ae68dfc605fc077c6a7610b97994a (patch)
tree2e9f98b014add9cfd2996aef4a9d8577ba7a9991
parentd12bc3bfc47f9a4252e3f9eb60b1326eade7d3e5 (diff)
downloadgitlab-ce-24d682254a0ae68dfc605fc077c6a7610b97994a.tar.gz
Move Project#resolve_ref to Repository
-rw-r--r--app/models/project.rb17
-rw-r--r--app/models/repository.rb15
-rw-r--r--lib/gitlab/ci/pipeline/chain/validate/repository.rb2
-rw-r--r--spec/models/project_spec.rb51
-rw-r--r--spec/models/repository_spec.rb47
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' }