Commit 0e432224 authored by Sean Arnold's avatar Sean Arnold

Merge branch '337136-merge-requests-sort-alphabetically' into 'master'

Merge Requests: sort alphabetically

See merge request gitlab-org/gitlab!76472
parents cc16de42 52045332
......@@ -9,5 +9,7 @@ module Types
value 'MERGED_AT_DESC', 'Merge time by descending order.', value: :merged_at_desc
value 'CLOSED_AT_ASC', 'Closed time by ascending order.', value: :closed_at_asc
value 'CLOSED_AT_DESC', 'Closed time by descending order.', value: :closed_at_desc
value 'TITLE_ASC', 'Title by ascending order.', value: :title_asc
value 'TITLE_DESC', 'Title by descending order.', value: :title_desc
end
end
......@@ -21,6 +21,6 @@
= sortable_item(sort_title_merged_date, page_filter_path(sort: sort_value_merged_date), sort_title) if viewing_merge_requests
= sortable_item(sort_title_closed_date, page_filter_path(sort: sort_value_closed_date), sort_title) if viewing_merge_requests
= sortable_item(sort_title_relative_position, page_filter_path(sort: sort_value_relative_position), sort_title) if viewing_issues
= sortable_item(sort_title_title, page_filter_path(sort: sort_value_title), sort_title) if viewing_issues
= sortable_item(sort_title_title, page_filter_path(sort: sort_value_title), sort_title)
= render_if_exists('shared/ee/issuable/sort_dropdown', viewing_issues: viewing_issues, sort_title: sort_title)
= issuable_sort_direction_button(sort_value)
......@@ -17116,6 +17116,8 @@ Values for sorting merge requests.
| <a id="mergerequestsortmilestone_due_desc"></a>`MILESTONE_DUE_DESC` | Milestone due date by descending order. |
| <a id="mergerequestsortpriority_asc"></a>`PRIORITY_ASC` | Priority by ascending order. |
| <a id="mergerequestsortpriority_desc"></a>`PRIORITY_DESC` | Priority by descending order. |
| <a id="mergerequestsorttitle_asc"></a>`TITLE_ASC` | Title by ascending order. |
| <a id="mergerequestsorttitle_desc"></a>`TITLE_DESC` | Title by descending order. |
| <a id="mergerequestsortupdated_asc"></a>`UPDATED_ASC` | Updated at ascending order. |
| <a id="mergerequestsortupdated_desc"></a>`UPDATED_DESC` | Updated at descending order. |
| <a id="mergerequestsortcreated_asc"></a>`created_asc` **{warning-solid}** | **Deprecated** in 13.5. This was renamed. Use: `CREATED_ASC`. |
......@@ -78,7 +78,7 @@ Parameters:
| Attribute | Type | Required | Description |
| ------------------------------- | -------------- | -------- | ---------------------------------------------------------------------------------------------------------------------- |
| `state` | string | no | Return all merge requests or just those that are `opened`, `closed`, `locked`, or `merged`. |
| `order_by` | string | no | Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`. |
| `order_by` | string | no | Return requests ordered by `created_at`, `title`, or `updated_at` fields. Default is `created_at`. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/331625) in GitLab 14.8.|
| `sort` | string | no | Return requests sorted in `asc` or `desc` order. Default is `desc`. |
| `milestone` | string | no | Return merge requests for a specific milestone. `None` returns merge requests with no milestone. `Any` returns merge requests that have an assigned milestone. |
| `view` | string | no | If `simple`, returns the `iid`, URL, title, description, and basic state of merge request. |
......@@ -273,7 +273,7 @@ Parameters:
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](index.md#namespaced-path-encoding) owned by the authenticated user. |
| `iids[]` | integer array | no | Return the request having the given `iid`. |
| `state` | string | no | Return all merge requests or just those that are `opened`, `closed`, `locked`, or `merged`. |
| `order_by` | string | no | Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`. |
| `order_by` | string | no | Return requests ordered by `created_at`, `title` or `updated_at` fields. Default is `created_at`. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/331625) in GitLab 14.8. |
| `sort` | string | no | Return requests sorted in `asc` or `desc` order. Default is `desc`. |
| `milestone` | string | no | Return merge requests for a specific milestone. `None` returns merge requests with no milestone. `Any` returns merge requests that have an assigned milestone. |
| `view` | string | no | If `simple`, returns the `iid`, URL, title, description, and basic state of merge request. |
......@@ -468,7 +468,7 @@ Parameters:
| ------------------------------- | -------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `id` | integer/string | yes | The ID or [URL-encoded path of the group](index.md#namespaced-path-encoding) owned by the authenticated user. |
| `state` | string | no | Return all merge requests or just those that are `opened`, `closed`, `locked`, or `merged`. |
| `order_by` | string | no | Return merge requests ordered by `created_at` or `updated_at` fields. Default is `created_at`. |
| `order_by` | string | no | Return merge requests ordered by `created_at`, `title` or `updated_at` fields. Default is `created_at`. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/331625) in GitLab 14.8. |
| `sort` | string | no | Return merge requests sorted in `asc` or `desc` order. Default is `desc`. |
| `milestone` | string | no | Return merge requests for a specific milestone. `None` returns merge requests with no milestone. `Any` returns merge requests that have an assigned milestone. |
| `view` | string | no | If `simple`, returns the `iid`, URL, title, description, and basic state of merge request. |
......
......@@ -47,9 +47,9 @@ module API
desc: 'Return opened, closed, locked, merged, or all merge requests'
optional :order_by,
type: String,
values: %w[created_at updated_at],
values: Helpers::MergeRequestsHelpers.sort_options,
default: 'created_at',
desc: 'Return merge requests ordered by `created_at` or `updated_at` fields.'
desc: "Return merge requests ordered by #{Helpers::MergeRequestsHelpers.sort_options_help} fields."
optional :sort,
type: String,
values: %w[asc desc],
......@@ -115,6 +115,22 @@ module API
render_validation_error!(merge_request)
end
def self.sort_options
%w[
created_at
label_priority
milestone_due
popularity
priority
title
updated_at
]
end
def self.sort_options_help
sort_options.map {|y| "`#{y}`" }.to_sentence(last_word_connector: ' or ')
end
end
end
end
......@@ -321,7 +321,7 @@ RSpec.describe Resolvers::MergeRequestsResolver do
end
describe 'sorting' do
let(:mrs) do
let_it_be(:mrs) do
[
merge_request_with_milestone, merge_request_6, merge_request_5, merge_request_4,
merge_request_3, merge_request_2, merge_request_1
......@@ -363,28 +363,44 @@ RSpec.describe Resolvers::MergeRequestsResolver do
def merged_at(mr)
nils_last(mr.metrics.merged_at)
end
end
context 'when sorting by closed at' do
before do
merge_request_1.metrics.update!(latest_closed_at: 10.days.ago)
merge_request_3.metrics.update!(latest_closed_at: 5.days.ago)
end
it 'sorts merge requests ascending' do
expect(resolve_mr(project, sort: :closed_at_asc))
.to match_array(mrs)
.and be_sorted(->(mr) { [closed_at(mr), -mr.id] })
end
it 'sorts merge requests descending' do
expect(resolve_mr(project, sort: :closed_at_desc))
.to match_array(mrs)
.and be_sorted(->(mr) { [-closed_at(mr), -mr.id] })
end
def closed_at(mr)
nils_last(mr.metrics.latest_closed_at)
end
end
context 'when sorting by title' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:mr1) { create(:merge_request, :unique_branches, title: 'foo', source_project: project) }
let_it_be(:mr2) { create(:merge_request, :unique_branches, title: 'bar', source_project: project) }
let_it_be(:mr3) { create(:merge_request, :unique_branches, title: 'baz', source_project: project) }
let_it_be(:mr4) { create(:merge_request, :unique_branches, title: 'Baz 2', source_project: project) }
it 'sorts issues ascending' do
expect(resolve_mr(project, sort: :title_asc).to_a).to eq [mr2, mr3, mr4, mr1]
end
context 'when sorting by closed at' do
before do
merge_request_1.metrics.update!(latest_closed_at: 10.days.ago)
merge_request_3.metrics.update!(latest_closed_at: 5.days.ago)
end
it 'sorts merge requests ascending' do
expect(resolve_mr(project, sort: :closed_at_asc))
.to match_array(mrs)
.and be_sorted(->(mr) { [closed_at(mr), -mr.id] })
end
it 'sorts merge requests descending' do
expect(resolve_mr(project, sort: :closed_at_desc))
.to match_array(mrs)
.and be_sorted(->(mr) { [-closed_at(mr), -mr.id] })
end
def closed_at(mr)
nils_last(mr.metrics.latest_closed_at)
end
it 'sorts issues descending' do
expect(resolve_mr(project, sort: :title_desc).to_a).to eq [mr1, mr4, mr3, mr2]
end
end
end
......
......@@ -585,6 +585,21 @@ RSpec.describe MergeRequest, factory_default: :keep do
expect(merge_requests).to eq([older_mr, newer_mr])
end
end
context 'title' do
let_it_be(:first_mr) { create(:merge_request, :closed, title: 'One') }
let_it_be(:second_mr) { create(:merge_request, :closed, title: 'Two') }
it 'sorts asc' do
merge_requests = described_class.sort_by_attribute(:title_asc)
expect(merge_requests).to eq([first_mr, second_mr])
end
it 'sorts desc' do
merge_requests = described_class.sort_by_attribute(:title_desc)
expect(merge_requests).to eq([second_mr, first_mr])
end
end
end
describe 'time to merge calculations' do
......
......@@ -436,6 +436,26 @@ RSpec.describe API::MergeRequests do
response_dates = json_response.map { |merge_request| merge_request['created_at'] }
expect(response_dates).to eq(response_dates.sort)
end
context 'returns an array of merge_requests ordered by title' do
it 'asc when requested' do
path = endpoint_path + '?order_by=title&sort=asc'
get api(path, user)
response_titles = json_response.map { |merge_request| merge_request['title'] }
expect(response_titles).to eq(response_titles.sort)
end
it 'desc when requested' do
path = endpoint_path + '?order_by=title&sort=desc'
get api(path, user)
response_titles = json_response.map { |merge_request| merge_request['title'] }
expect(response_titles).to eq(response_titles.sort.reverse)
end
end
end
context 'NOT params' do
......
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