Class: TodoService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::UsageData
Defined in:
app/services/todo_service.rb

Overview

TodoService class

Used for creating/updating todos after certain user actions

Ex.

TodoService.new.new_issue(issue, current_user)

Constant Summary

Constants included from Gitlab::Utils::UsageData

Gitlab::Utils::UsageData::FALLBACK

Instance Method Summary collapse

Methods included from Gitlab::Utils::UsageData

#alt_usage_data, #count, #distinct_count, #measure_duration, #redis_usage_data, #track_usage_event, #with_finished_at, #with_prometheus_client

Instance Method Details

#close_issue(issue, current_user) ⇒ Object

When close an issue we should:

* mark all pending todos related to the target for the current user as done

33
34
35
# File 'app/services/todo_service.rb', line 33

def close_issue(issue, current_user)
  resolve_todos_for_target(issue, current_user)
end

#close_merge_request(merge_request, current_user) ⇒ Object

When close a merge request we should:

* mark all pending todos related to the target for the current user as done

90
91
92
# File 'app/services/todo_service.rb', line 90

def close_merge_request(merge_request, current_user)
  resolve_todos_for_target(merge_request, current_user)
end

#destroy_target(target) {|target| ... } ⇒ Object

When we destroy a todo target we should:

* refresh the todos count cache for all users with todos on the target

This needs to yield back to the caller to destroy the target, because it collects the todo users before the todos themselves are deleted, then updates the todo counts for those users.

Yields:

  • (target)

45
46
47
48
49
50
51
# File 'app/services/todo_service.rb', line 45

def destroy_target(target)
  todo_users = UsersWithPendingTodosFinder.new(target).execute.to_a

  yield target

  todo_users.each(&:update_todos_count_cache)
end

#mark_todo(target, current_user) ⇒ Object

When user marks a target as todo


167
168
169
170
# File 'app/services/todo_service.rb', line 167

def mark_todo(target, current_user)
  attributes = attributes_for_todo(target.project, target, current_user, Todo::MARKED)
  create_todos(current_user, attributes)
end

#merge_merge_request(merge_request, current_user) ⇒ Object

When merge a merge request we should:

* mark all pending todos related to the target for the current user as done

98
99
100
# File 'app/services/todo_service.rb', line 98

def merge_merge_request(merge_request, current_user)
  resolve_todos_for_target(merge_request, current_user)
end

#merge_request_became_unmergeable(merge_request) ⇒ Object

When a merge request could not be merged due to its unmergeable state we should:

* create a todo for each merge participant

134
135
136
137
138
# File 'app/services/todo_service.rb', line 134

def merge_request_became_unmergeable(merge_request)
  merge_request.merge_participants.each do |user|
    create_unmergeable_todo(merge_request, user)
  end
end

#merge_request_build_failed(merge_request) ⇒ Object

When a build fails on the HEAD of a merge request we should:

* create a todo for each merge participant

106
107
108
109
110
# File 'app/services/todo_service.rb', line 106

def merge_request_build_failed(merge_request)
  merge_request.merge_participants.each do |user|
    create_build_failed_todo(merge_request, user)
  end
end

#merge_request_build_retried(merge_request) ⇒ Object

When a build is retried to a merge request we should:

* mark all pending todos related to the merge request as done for each merge participant

124
125
126
127
128
# File 'app/services/todo_service.rb', line 124

def merge_request_build_retried(merge_request)
  merge_request.merge_participants.each do |user|
    resolve_todos_for_target(merge_request, user)
  end
end

#merge_request_push(merge_request, current_user) ⇒ Object

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

116
117
118
# File 'app/services/todo_service.rb', line 116

def merge_request_push(merge_request, current_user)
  resolve_todos_for_target(merge_request, current_user)
end

#new_award_emoji(awardable, current_user) ⇒ Object

When an emoji is awarded we should:

* mark all pending todos related to the awardable for the current user as done

162
163
164
# File 'app/services/todo_service.rb', line 162

def new_award_emoji(awardable, current_user)
  resolve_todos_for_target(awardable, current_user)
end

#new_issue(issue, current_user) ⇒ Object

When create an issue we should:

* create a todo for assignee if issue is assigned
* create a todo for each mentioned user on issue

17
18
19
# File 'app/services/todo_service.rb', line 17

def new_issue(issue, current_user)
  new_issuable(issue, current_user)
end

#new_merge_request(merge_request, current_user) ⇒ Object

