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

Factor out group labels entity

parent 70ed87ba
...@@ -10,8 +10,4 @@ class GroupLabel < Label ...@@ -10,8 +10,4 @@ class GroupLabel < Label
def subject_foreign_key def subject_foreign_key
'group_id' 'group_id'
end end
def priority(parent)
nil
end
end end
...@@ -1006,7 +1006,7 @@ module API ...@@ -1006,7 +1006,7 @@ module API
expose :id, :name, :color, :description expose :id, :name, :color, :description
end end
class Label < LabelBasic class GroupLabel < LabelBasic
expose :open_issues_count do |label, options| expose :open_issues_count do |label, options|
label.open_issues_count(options[:current_user]) label.open_issues_count(options[:current_user])
end end
...@@ -1019,15 +1019,17 @@ module API ...@@ -1019,15 +1019,17 @@ module API
label.open_merge_requests_count(options[:current_user]) label.open_merge_requests_count(options[:current_user])
end end
expose :priority do |label, options|
label.priority(options[:project])
end
expose :subscribed do |label, options| expose :subscribed do |label, options|
label.subscribed?(options[:current_user], options[:project]) label.subscribed?(options[:current_user], options[:project])
end end
end end
class ProjectLabel < GroupLabel
expose :priority do |label, options|
label.priority(options[:project])
end
end
class List < Grape::Entity class List < Grape::Entity
expose :id expose :id
expose :label, using: Entities::LabelBasic expose :label, using: Entities::LabelBasic
......
...@@ -12,7 +12,7 @@ module API ...@@ -12,7 +12,7 @@ module API
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.7'
success Entities::Label success Entities::GroupLabel
end end
params do params do
use :pagination use :pagination
...@@ -20,12 +20,12 @@ module API ...@@ -20,12 +20,12 @@ module API
get ':id/labels' do get ':id/labels' do
group_labels = available_labels_for(user_group) 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 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.7'
success Entities::Label success Entities::GroupLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be created' requires :name, type: String, desc: 'The name of the label to be created'
...@@ -41,7 +41,7 @@ module API ...@@ -41,7 +41,7 @@ module API
label = ::Labels::CreateService.new(declared_params(include_missing: false)).execute(group: user_group) label = ::Labels::CreateService.new(declared_params(include_missing: false)).execute(group: user_group)
if label.persisted? 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 else
render_validation_error!(label) render_validation_error!(label)
end end
...@@ -49,7 +49,7 @@ module API ...@@ -49,7 +49,7 @@ module API
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.7'
success Entities::Label success Entities::GroupLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be deleted' requires :name, type: String, desc: 'The name of the label to be deleted'
...@@ -64,7 +64,7 @@ module API ...@@ -64,7 +64,7 @@ module API
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.7'
success Entities::Label success Entities::GroupLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be updated' requires :name, type: String, desc: 'The name of the label to be updated'
...@@ -78,14 +78,10 @@ module API ...@@ -78,14 +78,10 @@ module API
label = find_label(user_group, params[:name]) label = find_label(user_group, params[:name])
label_params = declared_params(include_missing: false) label = ::Labels::UpdateService.new(declared_params(include_missing: false)).execute(label)
# 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)
render_validation_error!(label) unless label.valid? 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 end
end end
......
...@@ -11,17 +11,17 @@ module API ...@@ -11,17 +11,17 @@ module API
end end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get all labels of the project' do desc 'Get all labels of the project' do
success Entities::Label success Entities::ProjectLabel
end end
params do params do
use :pagination use :pagination
end end
get ':id/labels' do 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 end
desc 'Create a new label' do desc 'Create a new label' do
success Entities::Label success Entities::ProjectLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be created' requires :name, type: String, desc: 'The name of the label to be created'
...@@ -41,7 +41,7 @@ module API ...@@ -41,7 +41,7 @@ module API
if label.valid? if label.valid?
label.prioritize!(user_project, priority) if priority 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 else
render_validation_error!(label) render_validation_error!(label)
end end
...@@ -49,7 +49,7 @@ module API ...@@ -49,7 +49,7 @@ module API
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
desc 'Delete an existing label' do desc 'Delete an existing label' do
success Entities::Label success Entities::ProjectLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be deleted' requires :name, type: String, desc: 'The name of the label to be deleted'
...@@ -66,7 +66,7 @@ module API ...@@ -66,7 +66,7 @@ module API
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
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
success Entities::Label success Entities::ProjectLabel
end end
params do params do
requires :name, type: String, desc: 'The name of the label to be updated' requires :name, type: String, desc: 'The name of the label to be updated'
...@@ -85,11 +85,8 @@ module API ...@@ -85,11 +85,8 @@ module API
update_priority = params.key?(:priority) update_priority = params.key?(:priority)
priority = params.delete(: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? render_validation_error!(label) unless label.valid?
if update_priority if update_priority
...@@ -100,7 +97,7 @@ module API ...@@ -100,7 +97,7 @@ module API
end end
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 end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
......
...@@ -5,15 +5,14 @@ module API ...@@ -5,15 +5,14 @@ module API
before { authenticate! } before { authenticate! }
subscribables = [ subscribables = [
{ type: 'merge_requests', source: Project, finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) }, parent_resource: -> { user_project } }, { 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', source: Project, finder: ->(id) { find_project_issue(id) }, parent_resource: -> { user_project } }, { type: 'issues', entity: Entities::Issue, 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', entity: Entities::ProjectLabel, 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: 'labels', entity: Entities::GroupLabel, source: Group, finder: ->(id) { find_label(user_group, id) }, parent_resource: -> { nil } }
] ]
subscribables.each do |subscribable| subscribables.each do |subscribable|
source_type = subscribable[:source].name.underscore source_type = subscribable[:source].name.underscore
entity_class = Entities.const_get(subscribable[:type].singularize.camelcase)
params do params do
requires :id, type: String, desc: "The #{source_type} ID" requires :id, type: String, desc: "The #{source_type} ID"
...@@ -21,7 +20,7 @@ module API ...@@ -21,7 +20,7 @@ module API
end end
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Subscribe to a resource' do desc 'Subscribe to a resource' do
success entity_class success subscribable[:entity]
end end
post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do
parent = instance_exec(&subscribable[:parent_resource]) parent = instance_exec(&subscribable[:parent_resource])
...@@ -31,12 +30,12 @@ module API ...@@ -31,12 +30,12 @@ module API
not_modified! not_modified!
else else
resource.subscribe(current_user, parent) 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
end end
desc 'Unsubscribe from a resource' do desc 'Unsubscribe from a resource' do
success entity_class success subscribable[:entity]
end end
post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do
parent = instance_exec(&subscribable[:parent_resource]) parent = instance_exec(&subscribable[:parent_resource])
...@@ -46,7 +45,7 @@ module API ...@@ -46,7 +45,7 @@ module API
not_modified! not_modified!
else else
resource.unsubscribe(current_user, parent) 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 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