2018-07-24 10:00:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
class MergeRequestPresenter < Gitlab::View::Presenter::Delegated
|
|
|
|
include GitlabRoutingHelper
|
|
|
|
include MarkupHelper
|
|
|
|
include TreeHelper
|
2018-04-06 14:02:36 +00:00
|
|
|
include ChecksCollaboration
|
2022-02-17 15:18:08 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
delegator_override_with Gitlab::Utils::StrongMemoize # This module inclusion is expected. See https://gitlab.com/gitlab-org/gitlab/-/issues/352884.
|
2021-09-21 09:12:21 +00:00
|
|
|
|
2020-07-15 18:09:09 +00:00
|
|
|
APPROVALS_WIDGET_BASE_TYPE = 'base'
|
|
|
|
|
2021-09-21 09:12:21 +00:00
|
|
|
presents ::MergeRequest, as: :merge_request
|
2017-05-09 04:15:34 +00:00
|
|
|
|
|
|
|
def ci_status
|
|
|
|
if pipeline
|
|
|
|
status = pipeline.status
|
2019-04-12 12:39:10 +00:00
|
|
|
status = "success-with-warnings" if pipeline.success? && pipeline.has_warnings?
|
2017-05-09 04:15:34 +00:00
|
|
|
|
|
|
|
status || "preparing"
|
|
|
|
else
|
2021-06-30 06:07:17 +00:00
|
|
|
ci_integration = source_project.try(:ci_integration)
|
|
|
|
ci_integration&.commit_status(diff_head_sha, source_branch)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-22 11:45:27 +00:00
|
|
|
def cancel_auto_merge_path
|
|
|
|
if can_cancel_auto_merge?(current_user)
|
|
|
|
cancel_auto_merge_project_merge_request_path(project, merge_request)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_issue_to_resolve_discussions_path
|
|
|
|
if can?(current_user, :create_issue, project) && project.issues_enabled?
|
2017-06-29 17:06:35 +00:00
|
|
|
new_project_issue_path(project, merge_request_to_resolve_discussions_of: iid)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_wip_path
|
2020-10-01 12:10:14 +00:00
|
|
|
if can?(current_user, :update_merge_request, merge_request.project)
|
2017-06-29 17:06:35 +00:00
|
|
|
remove_wip_project_merge_request_path(project, merge_request)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_path
|
|
|
|
if can_be_merged_by?(current_user)
|
2017-06-29 17:06:35 +00:00
|
|
|
merge_project_merge_request_path(project, merge_request)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_in_fork_path
|
2018-02-07 17:30:41 +00:00
|
|
|
if user_can_fork_project? && cached_can_be_reverted?
|
2017-05-09 04:15:34 +00:00
|
|
|
continue_params = {
|
|
|
|
to: merge_request_path(merge_request),
|
2019-04-16 10:32:05 +00:00
|
|
|
notice: _('%{edit_in_new_fork_notice} Try to cherry-pick this commit again.') % { edit_in_new_fork_notice: edit_in_new_fork_notice },
|
2017-05-09 04:15:34 +00:00
|
|
|
notice_now: edit_in_new_fork_notice_now
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:06:35 +00:00
|
|
|
project_forks_path(merge_request.project,
|
2017-05-09 04:15:34 +00:00
|
|
|
namespace_key: current_user.namespace.id,
|
|
|
|
continue: continue_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cherry_pick_in_fork_path
|
|
|
|
if user_can_fork_project? && can_be_cherry_picked?
|
|
|
|
continue_params = {
|
|
|
|
to: merge_request_path(merge_request),
|
2019-04-16 10:32:05 +00:00
|
|
|
notice: _('%{edit_in_new_fork_notice} Try to revert this commit again.') % { edit_in_new_fork_notice: edit_in_new_fork_notice },
|
2017-05-09 04:15:34 +00:00
|
|
|
notice_now: edit_in_new_fork_notice_now
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:06:35 +00:00
|
|
|
project_forks_path(project,
|
2017-05-09 04:15:34 +00:00
|
|
|
namespace_key: current_user.namespace.id,
|
|
|
|
continue: continue_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def conflict_resolution_path
|
2017-05-11 15:23:02 +00:00
|
|
|
if conflicts.can_be_resolved_in_ui? && conflicts.can_be_resolved_by?(current_user)
|
2017-06-29 17:06:35 +00:00
|
|
|
conflicts_project_merge_request_path(project, merge_request)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-20 09:01:21 +00:00
|
|
|
def rebase_path
|
2018-02-28 08:06:18 +00:00
|
|
|
if !rebase_in_progress? && should_be_rebased? && can_push_to_source_branch?
|
2017-12-20 09:01:21 +00:00
|
|
|
rebase_project_merge_request_path(project, merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 14:48:59 +00:00
|
|
|
def target_branch_tree_path
|
2017-05-09 04:15:34 +00:00
|
|
|
if target_branch_exists?
|
2017-07-11 14:48:59 +00:00
|
|
|
project_tree_path(project, target_branch)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 16:49:22 +00:00
|
|
|
def target_branch_commits_path
|
|
|
|
if target_branch_exists?
|
|
|
|
project_commits_path(project, target_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 10:05:04 +00:00
|
|
|
def target_branch_path
|
|
|
|
if target_branch_exists?
|
|
|
|
project_branch_path(project, target_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 08:34:38 +00:00
|
|
|
def source_branch_commits_path
|
|
|
|
if source_branch_exists?
|
|
|
|
project_commits_path(source_project, source_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
def source_branch_path
|
|
|
|
if source_branch_exists?
|
2017-06-29 17:06:35 +00:00
|
|
|
project_branch_path(source_project, source_branch)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_branch_with_namespace_link
|
|
|
|
namespace = source_project_namespace
|
|
|
|
branch = source_branch
|
|
|
|
|
2018-10-12 21:39:13 +00:00
|
|
|
namespace_link = source_branch_exists? ? link_to(namespace, project_path(source_project)) : ERB::Util.html_escape(namespace)
|
|
|
|
branch_link = source_branch_exists? ? link_to(branch, project_tree_path(source_project, source_branch)) : ERB::Util.html_escape(branch)
|
2017-05-09 04:15:34 +00:00
|
|
|
|
2018-10-12 21:39:13 +00:00
|
|
|
for_fork? ? "#{namespace_link}:#{branch_link}" : branch_link
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def closing_issues_links
|
2017-06-08 16:09:04 +00:00
|
|
|
markdown(
|
|
|
|
issues_sentence(project, closing_issues),
|
|
|
|
pipeline: :gfm,
|
|
|
|
author: author,
|
|
|
|
project: project,
|
2021-11-24 12:10:21 +00:00
|
|
|
issuable_reference_expansion_enabled: true
|
2017-06-08 16:09:04 +00:00
|
|
|
)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mentioned_issues_links
|
2017-06-08 16:09:04 +00:00
|
|
|
markdown(
|
|
|
|
issues_sentence(project, mentioned_issues),
|
|
|
|
pipeline: :gfm,
|
|
|
|
author: author,
|
|
|
|
project: project,
|
2021-11-24 12:10:21 +00:00
|
|
|
issuable_reference_expansion_enabled: true
|
2017-06-08 16:09:04 +00:00
|
|
|
)
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
2022-03-14 15:09:32 +00:00
|
|
|
def assign_to_closing_issues_path
|
|
|
|
assign_related_issues_project_merge_request_path(project, merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_to_closing_issues_count
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2021-05-12 03:10:21 +00:00
|
|
|
issues = MergeRequests::AssignIssuesService.new(project: project,
|
|
|
|
current_user: current_user,
|
|
|
|
params: {
|
|
|
|
merge_request: merge_request,
|
|
|
|
closes_issues: closing_issues
|
|
|
|
}).assignable_issues
|
2022-03-14 15:09:32 +00:00
|
|
|
issues.count
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_revert_on_current_merge_request?
|
2018-04-06 14:02:36 +00:00
|
|
|
can_collaborate_with_project?(project) && cached_can_be_reverted?
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_cherry_pick_on_current_merge_request?
|
2018-04-06 14:02:36 +00:00
|
|
|
can_collaborate_with_project?(project) && can_be_cherry_picked?
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
2017-12-20 09:01:21 +00:00
|
|
|
def can_push_to_source_branch?
|
2018-02-28 08:06:18 +00:00
|
|
|
return false unless source_branch_exists?
|
|
|
|
|
|
|
|
!!::Gitlab::UserAccess
|
2020-07-21 18:09:45 +00:00
|
|
|
.new(current_user, container: source_project)
|
2018-02-28 08:06:18 +00:00
|
|
|
.can_push_to_branch?(source_branch)
|
2017-12-20 09:01:21 +00:00
|
|
|
end
|
|
|
|
|
2021-09-21 09:12:21 +00:00
|
|
|
delegator_override :can_remove_source_branch?
|
2018-06-25 08:59:00 +00:00
|
|
|
def can_remove_source_branch?
|
|
|
|
source_branch_exists? && merge_request.can_remove_source_branch?(current_user)
|
|
|
|
end
|
|
|
|
|
2019-01-28 12:12:30 +00:00
|
|
|
def can_read_pipeline?
|
|
|
|
pipeline && can?(current_user, :read_pipeline, pipeline)
|
|
|
|
end
|
|
|
|
|
2018-05-21 07:52:24 +00:00
|
|
|
def mergeable_discussions_state
|
2022-04-08 03:09:41 +00:00
|
|
|
# This avoids calling MergeRequest#mergeable_discussions_state without
|
|
|
|
# considering the state of the MR first. If a MR isn't mergeable, we can
|
|
|
|
# safely short-circuit it.
|
|
|
|
if merge_request.mergeable_state?(skip_ci_check: true, skip_discussions_check: true)
|
2018-05-21 07:52:24 +00:00
|
|
|
merge_request.mergeable_discussions_state?
|
|
|
|
else
|
2022-04-08 03:09:41 +00:00
|
|
|
false
|
2018-05-21 07:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-21 09:12:21 +00:00
|
|
|
delegator_override :subscribed?
|
2018-05-21 07:52:24 +00:00
|
|
|
def subscribed?
|
|
|
|
merge_request.subscribed?(current_user, merge_request.target_project)
|
|
|
|
end
|
|
|
|
|
2019-04-29 07:29:54 +00:00
|
|
|
def source_branch_link
|
|
|
|
if source_branch_exists?
|
|
|
|
link_to(source_branch, source_branch_commits_path, class: 'ref-name')
|
|
|
|
else
|
|
|
|
content_tag(:span, source_branch, class: 'ref-name')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_branch_link
|
|
|
|
if target_branch_exists?
|
|
|
|
link_to(target_branch, target_branch_commits_path, class: 'ref-name')
|
|
|
|
else
|
|
|
|
content_tag(:span, target_branch, class: 'ref-name')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-15 18:09:09 +00:00
|
|
|
def api_approvals_path
|
|
|
|
expose_path(api_v4_projects_merge_requests_approvals_path(id: project.id, merge_request_iid: merge_request.iid))
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_approve_path
|
|
|
|
expose_path(api_v4_projects_merge_requests_approve_path(id: project.id, merge_request_iid: merge_request.iid))
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_unapprove_path
|
|
|
|
expose_path(api_v4_projects_merge_requests_unapprove_path(id: project.id, merge_request_iid: merge_request.iid))
|
|
|
|
end
|
|
|
|
|
|
|
|
def approvals_widget_type
|
|
|
|
APPROVALS_WIDGET_BASE_TYPE
|
|
|
|
end
|
|
|
|
|
2021-08-16 18:10:51 +00:00
|
|
|
def closing_issues
|
|
|
|
strong_memoize(:closing_issues) do
|
|
|
|
visible_closing_issues_for(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mentioned_issues
|
|
|
|
strong_memoize(:mentioned_issues) do
|
|
|
|
issues_mentioned_but_not_closing(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-06 18:14:09 +00:00
|
|
|
delegator_override :pipeline_coverage_delta
|
|
|
|
def pipeline_coverage_delta
|
|
|
|
return unless merge_request.pipeline_coverage_delta.present?
|
|
|
|
|
|
|
|
'%.2f' % merge_request.pipeline_coverage_delta
|
|
|
|
end
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
private
|
|
|
|
|
2018-02-07 17:30:41 +00:00
|
|
|
def cached_can_be_reverted?
|
2018-02-07 16:32:32 +00:00
|
|
|
strong_memoize(:can_be_reverted) do
|
|
|
|
can_be_reverted?(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 15:23:02 +00:00
|
|
|
def conflicts
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-05-11 15:23:02 +00:00
|
|
|
@conflicts ||= MergeRequests::Conflicts::ListService.new(merge_request)
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-05-11 15:23:02 +00:00
|
|
|
end
|
|
|
|
|
2017-05-09 04:15:34 +00:00
|
|
|
def pipeline
|
2017-12-01 13:26:51 +00:00
|
|
|
@pipeline ||= actual_head_pipeline
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def issues_sentence(project, issues)
|
|
|
|
# Sorting based on the `#123` or `group/project#123` reference will sort
|
|
|
|
# local issues first.
|
|
|
|
issues.map do |issue|
|
|
|
|
issue.to_reference(project)
|
|
|
|
end.sort.to_sentence
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_can_fork_project?
|
|
|
|
can?(current_user, :fork_project, project)
|
|
|
|
end
|
2022-02-18 03:17:36 +00:00
|
|
|
|
|
|
|
# Avoid including ActionView::Helpers::UrlHelper
|
|
|
|
def link_to(*args)
|
|
|
|
ApplicationController.helpers.link_to(*args)
|
|
|
|
end
|
2017-05-09 04:15:34 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
MergeRequestPresenter.prepend_mod_with('MergeRequestPresenter')
|