2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
module API
|
2021-01-03 14:25:43 +05:30
|
|
|
class Todos < ::API::Base
|
2017-08-17 22:00:37 +05:30
|
|
|
include PaginationParams
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
before { authenticate! }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
feature_category :team_planning
|
2022-07-16 23:28:13 +05:30
|
|
|
urgency :low
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
ISSUABLE_TYPES = {
|
2017-08-17 22:00:37 +05:30
|
|
|
'merge_requests' => ->(iid) { find_merge_request_with_access(iid) },
|
|
|
|
'issues' => ->(iid) { find_project_issue(iid) }
|
|
|
|
}.freeze
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
params do
|
2022-11-25 23:54:43 +05:30
|
|
|
requires :id, type: String, desc: 'The ID or URL-encoded path of the project owned by the authenticated user'
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-08-24 12:49:21 +05:30
|
|
|
ISSUABLE_TYPES.each do |type, finder|
|
2017-08-17 22:00:37 +05:30
|
|
|
type_id_str = "#{type.singularize}_iid".to_sym
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Create a to-do item on an issuable' do
|
2016-11-03 12:29:30 +05:30
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
2022-11-25 23:54:43 +05:30
|
|
|
requires type_id_str, type: Integer, desc: 'The internal ID of an issuable'
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
post ":id/#{type}/:#{type_id_str}/todo" do
|
|
|
|
issuable = instance_exec(params[type_id_str], &finder)
|
2021-02-11 23:33:58 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
resource :todos do
|
|
|
|
helpers do
|
2020-11-24 15:15:51 +05:30
|
|
|
params :todo_filters do
|
2022-11-25 23:54:43 +05:30
|
|
|
optional :action, type: String, values: Todo::ACTION_NAMES.values.map(&:to_s), desc: 'The action to be filtered'
|
|
|
|
optional :author_id, type: Integer, desc: 'The ID of an author'
|
|
|
|
optional :project_id, type: Integer, desc: 'The ID of a project'
|
|
|
|
optional :group_id, type: Integer, desc: 'The ID of a group'
|
|
|
|
optional :state, type: String, values: Todo.state_machine.states.map(&:name).map(&:to_s), desc: 'The state of the to-do item'
|
|
|
|
optional :type, type: String, values: TodosFinder.todo_types.map(&:to_s), desc: 'The type of to-do item'
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def find_todos
|
2020-11-24 15:15:51 +05:30
|
|
|
TodosFinder.new(current_user, declared_params(include_missing: false)).execute
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def issuable_and_awardable?(type)
|
2019-12-21 20:55:43 +05:30
|
|
|
obj_type = Object.const_get(type, false)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
(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-06-08 01:23:25 +05:30
|
|
|
options[type] = { issuable_metadata: Gitlab::IssuableMetadata.new(current_user, targets).data, include_subscribed: false }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Get a list of to-do items' do
|
2016-11-03 12:29:30 +05:30
|
|
|
success Entities::Todo
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
params do
|
2020-11-24 15:15:51 +05:30
|
|
|
use :pagination, :todo_filters
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
get do
|
2023-07-09 08:55:56 +05:30
|
|
|
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/408576')
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
todos = paginate(find_todos.with_entity_associations)
|
2021-08-04 16:29:09 +05:30
|
|
|
todos = ::Todos::AllowedTargetFilterService.new(todos, current_user).execute
|
2019-07-07 11:18:12 +05:30
|
|
|
options = { with: Entities::Todo, current_user: current_user }
|
|
|
|
batch_load_issuable_metadata(todos, options)
|
|
|
|
|
|
|
|
present todos, options
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Mark a to-do item as done' do
|
2016-11-03 12:29:30 +05:30
|
|
|
success Entities::Todo
|
|
|
|
end
|
|
|
|
params do
|
2022-11-25 23:54:43 +05:30
|
|
|
requires :id, type: Integer, desc: 'The ID of to-do item'
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
post ':id/mark_as_done' do
|
2018-03-17 18:26:18 +05:30
|
|
|
todo = current_user.todos.find(params[:id])
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
TodoService.new.resolve_todo(todo, current_user, resolved_by_action: :api_done)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
present todo, with: Entities::Todo, current_user: current_user
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
desc 'Mark all to-do items as done'
|
2017-08-17 22:00:37 +05:30
|
|
|
post '/mark_as_done' do
|
2016-08-24 12:49:21 +05:30
|
|
|
todos = find_todos
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
TodoService.new.resolve_todos(todos, current_user, resolved_by_action: :api_all_done)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
no_content!
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
API::Todos.prepend_mod_with('API::Todos')
|