Commit 27a19c51 authored by Robert Schilling's avatar Robert Schilling

Factor out group labels entity

parent 70ed87ba
......@@ -10,8 +10,4 @@ class GroupLabel < Label
def subject_foreign_key
'group_id'
end
def priority(parent)
nil
end
end
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
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