gitlab-org--gitlab-foss/lib/api/labels.rb

101 lines
3.2 KiB
Ruby
Raw Normal View History

2014-08-12 08:16:25 -04:00
module API
# Labels API
class Labels < Grape::API
before { authenticate! }
resource :projects do
# Get all labels of the project
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id/labels
get ':id/labels' do
present user_project.labels, with: Entities::Label, current_user: current_user
2014-08-12 08:16:25 -04:00
end
# Creates a new label
#
# Parameters:
2016-03-24 14:38:37 -04:00
# id (required) - The ID of a project
# name (required) - The name of the label to be created
# color (required) - Color of the label given in 6-digit hex
# notation with leading '#' sign (e.g. #FFAABB)
# description (optional) - The description of label to be created
2014-08-12 08:16:25 -04:00
# Example Request:
# POST /projects/:id/labels
post ':id/labels' do
authorize! :admin_label, user_project
2014-08-12 08:16:25 -04:00
required_attributes! [:name, :color]
2016-03-24 14:38:37 -04:00
attrs = attributes_for_keys [:name, :color, :description]
2014-08-12 08:16:25 -04:00
label = user_project.find_label(attrs[:name])
conflict!('Label already exists') if label
2014-08-12 08:16:25 -04:00
label = user_project.labels.create(attrs)
if label.valid?
present label, with: Entities::Label, current_user: current_user
2014-08-12 08:16:25 -04:00
else
render_validation_error!(label)
2014-08-12 08:16:25 -04:00
end
end
# Deletes an existing label
#
# Parameters:
# id (required) - The ID of a project
# name (required) - The name of the label to be deleted
#
# Example Request:
# DELETE /projects/:id/labels
delete ':id/labels' do
authorize! :admin_label, user_project
2014-08-12 08:16:25 -04:00
required_attributes! [:name]
label = user_project.find_label(params[:name])
not_found!('Label') unless label
2014-08-12 08:16:25 -04:00
label.destroy
end
2014-08-13 06:23:51 -04:00
# Updates an existing label. At least one optional parameter is required.
#
# Parameters:
2016-03-24 14:38:37 -04:00
# id (required) - The ID of a project
# name (required) - The name of the label to be deleted
# new_name (optional) - The new name of the label
# color (optional) - Color of the label given in 6-digit hex
# notation with leading '#' sign (e.g. #FFAABB)
# description (optional) - The description of label to be created
2014-08-13 06:23:51 -04:00
# Example Request:
# PUT /projects/:id/labels
put ':id/labels' do
authorize! :admin_label, user_project
2014-08-13 06:23:51 -04:00
required_attributes! [:name]
label = user_project.find_label(params[:name])
not_found!('Label not found') unless label
2014-08-13 06:23:51 -04:00
2016-03-24 14:38:37 -04:00
attrs = attributes_for_keys [:new_name, :color, :description]
2014-08-13 06:23:51 -04:00
if attrs.empty?
render_api_error!('Required parameters "new_name" or "color" ' \
'missing',
400)
2014-08-13 06:23:51 -04:00
end
# Rename new name to the actual label attribute name
attrs[:name] = attrs.delete(:new_name) if attrs.key?(:new_name)
if label.update(attrs)
present label, with: Entities::Label, current_user: current_user
2014-08-13 06:23:51 -04:00
else
render_validation_error!(label)
2014-08-13 06:23:51 -04:00
end
end
2014-08-12 08:16:25 -04:00
end
end
end