Commit bdfb349f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor and improve sorting objects in API for projects, issues and merge requests

parent 8952fc01
...@@ -18,6 +18,8 @@ Parameters: ...@@ -18,6 +18,8 @@ Parameters:
- `state` (optional) - Return `all` issues or just those that are `opened` or `closed` - `state` (optional) - Return `all` issues or just those that are `opened` or `closed`
- `labels` (optional) - Comma-separated list of label names - `labels` (optional) - Comma-separated list of label names
- `order_by` (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
```json ```json
[ [
...@@ -105,6 +107,8 @@ Parameters: ...@@ -105,6 +107,8 @@ Parameters:
- `state` (optional) - Return `all` issues or just those that are `opened` or `closed` - `state` (optional) - Return `all` issues or just those that are `opened` or `closed`
- `labels` (optional) - Comma-separated list of label names - `labels` (optional) - Comma-separated list of label names
- `milestone` (optional) - Milestone title - `milestone` (optional) - Milestone title
- `order_by` (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
## Single issue ## Single issue
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
## List merge requests ## List merge requests
Get all merge requests for this project. The `state` parameter can be used to get only merge requests with a given state (`opened`, `closed`, or `merged`) or all of them (`all`). The pagination parameters `page` and `per_page` can be used to restrict the list of merge requests. Get all merge requests for this project.
The `state` parameter can be used to get only merge requests with a given state (`opened`, `closed`, or `merged`) or all of them (`all`).
The pagination parameters `page` and `per_page` can be used to restrict the list of merge requests.
``` ```
GET /projects/:id/merge_requests GET /projects/:id/merge_requests
...@@ -14,8 +16,8 @@ Parameters: ...@@ -14,8 +16,8 @@ Parameters:
- `id` (required) - The ID of a project - `id` (required) - The ID of a project
- `state` (optional) - Return `all` requests or just those that are `merged`, `opened` or `closed` - `state` (optional) - Return `all` requests or just those that are `merged`, `opened` or `closed`
- `order_by` (optional) - Return requests ordered by `created_at` or `updated_at` fields - `order_by` (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order - `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
```json ```json
[ [
......
...@@ -11,8 +11,8 @@ GET /projects ...@@ -11,8 +11,8 @@ GET /projects
Parameters: Parameters:
- `archived` (optional) - if passed, limit by archived status - `archived` (optional) - if passed, limit by archived status
- `order_by` (optional) - Return requests ordered by `id`, `name`, `created_at` or `last_activity_at` fields - `order_by` (optional) - Return requests ordered by `id`, `name`, `path`, `created_at`, `updated_at` or `last_activity_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order - `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
- `search` (optional) - Return list of authorized projects according to a search criteria - `search` (optional) - Return list of authorized projects according to a search criteria
```json ```json
...@@ -98,6 +98,13 @@ Get a list of projects which are owned by the authenticated user. ...@@ -98,6 +98,13 @@ Get a list of projects which are owned by the authenticated user.
GET /projects/owned GET /projects/owned
``` ```
Parameters:
- `archived` (optional) - if passed, limit by archived status
- `order_by` (optional) - Return requests ordered by `id`, `name`, `path`, `created_at`, `updated_at` or `last_activity_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
- `search` (optional) - Return list of authorized projects according to a search criteria
### List ALL projects ### List ALL projects
Get a list of all GitLab projects (admin only). Get a list of all GitLab projects (admin only).
...@@ -106,6 +113,13 @@ Get a list of all GitLab projects (admin only). ...@@ -106,6 +113,13 @@ Get a list of all GitLab projects (admin only).
GET /projects/all GET /projects/all
``` ```
Parameters:
- `archived` (optional) - if passed, limit by archived status
- `order_by` (optional) - Return requests ordered by `id`, `name`, `path`, `created_at`, `updated_at` or `last_activity_at` fields. Default is `created_at`
- `sort` (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
- `search` (optional) - Return list of authorized projects according to a search criteria
### Get single project ### Get single project
Get a specific project, identified by project ID or NAMESPACE/PROJECT_NAME, which is owned by the authenticated user. Get a specific project, identified by project ID or NAMESPACE/PROJECT_NAME, which is owned by the authenticated user.
......
...@@ -154,6 +154,22 @@ module API ...@@ -154,6 +154,22 @@ module API
Gitlab::Access.options_with_owner.values.include? level.to_i Gitlab::Access.options_with_owner.values.include? level.to_i
end end
def issuable_order_by
if params["order_by"] == 'updated_at'
'updated_at'
else
'created_at'
end
end
def issuable_sort
if params["sort"] == 'asc'
:asc
else
:desc
end
end
# error helpers # error helpers
def forbidden!(reason = nil) def forbidden!(reason = nil)
......
...@@ -27,7 +27,9 @@ module API ...@@ -27,7 +27,9 @@ module API
# Parameters: # Parameters:
# state (optional) - Return "opened" or "closed" issues # state (optional) - Return "opened" or "closed" issues
# labels (optional) - Comma-separated list of label names # labels (optional) - Comma-separated list of label names
# order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
# sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
#
# Example Requests: # Example Requests:
# GET /issues # GET /issues
# GET /issues?state=opened # GET /issues?state=opened
...@@ -39,7 +41,7 @@ module API ...@@ -39,7 +41,7 @@ module API
issues = current_user.issues issues = current_user.issues
issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? issues = filter_issues_state(issues, params[:state]) unless params[:state].nil?
issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
issues.reorder(issuable_order_by => issuable_sort)
present paginate(issues), with: Entities::Issue present paginate(issues), with: Entities::Issue
end end
end end
...@@ -52,6 +54,8 @@ module API ...@@ -52,6 +54,8 @@ module API
# state (optional) - Return "opened" or "closed" issues # state (optional) - Return "opened" or "closed" issues
# labels (optional) - Comma-separated list of label names # labels (optional) - Comma-separated list of label names
# milestone (optional) - Milestone title # milestone (optional) - Milestone title
# order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
# sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
# #
# Example Requests: # Example Requests:
# GET /projects/:id/issues # GET /projects/:id/issues
...@@ -66,10 +70,12 @@ module API ...@@ -66,10 +70,12 @@ module API
issues = user_project.issues issues = user_project.issues
issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? issues = filter_issues_state(issues, params[:state]) unless params[:state].nil?
issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
unless params[:milestone].nil? unless params[:milestone].nil?
issues = filter_issues_milestone(issues, params[:milestone]) issues = filter_issues_milestone(issues, params[:milestone])
end end
issues.reorder(issuable_order_by => issuable_sort)
present paginate(issues), with: Entities::Issue present paginate(issues), with: Entities::Issue
end end
......
...@@ -25,6 +25,8 @@ module API ...@@ -25,6 +25,8 @@ module API
# Parameters: # Parameters:
# id (required) - The ID of a project # id (required) - The ID of a project
# state (optional) - Return requests "merged", "opened" or "closed" # state (optional) - Return requests "merged", "opened" or "closed"
# order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
# sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
# #
# Example: # Example:
# GET /projects/:id/merge_requests # GET /projects/:id/merge_requests
...@@ -37,25 +39,18 @@ module API ...@@ -37,25 +39,18 @@ module API
# #
get ":id/merge_requests" do get ":id/merge_requests" do
authorize! :read_merge_request, user_project authorize! :read_merge_request, user_project
merge_requests = user_project.merge_requests
merge_requests =
case params["state"]
when "opened" then merge_requests.opened
when "closed" then merge_requests.closed
when "merged" then merge_requests.merged
else merge_requests
end
mrs = case params["state"] merge_requests.reorder(issuable_order_by => issuable_sort)
when "opened" then user_project.merge_requests.opened present paginate(merge_requests), with: Entities::MergeRequest
when "closed" then user_project.merge_requests.closed
when "merged" then user_project.merge_requests.merged
else user_project.merge_requests
end
sort = case params["sort"]
when 'desc' then 'DESC'
else 'ASC'
end
mrs = case params["order_by"]
when 'updated_at' then mrs.order("updated_at #{sort}")
else mrs.order("created_at #{sort}")
end
present paginate(mrs), with: Entities::MergeRequest
end end
# Show MR # Show MR
......
...@@ -11,6 +11,37 @@ module API ...@@ -11,6 +11,37 @@ module API
attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true
attrs attrs
end end
def filter_projects(projects)
# If the archived parameter is passed, limit results accordingly
if params[:archived].present?
projects = projects.where(archived: parse_boolean(params[:archived]))
end
if params[:search].present?
projects = projects.search(params[:search])
end
projects.reorder(project_order_by => project_sort)
end
def project_order_by
order_fields = %w(id name path created_at updated_at last_activity_at)
if order_fields.include?(params['order_by'])
params['order_by']
else
'created_at'
end
end
def project_sort
if params["sort"] == 'asc'
:asc
else
:desc
end
end
end end
# Get a projects list for authenticated user # Get a projects list for authenticated user
...@@ -19,25 +50,7 @@ module API ...@@ -19,25 +50,7 @@ module API
# GET /projects # GET /projects
get do get do
@projects = current_user.authorized_projects @projects = current_user.authorized_projects
sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = filter_projects(@projects)
@projects = case params["order_by"]
when 'id' then @projects.reorder("id #{sort}")
when 'name' then @projects.reorder("name #{sort}")
when 'created_at' then @projects.reorder("created_at #{sort}")
when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}")
else @projects
end
# If the archived parameter is passed, limit results accordingly
if params[:archived].present?
@projects = @projects.where(archived: parse_boolean(params[:archived]))
end
if params[:search].present?
@projects = @projects.search(params[:search])
end
@projects = paginate @projects @projects = paginate @projects
present @projects, with: Entities::Project present @projects, with: Entities::Project
end end
...@@ -47,16 +60,8 @@ module API ...@@ -47,16 +60,8 @@ module API
# Example Request: # Example Request:
# GET /projects/owned # GET /projects/owned
get '/owned' do get '/owned' do
sort = params[:sort] == 'desc' ? 'desc' : 'asc'
@projects = current_user.owned_projects @projects = current_user.owned_projects
@projects = case params["order_by"] @projects = filter_projects(@projects)
when 'id' then @projects.reorder("id #{sort}")
when 'name' then @projects.reorder("name #{sort}")
when 'created_at' then @projects.reorder("created_at #{sort}")
when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}")
else @projects
end
@projects = paginate @projects @projects = paginate @projects
present @projects, with: Entities::Project present @projects, with: Entities::Project
end end
...@@ -67,16 +72,8 @@ module API ...@@ -67,16 +72,8 @@ module API
# GET /projects/all # GET /projects/all
get '/all' do get '/all' do
authenticated_as_admin! authenticated_as_admin!
sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = Project.all
@projects = filter_projects(@projects)
@projects = case params["order_by"]
when 'id' then Project.order("id #{sort}")
when 'name' then Project.order("name #{sort}")
when 'created_at' then Project.order("created_at #{sort}")
when 'last_activity_at' then Project.order("last_activity_at #{sort}")
else Project
end
@projects = paginate @projects @projects = paginate @projects
present @projects, with: Entities::Project present @projects, with: Entities::Project
end end
......
...@@ -28,6 +28,7 @@ describe API::API, api: true do ...@@ -28,6 +28,7 @@ describe API::API, api: true do
json_response.length.should == 3 json_response.length.should == 3
json_response.first['title'].should == merge_request.title json_response.first['title'].should == merge_request.title
end end
it "should return an array of all merge_requests" do it "should return an array of all merge_requests" do
get api("/projects/#{project.id}/merge_requests?state", user) get api("/projects/#{project.id}/merge_requests?state", user)
response.status.should == 200 response.status.should == 200
...@@ -35,6 +36,7 @@ describe API::API, api: true do ...@@ -35,6 +36,7 @@ describe API::API, api: true do
json_response.length.should == 3 json_response.length.should == 3
json_response.first['title'].should == merge_request.title json_response.first['title'].should == merge_request.title
end end
it "should return an array of open merge_requests" do it "should return an array of open merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=opened", user) get api("/projects/#{project.id}/merge_requests?state=opened", user)
response.status.should == 200 response.status.should == 200
...@@ -42,6 +44,7 @@ describe API::API, api: true do ...@@ -42,6 +44,7 @@ describe API::API, api: true do
json_response.length.should == 1 json_response.length.should == 1
json_response.first['title'].should == merge_request.title json_response.first['title'].should == merge_request.title
end end
it "should return an array of closed merge_requests" do it "should return an array of closed merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=closed", user) get api("/projects/#{project.id}/merge_requests?state=closed", user)
response.status.should == 200 response.status.should == 200
...@@ -50,6 +53,7 @@ describe API::API, api: true do ...@@ -50,6 +53,7 @@ describe API::API, api: true do
json_response.first['title'].should == merge_request_closed.title json_response.first['title'].should == merge_request_closed.title
json_response.second['title'].should == merge_request_merged.title json_response.second['title'].should == merge_request_merged.title
end end
it "should return an array of merged merge_requests" do it "should return an array of merged merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=merged", user) get api("/projects/#{project.id}/merge_requests?state=merged", user)
response.status.should == 200 response.status.should == 200
......
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