2016-02-20 13:59:59 +00:00
|
|
|
# TodoService class
|
2016-02-15 18:13:52 +00:00
|
|
|
#
|
2016-06-02 13:46:58 +00:00
|
|
|
# Used for creating/updating todos after certain user actions
|
2016-02-15 18:13:52 +00:00
|
|
|
#
|
|
|
|
# Ex.
|
2016-02-20 13:59:59 +00:00
|
|
|
# TodoService.new.new_issue(issue, current_user)
|
2016-02-15 18:13:52 +00:00
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
class TodoService
|
2016-02-15 18:13:52 +00:00
|
|
|
# When create an issue we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * create a todo for assignee if issue is assigned
|
|
|
|
# * create a todo for each mentioned user on issue
|
2016-02-15 18:13:52 +00:00
|
|
|
#
|
|
|
|
def new_issue(issue, current_user)
|
2016-02-16 23:01:14 +00:00
|
|
|
new_issuable(issue, current_user)
|
2016-02-15 18:13:52 +00:00
|
|
|
end
|
|
|
|
|
2016-02-17 22:04:14 +00:00
|
|
|
# When update an issue we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the issue for the current user as done
|
2016-02-17 22:04:14 +00:00
|
|
|
#
|
2017-03-28 06:25:43 +00:00
|
|
|
def update_issue(issue, current_user, skip_users = [])
|
|
|
|
update_issuable(issue, current_user, skip_users)
|
2016-02-17 22:04:14 +00:00
|
|
|
end
|
|
|
|
|
2016-02-16 20:31:40 +00:00
|
|
|
# When close an issue we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-16 20:31:40 +00:00
|
|
|
#
|
|
|
|
def close_issue(issue, current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
mark_pending_todos_as_done(issue, current_user)
|
2016-02-16 20:31:40 +00:00
|
|
|
end
|
|
|
|
|
2016-09-01 22:12:05 +00:00
|
|
|
# When we destroy an issue we should:
|
|
|
|
#
|
|
|
|
# * refresh the todos count cache for the current user
|
|
|
|
#
|
|
|
|
def destroy_issue(issue, current_user)
|
|
|
|
destroy_issuable(issue, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-15 18:13:52 +00:00
|
|
|
# When we reassign an issue we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * create a pending todo for new assignee if issue is assigned
|
2016-02-15 18:13:52 +00:00
|
|
|
#
|
|
|
|
def reassigned_issue(issue, current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
create_assignment_todo(issue, current_user)
|
2016-02-16 23:01:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# When create a merge request we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * creates a pending todo for assignee if merge request is assigned
|
|
|
|
# * create a todo for each mentioned user on merge request
|
2016-02-16 23:01:14 +00:00
|
|
|
#
|
|
|
|
def new_merge_request(merge_request, current_user)
|
|
|
|
new_issuable(merge_request, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-17 22:04:14 +00:00
|
|
|
# When update a merge request we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * create a todo for each mentioned user on merge request
|
2016-02-17 22:04:14 +00:00
|
|
|
#
|
2017-03-28 06:25:43 +00:00
|
|
|
def update_merge_request(merge_request, current_user, skip_users = [])
|
|
|
|
update_issuable(merge_request, current_user, skip_users)
|
2016-02-17 22:04:14 +00:00
|
|
|
end
|
|
|
|
|
2016-02-16 23:47:33 +00:00
|
|
|
# When close a merge request we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-16 23:47:33 +00:00
|
|
|
#
|
|
|
|
def close_merge_request(merge_request, current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
mark_pending_todos_as_done(merge_request, current_user)
|
2016-02-16 23:47:33 +00:00
|
|
|
end
|
|
|
|
|
2016-09-01 22:12:05 +00:00
|
|
|
# When we destroy a merge request we should:
|
|
|
|
#
|
|
|
|
# * refresh the todos count cache for the current user
|
|
|
|
#
|
|
|
|
def destroy_merge_request(merge_request, current_user)
|
|
|
|
destroy_issuable(merge_request, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:47:33 +00:00
|
|
|
# When we reassign a merge request we should:
|
2016-02-16 23:01:14 +00:00
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * creates a pending todo for new assignee if merge request is assigned
|
2016-02-16 23:01:14 +00:00
|
|
|
#
|
|
|
|
def reassigned_merge_request(merge_request, current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
create_assignment_todo(merge_request, current_user)
|
2016-02-15 18:13:52 +00:00
|
|
|
end
|
|
|
|
|
2016-02-17 01:54:10 +00:00
|
|
|
# When merge a merge request we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-17 01:54:10 +00:00
|
|
|
#
|
|
|
|
def merge_merge_request(merge_request, current_user)
|
2016-02-20 13:59:59 +00:00
|
|
|
mark_pending_todos_as_done(merge_request, current_user)
|
2016-02-17 01:54:10 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:22:50 +00:00
|
|
|
# When a build fails on the HEAD of a merge request we should:
|
|
|
|
#
|
2016-12-12 21:55:33 +00:00
|
|
|
# * create a todo for author of MR to fix it
|
|
|
|
# * create a todo for merge_user to keep an eye on it
|
2016-03-08 18:22:50 +00:00
|
|
|
#
|
|
|
|
def merge_request_build_failed(merge_request)
|
2016-12-12 21:55:33 +00:00
|
|
|
create_build_failed_todo(merge_request, merge_request.author)
|
2017-02-17 13:56:13 +00:00
|
|
|
create_build_failed_todo(merge_request, merge_request.merge_user) if merge_request.merge_when_pipeline_succeeds?
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# When a new commit is pushed to a merge request we should:
|
|
|
|
#
|
|
|
|
# * mark all pending todos related to the merge request for that user as done
|
|
|
|
#
|
|
|
|
def merge_request_push(merge_request, current_user)
|
|
|
|
mark_pending_todos_as_done(merge_request, current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
# When a build is retried to a merge request we should:
|
|
|
|
#
|
|
|
|
# * mark all pending todos related to the merge request for the author as done
|
2016-12-12 21:55:33 +00:00
|
|
|
# * mark all pending todos related to the merge request for the merge_user as done
|
2016-03-08 18:22:50 +00:00
|
|
|
#
|
|
|
|
def merge_request_build_retried(merge_request)
|
|
|
|
mark_pending_todos_as_done(merge_request, merge_request.author)
|
2017-02-17 13:56:13 +00:00
|
|
|
mark_pending_todos_as_done(merge_request, merge_request.merge_user) if merge_request.merge_when_pipeline_succeeds?
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
2017-03-28 06:25:43 +00:00
|
|
|
|
2016-12-14 14:37:31 +00:00
|
|
|
# When a merge request could not be automatically merged due to its unmergeable state we should:
|
|
|
|
#
|
|
|
|
# * create a todo for a merge_user
|
|
|
|
#
|
|
|
|
def merge_request_became_unmergeable(merge_request)
|
2017-02-17 13:56:13 +00:00
|
|
|
create_unmergeable_todo(merge_request, merge_request.merge_user) if merge_request.merge_when_pipeline_succeeds?
|
2016-12-14 14:37:31 +00:00
|
|
|
end
|
2017-03-28 06:25:43 +00:00
|
|
|
|
2016-02-16 21:29:11 +00:00
|
|
|
# When create a note we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the noteable for the note author as done
|
|
|
|
# * create a todo for each mentioned user on note
|
2016-02-16 21:29:11 +00:00
|
|
|
#
|
2016-02-18 21:12:52 +00:00
|
|
|
def new_note(note, current_user)
|
|
|
|
handle_note(note, current_user)
|
2016-02-16 21:29:11 +00:00
|
|
|
end
|
|
|
|
|
2016-02-16 22:21:24 +00:00
|
|
|
# When update a note we should:
|
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the noteable for the current user as done
|
|
|
|
# * create a todo for each new user mentioned on note
|
2016-02-16 22:21:24 +00:00
|
|
|
#
|
2017-03-28 06:25:43 +00:00
|
|
|
def update_note(note, current_user, skip_users = [])
|
|
|
|
handle_note(note, current_user, skip_users)
|
2016-02-18 21:12:52 +00:00
|
|
|
end
|
|
|
|
|
2016-04-16 19:09:08 +00:00
|
|
|
# When an emoji is awarded we should:
|
|
|
|
#
|
|
|
|
# * mark all pending todos related to the awardable for the current user as done
|
|
|
|
#
|
|
|
|
def new_award_emoji(awardable, current_user)
|
|
|
|
mark_pending_todos_as_done(awardable, current_user)
|
|
|
|
end
|
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
# When marking pending todos as done we should:
|
2016-02-18 21:12:52 +00:00
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
# * mark all pending todos related to the target for the current user as done
|
2016-02-18 21:12:52 +00:00
|
|
|
#
|
2016-02-20 13:59:59 +00:00
|
|
|
def mark_pending_todos_as_done(target, user)
|
2016-03-16 23:31:30 +00:00
|
|
|
attributes = attributes_for_target(target)
|
|
|
|
pending_todos(user, attributes).update_all(state: :done)
|
2016-06-02 13:46:58 +00:00
|
|
|
user.update_todos_count_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
# When user marks some todos as done
|
|
|
|
def mark_todos_as_done(todos, current_user)
|
2017-04-21 09:36:34 +00:00
|
|
|
update_todos_state(todos, current_user, :done)
|
2016-08-16 20:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-08-17 19:54:31 +00:00
|
|
|
def mark_todos_as_done_by_ids(ids, current_user)
|
2017-04-21 09:36:34 +00:00
|
|
|
todos = todos_by_ids(ids, current_user)
|
|
|
|
mark_todos_as_done(todos, current_user)
|
2017-01-16 13:11:08 +00:00
|
|
|
end
|
2016-06-02 13:46:58 +00:00
|
|
|
|
2017-01-16 13:11:08 +00:00
|
|
|
# When user marks some todos as pending
|
|
|
|
def mark_todos_as_pending(todos, current_user)
|
2017-04-21 09:36:34 +00:00
|
|
|
update_todos_state(todos, current_user, :pending)
|
2017-01-16 13:11:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mark_todos_as_pending_by_ids(ids, current_user)
|
2017-04-21 09:36:34 +00:00
|
|
|
todos = todos_by_ids(ids, current_user)
|
|
|
|
mark_todos_as_pending(todos, current_user)
|
2016-02-18 21:12:52 +00:00
|
|
|
end
|
|
|
|
|
2016-06-07 08:44:01 +00:00
|
|
|
# When user marks an issue as todo
|
|
|
|
def mark_todo(issuable, current_user)
|
|
|
|
attributes = attributes_for_todo(issuable.project, issuable, current_user, Todo::MARKED)
|
|
|
|
create_todos(current_user, attributes)
|
|
|
|
end
|
|
|
|
|
2016-08-11 17:00:06 +00:00
|
|
|
def todo_exist?(issuable, current_user)
|
|
|
|
TodosFinder.new(current_user).execute.exists?(target: issuable)
|
|
|
|
end
|
|
|
|
|
2016-02-18 21:12:52 +00:00
|
|
|
private
|
|
|
|
|
2017-04-21 09:36:34 +00:00
|
|
|
def todos_by_ids(ids, current_user)
|
|
|
|
current_user.todos.where(id: Array(ids))
|
|
|
|
end
|
2017-01-16 13:11:08 +00:00
|
|
|
|
2017-04-21 09:36:34 +00:00
|
|
|
def update_todos_state(todos, current_user, state)
|
2017-01-29 09:44:30 +00:00
|
|
|
# Only update those that are not really on that state
|
|
|
|
todos = todos.where.not(state: state)
|
|
|
|
todos_ids = todos.pluck(:id)
|
2017-04-02 17:48:58 +00:00
|
|
|
todos.unscope(:order).update_all(state: state)
|
2017-01-16 13:11:08 +00:00
|
|
|
current_user.update_todos_count_cache
|
2017-01-29 09:44:30 +00:00
|
|
|
todos_ids
|
2017-01-16 13:11:08 +00:00
|
|
|
end
|
|
|
|
|
2016-03-16 23:31:30 +00:00
|
|
|
def create_todos(users, attributes)
|
2016-06-09 15:12:59 +00:00
|
|
|
Array(users).map do |user|
|
2016-03-16 23:31:30 +00:00
|
|
|
next if pending_todos(user, attributes).exists?
|
2016-06-27 13:08:57 +00:00
|
|
|
todo = Todo.create(attributes.merge(user_id: user.id))
|
2016-06-02 13:46:58 +00:00
|
|
|
user.update_todos_count_cache
|
2016-06-27 13:08:57 +00:00
|
|
|
todo
|
2016-02-18 21:12:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_issuable(issuable, author)
|
2016-02-20 13:59:59 +00:00
|
|
|
create_assignment_todo(issuable, author)
|
|
|
|
create_mention_todos(issuable.project, issuable, author)
|
2016-02-18 21:12:52 +00:00
|
|
|
end
|
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
def update_issuable(issuable, author, skip_users = [])
|
2016-06-09 17:07:58 +00:00
|
|
|
# Skip toggling a task list item in a description
|
2016-06-17 14:34:11 +00:00
|
|
|
return if toggling_tasks?(issuable)
|
2016-06-09 17:07:58 +00:00
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
create_mention_todos(issuable.project, issuable, author, nil, skip_users)
|
2016-06-09 17:07:58 +00:00
|
|
|
end
|
|
|
|
|
2016-09-01 22:12:05 +00:00
|
|
|
def destroy_issuable(issuable, user)
|
|
|
|
user.update_todos_count_cache
|
|
|
|
end
|
|
|
|
|
2016-06-17 14:34:11 +00:00
|
|
|
def toggling_tasks?(issuable)
|
|
|
|
issuable.previous_changes.include?('description') &&
|
|
|
|
issuable.tasks? && issuable.updated_tasks.any?
|
|
|
|
end
|
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
def handle_note(note, author, skip_users = [])
|
2016-03-16 23:31:30 +00:00
|
|
|
# Skip system notes, and notes on project snippet
|
2016-03-31 07:20:27 +00:00
|
|
|
return if note.system? || note.for_snippet?
|
2016-02-17 22:04:14 +00:00
|
|
|
|
2016-02-18 19:26:52 +00:00
|
|
|
project = note.project
|
|
|
|
target = note.noteable
|
2016-02-17 22:04:14 +00:00
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
mark_pending_todos_as_done(target, author)
|
2017-03-28 06:25:43 +00:00
|
|
|
create_mention_todos(project, target, author, note, skip_users)
|
2016-02-18 21:12:52 +00:00
|
|
|
end
|
2016-02-17 22:04:14 +00:00
|
|
|
|
2016-02-20 13:59:59 +00:00
|
|
|
def create_assignment_todo(issuable, author)
|
2017-05-04 12:11:15 +00:00
|
|
|
if issuable.assignees.any?
|
2016-03-16 23:31:30 +00:00
|
|
|
attributes = attributes_for_todo(issuable.project, issuable, author, Todo::ASSIGNED)
|
2017-05-04 12:11:15 +00:00
|
|
|
create_todos(issuable.assignees, attributes)
|
2016-02-16 22:21:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
def create_mention_todos(project, target, author, note = nil, skip_users = [])
|
2016-12-05 12:12:22 +00:00
|
|
|
# Create Todos for directly addressed users
|
2017-03-28 06:25:43 +00:00
|
|
|
directly_addressed_users = filter_directly_addressed_users(project, note || target, author, skip_users)
|
2016-12-05 12:12:22 +00:00
|
|
|
attributes = attributes_for_todo(project, target, author, Todo::DIRECTLY_ADDRESSED, note)
|
|
|
|
create_todos(directly_addressed_users, attributes)
|
|
|
|
|
|
|
|
# Create Todos for mentioned users
|
2017-03-28 06:25:43 +00:00
|
|
|
mentioned_users = filter_mentioned_users(project, note || target, author, skip_users)
|
2016-03-16 23:31:30 +00:00
|
|
|
attributes = attributes_for_todo(project, target, author, Todo::MENTIONED, note)
|
|
|
|
create_todos(mentioned_users, attributes)
|
|
|
|
end
|
|
|
|
|
2016-12-12 21:55:33 +00:00
|
|
|
def create_build_failed_todo(merge_request, todo_author)
|
|
|
|
attributes = attributes_for_todo(merge_request.project, merge_request, todo_author, Todo::BUILD_FAILED)
|
|
|
|
create_todos(todo_author, attributes)
|
2016-03-08 18:22:50 +00:00
|
|
|
end
|
|
|
|
|
2016-12-14 14:37:31 +00:00
|
|
|
def create_unmergeable_todo(merge_request, merge_user)
|
|
|
|
attributes = attributes_for_todo(merge_request.project, merge_request, merge_user, Todo::UNMERGEABLE)
|
|
|
|
create_todos(merge_user, attributes)
|
|
|
|
end
|
|
|
|
|
2016-03-16 23:31:30 +00:00
|
|
|
def attributes_for_target(target)
|
|
|
|
attributes = {
|
2017-04-27 10:41:26 +00:00
|
|
|
project_id: target&.project&.id,
|
2016-03-16 23:31:30 +00:00
|
|
|
target_id: target.id,
|
|
|
|
target_type: target.class.name,
|
|
|
|
commit_id: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.is_a?(Commit)
|
|
|
|
attributes.merge!(target_id: nil, commit_id: target.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes_for_todo(project, target, author, action, note = nil)
|
|
|
|
attributes_for_target(target).merge!(
|
|
|
|
project_id: project.id,
|
|
|
|
author_id: author.id,
|
|
|
|
action: action,
|
|
|
|
note: note
|
|
|
|
)
|
2016-02-15 18:13:52 +00:00
|
|
|
end
|
2016-02-16 02:26:33 +00:00
|
|
|
|
2016-12-05 12:12:22 +00:00
|
|
|
def filter_todo_users(users, project, target)
|
|
|
|
reject_users_without_access(users, project, target).uniq
|
|
|
|
end
|
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
def filter_mentioned_users(project, target, author, skip_users = [])
|
|
|
|
mentioned_users = target.mentioned_users(author) - skip_users
|
2016-12-05 12:12:22 +00:00
|
|
|
filter_todo_users(mentioned_users, project, target)
|
|
|
|
end
|
|
|
|
|
2017-03-28 06:25:43 +00:00
|
|
|
def filter_directly_addressed_users(project, target, author, skip_users = [])
|
|
|
|
directly_addressed_users = target.directly_addressed_users(author) - skip_users
|
2016-12-05 12:12:22 +00:00
|
|
|
filter_todo_users(directly_addressed_users, project, target)
|
2016-02-17 17:46:26 +00:00
|
|
|
end
|
|
|
|
|
2016-03-30 21:41:21 +00:00
|
|
|
def reject_users_without_access(users, project, target)
|
2016-10-04 12:52:08 +00:00
|
|
|
if target.is_a?(Note) && (target.for_issue? || target.for_merge_request?)
|
2016-03-30 21:41:21 +00:00
|
|
|
target = target.noteable
|
|
|
|
end
|
|
|
|
|
2016-10-04 12:52:08 +00:00
|
|
|
if target.is_a?(Issuable)
|
|
|
|
select_users(users, :"read_#{target.to_ability_name}", target)
|
2016-03-30 21:41:21 +00:00
|
|
|
else
|
|
|
|
select_users(users, :read_project, project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_users(users, ability, subject)
|
|
|
|
users.select do |user|
|
|
|
|
user.can?(ability.to_sym, subject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-16 23:31:30 +00:00
|
|
|
def pending_todos(user, criteria = {})
|
|
|
|
valid_keys = [:project_id, :target_id, :target_type, :commit_id]
|
|
|
|
user.todos.pending.where(criteria.slice(*valid_keys))
|
2016-02-16 23:01:14 +00:00
|
|
|
end
|
2016-02-15 18:13:52 +00:00
|
|
|
end
|