Class: SystemNotes::IssuablesService

Inherits:
BaseService show all
Defined in:
app/services/system_notes/issuables_service.rb

Constant Summary collapse

USE_COMMIT_DATE_FOR_CROSS_REFERENCE_NOTE =

We create cross-referenced system notes when a commit relates to an issue. There are two options what time to use for the system note:

  1. The push date (default)

  2. The commit date

The commit date is useful when an existing Git repository is imported to GitLab. It helps to preserve an original order of all notes (comments, commits, status changes, e.t.c) in the imported issues. Otherwise, all commits will be linked before or after all other imported notes.

See also the discussion in gitlab.com/gitlab-org/gitlab/-/merge_requests/60700#note_612724683

false

Instance Attribute Summary

Attributes inherited from BaseService

#author, #noteable, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from SystemNotes::BaseService

Class Method Details

.issuable_eventsObject



17
18
19
20
21
22
23
24
# File 'app/services/system_notes/issuables_service.rb', line 17

def self.issuable_events
  {
    assigned: s_('IssuableEvents|assigned to'),
    unassigned: s_('IssuableEvents|unassigned'),
    review_requested: s_('IssuableEvents|requested review from'),
    review_request_removed: s_('IssuableEvents|removed review request for')
  }.freeze
end

Instance Method Details

#add_email_participants(body) ⇒ Object



433
434
435
# File 'app/services/system_notes/issuables_service.rb', line 433

def add_email_participants(body)
  create_note(NoteSummary.new(noteable, project, author, body))
end

#auto_resolve_prometheus_alertObject



454
455
456
# File 'app/services/system_notes/issuables_service.rb', line 454

def auto_resolve_prometheus_alert
  create_resource_state_event(status: 'closed', close_auto_resolve_prometheus_alert: true)
end

#change_assignee(assignee) ⇒ Object

Called when the assignee of a Noteable is changed or removed

assignee - User being assigned, or nil

Example Note text:

"removed assignee"

"assigned to @rspeicher"

Returns the created Note object



69
70
71
72
73
74
75
# File 'app/services/system_notes/issuables_service.rb', line 69

def change_assignee(assignee)
  body = assignee.nil? ? 'removed assignee' : "assigned to #{assignee.to_reference}"

  track_issue_event(:track_issue_assignee_changed_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'assignee'))
end

#change_descriptionObject

Called when the description of a Noteable is changed

