Commit 76c23746 authored by Robert Schilling's avatar Robert Schilling

Ability to update labels priority via API

parent c392b0cc
---
title: API: Ability to retrieve version information
merge_request: 7286
author: Robert Schilling
...@@ -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