Commit 06d92619 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'mc/feature/add-limit-offset-branch-names-graphql' into 'master'

Add offset and limit parameters to names resolver

See merge request gitlab-org/gitlab!61061
parents d2352ba0 e8c1d3e7
...@@ -10,9 +10,9 @@ module Repositories ...@@ -10,9 +10,9 @@ module Repositories
end end
def execute def execute
return unless search return unless search && offset && limit
repository.search_branch_names(search) repository.search_branch_names(search).lazy.drop(offset).take(limit) # rubocop:disable CodeReuse/ActiveRecord
end end
private private
...@@ -20,5 +20,13 @@ module Repositories ...@@ -20,5 +20,13 @@ module Repositories
def search def search
@params[:search].presence @params[:search].presence
end end
def offset
@params[:offset]
end
def limit
@params[:limit]
end
end end
end end
...@@ -10,8 +10,16 @@ module Resolvers ...@@ -10,8 +10,16 @@ module Resolvers
required: true, required: true,
description: 'The pattern to search for branch names by.' description: 'The pattern to search for branch names by.'
def resolve(search_pattern:) argument :offset, GraphQL::INT_TYPE,
Repositories::BranchNamesFinder.new(object, search: search_pattern).execute required: true,
description: 'The number of branch names to skip.'
argument :limit, GraphQL::INT_TYPE,
required: true,
description: 'The number of branch names to return.'
def resolve(search_pattern:, offset:, limit:)
Repositories::BranchNamesFinder.new(object, offset: offset, limit: limit, search: search_pattern).execute
end end
end end
end end
---
title: Add offset and limit to branch names resolver.
merge_request: 61061
author:
type: changed
...@@ -11760,6 +11760,8 @@ Returns [`[String!]`](#string). ...@@ -11760,6 +11760,8 @@ Returns [`[String!]`](#string).
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ---- | ---- | ----------- |
| <a id="repositorybranchnameslimit"></a>`limit` | [`Int!`](#int) | The number of branch names to return. |
| <a id="repositorybranchnamesoffset"></a>`offset` | [`Int!`](#int) | The number of branch names to skip. |
| <a id="repositorybranchnamessearchpattern"></a>`searchPattern` | [`String!`](#string) | The pattern to search for branch names by. | | <a id="repositorybranchnamessearchpattern"></a>`searchPattern` | [`String!`](#string) | The pattern to search for branch names by. |
##### `Repository.tree` ##### `Repository.tree`
......
...@@ -5,21 +5,34 @@ require 'spec_helper' ...@@ -5,21 +5,34 @@ require 'spec_helper'
RSpec.describe Repositories::BranchNamesFinder do RSpec.describe Repositories::BranchNamesFinder do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:branch_names_finder) { described_class.new(project.repository, search: 'conflict-*') }
describe '#execute' do describe '#execute' do
subject(:execute) { branch_names_finder.execute } it 'returns all filtered branch names' do
expect(create_branch_names_finder(0, 100).execute).to contain_exactly(
it 'filters branch names' do 'snippet/edit-file',
expect(execute).to contain_exactly( 'snippet/multiple-files',
'conflict-binary-file', 'snippet/no-files',
'conflict-resolvable', 'snippet/rename-and-edit-file',
'conflict-contains-conflict-markers', 'snippet/single-file'
'conflict-missing-side',
'conflict-start',
'conflict-non-utf8',
'conflict-too-large'
) )
end end
it 'returns a limited number of offset filtered branch names' do
starting_names = create_branch_names_finder(0, 3).execute
offset_names = create_branch_names_finder(3, 2).execute
expect(starting_names.count).to eq(3)
expect(offset_names.count).to eq(2)
expect(offset_names).not_to include(*starting_names)
all_names = create_branch_names_finder(0, 100).execute
expect(all_names).to contain_exactly(*starting_names, *offset_names)
end
private
def create_branch_names_finder(offset, limit)
described_class.new(project.repository, search: 'snippet/*', offset: offset, limit: limit)
end
end end
end end
...@@ -8,29 +8,50 @@ RSpec.describe Resolvers::RepositoryBranchNamesResolver do ...@@ -8,29 +8,50 @@ RSpec.describe Resolvers::RepositoryBranchNamesResolver do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
describe '#resolve' do describe '#resolve' do
subject(:resolve_branch_names) do
resolve(
described_class,
obj: project.repository,
args: { search_pattern: pattern },
ctx: { current_user: project.creator }
)
end
context 'with empty search pattern' do context 'with empty search pattern' do
let(:pattern) { '' } let(:pattern) { '' }
it 'returns nil' do it 'returns nil' do
expect(resolve_branch_names).to eq(nil) expect(resolve_branch_names(pattern, 0, 100)).to eq(nil)
end end
end end
context 'with a valid search pattern' do context 'with a valid search pattern' do
let(:pattern) { 'mas*' } let(:pattern) { 'snippet/*' }
it 'returns matching branches' do it 'returns matching branches' do
expect(resolve_branch_names).to match_array(['master']) expect(resolve_branch_names(pattern, 0, 100)).to contain_exactly(
'snippet/edit-file',
'snippet/multiple-files',
'snippet/no-files',
'snippet/rename-and-edit-file',
'snippet/single-file'
)
end
it 'properly offsets and limits branch name results' do
starting_names = resolve_branch_names(pattern, 0, 3)
offset_names = resolve_branch_names(pattern, 3, 2)
expect(starting_names.count).to eq(3)
expect(offset_names.count).to eq(2)
expect(offset_names).not_to include(*starting_names)
all_names = resolve_branch_names(pattern, 0, 100)
expect(all_names).to contain_exactly(*starting_names, *offset_names)
end end
end end
end end
private
def resolve_branch_names(pattern, offset, limit)
resolve(
described_class,
obj: project.repository,
args: { search_pattern: pattern, offset: offset, limit: limit },
ctx: { current_user: project.creator }
)
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