noteable - Noteable object that responds to ‘description` project - Project owning noteable author - User performing the change

Example Note text:

"changed the description"

Returns the created Note object



218
219
220
221
222
223
224
# File 'app/services/system_notes/issuables_service.rb', line 218

def change_description
  body = 'changed the description'

  track_issue_event(:track_issue_description_changed_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'description'))
end

#change_issuable_assignees(old_assignees) ⇒ Object

Called when the assignees of an issuable is changed or removed

assignees - Users being assigned, or nil

Example Note text:

"removed all assignees"

"assigned to @user1 additionally to @user2"

"assigned to @user1, @user2 and @user3 and unassigned @user4 and @user5"

"assigned to @user1 and @user2"

Returns the created Note object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/services/system_notes/issuables_service.rb', line 92

def change_issuable_assignees(old_assignees)
  unassigned_users = old_assignees - noteable.assignees
  added_users = noteable.assignees.to_a - old_assignees
  text_parts = []

  Gitlab::I18n.with_default_locale do
    text_parts << "#{self.class.issuable_events[:assigned]} #{added_users.map(&:to_reference).to_sentence}" if added_users.any?
    text_parts << "#{self.class.issuable_events[:unassigned]} #{unassigned_users.map(&:to_reference).to_sentence}" if unassigned_users.any?
  end

  body = text_parts.join(' and ')

  track_issue_event(:track_issue_assignee_changed_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'assignee'))
end

#change_issuable_contacts(added_count, removed_count) ⇒ Object

Called when the contacts of an issuable are changed or removed We intend to reference the contacts but for security we are just going to state how many were added/removed for now. See discussion: gitlab.com/gitlab-org/gitlab/-/merge_requests/77816#note_806114273

added_count - number of contacts added, or 0 removed_count - number of contacts removed, or 0

Example Note text:

"added 2 contacts"

"added 3 contacts and removed one contact"

Returns the created Note object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/services/system_notes/issuables_service.rb', line 150

def change_issuable_contacts(added_count, removed_count)
  text_parts = []

  Gitlab::I18n.with_default_locale do
    text_parts << "added #{added_count} #{'contact'.pluralize(added_count)}" if added_count > 0
    text_parts << "removed #{removed_count} #{'contact'.pluralize(removed_count)}" if removed_count > 0
  end

  return if text_parts.empty?

  body = text_parts.join(' and ')
  create_note(NoteSummary.new(noteable, project, author, body, action: 'contact'))
end

#change_issuable_reviewers(old_reviewers) ⇒ Object

Called when the reviewers of an issuable is changed or removed

reviewers - Users being requested to review, or nil

Example Note text:

"requested review from @user1 and @user2"

"requested review from @user1, @user2 and @user3 and removed review request for @user4 and @user5"

Returns the created Note object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/services/system_notes/issuables_service.rb', line 120

def change_issuable_reviewers(old_reviewers)
  unassigned_users = old_reviewers - noteable.reviewers
  added_users = noteable.reviewers - old_reviewers
  text_parts = []

  Gitlab::I18n.with_default_locale do
    text_parts << "#{self.class.issuable_events[:review_requested]} #{added_users.map(&:to_reference).to_sentence}" if added_users.any?
    text_parts << "#{self.class.issuable_events[:review_request_removed]} #{unassigned_users.map(&:to_reference).to_sentence}" if unassigned_users.any?
  end

  body = text_parts.join(' and ')

  create_note(NoteSummary.new(noteable, project, author, body, action: 'reviewer'))
end

#change_issue_confidentialityObject

Called when the confidentiality changes

Example Note text:

"made the issue confidential"

Returns the created Note object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/services/system_notes/issuables_service.rb', line 351

def change_issue_confidentiality
  if noteable.confidential
    body = "made the #{noteable_name} confidential"
    action = 'confidential'

    track_issue_event(:track_issue_made_confidential_action)
  else
    body = "made the #{noteable_name} visible to everyone"
    action = 'visible'

    track_issue_event(:track_issue_made_visible_action)
  end

  create_note(NoteSummary.new(noteable, project, author, body, action: action))
end

#change_issue_type(previous_type) ⇒ Object



458
459
460
461
462
463
464
# File 'app/services/system_notes/issuables_service.rb', line 458

def change_issue_type(previous_type)
  previous = previous_type.humanize(capitalize: false)
  new = noteable.issue_type.humanize(capitalize: false)
  body = "changed type from #{previous} to #{new}"

  create_note(NoteSummary.new(noteable, project, author, body, action: 'issue_type'))
end

#change_status(status, source = nil) ⇒ Object

Called when the status of a Noteable is changed

status - String status source - Mentionable performing the change, or nil

Example Note text:

"merged"

"closed via bc17db76"

Returns the created Note object



379
380
381
# File 'app/services/system_notes/issuables_service.rb', line 379

def change_status(status, source = nil)
  create_resource_state_event(status: status, mentionable_source: source)
end

#change_task_status(new_task) ⇒ Object

Called when the status of a Task has changed

new_task - TaskList::Item object.

Example Note text:

"marked the checklist item Whatever as completed."

Returns the created Note object



288
289
290
291
292
293
294
295
# File 'app/services/system_notes/issuables_service.rb', line 288

def change_task_status(new_task)
  status_label = new_task.complete? ? Taskable::COMPLETED : Taskable::INCOMPLETE
  body = "marked the checklist item **#{new_task.source}** as #{status_label}"

  track_issue_event(:track_issue_description_changed_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'task'))
end

#change_title(old_title) ⇒ Object

Called when the title of a Noteable is changed

old_title - Previous String title

Example Note text:

"changed title from **Old** to **New**"

Returns the created Note object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/services/system_notes/issuables_service.rb', line 173

def change_title(old_title)
  new_title = noteable.title.dup

  old_diffs, new_diffs = Gitlab::Diff::InlineDiff.new(old_title, new_title).inline_diffs

  marked_old_title = Gitlab::Diff::InlineDiffMarkdownMarker.new(old_title).mark(old_diffs)
  marked_new_title = Gitlab::Diff::InlineDiffMarkdownMarker.new(new_title).mark(new_diffs)

  body = "changed title from **#{marked_old_title}** to **#{marked_new_title}**"

  track_issue_event(:track_issue_title_changed_action)
  work_item_activity_counter.track_work_item_title_changed_action(author: author) if noteable.is_a?(WorkItem)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'title'))
end

#close_after_error_tracking_resolveObject



450
451
452
# File 'app/services/system_notes/issuables_service.rb', line 450

def close_after_error_tracking_resolve
  create_resource_state_event(status: 'closed', close_after_error_tracking_resolve: true)
end

#cross_reference(mentioned_in) ⇒ Note

Called when a Mentionable (the ‘mentioned_in`) references another Mentionable (the `mentioned`, passed to this service as `noteable`).

