Commit 0f058ff3 authored by Sean McGivern's avatar Sean McGivern

Merge branch '59296-get-milestone-by-title-via-api' into 'master'

Resolve "Get milestone by title via API"

Closes #59296

See merge request gitlab-org/gitlab-ce!26573
parents 978aa04f 035c8af1
---
title: Add select by title to milestones API
merge_request: 26573
author:
type: added
...@@ -12,6 +12,7 @@ GET /groups/:id/milestones?iids[]=42 ...@@ -12,6 +12,7 @@ GET /groups/:id/milestones?iids[]=42
GET /groups/:id/milestones?iids[]=42&iids[]=43 GET /groups/:id/milestones?iids[]=42&iids[]=43
GET /groups/:id/milestones?state=active GET /groups/:id/milestones?state=active
GET /groups/:id/milestones?state=closed GET /groups/:id/milestones?state=closed
GET /groups/:id/milestones?title=1.0
GET /groups/:id/milestones?search=version GET /groups/:id/milestones?search=version
``` ```
...@@ -22,6 +23,7 @@ Parameters: ...@@ -22,6 +23,7 @@ Parameters:
| `id` | integer/string | yes | The ID or [URL-encoded path of the group](README.md#namespaced-path-encoding) owned by the authenticated user | | `id` | integer/string | yes | The ID or [URL-encoded path of the group](README.md#namespaced-path-encoding) owned by the authenticated user |
| `iids[]` | Array[integer] | optional | Return only the milestones having the given `iid` | | `iids[]` | Array[integer] | optional | Return only the milestones having the given `iid` |
| `state` | string | optional | Return only `active` or `closed` milestones | | `state` | string | optional | Return only `active` or `closed` milestones |
| `title` | string | optional | Return only the milestones having the given `title` |
| `search` | string | optional | Return only milestones with a title or description matching the provided string | | `search` | string | optional | Return only milestones with a title or description matching the provided string |
```bash ```bash
......
...@@ -10,6 +10,7 @@ GET /projects/:id/milestones?iids[]=42 ...@@ -10,6 +10,7 @@ GET /projects/:id/milestones?iids[]=42
GET /projects/:id/milestones?iids[]=42&iids[]=43 GET /projects/:id/milestones?iids[]=42&iids[]=43
GET /projects/:id/milestones?state=active GET /projects/:id/milestones?state=active
GET /projects/:id/milestones?state=closed GET /projects/:id/milestones?state=closed
GET /projects/:id/milestones?title=1.0
GET /projects/:id/milestones?search=version GET /projects/:id/milestones?search=version
``` ```
...@@ -20,6 +21,7 @@ Parameters: ...@@ -20,6 +21,7 @@ Parameters:
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user | | `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user |
| `iids[]` | Array[integer] | optional | Return only the milestones having the given `iid` | | `iids[]` | Array[integer] | optional | Return only the milestones having the given `iid` |
| `state` | string | optional | Return only `active` or `closed` milestones | | `state` | string | optional | Return only `active` or `closed` milestones |
| `title` | string | optional | Return only the milestones having the given `title` |
| `search` | string | optional | Return only milestones with a title or description matching the provided string | | `search` | string | optional | Return only milestones with a title or description matching the provided string |
```bash ```bash
......
...@@ -302,6 +302,12 @@ module API ...@@ -302,6 +302,12 @@ module API
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def filter_by_title(items, title)
items.where(title: title)
end
# rubocop: enable CodeReuse/ActiveRecord
def filter_by_search(items, text) def filter_by_search(items, text)
items.search(text) items.search(text)
end end
......
...@@ -16,6 +16,7 @@ module API ...@@ -16,6 +16,7 @@ module API
optional :state, type: String, values: %w[active closed all], default: 'all', optional :state, type: String, values: %w[active closed all], default: 'all',
desc: 'Return "active", "closed", or "all" milestones' desc: 'Return "active", "closed", or "all" milestones'
optional :iids, type: Array[Integer], desc: 'The IIDs of the milestones' optional :iids, type: Array[Integer], desc: 'The IIDs of the milestones'
optional :title, type: String, desc: 'The title of the milestones'
optional :search, type: String, desc: 'The search criteria for the title or description of the milestone' optional :search, type: String, desc: 'The search criteria for the title or description of the milestone'
use :pagination use :pagination
end end
...@@ -33,6 +34,7 @@ module API ...@@ -33,6 +34,7 @@ module API
milestones = parent.milestones.order_id_desc milestones = parent.milestones.order_id_desc
milestones = Milestone.filter_by_state(milestones, params[:state]) milestones = Milestone.filter_by_state(milestones, params[:state])
milestones = filter_by_iid(milestones, params[:iids]) if params[:iids].present? milestones = filter_by_iid(milestones, params[:iids]) if params[:iids].present?
milestones = filter_by_title(milestones, params[:title]) if params[:title]
milestones = filter_by_search(milestones, params[:search]) if params[:search] milestones = filter_by_search(milestones, params[:search]) if params[:search]
present paginate(milestones), with: Entities::Milestone present paginate(milestones), with: Entities::Milestone
......
...@@ -72,6 +72,15 @@ shared_examples_for 'group and project milestones' do |route_definition| ...@@ -72,6 +72,15 @@ shared_examples_for 'group and project milestones' do |route_definition|
expect(json_response.first['id']).to eq closed_milestone.id expect(json_response.first['id']).to eq closed_milestone.id
end end
it 'returns a milestone by title' do
get api(route, user), params: { title: 'version2' }
expect(response).to have_gitlab_http_status(200)
expect(json_response.size).to eq(1)
expect(json_response.first['title']).to eq milestone.title
expect(json_response.first['id']).to eq milestone.id
end
it 'returns a milestone by searching for title' do it 'returns a milestone by searching for title' do
get api(route, user), params: { search: 'version2' } get api(route, user), params: { search: 'version2' }
......
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