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

101 lines
3.7 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module API
class Labels < Grape::API
2017-08-17 22:00:37 +05:30
include PaginationParams
2014-09-02 18:07:02 +05:30
before { authenticate! }
2017-08-17 22:00:37 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: { id: %r{[^/]+} } do
desc 'Get all labels of the project' do
success Entities::Label
end
params do
use :pagination
end
2014-09-02 18:07:02 +05:30
get ':id/labels' do
2017-08-17 22:00:37 +05:30
present paginate(available_labels), with: Entities::Label, current_user: current_user, project: user_project
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
desc 'Create a new label' do
success Entities::Label
end
params do
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) or one of the allowed CSS color names"
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
2014-09-02 18:07:02 +05:30
post ':id/labels' do
authorize! :admin_label, user_project
2017-08-17 22:00:37 +05:30
label = available_labels.find_by(title: params[:name])
2015-04-26 12:48:37 +05:30
conflict!('Label already exists') if label
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
priority = params.delete(:priority)
label = ::Labels::CreateService.new(declared_params(include_missing: false)).execute(project: user_project)
2014-09-02 18:07:02 +05:30
if label.valid?
2017-08-17 22:00:37 +05:30
label.prioritize!(user_project, priority) if priority
present label, with: Entities::Label, current_user: current_user, project: user_project
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
2017-08-17 22:00:37 +05:30
desc 'Delete an existing label' do
success Entities::Label
end
params do
requires :name, type: String, desc: 'The name of the label to be deleted'
end
2014-09-02 18:07:02 +05:30
delete ':id/labels' do
authorize! :admin_label, user_project
2016-11-24 13:41:30 +05:30
label = user_project.labels.find_by(title: 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
2017-08-17 22:00:37 +05:30
desc 'Update an existing label. At least one optional parameter is required.' do
success Entities::Label
end
params do
requires :name, type: String, desc: 'The name of the label to be updated'
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) or one of the allowed CSS color names"
optional :description, type: String, desc: 'The new description of label'
optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true
at_least_one_of :new_name, :color, :description, :priority
end
2014-09-02 18:07:02 +05:30
put ':id/labels' do
authorize! :admin_label, user_project
2016-11-24 13:41:30 +05:30
label = user_project.labels.find_by(title: 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
2017-08-17 22:00:37 +05:30
update_priority = params.key?(:priority)
priority = params.delete(:priority)
label_params = declared_params(include_missing: false)
2014-09-02 18:07:02 +05:30
# Rename new name to the actual label attribute name
2017-08-17 22:00:37 +05:30
label_params[:name] = label_params.delete(:new_name) if label_params.key?(:new_name)
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
label = ::Labels::UpdateService.new(label_params).execute(label)
render_validation_error!(label) unless label.valid?
if update_priority
if priority.nil?
label.unprioritize!(user_project)
else
label.prioritize!(user_project, priority)
end
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
present label, with: Entities::Label, current_user: current_user, project: user_project
2014-09-02 18:07:02 +05:30
end
end
end
end