Commit 9c00e765 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'api-priority-labels' into 'master'

Exposes the label priority to the API. Furthermore, it allows the user to modify the priority via the API.

Closes #21269

See merge request !7286
parents 72c440b8 c7c3b771
---
title: API: Ability to retrieve version information
merge_request: 7286
author: Robert Schilling
...@@ -26,7 +26,9 @@ Example response: ...@@ -26,7 +26,9 @@ Example response:
"description": "Bug reported by user", "description": "Bug reported by user",
"open_issues_count": 1, "open_issues_count": 1,
"closed_issues_count": 0, "closed_issues_count": 0,
"open_merge_requests_count": 1 "open_merge_requests_count": 1,
"subscribed": false,
"priority": 10
}, },
{ {
"color" : "#d9534f", "color" : "#d9534f",
...@@ -34,7 +36,9 @@ Example response: ...@@ -34,7 +36,9 @@ Example response:
"description": "Confirmed issue", "description": "Confirmed issue",
"open_issues_count": 2, "open_issues_count": 2,
"closed_issues_count": 5, "closed_issues_count": 5,
"open_merge_requests_count": 0 "open_merge_requests_count": 0,
"subscribed": false,
"priority": null
}, },
{ {
"name" : "critical", "name" : "critical",
...@@ -42,7 +46,9 @@ Example response: ...@@ -42,7 +46,9 @@ Example response:
"description": "Critical issue. Need fix ASAP", "description": "Critical issue. Need fix ASAP",
"open_issues_count": 1, "open_issues_count": 1,
"closed_issues_count": 3, "closed_issues_count": 3,
"open_merge_requests_count": 1 "open_merge_requests_count": 1,
"subscribed": false,
"priority": null
}, },
{ {
"name" : "documentation", "name" : "documentation",
...@@ -50,7 +56,9 @@ Example response: ...@@ -50,7 +56,9 @@ Example response:
"description": "Issue about documentation", "description": "Issue about documentation",
"open_issues_count": 1, "open_issues_count": 1,
"closed_issues_count": 0, "closed_issues_count": 0,
"open_merge_requests_count": 2 "open_merge_requests_count": 2,
"subscribed": false,
"priority": null
}, },
{ {
"color" : "#5cb85c", "color" : "#5cb85c",
...@@ -58,7 +66,9 @@ Example response: ...@@ -58,7 +66,9 @@ Example response:
"description": "Enhancement proposal", "description": "Enhancement proposal",
"open_issues_count": 1, "open_issues_count": 1,
"closed_issues_count": 0, "closed_issues_count": 0,
"open_merge_requests_count": 1 "open_merge_requests_count": 1,
"subscribed": false,
"priority": null
} }
] ]
``` ```
...@@ -80,6 +90,7 @@ POST /projects/:id/labels ...@@ -80,6 +90,7 @@ POST /projects/:id/labels
| `name` | string | yes | The name of the label | | `name` | string | yes | The name of the label |
| `color` | string | yes | The color of the label in 6-digit hex notation with leading `#` sign | | `color` | string | yes | The color of the label in 6-digit hex notation with leading `#` sign |
| `description` | string | no | The description of the label | | `description` | string | no | The description of the label |
| `priority` | integer | no | The priority of the label. Must be greater or equal than zero or `null` to remove the priority. |
```bash ```bash
curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects/1/labels" curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects/1/labels"
...@@ -91,7 +102,11 @@ Example response: ...@@ -91,7 +102,11 @@ Example response:
{ {
"name" : "feature", "name" : "feature",
"color" : "#5843AD", "color" : "#5843AD",
"description":null "open_issues_count": 1,
"closed_issues_count": 0,
"open_merge_requests_count": 1,
"description": null,
"priority": null
} }
``` ```
...@@ -127,7 +142,8 @@ Example response: ...@@ -127,7 +142,8 @@ Example response:
"template" : false, "template" : false,
"project_id" : 1, "project_id" : 1,
"created_at" : "2015-11-03T21:22:30.737Z", "created_at" : "2015-11-03T21:22:30.737Z",
"id" : 9 "id" : 9,
"priority": null
} }
``` ```
...@@ -151,6 +167,8 @@ PUT /projects/:id/labels ...@@ -151,6 +167,8 @@ PUT /projects/:id/labels
| `new_name` | string | yes if `color` is not provided | The new name of the label | | `new_name` | string | yes if `color` is not provided | The new name of the label |
| `color` | string | yes if `new_name` is not provided | The new color of the label in 6-digit hex notation with leading `#` sign | | `color` | string | yes if `new_name` is not provided | The new color of the label in 6-digit hex notation with leading `#` sign |
| `description` | string | no | The new description of the label | | `description` | string | no | The new description of the label |
| `priority` | integer | no | The new priority of the label. Must be greater or equal than zero or `null` to remove the priority. |
```bash ```bash
curl --request PUT --data "name=documentation&new_name=docs&color=#8E44AD&description=Documentation" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects/1/labels" curl --request PUT --data "name=documentation&new_name=docs&color=#8E44AD&description=Documentation" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v3/projects/1/labels"
...@@ -162,7 +180,11 @@ Example response: ...@@ -162,7 +180,11 @@ Example response:
{ {
"color" : "#8E44AD", "color" : "#8E44AD",
"name" : "docs", "name" : "docs",
"description": "Documentation" "description": "Documentation",
"open_issues_count": 1,
"closed_issues_count": 0,
"open_merge_requests_count": 1,
"priority": null
} }
``` ```
...@@ -197,7 +219,8 @@ Example response: ...@@ -197,7 +219,8 @@ Example response:
"open_issues_count": 0, "open_issues_count": 0,
"closed_issues_count": 0, "closed_issues_count": 0,
"open_merge_requests_count": 0, "open_merge_requests_count": 0,
"subscribed": true "subscribed": true,
"priority": null
} }
``` ```
...@@ -232,6 +255,7 @@ Example response: ...@@ -232,6 +255,7 @@ Example response:
"open_issues_count": 0, "open_issues_count": 0,
"closed_issues_count": 0, "closed_issues_count": 0,
"open_merge_requests_count": 0, "open_merge_requests_count": 0,
"subscribed": false "subscribed": false,
"priority": null
} }
``` ```
...@@ -438,6 +438,9 @@ module API ...@@ -438,6 +438,9 @@ module API
class Label < LabelBasic class Label < LabelBasic
expose :open_issues_count, :closed_issues_count, :open_merge_requests_count expose :open_issues_count, :closed_issues_count, :open_merge_requests_count
expose :priority do |label, options|
label.priority(options[:project])
end
expose :subscribed do |label, options| expose :subscribed do |label, options|
label.subscribed?(options[:current_user]) label.subscribed?(options[:current_user])
......
...@@ -11,7 +11,7 @@ module API ...@@ -11,7 +11,7 @@ module API
success Entities::Label success Entities::Label
end end
get ':id/labels' do get ':id/labels' do
present available_labels, with: Entities::Label, current_user: current_user present available_labels, with: Entities::Label, current_user: current_user, project: user_project
end end
desc 'Create a new label' do desc 'Create a new label' do
...@@ -21,6 +21,7 @@ module API ...@@ -21,6 +21,7 @@ module API
requires :name, type: String, desc: 'The name of the label to be created' requires :name, type: String, desc: 'The name of the label to be created'
requires :color, type: String, desc: "The color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)" requires :color, type: String, desc: "The color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)"
optional :description, type: String, desc: 'The description of label to be created' optional :description, type: String, desc: 'The description of label to be created'
optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true
end end
post ':id/labels' do post ':id/labels' do
authorize! :admin_label, user_project authorize! :admin_label, user_project
...@@ -28,10 +29,15 @@ module API ...@@ -28,10 +29,15 @@ module API
label = available_labels.find_by(title: params[:name]) label = available_labels.find_by(title: params[:name])
conflict!('Label already exists') if label conflict!('Label already exists') if label
label = user_project.labels.create(declared(params, include_parent_namespaces: false).to_h) priority = params.delete(:priority)
label_params = declared(params,
include_parent_namespaces: false,
include_missing: false).to_h
label = user_project.labels.create(label_params)
if label.valid? if label.valid?
present label, with: Entities::Label, current_user: current_user label.prioritize!(user_project, priority) if priority
present label, with: Entities::Label, current_user: current_user, project: user_project
else else
render_validation_error!(label) render_validation_error!(label)
end end
...@@ -49,7 +55,7 @@ module API ...@@ -49,7 +55,7 @@ module API
label = user_project.labels.find_by(title: params[:name]) label = user_project.labels.find_by(title: params[:name])
not_found!('Label') unless label not_found!('Label') unless label
present label.destroy, with: Entities::Label, current_user: current_user present label.destroy, with: Entities::Label, current_user: current_user, project: user_project
end end
desc 'Update an existing label. At least one optional parameter is required.' do desc 'Update an existing label. At least one optional parameter is required.' do
...@@ -60,7 +66,8 @@ module API ...@@ -60,7 +66,8 @@ module API
optional :new_name, type: String, desc: 'The new name of the label' optional :new_name, type: String, desc: 'The new name of the label'
optional :color, type: String, desc: "The new color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)" optional :color, type: String, desc: "The new color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB)"
optional :description, type: String, desc: 'The new description of label' optional :description, type: String, desc: 'The new description of label'
at_least_one_of :new_name, :color, :description optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true
at_least_one_of :new_name, :color, :description, :priority
end end
put ':id/labels' do put ':id/labels' do
authorize! :admin_label, user_project authorize! :admin_label, user_project
...@@ -68,17 +75,25 @@ module API ...@@ -68,17 +75,25 @@ module API
label = user_project.labels.find_by(title: params[:name]) label = user_project.labels.find_by(title: params[:name])
not_found!('Label not found') unless label not_found!('Label not found') unless label
update_params = declared(params, update_priority = params.key?(:priority)
include_parent_namespaces: false, priority = params.delete(:priority)
include_missing: false).to_h label_params = declared(params,
include_parent_namespaces: false,
include_missing: false).to_h
# Rename new name to the actual label attribute name # Rename new name to the actual label attribute name
update_params['name'] = update_params.delete('new_name') if update_params.key?('new_name') label_params[:name] = label_params.delete('new_name') if label_params.key?('new_name')
if label.update(update_params) render_validation_error!(label) unless label.update(label_params)
present label, with: Entities::Label, current_user: current_user
else if update_priority
render_validation_error!(label) if priority.nil?
label.unprioritize!(user_project)
else
label.prioritize!(user_project, priority)
end
end end
present label, with: Entities::Label, current_user: current_user, project: user_project
end end
end end
end end
......
...@@ -6,6 +6,7 @@ describe API::API, api: true do ...@@ -6,6 +6,7 @@ describe API::API, api: true do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) } let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
let!(:label1) { create(:label, title: 'label1', project: project) } let!(:label1) { create(:label, title: 'label1', project: project) }
let!(:priority_label) { create(:label, title: 'bug', project: project, priority: 3) }
before do before do
project.team << [user, :master] project.team << [user, :master]
...@@ -21,8 +22,16 @@ describe API::API, api: true do ...@@ -21,8 +22,16 @@ describe API::API, api: true do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.size).to eq(2) expect(json_response.size).to eq(3)
expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, label1.name]) expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, priority_label.name, label1.name])
expect(json_response.last['name']).to eq(label1.name)
expect(json_response.last['color']).to be_present
expect(json_response.last['description']).to be_nil
expect(json_response.last['open_issues_count']).to eq(0)
expect(json_response.last['closed_issues_count']).to eq(0)
expect(json_response.last['open_merge_requests_count']).to eq(0)
expect(json_response.last['priority']).to be_nil
expect(json_response.last['subscribed']).to be_falsey
end end
end end
...@@ -31,21 +40,39 @@ describe API::API, api: true do ...@@ -31,21 +40,39 @@ describe API::API, api: true do
post api("/projects/#{project.id}/labels", user), post api("/projects/#{project.id}/labels", user),
name: 'Foo', name: 'Foo',
color: '#FFAABB', color: '#FFAABB',
description: 'test' description: 'test',
priority: 2
expect(response).to have_http_status(201) expect(response).to have_http_status(201)
expect(json_response['name']).to eq('Foo') expect(json_response['name']).to eq('Foo')
expect(json_response['color']).to eq('#FFAABB') expect(json_response['color']).to eq('#FFAABB')
expect(json_response['description']).to eq('test') expect(json_response['description']).to eq('test')
expect(json_response['priority']).to eq(2)
end end
it 'returns created label when only required params' do it 'returns created label when only required params' do
post api("/projects/#{project.id}/labels", user), post api("/projects/#{project.id}/labels", user),
name: 'Foo & Bar', name: 'Foo & Bar',
color: '#FFAABB' color: '#FFAABB'
expect(response.status).to eq(201) expect(response.status).to eq(201)
expect(json_response['name']).to eq('Foo & Bar') expect(json_response['name']).to eq('Foo & Bar')
expect(json_response['color']).to eq('#FFAABB') expect(json_response['color']).to eq('#FFAABB')
expect(json_response['description']).to be_nil expect(json_response['description']).to be_nil
expect(json_response['priority']).to be_nil
end
it 'creates a prioritized label' do
post api("/projects/#{project.id}/labels", user),
name: 'Foo & Bar',
color: '#FFAABB',
priority: 3
expect(response.status).to eq(201)
expect(json_response['name']).to eq('Foo & Bar')
expect(json_response['color']).to eq('#FFAABB')
expect(json_response['description']).to be_nil
expect(json_response['priority']).to eq(3)
end end
it 'returns a 400 bad request if name not given' do it 'returns a 400 bad request if name not given' do
...@@ -95,6 +122,15 @@ describe API::API, api: true do ...@@ -95,6 +122,15 @@ describe API::API, api: true do
expect(json_response['message']).to eq('Label already exists') expect(json_response['message']).to eq('Label already exists')
end end
it 'returns 400 for invalid priority' do
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
color: '#FFAAFFFF',
priority: 'foo'
expect(response).to have_http_status(400)
end
it 'returns 409 if label already exists in project' do it 'returns 409 if label already exists in project' do
post api("/projects/#{project.id}/labels", user), post api("/projects/#{project.id}/labels", user),
name: 'label1', name: 'label1',
...@@ -155,11 +191,43 @@ describe API::API, api: true do ...@@ -155,11 +191,43 @@ describe API::API, api: true do
it 'returns 200 if description is changed' do it 'returns 200 if description is changed' do
put api("/projects/#{project.id}/labels", user), put api("/projects/#{project.id}/labels", user),
name: 'label1', name: 'bug',
description: 'test' description: 'test'
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response['name']).to eq(label1.name) expect(json_response['name']).to eq(priority_label.name)
expect(json_response['description']).to eq('test') expect(json_response['description']).to eq('test')
expect(json_response['priority']).to eq(3)
end
it 'returns 200 if priority is changed' do
put api("/projects/#{project.id}/labels", user),
name: 'bug',
priority: 10
expect(response.status).to eq(200)
expect(json_response['name']).to eq(priority_label.name)
expect(json_response['priority']).to eq(10)
end
it 'returns 200 if a priority is added' do
put api("/projects/#{project.id}/labels", user),
name: 'label1',
priority: 3
expect(response.status).to eq(200)
expect(json_response['name']).to eq(label1.name)
expect(json_response['priority']).to eq(3)
end
it 'returns 200 if the priority is removed' do
put api("/projects/#{project.id}/labels", user),
name: priority_label.name,
priority: nil
expect(response.status).to eq(200)
expect(json_response['name']).to eq(priority_label.name)
expect(json_response['priority']).to be_nil
end end
it 'returns 404 if label does not exist' do it 'returns 404 if label does not exist' do
...@@ -178,7 +246,7 @@ describe API::API, api: true do ...@@ -178,7 +246,7 @@ describe API::API, api: true do
it 'returns 400 if no new parameters given' do it 'returns 400 if no new parameters given' do
put api("/projects/#{project.id}/labels", user), name: 'label1' put api("/projects/#{project.id}/labels", user), name: 'label1'
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['error']).to eq('new_name, color, description are missing, '\ expect(json_response['error']).to eq('new_name, color, description, priority are missing, '\
'at least one parameter must be provided') 'at least one parameter must be provided')
end end
...@@ -206,6 +274,14 @@ describe API::API, api: true do ...@@ -206,6 +274,14 @@ describe API::API, api: true do
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']['color']).to eq(['must be a valid color code']) expect(json_response['message']['color']).to eq(['must be a valid color code'])
end end
it 'returns 400 for invalid priority' do
post api("/projects/#{project.id}/labels", user),
name: 'Foo',
priority: 'foo'
expect(response).to have_http_status(400)
end
end end
describe "POST /projects/:id/labels/:label_id/subscription" do describe "POST /projects/:id/labels/:label_id/subscription" do
......
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