BigW Consortium Gitlab

Commit 78ba3a9a by Rémy Coutable

Merge branch '40509_sorting_tags_api' into 'master'

sorting for tags api See merge request gitlab-org/gitlab-ce!15772
parents b0bc676f e7b40c2f
...@@ -686,7 +686,9 @@ class Repository ...@@ -686,7 +686,9 @@ class Repository
def tags_sorted_by(value) def tags_sorted_by(value)
case value case value
when 'name' when 'name_asc'
VersionSorter.sort(tags) { |tag| tag.name }
when 'name_desc'
VersionSorter.rsort(tags) { |tag| tag.name } VersionSorter.rsort(tags) { |tag| tag.name }
when 'updated_desc' when 'updated_desc'
tags_sorted_by_committed_date.reverse tags_sorted_by_committed_date.reverse
......
---
title: add support for sorting in tags api
merge_request: 15772
author: haseebeqx
type: added
...@@ -12,7 +12,11 @@ GET /projects/:id/repository/tags ...@@ -12,7 +12,11 @@ GET /projects/:id/repository/tags
Parameters: Parameters:
- `id` (required) - The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user | Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
| `id` | integer/string| yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user|
| `order_by` | string | no | Return tags ordered by `name` or `updated` fields. Default is `updated` |
| `sort` | string | no | Return tags sorted in `asc` or `desc` order. Default is `desc` |
```json ```json
[ [
......
...@@ -14,10 +14,15 @@ module API ...@@ -14,10 +14,15 @@ module API
success Entities::Tag success Entities::Tag
end end
params do params do
optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Return tags sorted in updated by `asc` or `desc` order.'
optional :order_by, type: String, values: %w[name updated], default: 'updated',
desc: 'Return tags ordered by `name` or `updated` fields.'
use :pagination use :pagination
end end
get ':id/repository/tags' do get ':id/repository/tags' do
tags = ::Kaminari.paginate_array(user_project.repository.tags.sort_by(&:name).reverse) tags = ::Kaminari.paginate_array(::TagsFinder.new(user_project.repository, sort: "#{params[:order_by]}_#{params[:sort]}").execute)
present paginate(tags), with: Entities::Tag, project: user_project present paginate(tags), with: Entities::Tag, project: user_project
end end
......
...@@ -59,12 +59,18 @@ describe Repository do ...@@ -59,12 +59,18 @@ describe Repository do
end end
describe 'tags_sorted_by' do describe 'tags_sorted_by' do
context 'name' do context 'name_desc' do
subject { repository.tags_sorted_by('name').map(&:name) } subject { repository.tags_sorted_by('name_desc').map(&:name) }
it { is_expected.to eq(['v1.1.0', 'v1.0.0']) } it { is_expected.to eq(['v1.1.0', 'v1.0.0']) }
end end
context 'name_asc' do
subject { repository.tags_sorted_by('name_asc').map(&:name) }
it { is_expected.to eq(['v1.0.0', 'v1.1.0']) }
end
context 'updated' do context 'updated' do
let(:tag_a) { repository.find_tag('v1.0.0') } let(:tag_a) { repository.find_tag('v1.0.0') }
let(:tag_b) { repository.find_tag('v1.1.0') } let(:tag_b) { repository.find_tag('v1.1.0') }
......
...@@ -16,6 +16,44 @@ describe API::Tags do ...@@ -16,6 +16,44 @@ describe API::Tags do
describe 'GET /projects/:id/repository/tags' do describe 'GET /projects/:id/repository/tags' do
let(:route) { "/projects/#{project_id}/repository/tags" } let(:route) { "/projects/#{project_id}/repository/tags" }
context 'sorting' do
let(:current_user) { user }
it 'sorts by descending order by default' do
get api(route, current_user)
desc_order_tags = project.repository.tags.sort_by { |tag| tag.dereferenced_target.committed_date }
desc_order_tags.reverse!.map! { |tag| tag.dereferenced_target.id }
expect(json_response.map { |tag| tag['commit']['id'] }).to eq(desc_order_tags)
end
it 'sorts by ascending order if specified' do
get api("#{route}?sort=asc", current_user)
asc_order_tags = project.repository.tags.sort_by { |tag| tag.dereferenced_target.committed_date }
asc_order_tags.map! { |tag| tag.dereferenced_target.id }
expect(json_response.map { |tag| tag['commit']['id'] }).to eq(asc_order_tags)
end
it 'sorts by name in descending order when requested' do
get api("#{route}?order_by=name", current_user)
ordered_by_name = project.repository.tags.map { |tag| tag.name }.sort.reverse
expect(json_response.map { |tag| tag['name'] }).to eq(ordered_by_name)
end
it 'sorts by name in ascending order when requested' do
get api("#{route}?order_by=name&sort=asc", current_user)
ordered_by_name = project.repository.tags.map { |tag| tag.name }.sort
expect(json_response.map { |tag| tag['name'] }).to eq(ordered_by_name)
end
end
shared_examples_for 'repository tags' do shared_examples_for 'repository tags' do
it 'returns the repository tags' do it 'returns the repository tags' do
get api(route, current_user) get api(route, current_user)
......
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