2020-02-10 19:09:06 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Entities
|
|
|
|
class Todo < Grape::Entity
|
|
|
|
expose :id
|
|
|
|
expose :project, using: Entities::ProjectIdentity, if: -> (todo, _) { todo.project_id }
|
|
|
|
expose :group, using: 'API::Entities::NamespaceBasic', if: -> (todo, _) { todo.group_id }
|
|
|
|
expose :author, using: Entities::UserBasic
|
|
|
|
expose :action_name
|
|
|
|
expose :target_type
|
|
|
|
|
|
|
|
expose :target do |todo, options|
|
|
|
|
todo_options = options.fetch(todo.target_type, {})
|
|
|
|
todo_target_class(todo.target_type).represent(todo.target, todo_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :target_url do |todo, options|
|
|
|
|
todo_target_url(todo)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :body
|
|
|
|
expose :state
|
|
|
|
expose :created_at
|
2020-04-23 05:09:46 -04:00
|
|
|
expose :updated_at
|
2020-02-10 19:09:06 -05:00
|
|
|
|
|
|
|
def todo_target_class(target_type)
|
|
|
|
# false as second argument prevents looking up in module hierarchy
|
|
|
|
# see also https://gitlab.com/gitlab-org/gitlab-foss/issues/59719
|
|
|
|
::API::Entities.const_get(target_type, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def todo_target_url(todo)
|
2020-05-11 20:10:11 -04:00
|
|
|
return design_todo_target_url(todo) if todo.for_design?
|
|
|
|
|
2020-02-10 19:09:06 -05:00
|
|
|
target_type = todo.target_type.underscore
|
|
|
|
target_url = "#{todo.resource_parent.class.to_s.underscore}_#{target_type}_url"
|
|
|
|
|
|
|
|
Gitlab::Routing
|
|
|
|
.url_helpers
|
|
|
|
.public_send(target_url, todo.resource_parent, todo.target, anchor: todo_target_anchor(todo)) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
|
|
|
def todo_target_anchor(todo)
|
|
|
|
"note_#{todo.note_id}" if todo.note_id?
|
|
|
|
end
|
2020-05-11 20:10:11 -04:00
|
|
|
|
|
|
|
def design_todo_target_url(todo)
|
|
|
|
design = todo.target
|
|
|
|
path_options = {
|
|
|
|
anchor: todo_target_anchor(todo),
|
|
|
|
vueroute: design.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
::Gitlab::Routing.url_helpers.designs_project_issue_url(design.project, design.issue, path_options)
|
|
|
|
end
|
2020-02-10 19:09:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 07:09:20 -05:00
|
|
|
|
|
|
|
API::Entities::Todo.prepend_if_ee('EE::API::Entities::Todo')
|