Commit 8a6e257d authored by Sean McGivern's avatar Sean McGivern

Merge branch '8525-related-issues-serializer' into 'master'

Use a Serializer for related issues/epics

Closes #8525

See merge request gitlab-org/gitlab-ee!9298
parents e4ea3c9d 06786aa5
# frozen_string_literal: true
class LinkedEpicEntity < Grape::Entity
include RequestAwareEntity
expose :id, :title, :state
expose :reference do |epic|
epic.to_reference(request.issuable.group)
end
expose :path do |epic|
group_epic_path(epic.group, epic)
end
expose :relation_path do |epic|
group_epic_link_path(epic.group, request.issuable.iid, epic.id)
end
end
# frozen_string_literal: true
class LinkedEpicIssueEntity < LinkedIssueEntity
include RequestAwareEntity
expose :relation_path, override: true do |issue|
if can_admin_issue_link?(issue)
group_epic_issue_path(issuable.group, issuable.iid, issue.epic_issue_id)
end
end
expose :reference, override: true do |issue|
issue.to_reference(full: true)
end
expose :epic_issue_id
private
def can_admin_issue_link?(issue)
Ability.allowed?(current_user, :admin_epic_issue, issue) && Ability.allowed?(current_user, :admin_epic, issuable)
end
end
# frozen_string_literal: true
class LinkedEpicIssueSerializer < BaseSerializer
entity LinkedEpicIssueEntity
end
# frozen_string_literal: true
class LinkedEpicSerializer < BaseSerializer
entity LinkedEpicEntity
end
# frozen_string_literal: true
class LinkedIssueEntity < Grape::Entity
include RequestAwareEntity
expose :id, :confidential, :title
expose :assignees, using: UserEntity
expose :state
expose :milestone, using: API::Entities::Milestone
expose :weight
expose :reference do |link|
link.to_reference(issuable.project)
end
expose :path do |link|
project_issue_path(link.project, link.iid)
end
expose :relation_path
expose :due_date, :created_at, :closed_at
private
def current_user
request.current_user
end
def issuable
request.issuable
end
end
# frozen_string_literal: true
class LinkedProjectIssueEntity < LinkedIssueEntity
include Gitlab::Utils::StrongMemoize
expose :relation_path, override: true do |issue|
# Make sure the user can admin both the current issue AND the
# referenced issue projects in order to return the removal link.
if can_admin_issue_link_on_current_project? && can_admin_issue_link?(issue.project)
project_issue_link_path(issuable.project, issuable.iid, issue.issue_link_id)
end
end
private
def can_admin_issue_link_on_current_project?
strong_memoize(:can_admin_on_current_project) do
can_admin_issue_link?(issuable.project)
end
end
def can_admin_issue_link?(project)
Ability.allowed?(current_user, :admin_issue_link, project)
end
end
# frozen_string_literal: true
class LinkedProjectIssueSerializer < BaseSerializer
entity LinkedProjectIssueEntity
end
......@@ -2,6 +2,8 @@
module EpicIssues
class ListService < IssuableLinks::ListService
extend ::Gitlab::Utils::Override
private
def child_issuables
......@@ -10,22 +12,9 @@ module EpicIssues
issuable.issues_readable_by(current_user, preload: preload_for_collection)
end
def relation_path(issue)
if can_admin_issue_link?(issue)
group_epic_issue_path(issuable.group, issuable.iid, issue.epic_issue_id)
end
end
def can_admin_issue_link?(issue)
Ability.allowed?(current_user, :admin_epic_issue, issue) && Ability.allowed?(current_user, :admin_epic, issuable)
end
def reference(issue)
issue.to_reference(full: true)
end
def to_hash(issue)
super.merge(epic_issue_id: issue.epic_issue_id)
override :serializer
def serializer
LinkedEpicIssueSerializer
end
end
end
......@@ -2,6 +2,8 @@
module EpicLinks
class ListService < IssuableLinks::ListService
extend ::Gitlab::Utils::Override
private
def child_issuables
......@@ -10,27 +12,9 @@ module EpicLinks
EpicsFinder.new(current_user, parent_id: issuable.id, group_id: issuable.group.id).execute
end
def reference(epic)
epic.to_reference(issuable.group)
end
def issuable_path(epic)
group_epic_path(epic.group, epic)
end
def relation_path(epic)
group_epic_link_path(epic.group, issuable.iid, epic.id)
end
def to_hash(object)
{
id: object.id,
title: object.title,
state: object.state,
reference: reference(object),
path: issuable_path(object),
relation_path: relation_path(object)
}
override :serializer
def serializer
LinkedEpicSerializer
end
end
end
......@@ -11,47 +11,17 @@ module IssuableLinks
end
def execute
child_issuables.map do |referenced_object|
to_hash(referenced_object)
end
serializer.new(current_user: current_user, issuable: issuable).represent(child_issuables)
end
private
def preload_for_collection
[{ project: :namespace }, :assignees]
end
def relation_path(object)
def serializer
raise NotImplementedError
end
def reference(object)
object.to_reference(issuable.project)
end
def issuable_path(object)
project_issue_path(object.project, object.iid)
end
# rubocop: disable CodeReuse/Serializer
def to_hash(object)
{
id: object.id,
confidential: object.confidential,
title: object.title,
assignees: UserSerializer.new.represent(object.assignees),
state: object.state,
milestone: MilestoneSerializer.new.represent(object.milestone),
weight: object.weight,
reference: reference(object),
path: issuable_path(object),
relation_path: relation_path(object),
due_date: object.due_date,
created_at: object.created_at,
closed_at: object.closed_at
}
def preload_for_collection
[{ project: :namespace }, :assignees]
end
# rubocop: enable CodeReuse/Serializer
end
end
......@@ -2,7 +2,7 @@
module IssueLinks
class ListService < IssuableLinks::ListService
include Gitlab::Utils::StrongMemoize
extend ::Gitlab::Utils::Override
private
......@@ -10,26 +10,9 @@ module IssueLinks
issuable.related_issues(current_user, preload: preload_for_collection)
end
def relation_path(issue)
# Make sure the user can admin both the current issue AND the
# referenced issue projects in order to return the removal link.
if can_admin_issue_link_on_current_project? && can_admin_issue_link?(issue.project)
project_issue_link_path(current_project, issuable.iid, issue.issue_link_id)
end
end
def can_admin_issue_link_on_current_project?
strong_memoize(:can_admin_on_current_project) do
can_admin_issue_link?(current_project)
end
end
def can_admin_issue_link?(project)
Ability.allowed?(current_user, :admin_issue_link, project)
end
def current_project
issuable.project
override :serializer
def serializer
LinkedProjectIssueSerializer
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