debian-mirror-gitlab/lib/api/labels.rb

101 lines
3.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
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
2016-11-03 12:29:30 +05:30
present available_labels, with: Entities::Label, current_user: current_user
2014-09-02 18:07:02 +05:30
end
# Creates a new label
#
# Parameters:
2016-06-02 11:05:42 +05:30
# 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-09-02 18:07:02 +05:30
# Example Request:
# POST /projects/:id/labels
post ':id/labels' do
authorize! :admin_label, user_project
required_attributes! [:name, :color]
2016-06-02 11:05:42 +05:30
attrs = attributes_for_keys [:name, :color, :description]
2014-09-02 18:07:02 +05:30
label = user_project.find_label(attrs[:name])
2015-04-26 12:48:37 +05:30
conflict!('Label already exists') if label
2014-09-02 18:07:02 +05:30
label = user_project.labels.create(attrs)
if label.valid?
2016-06-02 11:05:42 +05:30
present label, with: Entities::Label, current_user: current_user
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
render_validation_error!(label)
2014-09-02 18:07:02 +05:30
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
required_attributes! [:name]
label = user_project.find_label(params[:name])
2015-04-26 12:48:37 +05:30
not_found!('Label') unless label
2014-09-02 18:07:02 +05:30
label.destroy
end
# Updates an existing label. At least one optional parameter is required.
#
# Parameters:
2016-06-02 11:05:42 +05:30
# 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-09-02 18:07:02 +05:30
# Example Request:
# PUT /projects/:id/labels
put ':id/labels' do
authorize! :admin_label, user_project
required_attributes! [:name]
label = user_project.find_label(params[:name])
2015-04-26 12:48:37 +05:30
not_found!('Label not found') unless label
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
attrs = attributes_for_keys [:new_name, :color, :description]
2014-09-02 18:07:02 +05:30
if attrs.empty?
2015-04-26 12:48:37 +05:30
render_api_error!('Required parameters "new_name" or "color" ' \
'missing',
400)
2014-09-02 18:07:02 +05:30
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)
2016-06-02 11:05:42 +05:30
present label, with: Entities::Label, current_user: current_user
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
render_validation_error!(label)
2014-09-02 18:07:02 +05:30
end
end
end
end
end