diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-23 12:06:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-23 12:06:03 +0300 |
commit | b3e4ec8e8adf4fe96c982124e91b6a05021a9cda (patch) | |
tree | 5fda0011a7cc7de000186e465e61f893d478a1c8 /app/services | |
parent | 90cdc9391171e1be29b2b57a2e2aad0c02c2a7a9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/system_note_service.rb | 87 | ||||
-rw-r--r-- | app/services/system_notes/merge_requests_service.rb | 145 |
2 files changed, 157 insertions, 75 deletions
diff --git a/app/services/system_note_service.rb b/app/services/system_note_service.rb index b3eee01ea7a..25e3282d3fb 100644 --- a/app/services/system_note_service.rb +++ b/app/services/system_note_service.rb @@ -128,82 +128,37 @@ module SystemNoteService # Called when 'merge when pipeline succeeds' is executed def merge_when_pipeline_succeeds(noteable, project, author, sha) - body = "enabled an automatic merge when the pipeline for #{sha} succeeds" - - create_note(NoteSummary.new(noteable, project, author, body, action: 'merge')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).merge_when_pipeline_succeeds(sha) end # Called when 'merge when pipeline succeeds' is canceled def cancel_merge_when_pipeline_succeeds(noteable, project, author) - body = 'canceled the automatic merge' - - create_note(NoteSummary.new(noteable, project, author, body, action: 'merge')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).cancel_merge_when_pipeline_succeeds end # Called when 'merge when pipeline succeeds' is aborted def abort_merge_when_pipeline_succeeds(noteable, project, author, 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')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).abort_merge_when_pipeline_succeeds(reason) end def handle_merge_request_wip(noteable, project, author) - prefix = noteable.work_in_progress? ? "marked" : "unmarked" - - body = "#{prefix} as a **Work In Progress**" - - create_note(NoteSummary.new(noteable, project, author, body, action: 'title')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).handle_merge_request_wip end def add_merge_request_wip_from_commit(noteable, project, author, commit) - body = "marked as a **Work In Progress** from #{commit.to_reference(project)}" - - create_note(NoteSummary.new(noteable, project, author, body, action: 'title')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).add_merge_request_wip_from_commit(commit) end def resolve_all_discussions(merge_request, project, author) - body = "resolved all threads" - - create_note(NoteSummary.new(merge_request, project, author, body, action: 'discussion')) + ::SystemNotes::MergeRequestsService.new(noteable: merge_request, project: project, author: author).resolve_all_discussions end def discussion_continued_in_issue(discussion, project, author, issue) - body = "created #{issue.to_reference} to continue this discussion" - note_attributes = discussion.reply_attributes.merge(project: project, author: author, note: body) - - note = Note.create(note_attributes.merge(system: true, created_at: issue.system_note_timestamp)) - note.system_note_metadata = SystemNoteMetadata.new(action: 'discussion') - - note + ::SystemNotes::MergeRequestsService.new(project: project, author: author).discussion_continued_in_issue(discussion, issue) end def diff_discussion_outdated(discussion, project, author, 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 = Note.create(note_attributes.merge(system: true)) - note.system_note_metadata = SystemNoteMetadata.new(action: 'outdated') - - note + ::SystemNotes::MergeRequestsService.new(project: project, author: author).diff_discussion_outdated(discussion, change_position) end def change_title(noteable, project, author, old_title) @@ -233,9 +188,7 @@ module SystemNoteService # # Returns the created Note object def change_branch(noteable, project, author, branch_type, old_branch, new_branch) - body = "changed #{branch_type} branch from `#{old_branch}` to `#{new_branch}`" - - create_note(NoteSummary.new(noteable, project, author, body, action: 'branch')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).change_branch(branch_type, old_branch, new_branch) end # Called when a branch in Noteable is added or deleted @@ -253,16 +206,7 @@ module SystemNoteService # # Returns the created Note object def change_branch_presence(noteable, project, author, 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')) + ::SystemNotes::MergeRequestsService.new(noteable: noteable, project: project, author: author).change_branch_presence(branch_type, branch, presence) end # Called when a branch is created from the 'new branch' button on a issue @@ -270,18 +214,11 @@ module SystemNoteService # # "created branch `201-issue-branch-button`" def new_issue_branch(issue, project, author, 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(issue, project, author, body, action: 'branch')) + ::SystemNotes::MergeRequestsService.new(noteable: issue, project: project, author: author).new_issue_branch(branch, branch_project: branch_project) end def new_merge_request(issue, project, author, merge_request) - body = "created merge request #{merge_request.to_reference(project)} to address this issue" - - create_note(NoteSummary.new(issue, project, author, body, action: 'merge')) + ::SystemNotes::MergeRequestsService.new(noteable: issue, project: project, author: author).new_merge_request(merge_request) end def cross_reference(noteable, mentioner, author) diff --git a/app/services/system_notes/merge_requests_service.rb b/app/services/system_notes/merge_requests_service.rb new file mode 100644 index 00000000000..1d17f0ded57 --- /dev/null +++ b/app/services/system_notes/merge_requests_service.rb @@ -0,0 +1,145 @@ +# frozen_string_literal: true + +module SystemNotes + class MergeRequestsService < ::SystemNotes::BaseService + # Called when 'merge when pipeline succeeds' is executed + 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 + + # Called when 'merge when pipeline succeeds' is canceled + def cancel_merge_when_pipeline_succeeds + body = 'canceled the automatic merge' + + create_note(NoteSummary.new(noteable, project, author, body, action: 'merge')) + end + + # Called when 'merge when pipeline succeeds' is aborted + 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 + + def handle_merge_request_wip + prefix = noteable.work_in_progress? ? "marked" : "unmarked" + + body = "#{prefix} as a **Work In Progress**" + + create_note(NoteSummary.new(noteable, project, author, body, action: 'title')) + end + + def add_merge_request_wip_from_commit(commit) + body = "marked as a **Work In Progress** from #{commit.to_reference(project)}" + + create_note(NoteSummary.new(noteable, project, author, body, action: 'title')) + end + + def resolve_all_discussions + body = "resolved all threads" + + create_note(NoteSummary.new(noteable, project, author, body, action: 'discussion')) + end + + 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.system_note_metadata = SystemNoteMetadata.new(action: 'discussion') + end + end + + 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.system_note_metadata = SystemNoteMetadata.new(action: 'outdated') + end + end + + # Called when a branch in Noteable is changed + # + # branch_type - 'source' or 'target' + # old_branch - old branch name + # new_branch - new branch name + # + # Example Note text: + # + # "changed target branch from `Old` to `New`" + # + # Returns the created Note object + def change_branch(branch_type, old_branch, new_branch) + body = "changed #{branch_type} branch from `#{old_branch}` to `#{new_branch}`" + + create_note(NoteSummary.new(noteable, project, author, body, action: 'branch')) + end + + # 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 + 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 + + # Called when a branch is created from the 'new branch' button on a issue + # Example note text: + # + # "created branch `201-issue-branch-button`" + 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 + + 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 + end +end + +SystemNotes::MergeRequestsService.prepend_if_ee('::EE::SystemNotes::MergeRequestsService') |