diff options
author | Robert Schilling <rschilling@student.tugraz.at> | 2019-01-08 20:59:08 +0100 |
---|---|---|
committer | Robert Schilling <rschilling@student.tugraz.at> | 2019-01-31 13:49:52 +0100 |
commit | ad25e14844eeb0db92f69e637eb20ee8a1328659 (patch) | |
tree | d57889550c0623d251c068c14e21c6f055f101a6 /lib | |
parent | 0ce33f6b4f30ddab14025adb16138f1589d32b6f (diff) | |
download | gitlab-ce-ad25e14844eeb0db92f69e637eb20ee8a1328659.tar.gz |
Simplify label helper and correct version
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/group_labels.rb | 10 | ||||
-rw-r--r-- | lib/api/helpers/label_helpers.rb | 120 | ||||
-rw-r--r-- | lib/api/labels.rb | 2 | ||||
-rw-r--r-- | lib/api/subscriptions.rb | 2 |
4 files changed, 61 insertions, 73 deletions
diff --git a/lib/api/group_labels.rb b/lib/api/group_labels.rb index 3b74dbebb3e..e578293b2ef 100644 --- a/lib/api/group_labels.rb +++ b/lib/api/group_labels.rb @@ -2,8 +2,8 @@ module API class GroupLabels < Grape::API - include ::API::Helpers::LabelHelpers include PaginationParams + helpers ::API::Helpers::LabelHelpers before { authenticate! } @@ -12,7 +12,7 @@ module API end resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS 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 end params do @@ -23,7 +23,7 @@ module API end 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 end params do @@ -34,7 +34,7 @@ module API end 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 end params do @@ -49,7 +49,7 @@ module API end 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 end params do diff --git a/lib/api/helpers/label_helpers.rb b/lib/api/helpers/label_helpers.rb index f444358ebe0..c11e7d614ab 100644 --- a/lib/api/helpers/label_helpers.rb +++ b/lib/api/helpers/label_helpers.rb @@ -3,91 +3,79 @@ module API module Helpers module LabelHelpers - extend ActiveSupport::Concern - - 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 + extend Grape::API::Helpers - 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) + 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 - label || not_found!('Label') - end + 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) - def get_labels(parent, entity) - present paginate(available_labels_for(parent)), with: entity, current_user: current_user, parent: parent - end + label || not_found!('Label') + end - def create_label(parent, entity) - authorize! :admin_label, parent + def get_labels(parent, entity) + 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]) - conflict!('Label already exists') if label + def create_label(parent, entity) + authorize! :admin_label, parent - priority = params.delete(:priority) - label_params = declared_params(include_missing: false) + label = available_labels_for(parent).find_by_title(params[:name]) + conflict!('Label already exists') if label - label = - if parent.is_a?(Project) - ::Labels::CreateService.new(label_params).execute(project: parent) - else - ::Labels::CreateService.new(label_params).execute(group: parent) - end + priority = params.delete(:priority) + label_params = declared_params(include_missing: false) - if label.persisted? - if parent.is_a?(Project) - label.prioritize!(parent, priority) if priority - end + label = + if parent.is_a?(Project) + ::Labels::CreateService.new(label_params).execute(project: parent) + else + ::Labels::CreateService.new(label_params).execute(group: parent) + end - present label, with: entity, current_user: current_user, parent: parent - else - render_validation_error!(label) - end + if label.persisted? + if parent.is_a?(Project) + label.prioritize!(parent, priority) if priority end - def update_label(parent, entity) - authorize! :admin_label, parent + present label, with: entity, current_user: current_user, parent: parent + else + render_validation_error!(label) + end + end - label = find_label(parent, params[:name], include_ancestor_groups: false) - update_priority = params.key?(:priority) - priority = params.delete(:priority) + def update_label(parent, entity) + authorize! :admin_label, parent - label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label) - render_validation_error!(label) unless label.valid? + label = find_label(parent, params[:name], include_ancestor_groups: false) + update_priority = params.key?(:priority) + priority = params.delete(:priority) - if parent.is_a?(Project) && update_priority - if priority.nil? - label.unprioritize!(parent) - else - label.prioritize!(parent, priority) - end - end + label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label) + render_validation_error!(label) unless label.valid? - 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 - def delete_label(parent) - authorize! :admin_label, parent + present label, with: entity, current_user: current_user, parent: parent + end - label = find_label(parent, params[:name], include_ancestor_groups: false) + def delete_label(parent) + authorize! :admin_label, parent - destroy_conditionally!(label) - end - end + label = find_label(parent, params[:name], include_ancestor_groups: false) + + destroy_conditionally!(label) end end end diff --git a/lib/api/labels.rb b/lib/api/labels.rb index d464754411a..d729d3ee625 100644 --- a/lib/api/labels.rb +++ b/lib/api/labels.rb @@ -2,8 +2,8 @@ module API class Labels < Grape::API - include ::API::Helpers::LabelHelpers include PaginationParams + helpers ::API::Helpers::LabelHelpers before { authenticate! } diff --git a/lib/api/subscriptions.rb b/lib/api/subscriptions.rb index b219a22f924..dfb54446ddf 100644 --- a/lib/api/subscriptions.rb +++ b/lib/api/subscriptions.rb @@ -2,7 +2,7 @@ module API class Subscriptions < Grape::API - include ::API::Helpers::LabelHelpers + helpers ::API::Helpers::LabelHelpers before { authenticate! } |