Class: SystemNotes::MergeRequestsService

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

Instance Attribute Summary

Attributes inherited from BaseService

#author, #noteable, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from SystemNotes::BaseService

Instance Method Details

#abort_merge_when_pipeline_succeeds(reason) ⇒ Object

Called when ‘merge when pipeline succeeds’ is aborted



20
21
22
23
24
25
26
27
# File 'app/services/system_notes/merge_requests_service.rb', line 20

def abort_merge_when_pipeline_succeeds(reason)
  body = "aborted the automatic merge because #{reason}"

  ##
  # TODO: Abort message should be sent by the system, not a particular user.
  # See https://gitlab.com/gitlab-org/gitlab-foss/issues/63187.
  create_note(NoteSummary.new(noteable, project, author, body, action: 'merge'))
end

#add_merge_request_draft_from_commit(commit) ⇒ Object



37
38
39
40
41
# File 'app/services/system_notes/merge_requests_service.rb', line 37

def add_merge_request_draft_from_commit(commit)
  body = "marked this merge request as **draft** from #{commit.to_reference(project)}"

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

#approve_mrObject

Called when the merge request is approved by user

Example Note text:

"approved this merge request"

Returns the created Note object



171
172
173
174
175
# File 'app/services/system_notes/merge_requests_service.rb', line 171

def approve_mr
  body = "approved this merge request"

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

#cancel_merge_when_pipeline_succeedsObject

Called when ‘merge when pipeline succeeds’ is canceled



13
14
15
16
17
# File 'app/services/system_notes/merge_requests_service.rb', line 13

def cancel_merge_when_pipeline_succeeds
  body = 'canceled the automatic merge'

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

#change_branch(branch_type, event_type, old_branch, new_branch) ⇒ Object

Example Note text is based on event_type:

update: "changed target branch from `Old` to `New`"
delete: "deleted the `Old` branch. This merge request now targets the `New` branch"

Returns the created Note object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/services/system_notes/merge_requests_service.rb', line 96

def change_branch(branch_type, event_type, old_branch, new_branch)
  body =
    case event_type.to_s
    when 'delete'
      "deleted the `#{old_branch}` branch. This merge request now targets the `#{new_branch}` branch"
    when 'update'
      "changed #{branch_type} branch from `#{old_branch}` to `#{new_branch}`"
    else
      raise ArgumentError, "invalid value for event_type: #{event_type}"
    end

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

#change_branch_presence(branch_type, branch, presence) ⇒ Object

Called when a branch in Noteable is added or deleted

branch_type - :source or :target branch - branch name presence - :add or :delete

Example Note text:

"restored target branch `feature`"

Returns the created Note object



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

def change_branch_presence(branch_type, branch, presence)
  verb =
    if presence == :add
      'restored'
    else
      'deleted'
    end

  body = "#{verb} #{branch_type} branch `#{branch}`"

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

#diff_discussion_outdated(discussion, change_position) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/services/system_notes/merge_requests_service.rb', line 58

def diff_discussion_outdated(discussion, change_position)
  merge_request = discussion.noteable
  diff_refs = change_position.diff_refs
  version_index = merge_request.merge_request_diffs.viewable.count
  position_on_text = change_position.on_text?
  text_parts = ["changed this #{position_on_text ? 'line' : 'file'} in"]

  if version_params = merge_request.version_params_for(diff_refs)
    repository = project.repository
    anchor = position_on_text ? change_position.line_code(repository) : change_position.file_hash
    url = url_helpers.diffs_project_merge_request_path(project, merge_request, version_params.merge(anchor: anchor))

    text_parts << "[version #{version_index} of the diff](#{url})"
  else
    text_parts << "version #{version_index} of the diff"
  end

  body = text_parts.join(' ')
  note_attributes = discussion.reply_attributes.merge(project: project, author: author, note: body)

  Note.create(note_attributes.merge(system: true)).tap do |note|
    note. = SystemNoteMetadata.new(action: 'outdated')
  end
end

#discussion_continued_in_issue(discussion, issue) ⇒ Object



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

def discussion_continued_in_issue(discussion, issue)
  body = "created #{issue.to_reference} to continue this discussion"
  note_attributes = discussion.reply_attributes.merge(project: project, author: author, note: body)

  Note.create(note_attributes.merge(system: true, created_at: issue.system_note_timestamp)).tap do |note|
    note. = SystemNoteMetadata.new(action: 'discussion')
  end
end

#handle_merge_request_draftObject



29
30
31
32
33
34
35
# File 'app/services/system_notes/merge_requests_service.rb', line 29

def handle_merge_request_draft
  action = noteable.draft? ? "draft" : "ready"

  body = "marked this merge request as **#{action}**"

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

#merge_when_pipeline_succeeds(sha) ⇒ Object

Called when ‘merge when pipeline succeeds’ is executed



6
7
8
9
10
# File 'app/services/system_notes/merge_requests_service.rb', line 6

def merge_when_pipeline_succeeds(sha)
  body = "enabled an automatic merge when the pipeline for #{sha} succeeds"

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

#new_issue_branch(branch, branch_project: nil) ⇒ Object

Called when a branch is created from the ‘new branch’ button on a issue Example note text:

"created branch `201-issue-branch-button`"


138
139
140
141
142
143
144
145
# File 'app/services/system_notes/merge_requests_service.rb', line 138

def new_issue_branch(branch, branch_project: nil)
  branch_project ||= project
  link = url_helpers.project_compare_path(branch_project, from: branch_project.default_branch, to: branch)

  body = "created branch [`#{branch}`](#{link}) to address this issue"

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

#new_merge_request(merge_request) ⇒ Object



147
148
149
150
151
# File 'app/services/system_notes/merge_requests_service.rb', line 147

def new_merge_request(merge_request)
  body = "created merge request #{merge_request.to_reference(project)} to address this issue"

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

#picked_into_branch(branch_name, pick_commit) ⇒ Object



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

def picked_into_branch(branch_name, pick_commit)
  link = url_helpers.project_tree_path(project, branch_name)

  body = "picked the changes into the branch [`#{branch_name}`](#{link}) with commit #{pick_commit}"

  summary = NoteSummary.new(noteable, project, author, body, action: 'cherry_pick')
  summary.note[:commit_id] = pick_commit

  create_note(summary)
end

#resolve_all_discussionsObject



43
44
45
46
47
# File 'app/services/system_notes/merge_requests_service.rb', line 43

def resolve_all_discussions
  body = "resolved all threads"

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

#unapprove_mrObject



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

def unapprove_mr
  body = "unapproved this merge request"

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