Commit 53b52b11 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #6699 from criteo/api_get_open_merge_requests

Add optional parameter to list merge requests
parents 481bbd75 66209fbc
## List merge requests
Get all merge requests for this project. This function takes pagination parameters
`page` and `per_page` 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?state=opened
GET /projects/:id/merge_requests?state=all
```
Parameters:
+ `id` (required) - The ID of a project
+ `state` (optional) - Return `all` requests or just those that are `merged`, `opened` or `closed`
```json
[
......
......@@ -19,14 +19,24 @@ module API
#
# Parameters:
# id (required) - The ID of a project
# state (optional) - Return requests "merged", "opened" or "closed"
#
# Example:
# GET /projects/:id/merge_requests
# GET /projects/:id/merge_requests?state=opened
# GET /projects/:id/merge_requests?state=closed
#
get ":id/merge_requests" do
authorize! :read_merge_request, user_project
present paginate(user_project.merge_requests), with: Entities::MergeRequest
mrs = case params["state"]
when "opened" then user_project.merge_requests.opened
when "closed" then user_project.merge_requests.closed
when "merged" then user_project.merge_requests.merged
else user_project.merge_requests
end
present paginate(mrs), with: Entities::MergeRequest
end
# Show MR
......
......@@ -7,6 +7,8 @@ describe API::API, api: true do
let(:user) { create(:user) }
let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, target_project: project, title: "Closed test") }
let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test") }
let!(:note) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "a comment on a MR") }
before {
project.team << [user, :reporters]
......@@ -21,12 +23,42 @@ describe API::API, api: true do
end
context "when authenticated" do
it "should return an array of merge_requests" do
it "should return an array of all merge_requests" do
get api("/projects/#{project.id}/merge_requests", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 3
json_response.first['title'].should == merge_request.title
end
it "should return an array of all merge_requests" do
get api("/projects/#{project.id}/merge_requests?state", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 3
json_response.first['title'].should == merge_request.title
end
it "should return an array of open merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=opened", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 1
json_response.first['title'].should == merge_request.title
end
it "should return an array of closed merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=closed", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 2
json_response.first['title'].should == merge_request_closed.title
json_response.second['title'].should == merge_request_merged.title
end
it "should return an array of merged merge_requests" do
get api("/projects/#{project.id}/merge_requests?state=merged", user)
response.status.should == 200
json_response.should be_an Array
json_response.length.should == 1
json_response.first['title'].should == merge_request_merged.title
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