summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schilling <rschilling@student.tugraz.at>2018-12-28 10:47:00 +0100
committerRobert Schilling <rschilling@student.tugraz.at>2019-01-31 13:49:51 +0100
commit27a19c515a6876f855a12400f5f8168a53664832 (patch)
tree6fca35d974d8d4ef011e57ff0358b9fda579357a
parent70ed87bacd2dfffcb359235104f02d0905d612f0 (diff)
downloadgitlab-ce-27a19c515a6876f855a12400f5f8168a53664832.tar.gz
Factor out group labels entity
-rw-r--r--app/models/group_label.rb4
-rw-r--r--lib/api/entities.rb12
-rw-r--r--lib/api/group_labels.rb20
-rw-r--r--lib/api/labels.rb19
-rw-r--r--lib/api/subscriptions.rb17
5 files changed, 31 insertions, 41 deletions
diff --git a/app/models/group_label.rb b/app/models/group_label.rb
index d1499129130..ff14529c6e6 100644
--- a/app/models/group_label.rb
+++ b/app/models/group_label.rb
@@ -10,8 +10,4 @@ class GroupLabel < Label
def subject_foreign_key
'group_id'
end
-
- def priority(parent)
- nil
- end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 4edec631e8d..25052a29c31 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -1006,7 +1006,7 @@ module API
expose :id, :name, :color, :description
end
- class Label < LabelBasic
+ class GroupLabel < LabelBasic
expose :open_issues_count do |label, options|
label.open_issues_count(options[:current_user])
end
@@ -1019,15 +1019,17 @@ module API
label.open_merge_requests_count(options[:current_user])
end
- expose :priority do |label, options|
- label.priority(options[:project])
- end
-
expose :subscribed do |label, options|
label.subscribed?(options[:current_user], options[:project])
end
end
+ class ProjectLabel < GroupLabel
+ expose :priority do |label, options|
+ label.priority(options[:project])
+ end
+ end
+
class List < Grape::Entity
expose :id
expose :label, using: Entities::LabelBasic
diff --git a/lib/api/group_labels.rb b/lib/api/group_labels.rb
index e62be520003..e3c4033c508 100644
--- a/lib/api/group_labels.rb
+++ b/lib/api/group_labels.rb
@@ -12,7 +12,7 @@ module API
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'
- success Entities::Label
+ success Entities::GroupLabel
end
params do
use :pagination
@@ -20,12 +20,12 @@ module API
get ':id/labels' do
group_labels = available_labels_for(user_group)
- present paginate(group_labels), with: Entities::Label, current_user: current_user, parent: user_group
+ present paginate(group_labels), with: Entities::GroupLabel, current_user: current_user, parent: user_group
end
desc 'Create a new label' do
detail 'This feature was added in GitLab 11.7'
- success Entities::Label
+ success Entities::GroupLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be created'
@@ -41,7 +41,7 @@ module API
label = ::Labels::CreateService.new(declared_params(include_missing: false)).execute(group: user_group)
if label.persisted?
- present label, with: Entities::Label, current_user: current_user, parent: user_group
+ present label, with: Entities::GroupLabel, current_user: current_user, parent: user_group
else
render_validation_error!(label)
end
@@ -49,7 +49,7 @@ module API
desc 'Delete an existing label' do
detail 'This feature was added in GitLab 11.7'
- success Entities::Label
+ success Entities::GroupLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be deleted'
@@ -64,7 +64,7 @@ module API
desc 'Update an existing label. At least one optional parameter is required.' do
detail 'This feature was added in GitLab 11.7'
- success Entities::Label
+ success Entities::GroupLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be updated'
@@ -78,14 +78,10 @@ module API
label = find_label(user_group, params[:name])
- label_params = declared_params(include_missing: false)
- # Rename new name to the actual label attribute name
- label_params[:name] = label_params.delete(:new_name) if label_params.key?(:new_name)
-
- label = ::Labels::UpdateService.new(label_params).execute(label)
+ label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label)
render_validation_error!(label) unless label.valid?
- present label, with: Entities::Label, current_user: current_user, parent: user_group
+ present label, with: Entities::GroupLabel, current_user: current_user, parent: user_group
end
end
end
diff --git a/lib/api/labels.rb b/lib/api/labels.rb
index d5eb2b94669..53629fc313e 100644
--- a/lib/api/labels.rb
+++ b/lib/api/labels.rb
@@ -11,17 +11,17 @@ module API
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all labels of the project' do
- success Entities::Label
+ success Entities::ProjectLabel
end
params do
use :pagination
end
get ':id/labels' do
- present paginate(available_labels_for(user_project)), with: Entities::Label, current_user: current_user, project: user_project
+ present paginate(available_labels_for(user_project)), with: Entities::ProjectLabel, current_user: current_user, project: user_project
end
desc 'Create a new label' do
- success Entities::Label
+ success Entities::ProjectLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be created'
@@ -41,7 +41,7 @@ module API
if label.valid?
label.prioritize!(user_project, priority) if priority
- present label, with: Entities::Label, current_user: current_user, project: user_project
+ present label, with: Entities::ProjectLabel, current_user: current_user, project: user_project
else
render_validation_error!(label)
end
@@ -49,7 +49,7 @@ module API
# rubocop: enable CodeReuse/ActiveRecord
desc 'Delete an existing label' do
- success Entities::Label
+ success Entities::ProjectLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be deleted'
@@ -66,7 +66,7 @@ module API
# rubocop: enable CodeReuse/ActiveRecord
desc 'Update an existing label. At least one optional parameter is required.' do
- success Entities::Label
+ success Entities::ProjectLabel
end
params do
requires :name, type: String, desc: 'The name of the label to be updated'
@@ -85,11 +85,8 @@ module API
update_priority = params.key?(:priority)
priority = params.delete(:priority)
- label_params = declared_params(include_missing: false)
- # Rename new name to the actual label attribute name
- label_params[:name] = label_params.delete(:new_name) if label_params.key?(:new_name)
- label = ::Labels::UpdateService.new(label_params).execute(label)
+ label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label)
render_validation_error!(label) unless label.valid?
if update_priority
@@ -100,7 +97,7 @@ module API
end
end
- present label, with: Entities::Label, current_user: current_user, project: user_project
+ present label, with: Entities::ProjectLabel, current_user: current_user, project: user_project
end
# rubocop: enable CodeReuse/ActiveRecord
end
diff --git a/lib/api/subscriptions.rb b/lib/api/subscriptions.rb
index ed9e1a015f1..b15875027ee 100644
--- a/lib/api/subscriptions.rb
+++ b/lib/api/subscriptions.rb
@@ -5,15 +5,14 @@ module API
before { authenticate! }
subscribables = [
- { type: 'merge_requests', source: Project, finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) }, parent_resource: -> { user_project } },
- { type: 'issues', source: Project, finder: ->(id) { find_project_issue(id) }, parent_resource: -> { user_project } },
- { type: 'labels', source: Project, finder: ->(id) { find_label(user_project, id) }, parent_resource: -> { user_project } },
- { type: 'labels', source: Group, finder: ->(id) { find_label(user_group, id) }, parent_resource: -> { nil } }
+ { type: 'merge_requests', entity: Entities::MergeRequest, source: Project, finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) }, parent_resource: -> { user_project } },
+ { type: 'issues', entity: Entities::Issue, source: Project, finder: ->(id) { find_project_issue(id) }, parent_resource: -> { user_project } },
+ { type: 'labels', entity: Entities::ProjectLabel, source: Project, finder: ->(id) { find_label(user_project, id) }, parent_resource: -> { user_project } },
+ { type: 'labels', entity: Entities::GroupLabel, source: Group, finder: ->(id) { find_label(user_group, id) }, parent_resource: -> { nil } }
]
subscribables.each do |subscribable|
source_type = subscribable[:source].name.underscore
- entity_class = Entities.const_get(subscribable[:type].singularize.camelcase)
params do
requires :id, type: String, desc: "The #{source_type} ID"
@@ -21,7 +20,7 @@ module API
end
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Subscribe to a resource' do
- success entity_class
+ success subscribable[:entity]
end
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do
parent = instance_exec(&subscribable[:parent_resource])
@@ -31,12 +30,12 @@ module API
not_modified!
else
resource.subscribe(current_user, parent)
- present resource, with: entity_class, current_user: current_user, project: parent
+ present resource, with: subscribable[:entity], current_user: current_user, project: parent
end
end
desc 'Unsubscribe from a resource' do
- success entity_class
+ success subscribable[:entity]
end
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do
parent = instance_exec(&subscribable[:parent_resource])
@@ -46,7 +45,7 @@ module API
not_modified!
else
resource.unsubscribe(current_user, parent)
- present resource, with: entity_class, current_user: current_user, project: parent
+ present resource, with: subscribable[:entity], current_user: current_user, project: parent
end
end
end