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, #count, #distinct_count, #epics_deepest_relationship_level, #estimate_batch_distinct_count, #histogram, #jira_integration_data, #maximum_id, #measure_duration, #minimum_id, #redis_usage_data, #sum, #track_usage_event, #with_duration, #with_finished_at, #with_prometheus_client

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

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_attention_requested_todo(target, author, users) ⇒ Object


221
222
223
224
# File 'app/services/todo_service.rb', line 221

def create_attention_requested_todo(target, author, users)
  attributes = attributes_for_todo(target.project, target, author, Todo::ATTENTION_REQUESTED)
  create_todos(users, attributes)
end

#create_request_review_todo(target, author, reviewers) ⇒ Object


216
217
218
219
# File 'app/services/todo_service.rb', line 216

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


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

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

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_todo(todo, current_user, resolution: :done, resolved_by_action: :system_done) ⇒ Object


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

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


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

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


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

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


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

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


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

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)

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

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