Example Note text:

"mentioned in #1"

"mentioned in !2"

"mentioned in 54f7727c"

See cross_reference_note_content.

Parameters:

Returns:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/services/system_notes/issuables_service.rb', line 241

def cross_reference(mentioned_in)
  return if cross_reference_disallowed?(mentioned_in)

  gfm_reference = mentioned_in.gfm_reference(noteable.project || noteable.group)
  body = cross_reference_note_content(gfm_reference)

  if noteable.is_a?(ExternalIssue)
    Integrations::CreateExternalCrossReferenceWorker.perform_async(
      noteable.project_id,
      noteable.id,
      mentioned_in.class.name,
      mentioned_in.id,
      author.id
    )
  else
    track_cross_reference_action

    created_at = mentioner.created_at if USE_COMMIT_DATE_FOR_CROSS_REFERENCE_NOTE && mentioner.is_a?(Commit)
    create_note(NoteSummary.new(noteable, noteable.project, author, body, action: 'cross_reference', created_at: created_at))
  end
end

#cross_reference_disallowed?(mentioned_in) ⇒ Boolean

Check if a cross-reference is disallowed

This method prevents adding a “mentioned in !1” note on every single commit in a merge request. Additionally, it prevents the creation of references to external issues (which would fail).

Parameters:

Returns:

  • (Boolean)


271
272
273
274
275
276
277
# File 'app/services/system_notes/issuables_service.rb', line 271

def cross_reference_disallowed?(mentioned_in)
  return true if noteable.is_a?(ExternalIssue) && !noteable.project&.external_references_supported?
  return false unless mentioned_in.is_a?(MergeRequest)
  return false unless noteable.is_a?(Commit)

  mentioned_in.commits.include?(noteable)
end

#cross_reference_exists?(mentioned_in) ⇒ Boolean

Check if a cross reference to a Mentionable from the ‘mentioned_in` Mentionable already exists.

This method is used to prevent multiple notes being created for a mention when a issue is updated, for example. The method also calls ‘existing_mentions_for` to check if the mention is in a commit, and return matches only on commit hash instead of project + commit, to avoid repeated mentions from forks.

Parameters:

Returns:

  • (Boolean)


393
394
395
396
# File 'app/services/system_notes/issuables_service.rb', line 393

def cross_reference_exists?(mentioned_in)
  notes = noteable.notes.system
  existing_mentions_for(mentioned_in, noteable, notes).exists?
end

#discussion_lockObject



437
438
439
440
441
442
443
444
445
446
447
448
# File 'app/services/system_notes/issuables_service.rb', line 437

def discussion_lock
  action = noteable.discussion_locked? ? 'locked' : 'unlocked'
  body = "#{action} this #{noteable.class.to_s.titleize.downcase}"

  if action == 'locked'
    track_issue_event(:track_issue_locked_action)
  else
    track_issue_event(:track_issue_unlocked_action)
  end

  create_note(NoteSummary.new(noteable, project, author, body, action: action))
end

#hierarchy_changed(work_item, action) ⇒ Object

Called when the hierarchy of a work item is changed

