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::DISTRIBUTED_HLL_FALLBACK, Gitlab::Utils::UsageData::FALLBACK, Gitlab::Utils::UsageData::HISTOGRAM_FALLBACK, Gitlab::Utils::UsageData::MAX_BUCKET_SIZE

Instance Method Summary collapse

Methods included from Gitlab::Utils::UsageData

#add, #add_metric, #alt_usage_data, #average, #count, #distinct_count, #estimate_batch_distinct_count, #histogram, #maximum_id, #measure_duration, #minimum_id, #redis_usage_data, #sum, #track_usage_event, #with_finished_at, #with_metadata, #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


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

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


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

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

#create_member_access_request_todos(member) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'app/services/todo_service.rb', line 283

def create_member_access_request_todos(member)
  source = member.source
  attributes = attributes_for_access_request_todos(source, member.user, Todo::MEMBER_ACCESS_REQUESTED)

  approvers = source.access_request_approvers_to_be_notified.map(&:user)
  return true if approvers.empty?

  if source.instance_of? Project
    project = source
    namespace = project.namespace
  else
    project = nil
    namespace = source
  end

  create_todos(approvers, attributes, namespace, project)
end

#create_request_review_todo(target, author, reviewers) ⇒ Object



277
278
279
280
281
# File 'app/services/todo_service.rb', line 277

def create_request_review_todo(target, author, reviewers)
  project = target.project
  attributes = attributes_for_todo(project, target, author, Todo::REVIEW_REQUESTED)
  create_todos(reviewers, attributes, project.namespace, project)
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)


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

def destroy_target(target)
  todo_user_ids = target.todos.distinct_user_ids

  yield target

  Users::UpdateTodoCountCacheService.new(todo_user_ids).execute if todo_user_ids.present?
end

#mark_todo(target, current_user) ⇒ Object

When user marks a target as todo



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

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

  todos = create_todos(current_user, attributes, target_namespace(target), project)
  work_item_activity_counter.track_work_item_mark_todo_action(author: current_user) if target.is_a?(WorkItem)

  todos
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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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

#resolve_access_request_todos(member) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/services/todo_service.rb', line 245

def resolve_access_request_todos(member)
  return if member.nil?

  # Group or Project
  target = member.source

  todos_params = {
    state: :pending,
    author_id: member.user_id,
    action: ::Todo::MEMBER_ACCESS_REQUESTED,
    type: target.class.polymorphic_name
  }

  resolve_todos_with_attributes_for_target(target, todos_params)
end

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



235
236
237
238
239
240
241
242
243
# File 'app/services/todo_service.rb', line 235

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)

  GraphqlTriggers.issuable_todo_updated(todo.target)

  current_user.update_todos_count_cache
end

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



227
228
229
230
231
232
233
# File 'app/services/todo_service.rb', line 227

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 for the current_user



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

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

  resolve_todos(pending_todos([current_user], attributes), current_user)

  GraphqlTriggers.issuable_todo_updated(target)
end

#resolve_todos_with_attributes_for_target(target, attributes, resolution: :done, resolved_by_action: :system_done) ⇒ Object

Resolves all todos related to target for all users



215
216
217
218
219
220
221
222
223
224
225
# File 'app/services/todo_service.rb', line 215

def resolve_todos_with_attributes_for_target(target, attributes, resolution: :done, resolved_by_action: :system_done)
  target_attributes = { target_id: target.id, target_type: target.class.polymorphic_name }
  attributes.merge!(target_attributes)
  attributes[:preload_user_association] = true

  todos = PendingTodosFinder.new(attributes).execute
  users = todos.map(&:user)
  todos_ids = todos.batch_update(state: resolution, resolved_by_action: resolved_by_action)
  users.each(&:update_todos_count_cache)
  todos_ids
end

#restore_todo(todo, current_user) ⇒ Object



269
270
271
272
273
274
275
# File 'app/services/todo_service.rb', line 269

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



261
262
263
264
265
266
267
# File 'app/services/todo_service.rb', line 261

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

  current_user.update_todos_count_cache

  todos_ids
end

#ssh_key_expired(ssh_keys) ⇒ Object

When a SSH key expired we should:

  • resolve any corresponding “expiring soon” todo

  • create a todo for the user owning that SSH key



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

def ssh_key_expired(ssh_keys)
  ssh_keys = Array(ssh_keys)

  # Resolve any pending "expiring soon" todos for these keys
  expiring_key_todos = ::Todo.pending_for_expiring_ssh_keys(ssh_keys.map(&:id))
  expiring_key_todos.batch_update(state: :done, resolved_by_action: :system_done)

  create_ssh_key_todos(ssh_keys, ::Todo::SSH_KEY_EXPIRED)
end

#ssh_key_expiring_soon(ssh_keys) ⇒ Object

When a SSH key is expiring soon we should:

  • create a todo for the user owning that SSH key



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

def ssh_key_expiring_soon(ssh_keys)
  create_ssh_key_todos(Array(ssh_keys), ::Todo::SSH_KEY_EXPIRING_SOON)
end

#todo_exist?(issuable, current_user) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'app/services/todo_service.rb', line 201

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


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

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


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

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


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

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