Skip to content
Snippets Groups Projects
Commit 613c4b50 authored by Jerry Seto's avatar Jerry Seto 2️⃣
Browse files

Refactors

parent acec30b4
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ def resolve(**args) ...@@ -12,7 +12,7 @@ def resolve(**args)
# Ensure merge commits can be returned by sending nil to Gitaly instead of '/' # Ensure merge commits can be returned by sending nil to Gitaly instead of '/'
path = tree.path == '/' ? nil : tree.path path = tree.path == '/' ? nil : tree.path
commit = Gitlab::Git::Commit.last_for_path(tree.repository, commit = Gitlab::Git::Commit.last_for_path(tree.repository,
tree.ref_type.present? ? "refs/#{tree.ref_type}/#{tree.sha}" : tree.sha, path, literal_pathspec: true) ExtractsRef.fully_qualified_ref(tree.sha, tree.ref_type), path, literal_pathspec: true)
::Commit.new(commit, tree.repository.project) if commit ::Commit.new(commit, tree.repository.project) if commit
end end
......
...@@ -20,7 +20,7 @@ class PaginatedTreeResolver < BaseResolver ...@@ -20,7 +20,7 @@ class PaginatedTreeResolver < BaseResolver
description: 'Commit ref to get the tree for. Default value is HEAD.' description: 'Commit ref to get the tree for. Default value is HEAD.'
argument :ref_type, GraphQL::Types::String, argument :ref_type, GraphQL::Types::String,
required: false, required: false,
description: 'ref type.' description: 'Type of the ref. heads for branches and tags for tags.'
alias_method :repository, :object alias_method :repository, :object
......
...@@ -19,7 +19,7 @@ class TreeResolver < BaseResolver ...@@ -19,7 +19,7 @@ class TreeResolver < BaseResolver
description: 'Commit ref to get the tree for. Default value is HEAD.' description: 'Commit ref to get the tree for. Default value is HEAD.'
argument :ref_type, GraphQL::Types::String, argument :ref_type, GraphQL::Types::String,
required: false, required: false,
description: 'ref type.' description: 'Type of the ref. heads for branches and tags for tags.'
alias_method :repository, :object alias_method :repository, :object
......
...@@ -13,18 +13,18 @@ def initialize( ...@@ -13,18 +13,18 @@ def initialize(
@repository = repository @repository = repository
@sha = sha @sha = sha
@path = path @path = path
@ref_type = ref_type @ref_type = ExtractsRef.ref_type(ref_type)
git_repo = @repository.raw_repository git_repo = @repository.raw_repository
ref = @sha
if ref_type.present? ref = if ref_type.present?
@ref_type = ref_type == 'heads' ? 'heads' : 'tags' ExtractsRef.fully_qualified_ref(@sha, ref_type)
ref = "refs/#{@ref_type}/#{@sha}" else
end @sha
end
@entries, @cursor = Gitlab::Git::Tree.where(git_repo, ref, @path, recursive, skip_flat_paths, pagination_params) @entries, @cursor = Gitlab::Git::Tree.where(git_repo, ref, @path, recursive, skip_flat_paths, pagination_params)
@entries.each do |entry| @entries.each do |entry|
entry.ref_type = ref_type entry.ref_type = @ref_type
end end
end end
......
...@@ -21464,7 +21464,7 @@ four standard [pagination arguments](#connection-pagination-arguments): ...@@ -21464,7 +21464,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
| <a id="repositorypaginatedtreepath"></a>`path` | [`String`](#string) | Path to get the tree for. Default value is the root of the repository. | | <a id="repositorypaginatedtreepath"></a>`path` | [`String`](#string) | Path to get the tree for. Default value is the root of the repository. |
| <a id="repositorypaginatedtreerecursive"></a>`recursive` | [`Boolean`](#boolean) | Used to get a recursive tree. Default is false. | | <a id="repositorypaginatedtreerecursive"></a>`recursive` | [`Boolean`](#boolean) | Used to get a recursive tree. Default is false. |
| <a id="repositorypaginatedtreeref"></a>`ref` | [`String`](#string) | Commit ref to get the tree for. Default value is HEAD. | | <a id="repositorypaginatedtreeref"></a>`ref` | [`String`](#string) | Commit ref to get the tree for. Default value is HEAD. |
| <a id="repositorypaginatedtreereftype"></a>`refType` | [`String`](#string) | ref type. | | <a id="repositorypaginatedtreereftype"></a>`refType` | [`String`](#string) | Type of the ref. heads for branches and tags for tags. |
   
##### `Repository.tree` ##### `Repository.tree`
   
...@@ -21479,7 +21479,7 @@ Returns [`Tree`](#tree). ...@@ -21479,7 +21479,7 @@ Returns [`Tree`](#tree).
| <a id="repositorytreepath"></a>`path` | [`String`](#string) | Path to get the tree for. Default value is the root of the repository. | | <a id="repositorytreepath"></a>`path` | [`String`](#string) | Path to get the tree for. Default value is the root of the repository. |
| <a id="repositorytreerecursive"></a>`recursive` | [`Boolean`](#boolean) | Used to get a recursive tree. Default is false. | | <a id="repositorytreerecursive"></a>`recursive` | [`Boolean`](#boolean) | Used to get a recursive tree. Default is false. |
| <a id="repositorytreeref"></a>`ref` | [`String`](#string) | Commit ref to get the tree for. Default value is HEAD. | | <a id="repositorytreeref"></a>`ref` | [`String`](#string) | Commit ref to get the tree for. Default value is HEAD. |
| <a id="repositorytreereftype"></a>`refType` | [`String`](#string) | ref type. | | <a id="repositorytreereftype"></a>`refType` | [`String`](#string) | Type of the ref. heads for branches and tags for tags. |
   
### `RepositoryBlob` ### `RepositoryBlob`
   
...@@ -7,6 +7,8 @@ module ExtractsRef ...@@ -7,6 +7,8 @@ module ExtractsRef
InvalidPathError = Class.new(StandardError) InvalidPathError = Class.new(StandardError)
BRANCH_REF_TYPE = 'heads' BRANCH_REF_TYPE = 'heads'
TAG_REF_TYPE = 'tags' TAG_REF_TYPE = 'tags'
REF_TYPES = [BRANCH_REF_TYPE, TAG_REF_TYPE].freeze
# Given a string containing both a Git tree-ish, such as a branch or tag, and # Given a string containing both a Git tree-ish, such as a branch or tag, and
# a filesystem path joined by forward slashes, attempts to separate the two. # a filesystem path joined by forward slashes, attempts to separate the two.
# #
...@@ -60,7 +62,6 @@ def extract_ref(id) ...@@ -60,7 +62,6 @@ def extract_ref(id)
# #
# If the :id parameter appears to be requesting a specific response format, # If the :id parameter appears to be requesting a specific response format,
# that will be handled as well. # that will be handled as well.
#
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
def assign_ref_vars def assign_ref_vars
@id, @ref, @path = extract_ref_path @id, @ref, @path = extract_ref_path
...@@ -70,7 +71,7 @@ def assign_ref_vars ...@@ -70,7 +71,7 @@ def assign_ref_vars
return unless @ref.present? return unless @ref.present?
@commit = if ref_type @commit = if ref_type
@fully_qualified_ref = %(refs/#{ref_type}/#{@ref}) @fully_qualified_ref = ExtractsRef.fully_qualified_ref(@ref, ref_type)
@repo.commit(@fully_qualified_ref) @repo.commit(@fully_qualified_ref)
else else
@repo.commit(@ref) @repo.commit(@ref)
...@@ -90,9 +91,20 @@ def extract_ref_path ...@@ -90,9 +91,20 @@ def extract_ref_path
end end
def ref_type def ref_type
return unless params[:ref_type].present? ExtractsRef.ref_type(params[:ref_type])
end
def self.ref_type(type)
return unless REF_TYPES.include?(type)
type
end
def self.fully_qualified_ref(ref, type)
validated_type = ref_type(type)
return ref unless validated_type
params[:ref_type] == TAG_REF_TYPE ? TAG_REF_TYPE : BRANCH_REF_TYPE %(refs/#{validated_type}/#{ref})
end end
private private
...@@ -156,6 +168,7 @@ def repository_container ...@@ -156,6 +168,7 @@ def repository_container
raise NotImplementedError raise NotImplementedError
end end
# deprecated in favor of ExtractsRef::RequestedRef
def ambiguous_ref?(project, ref) def ambiguous_ref?(project, ref)
return true if project.repository.ambiguous_ref?(ref) return true if project.repository.ambiguous_ref?(ref)
......
...@@ -57,5 +57,64 @@ ...@@ -57,5 +57,64 @@
end end
end end
describe '#ref_type' do
let(:params) { ActionController::Parameters.new(ref_type: 'heads') }
it 'delegates to .ref_type' do
expect(described_class).to receive(:ref_type).with('heads')
ref_type
end
end
describe '.ref_type' do
subject { described_class.ref_type(ref_type) }
context 'when ref_type is nil' do
let(:ref_type) { nil }
it { is_expected.to eq(nil) }
end
context 'when ref_type is heads' do
let(:ref_type) { 'heads' }
it { is_expected.to eq('heads') }
end
context 'when ref_type is tags' do
let(:ref_type) { 'tags' }
it { is_expected.to eq('tags') }
end
context 'when ref_type is invalid' do
let(:ref_type) { 'invalid' }
it { is_expected.to eq(nil) }
end
end
describe '.fully_qualified_ref' do
subject { described_class.fully_qualified_ref(ref, ref_type) }
context 'when ref_type is nil' do
let(:ref_type) { nil }
it { is_expected.to eq(ref) }
end
context 'when ref_type valid' do
let(:ref_type) { 'heads' }
it { is_expected.to eq("refs/#{ref_type}/#{ref}") }
end
context 'when ref_type is invalid' do
let(:ref_type) { 'invalid' }
it { is_expected.to eq(ref) }
end
end
it_behaves_like 'extracts refs' it_behaves_like 'extracts refs'
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment