Commit ad25e148 authored by Robert Schilling's avatar Robert Schilling

Simplify label helper and correct version

parent 0ce33f6b
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
module API module API
class GroupLabels < Grape::API class GroupLabels < Grape::API
include ::API::Helpers::LabelHelpers
include PaginationParams include PaginationParams
helpers ::API::Helpers::LabelHelpers
before { authenticate! } before { authenticate! }
...@@ -12,7 +12,7 @@ module API ...@@ -12,7 +12,7 @@ module API
end end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all labels of the group' do desc 'Get all labels of the group' do
detail 'This feature was added in GitLab 11.7' detail 'This feature was added in GitLab 11.8'
success Entities::GroupLabel success Entities::GroupLabel
end end
params do params do
...@@ -23,7 +23,7 @@ module API ...@@ -23,7 +23,7 @@ module API
end end
desc 'Create a new label' do desc 'Create a new label' do
detail 'This feature was added in GitLab 11.7' detail 'This feature was added in GitLab 11.8'
success Entities::GroupLabel success Entities::GroupLabel
end end
params do params do
...@@ -34,7 +34,7 @@ module API ...@@ -34,7 +34,7 @@ module API
end end
desc 'Update an existing label. At least one optional parameter is required.' do desc 'Update an existing label. At least one optional parameter is required.' do
detail 'This feature was added in GitLab 11.7' detail 'This feature was added in GitLab 11.8'
success Entities::GroupLabel success Entities::GroupLabel
end end
params do params do
...@@ -49,7 +49,7 @@ module API ...@@ -49,7 +49,7 @@ module API
end end
desc 'Delete an existing label' do desc 'Delete an existing label' do
detail 'This feature was added in GitLab 11.7' detail 'This feature was added in GitLab 11.8'
success Entities::GroupLabel success Entities::GroupLabel
end end
params do params do
......
...@@ -3,91 +3,79 @@ ...@@ -3,91 +3,79 @@
module API module API
module Helpers module Helpers
module LabelHelpers module LabelHelpers
extend ActiveSupport::Concern extend Grape::API::Helpers
included do
helpers do
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
params :label_update_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'
at_least_one_of :new_name, :color, :description
end
def find_label(parent, id, include_ancestor_groups: true) params :label_create_params do
labels = available_labels_for(parent, include_ancestor_groups: include_ancestor_groups) requires :name, type: String, desc: 'The name of the label to be created'
label = labels.find_by_id(id) || labels.find_by_title(id) 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
label || not_found!('Label') def find_label(parent, id, include_ancestor_groups: true)
end labels = available_labels_for(parent, include_ancestor_groups: include_ancestor_groups)
label = labels.find_by_id(id) || labels.find_by_title(id)
def get_labels(parent, entity) label || not_found!('Label')
present paginate(available_labels_for(parent)), with: entity, current_user: current_user, parent: parent end
end
def create_label(parent, entity) def get_labels(parent, entity)
authorize! :admin_label, parent present paginate(available_labels_for(parent)), with: entity, current_user: current_user, parent: parent
end
label = available_labels_for(parent).find_by_title(params[:name]) def create_label(parent, entity)
conflict!('Label already exists') if label authorize! :admin_label, parent
priority = params.delete(:priority) label = available_labels_for(parent).find_by_title(params[:name])
label_params = declared_params(include_missing: false) conflict!('Label already exists') if label
label = priority = params.delete(:priority)
if parent.is_a?(Project) label_params = declared_params(include_missing: false)
::Labels::CreateService.new(label_params).execute(project: parent)
else
::Labels::CreateService.new(label_params).execute(group: parent)
end
if label.persisted? label =
if parent.is_a?(Project) if parent.is_a?(Project)
label.prioritize!(parent, priority) if priority ::Labels::CreateService.new(label_params).execute(project: parent)
end else
::Labels::CreateService.new(label_params).execute(group: parent)
end
present label, with: entity, current_user: current_user, parent: parent if label.persisted?
else if parent.is_a?(Project)
render_validation_error!(label) label.prioritize!(parent, priority) if priority
end
end end
def update_label(parent, entity) present label, with: entity, current_user: current_user, parent: parent
authorize! :admin_label, parent else
render_validation_error!(label)
end
end
label = find_label(parent, params[:name], include_ancestor_groups: false) def update_label(parent, entity)
update_priority = params.key?(:priority) authorize! :admin_label, parent
priority = params.delete(:priority)
label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label) label = find_label(parent, params[:name], include_ancestor_groups: false)
render_validation_error!(label) unless label.valid? update_priority = params.key?(:priority)
priority = params.delete(:priority)
if parent.is_a?(Project) && update_priority label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label)
if priority.nil? render_validation_error!(label) unless label.valid?
label.unprioritize!(parent)
else
label.prioritize!(parent, priority)
end
end
present label, with: entity, current_user: current_user, parent: parent if parent.is_a?(Project) && update_priority
if priority.nil?
label.unprioritize!(parent)
else
label.prioritize!(parent, priority)
end end
end
def delete_label(parent) present label, with: entity, current_user: current_user, parent: parent
authorize! :admin_label, parent end
label = find_label(parent, params[:name], include_ancestor_groups: false) def delete_label(parent)
authorize! :admin_label, parent
destroy_conditionally!(label) label = find_label(parent, params[:name], include_ancestor_groups: false)
end
end destroy_conditionally!(label)
end end
end end
end end
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
module API module API
class Labels < Grape::API class Labels < Grape::API
include ::API::Helpers::LabelHelpers
include PaginationParams include PaginationParams
helpers ::API::Helpers::LabelHelpers
before { authenticate! } before { authenticate! }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module API module API
class Subscriptions < Grape::API class Subscriptions < Grape::API
include ::API::Helpers::LabelHelpers helpers ::API::Helpers::LabelHelpers
before { authenticate! } before { authenticate! }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment