summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Jankovski <marin@gitlab.com>2014-09-16 06:46:17 +0000
committerMarin Jankovski <marin@gitlab.com>2014-09-16 06:46:17 +0000
commit71e055444942e93ad028f50896fe680cdf2caf5d (patch)
tree46681ad3de5693a8e2ed425cadbcecd43beca22a
parent892371bc22813abe855f563bf4f0ee355fe067ab (diff)
parent685754b0887de5228485083d7ee9dcbbb763f67e (diff)
downloadgitlab-ce-71e055444942e93ad028f50896fe680cdf2caf5d.tar.gz
Merge branch 'order_mr_api' into 'master'
API MR ordering Fixes #1477 See merge request !1076
-rw-r--r--CHANGELOG1
-rw-r--r--doc/api/merge_requests.md2
-rw-r--r--lib/api/merge_requests.rb14
-rw-r--r--spec/requests/api/merge_requests_spec.rb56
4 files changed, 73 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 9c92935dc24..7271f850381 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -30,6 +30,7 @@ v 7.3.0
- Enabled Shibboleth authentication support (Matus Banas)
- Zen mode (fullscreen) for issues/MR/notes (Robert Schilling)
- Add ability to configure webhook timeout via gitlab.yml (Wes Gurney)
+ - Sort project merge requests in asc or desc order for updated_at or created_at field (sponsored by O'Reilly Media)
v 7.2.1
- Delete orphaned labels during label migration (James Brooks)
diff --git a/doc/api/merge_requests.md b/doc/api/merge_requests.md
index 3616e29ef7c..14884e53915 100644
--- a/doc/api/merge_requests.md
+++ b/doc/api/merge_requests.md
@@ -14,6 +14,8 @@ Parameters:
- `id` (required) - The ID of a project
- `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
+- `sort` (optional) - Return requests sorted in `asc` or `desc` order
```json
[
diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb
index 8726379bf3c..172c8504d0f 100644
--- a/lib/api/merge_requests.rb
+++ b/lib/api/merge_requests.rb
@@ -25,6 +25,10 @@ module API
# GET /projects/:id/merge_requests
# GET /projects/:id/merge_requests?state=opened
# GET /projects/:id/merge_requests?state=closed
+ # GET /projects/:id/merge_requests?order_by=created_at
+ # GET /projects/:id/merge_requests?order_by=updated_at
+ # GET /projects/:id/merge_requests?sort=desc
+ # GET /projects/:id/merge_requests?sort=asc
#
get ":id/merge_requests" do
authorize! :read_merge_request, user_project
@@ -36,6 +40,16 @@ module API
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
diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb
index 06a25c5e3a5..d39962670b1 100644
--- a/spec/requests/api/merge_requests_spec.rb
+++ b/spec/requests/api/merge_requests_spec.rb
@@ -57,6 +57,46 @@ describe API::API, api: true do
json_response.length.should == 1
json_response.first['title'].should == merge_request_merged.title
end
+
+ context "with ordering" do
+ before do
+ @mr_later = mr_with_later_created_and_updated_at_time
+ @mr_earlier = mr_with_earlier_created_and_updated_at_time
+ end
+
+ it "should return an array of merge_requests in ascending order" do
+ get api("/projects/#{project.id}/merge_requests?sort=asc", user)
+ response.status.should == 200
+ json_response.should be_an Array
+ json_response.length.should == 3
+ json_response.first['id'].should == @mr_earlier.id
+ json_response.last['id'].should == @mr_later.id
+ end
+ it "should return an array of merge_requests in descending order" do
+ get api("/projects/#{project.id}/merge_requests?sort=desc", user)
+ response.status.should == 200
+ json_response.should be_an Array
+ json_response.length.should == 3
+ json_response.first['id'].should == @mr_later.id
+ json_response.last['id'].should == @mr_earlier.id
+ end
+ it "should return an array of merge_requests ordered by updated_at" do
+ get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user)
+ response.status.should == 200
+ json_response.should be_an Array
+ json_response.length.should == 3
+ json_response.first['id'].should == @mr_earlier.id
+ json_response.last['id'].should == @mr_later.id
+ end
+ it "should return an array of merge_requests ordered by created_at" do
+ get api("/projects/#{project.id}/merge_requests?sort=created_at", user)
+ response.status.should == 200
+ json_response.should be_an Array
+ json_response.length.should == 3
+ json_response.first['id'].should == @mr_earlier.id
+ json_response.last['id'].should == @mr_later.id
+ end
+ end
end
end
@@ -296,4 +336,20 @@ describe API::API, api: true do
response.status.should == 404
end
end
+
+ def mr_with_later_created_and_updated_at_time
+ merge_request
+ merge_request.created_at += 1.hour
+ merge_request.updated_at += 30.minutes
+ merge_request.save
+ merge_request
+ end
+
+ def mr_with_earlier_created_and_updated_at_time
+ merge_request_closed
+ merge_request_closed.created_at -= 1.hour
+ merge_request_closed.updated_at -= 30.minutes
+ merge_request_closed.save
+ merge_request_closed
+ end
end