2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-11 14:04:42 -05:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class Todos < ::API::Base
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-03-11 14:04:42 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
2021-10-27 11:13:41 -04:00
|
|
|
feature_category :team_planning
|
2020-10-30 14:08:56 -04:00
|
|
|
|
2016-06-28 12:04:44 -04:00
|
|
|
ISSUABLE_TYPES = {
|
2017-02-28 03:29:14 -05:00
|
|
|
'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
|
|
|
|
'issues' => ->(iid) { find_project_issue(iid) }
|
2017-02-21 18:32:18 -05:00
|
|
|
}.freeze
|
2016-06-28 12:04:44 -04:00
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2019-01-16 07:09:29 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-06-28 12:04:44 -04:00
|
|
|
ISSUABLE_TYPES.each do |type, finder|
|
2017-02-28 03:29:14 -05:00
|
|
|
type_id_str = "#{type.singularize}_iid".to_sym
|
2016-06-28 12:04:44 -04:00
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Create a todo on an issuable' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 03:29:14 -05:00
|
|
|
requires type_id_str, type: Integer, desc: 'The IID of an issuable'
|
2016-10-14 03:16:55 -04:00
|
|
|
end
|
2016-06-28 12:04:44 -04:00
|
|
|
post ":id/#{type}/:#{type_id_str}/todo" do
|
|
|
|
issuable = instance_exec(params[type_id_str], &finder)
|
2021-02-11 10:09:11 -05:00
|
|
|
|
|
|
|
unless can?(current_user, :read_merge_request, issuable.project)
|
|
|
|
not_found!(type.split("_").map(&:capitalize).join(" "))
|
|
|
|
end
|
|
|
|
|
2016-06-28 12:04:44 -04:00
|
|
|
todo = TodoService.new.mark_todo(issuable, current_user).first
|
|
|
|
|
|
|
|
if todo
|
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
|
|
|
else
|
|
|
|
not_modified!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 14:04:42 -05:00
|
|
|
resource :todos do
|
|
|
|
helpers do
|
2020-09-08 14:08:48 -04:00
|
|
|
params :todo_filters do
|
|
|
|
optional :action, String, values: Todo::ACTION_NAMES.values.map(&:to_s)
|
|
|
|
optional :author_id, Integer
|
|
|
|
optional :state, String, values: Todo.state_machine.states.map(&:name).map(&:to_s)
|
|
|
|
optional :type, String, values: TodosFinder.todo_types
|
|
|
|
optional :project_id, Integer
|
|
|
|
optional :group_id, Integer
|
|
|
|
end
|
|
|
|
|
2020-03-05 04:08:31 -05:00
|
|
|
def find_todos
|
2020-09-08 14:08:48 -04:00
|
|
|
TodosFinder.new(current_user, declared_params(include_missing: false)).execute
|
2020-03-05 04:08:31 -05:00
|
|
|
end
|
|
|
|
|
2019-03-02 12:31:36 -05:00
|
|
|
def issuable_and_awardable?(type)
|
2019-10-15 23:06:12 -04:00
|
|
|
obj_type = Object.const_get(type, false)
|
2019-03-02 12:31:36 -05:00
|
|
|
|
|
|
|
(obj_type < Issuable) && (obj_type < Awardable)
|
|
|
|
rescue NameError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def batch_load_issuable_metadata(todos, options)
|
|
|
|
# This should be paginated and will cause Rails to SELECT for all the Todos
|
|
|
|
todos_by_type = todos.group_by(&:target_type)
|
|
|
|
|
|
|
|
todos_by_type.keys.each do |type|
|
|
|
|
next unless issuable_and_awardable?(type)
|
|
|
|
|
|
|
|
collection = todos_by_type[type]
|
|
|
|
|
|
|
|
next unless collection
|
|
|
|
|
|
|
|
targets = collection.map(&:target)
|
2021-05-06 05:18:56 -04:00
|
|
|
options[type] = { issuable_metadata: Gitlab::IssuableMetadata.new(current_user, targets).data, include_subscribed: false }
|
2019-03-02 12:31:36 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Get a todo list' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
2016-12-04 12:11:19 -05:00
|
|
|
params do
|
2020-09-08 14:08:48 -04:00
|
|
|
use :pagination, :todo_filters
|
2016-12-04 12:11:19 -05:00
|
|
|
end
|
2016-03-11 14:04:42 -05:00
|
|
|
get do
|
2019-06-24 13:48:50 -04:00
|
|
|
todos = paginate(find_todos.with_entity_associations)
|
2021-08-03 17:09:39 -04:00
|
|
|
todos = ::Todos::AllowedTargetFilterService.new(todos, current_user).execute
|
2019-03-02 12:31:36 -05:00
|
|
|
options = { with: Entities::Todo, current_user: current_user }
|
|
|
|
batch_load_issuable_metadata(todos, options)
|
|
|
|
|
|
|
|
present todos, options
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Mark a todo as done' do
|
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the todo being marked as done'
|
|
|
|
end
|
2017-02-21 04:00:33 -05:00
|
|
|
post ':id/mark_as_done' do
|
2018-01-18 18:10:19 -05:00
|
|
|
todo = current_user.todos.find(params[:id])
|
2016-03-11 14:04:42 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :api_done)
|
|
|
|
|
2017-04-21 05:36:34 -04:00
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
|
2016-10-14 03:16:55 -04:00
|
|
|
desc 'Mark all todos as done'
|
2017-02-21 04:00:33 -05:00
|
|
|
post '/mark_as_done' do
|
2016-05-21 13:01:11 -04:00
|
|
|
todos = find_todos
|
2020-05-28 14:08:37 -04:00
|
|
|
|
|
|
|
TodoService.new.resolve_todos(todos, current_user, resolved_by_action: :api_all_done)
|
2017-02-21 04:00:33 -05:00
|
|
|
|
|
|
|
no_content!
|
2016-03-11 14:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-08-15 09:16:29 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
API::Todos.prepend_mod_with('API::Todos')
|