noteable - Noteable object that responds to ‘work_item_parent` and `work_item_children` project - Project owning noteable author - User performing the change

Example Note text:

"added #1 as child Task"

Returns the created Note object



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

def hierarchy_changed(work_item, action)
  params = hierarchy_note_params(action, noteable, work_item)

  create_note(NoteSummary.new(noteable, project, author, params[:parent_note_body], action: params[:parent_action]))
  create_note(NoteSummary.new(work_item, project, author, params[:child_note_body], action: params[:child_action]))
end

#mark_canonical_issue_of_duplicate(duplicate_issue) ⇒ Object

Called when a Noteable has been marked as the canonical Issue of a duplicate

duplicate_issue - Issue that was a duplicate of this

Example Note text:

"marked #1234 as a duplicate of this issue"

"marked other_project#5678 as a duplicate of this issue"

Returns the created Note object



409
410
411
412
# File 'app/services/system_notes/issuables_service.rb', line 409

def mark_canonical_issue_of_duplicate(duplicate_issue)
  body = "marked #{duplicate_issue.to_reference(project)} as a duplicate of this issue"
  create_note(NoteSummary.new(noteable, project, author, body, action: 'duplicate'))
end

#mark_duplicate_issue(canonical_issue) ⇒ Object

Called when a Noteable has been marked as a duplicate of another Issue

canonical_issue - Issue that this is a duplicate of

Example Note text:

"marked this issue as a duplicate of #1234"

"marked this issue as a duplicate of other_project#5678"

Returns the created Note object



425
426
427
428
429
430
431
# File 'app/services/system_notes/issuables_service.rb', line 425

def mark_duplicate_issue(canonical_issue)
  body = "marked this issue as a duplicate of #{canonical_issue.to_reference(project)}"

  track_issue_event(:track_issue_marked_as_duplicate_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'duplicate'))
end

#noteable_cloned(noteable_ref, direction, created_at: nil) ⇒ Object

Called when noteable has been cloned

noteable_ref - Referenced noteable direction - symbol, :to or :from created_at - timestamp for the system note, defaults to current time

Example Note text:

"cloned to some_namespace/project_new#11"

Returns the created Note object



331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/services/system_notes/issuables_service.rb', line 331

def noteable_cloned(noteable_ref, direction, created_at: nil)
  unless [:to, :from].include?(direction)
    raise ArgumentError, "Invalid direction `#{direction}`"
  end

  cross_reference = noteable_ref.to_reference(project)
  body = "cloned #{direction} #{cross_reference}"

  track_issue_event(:track_issue_cloned_action) if direction == :to

  create_note(NoteSummary.new(noteable, project, author, body, action: 'cloned', created_at: created_at))
end

#noteable_moved(noteable_ref, direction) ⇒ Object

Called when noteable has been moved to another project

noteable_ref - Referenced noteable direction - symbol, :to or :from

Example Note text:

"moved to some_namespace/project_new#11"

Returns the created Note object



307
308
309
310
311
312
313
314
315
316
317
318
# File 'app/services/system_notes/issuables_service.rb', line 307

def noteable_moved(noteable_ref, direction)
  unless [:to, :from].include?(direction)
    raise ArgumentError, "Invalid direction `#{direction}`"
  end

  cross_reference = noteable_ref.to_reference(project)
  body = "moved #{direction} #{cross_reference}"

  track_issue_event(:track_issue_moved_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'moved'))
end

#relate_issuable(noteable_ref) ⇒ Object

noteable_ref - Referenced noteable object

Example Note text:

"marked this issue as related to gitlab-foss#9001"

Returns the created Note object



34
35
36
37
38
39
40
# File 'app/services/system_notes/issuables_service.rb', line 34

def relate_issuable(noteable_ref)
  body = "marked this #{noteable_name} as related to #{noteable_ref.to_reference(noteable.resource_parent)}"

  track_issue_event(:track_issue_related_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'relate'))
end

#unrelate_issuable(noteable_ref) ⇒ Object

noteable_ref - Referenced noteable object

Example Note text:

"removed the relation with gitlab-foss#9001"

Returns the created Note object



50
51
52
53
54
55
56
# File 'app/services/system_notes/issuables_service.rb', line 50

def unrelate_issuable(noteable_ref)
  body = "removed the relation with #{noteable_ref.to_reference(noteable.resource_parent)}"

  track_issue_event(:track_issue_unrelated_action)

  create_note(NoteSummary.new(noteable, project, author, body, action: 'unrelate'))
end