When create a merge request we should:

* creates a pending todo for assignee if merge request is assigned
* create a todo for each mentioned user on merge request

74
75
76
# File 'app/services/todo_service.rb', line 74

def new_merge_request(merge_request, current_user)
  new_issuable(merge_request, current_user)
end

#new_note(note, current_user) ⇒ Object

When create a note we should:

* mark all pending todos related to the noteable for the note author as done
* create a todo for each mentioned user on note

145
146
147
# File 'app/services/todo_service.rb', line 145

def new_note(note, current_user)
  handle_note(note, current_user)
end

#reassigned_assignable(issuable, current_user, old_assignees = []) ⇒ Object

When we reassign an assignable object (issuable, alert) we should:

* create a pending todo for new assignee if object is assigned

57
58
59
# File 'app/services/todo_service.rb', line 57

def reassigned_assignable(issuable, current_user, old_assignees = [])
  create_assignment_todo(issuable, current_user, old_assignees)
end

#reassigned_reviewable(issuable, current_user, old_reviewers = []) ⇒ Object

When we reassign an reviewable object (merge request) we should:

* create a pending todo for new reviewer if object is assigned

65
66
67
# File 'app/services/todo_service.rb', line 65

def reassigned_reviewable(issuable, current_user, old_reviewers = [])
  create_reviewer_todo(issuable, current_user, old_reviewers)
end

#resolve_todo(todo, current_user, resolution: :done, resolved_by_action: :system_done) ⇒ Object


191
192
193
194
195
196
197
# File 'app/services/todo_service.rb', line 191

def resolve_todo(todo, current_user, resolution: :done, resolved_by_action: :system_done)
  return if todo.done?

  todo.update(state: resolution, resolved_by_action: resolved_by_action)

  current_user.update_todos_count_cache
end

#resolve_todos(todos, current_user, resolution: :done, resolved_by_action: :system_done) ⇒ Object


183
184
185
186
187
188
189
# File 'app/services/todo_service.rb', line 183

def resolve_todos(todos, current_user, resolution: :done, resolved_by_action: :system_done)
  todos_ids = todos.batch_update(state: resolution, resolved_by_action: resolved_by_action)

  current_user.update_todos_count_cache

  todos_ids
end

#resolve_todos_for_target(target, current_user) ⇒ Object

Resolves all todos related to target


177
178
179
180
181
# File 'app/services/todo_service.rb', line 177

def resolve_todos_for_target(target, current_user)
  attributes = attributes_for_target(target)

  resolve_todos(pending_todos(current_user, attributes), current_user)
end

#restore_todo(todo, current_user) ⇒ Object


207
208
209
210
211
212
213
# File 'app/services/todo_service.rb', line 207

def restore_todo(todo, current_user)
  return if todo.pending?

  todo.update(state: :pending)

  current_user.update_todos_count_cache
end

#restore_todos(todos, current_user) ⇒ Object


199
200
201
202
203
204
205
# File 'app/services/todo_service.rb', line 199

def restore_todos(todos, current_user)
  todos_ids = todos.batch_update(state: :pending)

  current_user.update_todos_count_cache

  todos_ids
end

#todo_exist?(issuable, current_user) ⇒ Boolean

Returns:

  • (Boolean)

172
173
174
# File 'app/services/todo_service.rb', line 172

def todo_exist?(issuable, current_user)
  TodosFinder.new(current_user).any_for_target?(issuable, :pending)
end

#update_issue(issue, current_user, skip_users = []) ⇒ Object

When update an issue we should:

* mark all pending todos related to the issue for the current user as done

25
26
27
# File 'app/services/todo_service.rb', line 25

def update_issue(issue, current_user, skip_users = [])
  update_issuable(issue, current_user, skip_users)
end

#update_merge_request(merge_request, current_user, skip_users = []) ⇒ Object

When update a merge request we should:

* create a todo for each mentioned user on merge request

82
83
84
# File 'app/services/todo_service.rb', line 82

def update_merge_request(merge_request, current_user, skip_users = [])
  update_issuable(merge_request, current_user, skip_users)
end

#update_note(note, current_user, skip_users = []) ⇒ Object

When update a note we should:

* mark all pending todos related to the noteable for the current user as done
* create a todo for each new user mentioned on note

154
155
156
# File 'app/services/todo_service.rb', line 154

def update_note(note, current_user, skip_users = [])
  handle_note(note, current_user, skip_users)
end