2019-01-06 14:31:37 -05:00
# frozen_string_literal: true
module API
module Helpers
module LabelHelpers
2019-01-08 14:59:08 -05:00
extend Grape :: API :: Helpers
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -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 "
2021-05-19 08:10:33 -04:00
optional :description , type : String , desc : 'The description of label to be created'
2019-01-08 14:59:08 -05:00
end
2019-01-06 14:31:37 -05:00
2019-09-27 11:06:16 -04:00
params :label_update_params do
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 "
2021-05-19 08:10:33 -04:00
optional :description , type : String , desc : 'The new description of label'
2019-09-27 11:06:16 -04:00
end
params :project_label_update_params do
use :label_update_params
optional :priority , type : Integer , desc : 'The priority of the label' , allow_blank : true
2021-05-19 08:10:33 -04:00
at_least_one_of :new_name , :color , :description , :priority
2019-09-27 11:06:16 -04:00
end
params :group_label_update_params do
use :label_update_params
2021-05-19 08:10:33 -04:00
at_least_one_of :new_name , :color , :description
2019-09-27 11:06:16 -04:00
end
2020-10-23 14:08:31 -04:00
def find_label ( parent , id_or_title , params = { include_ancestor_groups : true } )
labels = available_labels_for ( parent , params )
2019-08-23 13:45:42 -04:00
label = labels . find_by_id ( id_or_title ) || labels . find_by_title ( id_or_title )
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -05:00
label || not_found! ( 'Label' )
end
2019-01-06 14:31:37 -05:00
2020-10-23 14:08:31 -04:00
def get_labels ( parent , entity , params = { } )
present paginate ( available_labels_for ( parent , params ) ) ,
2019-08-06 12:27:46 -04:00
with : entity ,
current_user : current_user ,
parent : parent ,
with_counts : params [ :with_counts ]
2019-01-08 14:59:08 -05:00
end
2019-01-06 14:31:37 -05:00
2020-10-23 14:08:31 -04:00
def get_label ( parent , entity , params = { } )
label = find_label ( parent , params_id_or_title , params )
2019-09-27 11:06:16 -04:00
present label , with : entity , current_user : current_user , parent : parent
end
2019-01-08 14:59:08 -05:00
def create_label ( parent , entity )
authorize! :admin_label , parent
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -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
2019-01-08 14:59:08 -05:00
priority = params . delete ( :priority )
label_params = declared_params ( include_missing : false )
2019-01-06 14:31:37 -05:00
2019-08-23 13:45:42 -04:00
label = :: Labels :: CreateService . new ( label_params ) . execute ( create_service_params ( parent ) )
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -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
2019-01-08 14:59:08 -05:00
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
2019-01-08 14:59:08 -05:00
def update_label ( parent , entity )
authorize! :admin_label , parent
2019-01-06 14:31:37 -05:00
2019-08-23 13:45:42 -04:00
label = find_label ( parent , params_id_or_title , include_ancestor_groups : false )
2019-01-08 14:59:08 -05:00
update_priority = params . key? ( :priority )
priority = params . delete ( :priority )
2019-01-06 14:31:37 -05:00
2019-08-23 13:45:42 -04:00
# params is used to update the label so we need to remove this field here
params . delete ( :label_id )
2019-09-27 11:06:16 -04:00
params . delete ( :name )
2019-08-23 13:45:42 -04:00
2019-01-08 14:59:08 -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
2019-01-08 14:59:08 -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
2019-01-08 14:59:08 -05:00
end
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -05:00
present label , with : entity , current_user : current_user , parent : parent
end
2019-01-06 14:31:37 -05:00
2019-01-08 14:59:08 -05:00
def delete_label ( parent )
authorize! :admin_label , parent
2019-01-06 14:31:37 -05:00
2019-08-23 13:45:42 -04:00
label = find_label ( parent , params_id_or_title , include_ancestor_groups : false )
2019-01-08 14:59:08 -05:00
destroy_conditionally! ( label )
2019-01-06 14:31:37 -05:00
end
2019-08-23 13:45:42 -04:00
2019-09-27 11:06:16 -04:00
def promote_label ( parent )
authorize! :admin_label , parent
label = find_label ( parent , params [ :name ] , include_ancestor_groups : false )
begin
group_label = :: Labels :: PromoteService . new ( parent , current_user ) . execute ( label )
if group_label
present group_label , with : Entities :: GroupLabel , current_user : current_user , parent : parent . group
else
render_api_error! ( 'Failed to promote project label to group label' , 400 )
end
2021-04-26 08:09:44 -04:00
rescue StandardError = > error
2019-09-27 11:06:16 -04:00
render_api_error! ( error . to_s , 400 )
end
end
2019-08-23 13:45:42 -04:00
def params_id_or_title
@params_id_or_title || = params [ :label_id ] || params [ :name ]
end
def create_service_params ( parent )
if parent . is_a? ( Project )
{ project : parent }
elsif parent . is_a? ( Group )
{ group : parent }
else
raise TypeError , 'Parent type is not supported'
end
end
2019-01-06 14:31:37 -05:00
end
end
end