gitlab-org--gitlab-foss/lib/api/helpers/label_helpers.rb

83 lines
2.7 KiB
Ruby
Raw Normal View History

2019-01-06 14:31:37 -05:00
# frozen_string_literal: true
module API
module Helpers
module LabelHelpers
extend Grape::API::Helpers
2019-01-06 14:31:37 -05:00
params :label_create_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'
end
2019-01-06 14:31:37 -05:00
def find_label(parent, id, include_ancestor_groups: true)
labels = available_labels_for(parent, include_ancestor_groups: include_ancestor_groups)
label = labels.find_by_id(id) || labels.find_by_title(id)
2019-01-06 14:31:37 -05:00
label || not_found!('Label')
end
2019-01-06 14:31:37 -05:00
def get_labels(parent, entity)
present paginate(available_labels_for(parent)), with: entity, current_user: current_user, parent: parent
end
2019-01-06 14:31:37 -05:00
def create_label(parent, entity)
authorize! :admin_label, parent
2019-01-06 14:31:37 -05:00
label = available_labels_for(parent).find_by_title(params[:name])
conflict!('Label already exists') if label
2019-01-06 14:31:37 -05:00
priority = params.delete(:priority)
label_params = declared_params(include_missing: false)
2019-01-06 14:31:37 -05:00
label =
if parent.is_a?(Project)
::Labels::CreateService.new(label_params).execute(project: parent)
else
::Labels::CreateService.new(label_params).execute(group: parent)
end
2019-01-06 14:31:37 -05:00
if label.persisted?
if parent.is_a?(Project)
label.prioritize!(parent, priority) if priority
2019-01-06 14:31:37 -05:00
end
present label, with: entity, current_user: current_user, parent: parent
else
render_validation_error!(label)
end
end
2019-01-06 14:31:37 -05:00
def update_label(parent, entity)
authorize! :admin_label, parent
2019-01-06 14:31:37 -05:00
label = find_label(parent, params[:name], include_ancestor_groups: false)
update_priority = params.key?(:priority)
priority = params.delete(:priority)
2019-01-06 14:31:37 -05:00
label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label)
render_validation_error!(label) unless label.valid?
2019-01-06 14:31:37 -05:00
if parent.is_a?(Project) && update_priority
if priority.nil?
label.unprioritize!(parent)
else
label.prioritize!(parent, priority)
2019-01-06 14:31:37 -05:00
end
end
2019-01-06 14:31:37 -05:00
present label, with: entity, current_user: current_user, parent: parent
end
2019-01-06 14:31:37 -05:00
def delete_label(parent)
authorize! :admin_label, parent
2019-01-06 14:31:37 -05:00
label = find_label(parent, params[:name], include_ancestor_groups: false)
destroy_conditionally!(label)
2019-01-06 14:31:37 -05:00
end
end
end
end