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

101 lines
3.8 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
2018-03-17 18:26:18 +05:30
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
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
2018-03-17 18:26:18 +05:30
present paginate(available_labels_for(user_project)), 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
2018-03-17 18:26:18 +05:30
label = available_labels_for(user_project).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
2018-03-17 18:26:18 +05:30
destroy_conditionally!(label)
2014-09-02 18:07:02 +05:30
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