Commit 7381a33f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '44799-api-naming-issue-scope' into 'master'

Resolve "API naming for issue scope"

Closes #44799

See merge request gitlab-org/gitlab-ce!18935
parents cf7f3606 d839b880
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# klass - actual class like Issue or MergeRequest # klass - actual class like Issue or MergeRequest
# current_user - which user use # current_user - which user use
# params: # params:
# scope: 'created-by-me' or 'assigned-to-me' or 'all' # scope: 'created_by_me' or 'assigned_to_me' or 'all'
# state: 'opened' or 'closed' or 'all' # state: 'opened' or 'closed' or 'all'
# group_id: integer # group_id: integer
# project_id: integer # project_id: integer
...@@ -282,9 +282,9 @@ class IssuableFinder ...@@ -282,9 +282,9 @@ class IssuableFinder
return items.none if current_user_related? && !current_user return items.none if current_user_related? && !current_user
case params[:scope] case params[:scope]
when 'created-by-me', 'authored' when 'created_by_me', 'authored'
items.where(author_id: current_user.id) items.where(author_id: current_user.id)
when 'assigned-to-me' when 'assigned_to_me'
items.assigned_to(current_user) items.assigned_to(current_user)
else else
items items
...@@ -426,6 +426,7 @@ class IssuableFinder ...@@ -426,6 +426,7 @@ class IssuableFinder
end end
def current_user_related? def current_user_related?
params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me' scope = params[:scope]
scope == 'created_by_me' || scope == 'authored' || scope == 'assigned_to_me'
end end
end end
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# Arguments: # Arguments:
# current_user - which user use # current_user - which user use
# params: # params:
# scope: 'created-by-me' or 'assigned-to-me' or 'all' # scope: 'created_by_me' or 'assigned_to_me' or 'all'
# state: 'open' or 'closed' or 'all' # state: 'open' or 'closed' or 'all'
# group_id: integer # group_id: integer
# project_id: integer # project_id: integer
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# Arguments: # Arguments:
# current_user - which user use # current_user - which user use
# params: # params:
# scope: 'created-by-me' or 'assigned-to-me' or 'all' # scope: 'created_by_me' or 'assigned_to_me' or 'all'
# state: 'open', 'closed', 'merged', or 'all' # state: 'open', 'closed', 'merged', or 'all'
# group_id: integer # group_id: integer
# project_id: integer # project_id: integer
......
---
title: Rename issue scope created-by-me to created_by_me, and assigned-to-me to assigned_to_me
merge_request: 44799
author:
type: deprecated
...@@ -38,8 +38,8 @@ GET /issues?my_reaction_emoji=star ...@@ -38,8 +38,8 @@ GET /issues?my_reaction_emoji=star
| `state` | string | no | Return all issues or just those that are `opened` or `closed` | | `state` | string | no | Return all issues or just those that are `opened` or `closed` |
| `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels | | `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels |
| `milestone` | string | no | The milestone title | | `milestone` | string | no | The milestone title |
| `scope` | string | no | Return issues for the given scope: `created-by-me`, `assigned-to-me` or `all`. Defaults to `created-by-me` _([Introduced][ce-13004] in GitLab 9.5)_ | | `scope` | string | no | Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`. Defaults to `created_by_me`<br> For versions before 11.0, use the now deprecated `created-by-me` or `assigned-to-me` scopes instead.<br> _([Introduced][ce-13004] in GitLab 9.5. [Changed to snake_case][ce-18935] in GitLab 11.0)_ |
| `author_id` | integer | no | Return issues created by the given user `id`. Combine with `scope=all` or `scope=assigned-to-me`. _([Introduced][ce-13004] in GitLab 9.5)_ | | `author_id` | integer | no | Return issues created by the given user `id`. Combine with `scope=all` or `scope=assigned_to_me`. _([Introduced][ce-13004] in GitLab 9.5)_ |
| `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ | | `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ |
| `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ | | `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ |
| `iids[]` | Array[integer] | no | Return only the issues having the given `iid` | | `iids[]` | Array[integer] | no | Return only the issues having the given `iid` |
...@@ -152,7 +152,7 @@ GET /groups/:id/issues?my_reaction_emoji=star ...@@ -152,7 +152,7 @@ GET /groups/:id/issues?my_reaction_emoji=star
| `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels | | `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels |
| `iids[]` | Array[integer] | no | Return only the issues having the given `iid` | | `iids[]` | Array[integer] | no | Return only the issues having the given `iid` |
| `milestone` | string | no | The milestone title | | `milestone` | string | no | The milestone title |
| `scope` | string | no | Return issues for the given scope: `created-by-me`, `assigned-to-me` or `all` _([Introduced][ce-13004] in GitLab 9.5)_ | | `scope` | string | no | Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`.<br> For versions before 11.0, use the now deprecated `created-by-me` or `assigned-to-me` scopes instead.<br> _([Introduced][ce-13004] in GitLab 9.5. [Changed to snake_case][ce-18935] in GitLab 11.0)_ |
| `author_id` | integer | no | Return issues created by the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ | | `author_id` | integer | no | Return issues created by the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ |
| `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ | | `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ |
| `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ | | `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ |
...@@ -266,7 +266,7 @@ GET /projects/:id/issues?my_reaction_emoji=star ...@@ -266,7 +266,7 @@ GET /projects/:id/issues?my_reaction_emoji=star
| `state` | string | no | Return all issues or just those that are `opened` or `closed` | | `state` | string | no | Return all issues or just those that are `opened` or `closed` |
| `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels | | `labels` | string | no | Comma-separated list of label names, issues must have all labels to be returned. `No+Label` lists all issues with no labels |
| `milestone` | string | no | The milestone title | | `milestone` | string | no | The milestone title |
| `scope` | string | no | Return issues for the given scope: `created-by-me`, `assigned-to-me` or `all` _([Introduced][ce-13004] in GitLab 9.5)_ | | `scope` | string | no | Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`.<br> For versions before 11.0, use the now deprecated `created-by-me` or `assigned-to-me` scopes instead.<br> _([Introduced][ce-13004] in GitLab 9.5. [Changed to snake_case][ce-18935] in GitLab 11.0)_ |
| `author_id` | integer | no | Return issues created by the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ | | `author_id` | integer | no | Return issues created by the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ |
| `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ | | `assignee_id` | integer | no | Return issues assigned to the given user `id` _([Introduced][ce-13004] in GitLab 9.5)_ |
| `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ | | `my_reaction_emoji` | string | no | Return issues reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ |
...@@ -1254,3 +1254,4 @@ Example response: ...@@ -1254,3 +1254,4 @@ Example response:
[ce-13004]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13004 [ce-13004]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13004
[ce-14016]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14016 [ce-14016]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14016
[ce-17042]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/17042 [ce-17042]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/17042
[ce-18935]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18935
...@@ -28,7 +28,7 @@ GET /merge_requests?milestone=release ...@@ -28,7 +28,7 @@ GET /merge_requests?milestone=release
GET /merge_requests?labels=bug,reproduced GET /merge_requests?labels=bug,reproduced
GET /merge_requests?author_id=5 GET /merge_requests?author_id=5
GET /merge_requests?my_reaction_emoji=star GET /merge_requests?my_reaction_emoji=star
GET /merge_requests?scope=assigned-to-me GET /merge_requests?scope=assigned_to_me
``` ```
Parameters: Parameters:
...@@ -45,8 +45,8 @@ Parameters: ...@@ -45,8 +45,8 @@ Parameters:
| `created_before` | datetime | no | Return merge requests created on or before the given time | | `created_before` | datetime | no | Return merge requests created on or before the given time |
| `updated_after` | datetime | no | Return merge requests updated on or after the given time | | `updated_after` | datetime | no | Return merge requests updated on or after the given time |
| `updated_before` | datetime | no | Return merge requests updated on or before the given time | | `updated_before` | datetime | no | Return merge requests updated on or before the given time |
| `scope` | string | no | Return merge requests for the given scope: `created-by-me`, `assigned-to-me` or `all`. Defaults to `created-by-me` | | `scope` | string | no | Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`. Defaults to `created_by_me`<br> For versions before 11.0, use the now deprecated `created-by-me` or `assigned-to-me` scopes instead. |
| `author_id` | integer | no | Returns merge requests created by the given user `id`. Combine with `scope=all` or `scope=assigned-to-me` | | `author_id` | integer | no | Returns merge requests created by the given user `id`. Combine with `scope=all` or `scope=assigned_to_me` |
| `assignee_id` | integer | no | Returns merge requests assigned to the given user `id` | | `assignee_id` | integer | no | Returns merge requests assigned to the given user `id` |
| `my_reaction_emoji` | string | no | Return merge requests reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ | | `my_reaction_emoji` | string | no | Return merge requests reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ |
| `source_branch` | string | no | Return merge requests with the given source branch | | `source_branch` | string | no | Return merge requests with the given source branch |
...@@ -164,7 +164,7 @@ Parameters: ...@@ -164,7 +164,7 @@ Parameters:
| `created_before` | datetime | no | Return merge requests created on or before the given time | | `created_before` | datetime | no | Return merge requests created on or before the given time |
| `updated_after` | datetime | no | Return merge requests updated on or after the given time | | `updated_after` | datetime | no | Return merge requests updated on or after the given time |
| `updated_before` | datetime | no | Return merge requests updated on or before the given time | | `updated_before` | datetime | no | Return merge requests updated on or before the given time |
| `scope` | string | no | Return merge requests for the given scope: `created-by-me`, `assigned-to-me` or `all` _([Introduced][ce-13060] in GitLab 9.5)_ | | `scope` | string | no | Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`.<br> For versions before 11.0, use the now deprecated `created-by-me` or `assigned-to-me` scopes instead.<br> _([Introduced][ce-13060] in GitLab 9.5. [Changed to snake_case][ce-18935] in GitLab 11.0)_ |
| `author_id` | integer | no | Returns merge requests created by the given user `id` _([Introduced][ce-13060] in GitLab 9.5)_ | | `author_id` | integer | no | Returns merge requests created by the given user `id` _([Introduced][ce-13060] in GitLab 9.5)_ |
| `assignee_id` | integer | no | Returns merge requests assigned to the given user `id` _([Introduced][ce-13060] in GitLab 9.5)_ | | `assignee_id` | integer | no | Returns merge requests assigned to the given user `id` _([Introduced][ce-13060] in GitLab 9.5)_ |
| `my_reaction_emoji` | string | no | Return merge requests reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ | | `my_reaction_emoji` | string | no | Return merge requests reacted by the authenticated user by the given `emoji` _([Introduced][ce-14016] in GitLab 10.0)_ |
...@@ -1460,3 +1460,4 @@ Example response: ...@@ -1460,3 +1460,4 @@ Example response:
[ce-13060]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13060 [ce-13060]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/13060
[ce-14016]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14016 [ce-14016]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/14016
[ce-15454]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15454 [ce-15454]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15454
[ce-18935]: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18935
...@@ -13,6 +13,7 @@ module API ...@@ -13,6 +13,7 @@ module API
args.delete(:id) args.delete(:id)
args[:milestone_title] = args.delete(:milestone) args[:milestone_title] = args.delete(:milestone)
args[:label_name] = args.delete(:labels) args[:label_name] = args.delete(:labels)
args[:scope] = args[:scope].underscore if args[:scope]
issues = IssuesFinder.new(current_user, args).execute issues = IssuesFinder.new(current_user, args).execute
.preload(:assignees, :labels, :notes, :timelogs) .preload(:assignees, :labels, :notes, :timelogs)
...@@ -36,8 +37,8 @@ module API ...@@ -36,8 +37,8 @@ module API
optional :updated_before, type: DateTime, desc: 'Return issues updated before the specified time' optional :updated_before, type: DateTime, desc: 'Return issues updated before the specified time'
optional :author_id, type: Integer, desc: 'Return issues which are authored by the user with the given ID' optional :author_id, type: Integer, desc: 'Return issues which are authored by the user with the given ID'
optional :assignee_id, type: Integer, desc: 'Return issues which are assigned to the user with the given ID' optional :assignee_id, type: Integer, desc: 'Return issues which are assigned to the user with the given ID'
optional :scope, type: String, values: %w[created-by-me assigned-to-me all], optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
desc: 'Return issues for the given scope: `created-by-me`, `assigned-to-me` or `all`' desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji' optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
use :pagination use :pagination
end end
...@@ -66,8 +67,8 @@ module API ...@@ -66,8 +67,8 @@ module API
optional :state, type: String, values: %w[opened closed all], default: 'all', optional :state, type: String, values: %w[opened closed all], default: 'all',
desc: 'Return opened, closed, or all issues' desc: 'Return opened, closed, or all issues'
use :issues_params use :issues_params
optional :scope, type: String, values: %w[created-by-me assigned-to-me all], default: 'created-by-me', optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all], default: 'created_by_me',
desc: 'Return issues for the given scope: `created-by-me`, `assigned-to-me` or `all`' desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
end end
get do get do
authenticate! unless params[:scope] == 'all' authenticate! unless params[:scope] == 'all'
......
...@@ -38,6 +38,7 @@ module API ...@@ -38,6 +38,7 @@ module API
args[:milestone_title] = args.delete(:milestone) args[:milestone_title] = args.delete(:milestone)
args[:label_name] = args.delete(:labels) args[:label_name] = args.delete(:labels)
args[:scope] = args[:scope].underscore if args[:scope]
merge_requests = MergeRequestsFinder.new(current_user, args).execute merge_requests = MergeRequestsFinder.new(current_user, args).execute
.reorder(args[:order_by] => args[:sort]) .reorder(args[:order_by] => args[:sort])
...@@ -79,8 +80,8 @@ module API ...@@ -79,8 +80,8 @@ module API
optional :view, type: String, values: %w[simple], desc: 'If simple, returns the `iid`, URL, title, description, and basic state of merge request' optional :view, type: String, values: %w[simple], desc: 'If simple, returns the `iid`, URL, title, description, and basic state of merge request'
optional :author_id, type: Integer, desc: 'Return merge requests which are authored by the user with the given ID' optional :author_id, type: Integer, desc: 'Return merge requests which are authored by the user with the given ID'
optional :assignee_id, type: Integer, desc: 'Return merge requests which are assigned to the user with the given ID' optional :assignee_id, type: Integer, desc: 'Return merge requests which are assigned to the user with the given ID'
optional :scope, type: String, values: %w[created-by-me assigned-to-me all], optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
desc: 'Return merge requests for the given scope: `created-by-me`, `assigned-to-me` or `all`' desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji' optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
optional :source_branch, type: String, desc: 'Return merge requests with the given source branch' optional :source_branch, type: String, desc: 'Return merge requests with the given source branch'
optional :target_branch, type: String, desc: 'Return merge requests with the given target branch' optional :target_branch, type: String, desc: 'Return merge requests with the given target branch'
...@@ -95,8 +96,8 @@ module API ...@@ -95,8 +96,8 @@ module API
end end
params do params do
use :merge_requests_params use :merge_requests_params
optional :scope, type: String, values: %w[created-by-me assigned-to-me all], default: 'created-by-me', optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all], default: 'created_by_me',
desc: 'Return merge requests for the given scope: `created-by-me`, `assigned-to-me` or `all`' desc: 'Return merge requests for the given scope: `created_by_me`, `assigned_to_me` or `all`'
end end
get do get do
authenticate! unless params[:scope] == 'all' authenticate! unless params[:scope] == 'all'
......
...@@ -372,7 +372,7 @@ describe IssuesFinder do ...@@ -372,7 +372,7 @@ describe IssuesFinder do
end end
context 'personal scope' do context 'personal scope' do
let(:scope) { 'assigned-to-me' } let(:scope) { 'assigned_to_me' }
it 'returns issue assigned to the user' do it 'returns issue assigned to the user' do
expect(issues).to contain_exactly(issue1, issue2) expect(issues).to contain_exactly(issue1, issue2)
......
...@@ -126,6 +126,15 @@ describe API::Issues do ...@@ -126,6 +126,15 @@ describe API::Issues do
it 'returns issues assigned to me' do it 'returns issues assigned to me' do
issue2 = create(:issue, assignees: [user2], project: project) issue2 = create(:issue, assignees: [user2], project: project)
get api('/issues', user2), scope: 'assigned_to_me'
expect_paginated_array_response(size: 1)
expect(first_issue['id']).to eq(issue2.id)
end
it 'returns issues assigned to me (kebab-case)' do
issue2 = create(:issue, assignees: [user2], project: project)
get api('/issues', user2), scope: 'assigned-to-me' get api('/issues', user2), scope: 'assigned-to-me'
expect_paginated_array_response(size: 1) expect_paginated_array_response(size: 1)
......
...@@ -34,8 +34,7 @@ describe API::MergeRequests do ...@@ -34,8 +34,7 @@ describe API::MergeRequests do
it 'returns an array of all merge requests' do it 'returns an array of all merge requests' do
get api('/merge_requests', user), scope: 'all' get api('/merge_requests', user), scope: 'all'
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
end end
it "returns authentication error without any scope" do it "returns authentication error without any scope" do
...@@ -50,11 +49,23 @@ describe API::MergeRequests do ...@@ -50,11 +49,23 @@ describe API::MergeRequests do
expect(response).to have_gitlab_http_status(401) expect(response).to have_gitlab_http_status(401)
end end
it "returns authentication error when scope is assigned_to_me" do
get api("/merge_requests"), scope: 'assigned_to_me'
expect(response).to have_gitlab_http_status(401)
end
it "returns authentication error when scope is created-by-me" do it "returns authentication error when scope is created-by-me" do
get api("/merge_requests"), scope: 'created-by-me' get api("/merge_requests"), scope: 'created-by-me'
expect(response).to have_gitlab_http_status(401) expect(response).to have_gitlab_http_status(401)
end end
it "returns authentication error when scope is created_by_me" do
get api("/merge_requests"), scope: 'created_by_me'
expect(response).to have_gitlab_http_status(401)
end
end end
context 'when authenticated' do context 'when authenticated' do
...@@ -62,27 +73,14 @@ describe API::MergeRequests do ...@@ -62,27 +73,14 @@ describe API::MergeRequests do
let!(:merge_request2) { create(:merge_request, :simple, author: user, assignee: user, source_project: project2, target_project: project2) } let!(:merge_request2) { create(:merge_request, :simple, author: user, assignee: user, source_project: project2, target_project: project2) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
it 'returns an array of all merge requests' do it 'returns an array of all merge requests except unauthorized ones' do
get api('/merge_requests', user), scope: :all
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.map { |mr| mr['id'] })
.to contain_exactly(merge_request.id, merge_request_closed.id, merge_request_merged.id, merge_request2.id)
end
it 'does not return unauthorized merge requests' do
private_project = create(:project, :private) private_project = create(:project, :private)
merge_request3 = create(:merge_request, :simple, source_project: private_project, target_project: private_project, source_branch: 'other-branch') merge_request3 = create(:merge_request, :simple, source_project: private_project, target_project: private_project, source_branch: 'other-branch')
get api('/merge_requests', user), scope: :all get api('/merge_requests', user), scope: :all
expect(response).to have_gitlab_http_status(200) expect_response_contain_exactly(merge_request2, merge_request_merged, merge_request_closed, merge_request)
expect(response).to include_pagination_headers expect(json_response.map { |mr| mr['id'] }).not_to include(merge_request3.id)
expect(json_response).to be_an Array
expect(json_response.map { |mr| mr['id'] })
.not_to include(merge_request3.id)
end end
it 'returns an array of merge requests created by current user if no scope is given' do it 'returns an array of merge requests created by current user if no scope is given' do
...@@ -90,10 +88,7 @@ describe API::MergeRequests do ...@@ -90,10 +88,7 @@ describe API::MergeRequests do
get api('/merge_requests', user2) get api('/merge_requests', user2)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
it 'returns an array of merge requests authored by the given user' do it 'returns an array of merge requests authored by the given user' do
...@@ -101,10 +96,7 @@ describe API::MergeRequests do ...@@ -101,10 +96,7 @@ describe API::MergeRequests do
get api('/merge_requests', user), author_id: user2.id, scope: :all get api('/merge_requests', user), author_id: user2.id, scope: :all
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
it 'returns an array of merge requests assigned to the given user' do it 'returns an array of merge requests assigned to the given user' do
...@@ -112,32 +104,39 @@ describe API::MergeRequests do ...@@ -112,32 +104,39 @@ describe API::MergeRequests do
get api('/merge_requests', user), assignee_id: user2.id, scope: :all get api('/merge_requests', user), assignee_id: user2.id, scope: :all
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
it 'returns an array of merge requests assigned to me' do it 'returns an array of merge requests assigned to me' do
merge_request3 = create(:merge_request, :simple, author: user, assignee: user2, source_project: project2, target_project: project2, source_branch: 'other-branch') merge_request3 = create(:merge_request, :simple, author: user, assignee: user2, source_project: project2, target_project: project2, source_branch: 'other-branch')
get api('/merge_requests', user2), scope: 'assigned_to_me'
expect_response_ordered_exactly(merge_request3)
end
it 'returns an array of merge requests assigned to me (kebab-case)' do
merge_request3 = create(:merge_request, :simple, author: user, assignee: user2, source_project: project2, target_project: project2, source_branch: 'other-branch')
get api('/merge_requests', user2), scope: 'assigned-to-me' get api('/merge_requests', user2), scope: 'assigned-to-me'
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
it 'returns an array of merge requests created by me' do it 'returns an array of merge requests created by me' do
merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch') merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch')
get api('/merge_requests', user2), scope: 'created_by_me'
expect_response_ordered_exactly(merge_request3)
end
it 'returns an array of merge requests created by me (kebab-case)' do
merge_request3 = create(:merge_request, :simple, author: user2, assignee: user, source_project: project2, target_project: project2, source_branch: 'other-branch')
get api('/merge_requests', user2), scope: 'created-by-me' get api('/merge_requests', user2), scope: 'created-by-me'
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
it 'returns merge requests reacted by the authenticated user by the given emoji' do it 'returns merge requests reacted by the authenticated user by the given emoji' do
...@@ -146,19 +145,14 @@ describe API::MergeRequests do ...@@ -146,19 +145,14 @@ describe API::MergeRequests do
get api('/merge_requests', user2), my_reaction_emoji: award_emoji.name, scope: 'all' get api('/merge_requests', user2), my_reaction_emoji: award_emoji.name, scope: 'all'
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request3)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request3.id)
end end
context 'source_branch param' do context 'source_branch param' do
it 'returns merge requests with the given source branch' do it 'returns merge requests with the given source branch' do
get api('/merge_requests', user), source_branch: merge_request_closed.source_branch, state: 'all' get api('/merge_requests', user), source_branch: merge_request_closed.source_branch, state: 'all'
expect(json_response.length).to eq(2) expect_response_contain_exactly(merge_request_closed, merge_request_merged)
expect(json_response.map { |mr| mr['id'] })
.to contain_exactly(merge_request_closed.id, merge_request_merged.id)
end end
end end
...@@ -166,9 +160,7 @@ describe API::MergeRequests do ...@@ -166,9 +160,7 @@ describe API::MergeRequests do
it 'returns merge requests with the given target branch' do it 'returns merge requests with the given target branch' do
get api('/merge_requests', user), target_branch: merge_request_closed.target_branch, state: 'all' get api('/merge_requests', user), target_branch: merge_request_closed.target_branch, state: 'all'
expect(json_response.length).to eq(2) expect_response_contain_exactly(merge_request_closed, merge_request_merged)
expect(json_response.map { |mr| mr['id'] })
.to contain_exactly(merge_request_closed.id, merge_request_merged.id)
end end
end end
...@@ -177,8 +169,7 @@ describe API::MergeRequests do ...@@ -177,8 +169,7 @@ describe API::MergeRequests do
get api('/merge_requests?created_before=2000-01-02T00:00:00.060Z', user) get api('/merge_requests?created_before=2000-01-02T00:00:00.060Z', user)
expect(json_response.size).to eq(1) expect_response_ordered_exactly(merge_request2)
expect(json_response.first['id']).to eq(merge_request2.id)
end end
it 'returns merge requests created after a specific date' do it 'returns merge requests created after a specific date' do
...@@ -186,8 +177,7 @@ describe API::MergeRequests do ...@@ -186,8 +177,7 @@ describe API::MergeRequests do
get api("/merge_requests?created_after=#{merge_request2.created_at}", user) get api("/merge_requests?created_after=#{merge_request2.created_at}", user)
expect(json_response.size).to eq(1) expect_response_ordered_exactly(merge_request2)
expect(json_response.first['id']).to eq(merge_request2.id)
end end
it 'returns merge requests updated before a specific date' do it 'returns merge requests updated before a specific date' do
...@@ -195,8 +185,7 @@ describe API::MergeRequests do ...@@ -195,8 +185,7 @@ describe API::MergeRequests do
get api('/merge_requests?updated_before=2000-01-02T00:00:00.060Z', user) get api('/merge_requests?updated_before=2000-01-02T00:00:00.060Z', user)
expect(json_response.size).to eq(1) expect_response_ordered_exactly(merge_request2)
expect(json_response.first['id']).to eq(merge_request2.id)
end end
it 'returns merge requests updated after a specific date' do it 'returns merge requests updated after a specific date' do
...@@ -204,8 +193,7 @@ describe API::MergeRequests do ...@@ -204,8 +193,7 @@ describe API::MergeRequests do
get api("/merge_requests?updated_after=#{merge_request2.updated_at}", user) get api("/merge_requests?updated_after=#{merge_request2.updated_at}", user)
expect(json_response.size).to eq(1) expect_response_ordered_exactly(merge_request2)
expect(json_response.first['id']).to eq(merge_request2.id)
end end
context 'search params' do context 'search params' do
...@@ -216,15 +204,13 @@ describe API::MergeRequests do ...@@ -216,15 +204,13 @@ describe API::MergeRequests do
it 'returns merge requests matching given search string for title' do it 'returns merge requests matching given search string for title' do
get api("/merge_requests", user), search: merge_request.title get api("/merge_requests", user), search: merge_request.title
expect(json_response.length).to eq(1) expect_response_ordered_exactly(merge_request)
expect(json_response.first['id']).to eq(merge_request.id)
end end
it 'returns merge requests for project matching given search string for description' do it 'returns merge requests for project matching given search string for description' do
get api("/merge_requests", user), project_id: project.id, search: merge_request.description get api("/merge_requests", user), project_id: project.id, search: merge_request.description
expect(json_response.length).to eq(1) expect_response_ordered_exactly(merge_request)
expect(json_response.first['id']).to eq(merge_request.id)
end end
end end
end end
...@@ -235,8 +221,7 @@ describe API::MergeRequests do ...@@ -235,8 +221,7 @@ describe API::MergeRequests do
it 'returns merge requests for public projects' do it 'returns merge requests for public projects' do
get api("/projects/#{project.id}/merge_requests") get api("/projects/#{project.id}/merge_requests")
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
end end
it "returns 404 for non public projects" do it "returns 404 for non public projects" do
...@@ -265,10 +250,7 @@ describe API::MergeRequests do ...@@ -265,10 +250,7 @@ describe API::MergeRequests do
it "returns an array of all merge_requests" do it "returns an array of all merge_requests" do
get api("/projects/#{project.id}/merge_requests", user) get api("/projects/#{project.id}/merge_requests", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_merged, merge_request_closed, merge_request)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
expect(json_response.last['title']).to eq(merge_request.title) expect(json_response.last['title']).to eq(merge_request.title)
expect(json_response.last).to have_key('web_url') expect(json_response.last).to have_key('web_url')
expect(json_response.last['sha']).to eq(merge_request.diff_head_sha) expect(json_response.last['sha']).to eq(merge_request.diff_head_sha)
...@@ -286,11 +268,8 @@ describe API::MergeRequests do ...@@ -286,11 +268,8 @@ describe API::MergeRequests do
it "returns an array of all merge_requests using simple mode" do it "returns an array of all merge_requests using simple mode" do
get api("/projects/#{project.id}/merge_requests?view=simple", user) get api("/projects/#{project.id}/merge_requests?view=simple", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_merged, merge_request_closed, merge_request)
expect(response).to include_pagination_headers
expect(json_response.last.keys).to match_array(%w(id iid title web_url created_at description project_id state updated_at)) expect(json_response.last.keys).to match_array(%w(id iid title web_url created_at description project_id state updated_at))
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
expect(json_response.last['iid']).to eq(merge_request.iid) expect(json_response.last['iid']).to eq(merge_request.iid)
expect(json_response.last['title']).to eq(merge_request.title) expect(json_response.last['title']).to eq(merge_request.title)
expect(json_response.last).to have_key('web_url') expect(json_response.last).to have_key('web_url')
...@@ -302,51 +281,36 @@ describe API::MergeRequests do ...@@ -302,51 +281,36 @@ describe API::MergeRequests do
it "returns an array of all merge_requests" do it "returns 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)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_merged, merge_request_closed, merge_request)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
expect(json_response.last['title']).to eq(merge_request.title) expect(json_response.last['title']).to eq(merge_request.title)
end end
it "returns an array of open merge_requests" do it "returns 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)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.last['title']).to eq(merge_request.title) expect(json_response.last['title']).to eq(merge_request.title)
end end
it "returns an array of closed merge_requests" do it "returns 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)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_closed)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['title']).to eq(merge_request_closed.title) expect(json_response.first['title']).to eq(merge_request_closed.title)
end end
it "returns an array of merged merge_requests" do it "returns 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)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_merged)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['title']).to eq(merge_request_merged.title) expect(json_response.first['title']).to eq(merge_request_merged.title)
end end
it 'returns merge_request by "iids" array' do it 'returns merge_request by "iids" array' do
get api("/projects/#{project.id}/merge_requests", user), iids: [merge_request.iid, merge_request_closed.iid] get api("/projects/#{project.id}/merge_requests", user), iids: [merge_request.iid, merge_request_closed.iid]
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_closed, merge_request)
expect(json_response).to be_an Array
expect(json_response.length).to eq(2)
expect(json_response.first['title']).to eq merge_request_closed.title expect(json_response.first['title']).to eq merge_request_closed.title
expect(json_response.first['id']).to eq merge_request_closed.id
end end
it 'matches V4 response schema' do it 'matches V4 response schema' do
...@@ -359,16 +323,14 @@ describe API::MergeRequests do ...@@ -359,16 +323,14 @@ describe API::MergeRequests do
it 'returns an empty array if no issue matches milestone' do it 'returns an empty array if no issue matches milestone' do
get api("/projects/#{project.id}/merge_requests", user), milestone: '1.0.0' get api("/projects/#{project.id}/merge_requests", user), milestone: '1.0.0'
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
expect(json_response.length).to eq(0) expect(json_response.length).to eq(0)
end end
it 'returns an empty array if milestone does not exist' do it 'returns an empty array if milestone does not exist' do
get api("/projects/#{project.id}/merge_requests", user), milestone: 'foo' get api("/projects/#{project.id}/merge_requests", user), milestone: 'foo'
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
expect(json_response.length).to eq(0) expect(json_response.length).to eq(0)
end end
...@@ -382,17 +344,13 @@ describe API::MergeRequests do ...@@ -382,17 +344,13 @@ describe API::MergeRequests do
it 'returns an array of merge requests matching state in milestone' do it 'returns an array of merge requests matching state in milestone' do
get api("/projects/#{project.id}/merge_requests", user), milestone: '0.9', state: 'closed' get api("/projects/#{project.id}/merge_requests", user), milestone: '0.9', state: 'closed'
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(merge_request_closed)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(merge_request_closed.id)
end end
it 'returns an array of labeled merge requests' do it 'returns an array of labeled merge requests' do
get api("/projects/#{project.id}/merge_requests?labels=#{label.title}", user) get api("/projects/#{project.id}/merge_requests?labels=#{label.title}", user)
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
expect(json_response.length).to eq(1) expect(json_response.length).to eq(1)
expect(json_response.first['labels']).to eq([label2.title, label.title]) expect(json_response.first['labels']).to eq([label2.title, label.title])
end end
...@@ -400,16 +358,14 @@ describe API::MergeRequests do ...@@ -400,16 +358,14 @@ describe API::MergeRequests do
it 'returns an array of labeled merge requests where all labels match' do it 'returns an array of labeled merge requests where all labels match' do
get api("/projects/#{project.id}/merge_requests?labels=#{label.title},foo,bar", user) get api("/projects/#{project.id}/merge_requests?labels=#{label.title},foo,bar", user)
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
expect(json_response.length).to eq(0) expect(json_response.length).to eq(0)
end end
it 'returns an empty array if no merge request matches labels' do it 'returns an empty array if no merge request matches labels' do
get api("/projects/#{project.id}/merge_requests?labels=foo,bar", user) get api("/projects/#{project.id}/merge_requests?labels=foo,bar", user)
expect(response).to have_gitlab_http_status(200) expect_paginated_array_response
expect(json_response).to be_an Array
expect(json_response.length).to eq(0) expect(json_response.length).to eq(0)
end end
...@@ -427,13 +383,12 @@ describe API::MergeRequests do ...@@ -427,13 +383,12 @@ describe API::MergeRequests do
get api("/projects/#{project.id}/merge_requests?labels=#{bug_label.title}&milestone=#{milestone1.title}&state=merged", user) get api("/projects/#{project.id}/merge_requests?labels=#{bug_label.title}&milestone=#{milestone1.title}&state=merged", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(mr2)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(mr2.id)
end end
context "with ordering" do context "with ordering" do
let(:merge_requests) { [merge_request_merged, merge_request_closed, merge_request] }
before do before do
@mr_later = mr_with_later_created_and_updated_at_time @mr_later = mr_with_later_created_and_updated_at_time
@mr_earlier = mr_with_earlier_created_and_updated_at_time @mr_earlier = mr_with_earlier_created_and_updated_at_time
...@@ -442,45 +397,25 @@ describe API::MergeRequests do ...@@ -442,45 +397,25 @@ describe API::MergeRequests do
it "returns an array of merge_requests in ascending order" do it "returns an array of merge_requests in ascending order" do
get api("/projects/#{project.id}/merge_requests?sort=asc", user) get api("/projects/#{project.id}/merge_requests?sort=asc", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(*merge_requests.sort_by { |mr| mr['created_at'] })
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
response_dates = json_response.map { |merge_request| merge_request['created_at'] }
expect(response_dates).to eq(response_dates.sort)
end end
it "returns an array of merge_requests in descending order" do it "returns an array of merge_requests in descending order" do
get api("/projects/#{project.id}/merge_requests?sort=desc", user) get api("/projects/#{project.id}/merge_requests?sort=desc", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(*merge_requests.sort_by { |mr| mr['created_at'] }.reverse)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
response_dates = json_response.map { |merge_request| merge_request['created_at'] }
expect(response_dates).to eq(response_dates.sort.reverse)
end end
it "returns an array of merge_requests ordered by updated_at" do it "returns an array of merge_requests ordered by updated_at" do
get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user) get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(*merge_requests.sort_by { |mr| mr['updated_at'] }.reverse)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
response_dates = json_response.map { |merge_request| merge_request['updated_at'] }
expect(response_dates).to eq(response_dates.sort.reverse)
end end
it "returns an array of merge_requests ordered by created_at" do it "returns an array of merge_requests ordered by created_at" do
get api("/projects/#{project.id}/merge_requests?order_by=created_at&sort=asc", user) get api("/projects/#{project.id}/merge_requests?order_by=created_at&sort=asc", user)
expect(response).to have_gitlab_http_status(200) expect_response_ordered_exactly(*merge_requests.sort_by { |mr| mr['created_at'] })
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(3)
response_dates = json_response.map { |merge_request| merge_request['created_at'] }
expect(response_dates).to eq(response_dates.sort)
end end
end end
...@@ -488,9 +423,7 @@ describe API::MergeRequests do ...@@ -488,9 +423,7 @@ describe API::MergeRequests do
it 'returns merge requests with the given source branch' do it 'returns merge requests with the given source branch' do
get api('/merge_requests', user), source_branch: merge_request_closed.source_branch, state: 'all' get api('/merge_requests', user), source_branch: merge_request_closed.source_branch, state: 'all'
expect(json_response.length).to eq(2) expect_response_contain_exactly(merge_request_closed, merge_request_merged)
expect(json_response.map { |mr| mr['id'] })
.to contain_exactly(merge_request_closed.id, merge_request_merged.id)
end end
end end
...@@ -498,9 +431,7 @@ describe API::MergeRequests do ...@@ -498,9 +431,7 @@ describe API::MergeRequests do
it 'returns merge requests with the given target branch' do it 'returns merge requests with the given target branch' do
get api('/merge_requests', user), target_branch: merge_request_closed.target_branch, state: 'all' get api('/merge_requests', user), target_branch: merge_request_closed.target_branch, state: 'all'
expect(json_response.length).to eq(2) expect_response_contain_exactly(merge_request_closed, merge_request_merged)
expect(json_response.map { |mr| mr['id'] })
.to contain_exactly(merge_request_closed.id, merge_request_merged.id)
end end
end end
end end
...@@ -1341,4 +1272,22 @@ describe API::MergeRequests do ...@@ -1341,4 +1272,22 @@ describe API::MergeRequests do
merge_request_closed.save merge_request_closed.save
merge_request_closed merge_request_closed
end end
def expect_response_contain_exactly(*items)
expect_paginated_array_response
expect(json_response.length).to eq(items.size)
expect(json_response.map { |element| element['id'] }).to contain_exactly(*items.map(&:id))
end
def expect_response_ordered_exactly(*items)
expect_paginated_array_response
expect(json_response.length).to eq(items.size)
expect(json_response.map { |element| element['id'] }).to eq(items.map(&:id))
end
def expect_paginated_array_response
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
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