2018-07-24 06:00:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
class MergeRequestPresenter < Gitlab::View::Presenter::Delegated
|
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include GitlabRoutingHelper
|
|
|
|
include MarkupHelper
|
|
|
|
include TreeHelper
|
2018-04-06 10:02:36 -04:00
|
|
|
include ChecksCollaboration
|
2018-02-07 11:32:32 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
presents :merge_request
|
|
|
|
|
|
|
|
def ci_status
|
|
|
|
if pipeline
|
|
|
|
status = pipeline.status
|
|
|
|
status = "success_with_warnings" if pipeline.success? && pipeline.has_warnings?
|
|
|
|
|
|
|
|
status || "preparing"
|
|
|
|
else
|
|
|
|
ci_service = source_project.try(:ci_service)
|
|
|
|
ci_service&.commit_status(diff_head_sha, source_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancel_merge_when_pipeline_succeeds_path
|
|
|
|
if can_cancel_merge_when_pipeline_succeeds?(current_user)
|
2017-06-29 13:06:35 -04:00
|
|
|
cancel_merge_when_pipeline_succeeds_project_merge_request_path(project, merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_issue_to_resolve_discussions_path
|
|
|
|
if can?(current_user, :create_issue, project) && project.issues_enabled?
|
2017-06-29 13:06:35 -04:00
|
|
|
new_project_issue_path(project, merge_request_to_resolve_discussions_of: iid)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_wip_path
|
2017-10-03 17:13:13 -04:00
|
|
|
if work_in_progress? && can?(current_user, :update_merge_request, merge_request.project)
|
2017-06-29 13:06:35 -04:00
|
|
|
remove_wip_project_merge_request_path(project, merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_path
|
|
|
|
if can_be_merged_by?(current_user)
|
2017-06-29 13:06:35 -04:00
|
|
|
merge_project_merge_request_path(project, merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_in_fork_path
|
2018-02-07 12:30:41 -05:00
|
|
|
if user_can_fork_project? && cached_can_be_reverted?
|
2017-05-09 00:15:34 -04:00
|
|
|
continue_params = {
|
|
|
|
to: merge_request_path(merge_request),
|
|
|
|
notice: "#{edit_in_new_fork_notice} Try to cherry-pick this commit again.",
|
|
|
|
notice_now: edit_in_new_fork_notice_now
|
|
|
|
}
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
project_forks_path(merge_request.project,
|
2017-05-09 00:15:34 -04: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),
|
|
|
|
notice: "#{edit_in_new_fork_notice} Try to revert this commit again.",
|
|
|
|
notice_now: edit_in_new_fork_notice_now
|
|
|
|
}
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
project_forks_path(project,
|
2017-05-09 00:15:34 -04:00
|
|
|
namespace_key: current_user.namespace.id,
|
|
|
|
continue: continue_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def conflict_resolution_path
|
2017-05-11 11:23:02 -04:00
|
|
|
if conflicts.can_be_resolved_in_ui? && conflicts.can_be_resolved_by?(current_user)
|
2017-06-29 13:06:35 -04:00
|
|
|
conflicts_project_merge_request_path(project, merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
def rebase_path
|
2018-02-28 03:06:18 -05:00
|
|
|
if !rebase_in_progress? && should_be_rebased? && can_push_to_source_branch?
|
2017-12-20 04:01:21 -05:00
|
|
|
rebase_project_merge_request_path(project, merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 10:48:59 -04:00
|
|
|
def target_branch_tree_path
|
2017-05-09 00:15:34 -04:00
|
|
|
if target_branch_exists?
|
2017-07-11 10:48:59 -04:00
|
|
|
project_tree_path(project, target_branch)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 12:49:22 -04:00
|
|
|
def target_branch_commits_path
|
|
|
|
if target_branch_exists?
|
|
|
|
project_commits_path(project, target_branch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def source_branch_path
|
|
|
|
if source_branch_exists?
|
2017-06-29 13:06:35 -04:00
|
|
|
project_branch_path(source_project, source_branch)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_branch_with_namespace_link
|
|
|
|
namespace = source_project_namespace
|
|
|
|
branch = source_branch
|
|
|
|
|
2018-10-12 17:39:13 -04: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 00:15:34 -04:00
|
|
|
|
2018-10-12 17:39:13 -04:00
|
|
|
for_fork? ? "#{namespace_link}:#{branch_link}" : branch_link
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def closing_issues_links
|
2017-06-08 12:09:04 -04:00
|
|
|
markdown(
|
|
|
|
issues_sentence(project, closing_issues),
|
|
|
|
pipeline: :gfm,
|
|
|
|
author: author,
|
|
|
|
project: project,
|
|
|
|
issuable_state_filter_enabled: true
|
|
|
|
)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def mentioned_issues_links
|
|
|
|
mentioned_issues = issues_mentioned_but_not_closing(current_user)
|
2017-06-08 12:09:04 -04:00
|
|
|
markdown(
|
|
|
|
issues_sentence(project, mentioned_issues),
|
|
|
|
pipeline: :gfm,
|
|
|
|
author: author,
|
|
|
|
project: project,
|
|
|
|
issuable_state_filter_enabled: true
|
|
|
|
)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assign_to_closing_issues_link
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-05-09 00:15:34 -04:00
|
|
|
issues = MergeRequests::AssignIssuesService.new(project,
|
|
|
|
current_user,
|
|
|
|
merge_request: merge_request,
|
|
|
|
closes_issues: closing_issues
|
|
|
|
).assignable_issues
|
2017-06-29 13:06:35 -04:00
|
|
|
path = assign_related_issues_project_merge_request_path(project, merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
if issues.present?
|
|
|
|
pluralize_this_issue = issues.count > 1 ? "these issues" : "this issue"
|
|
|
|
link_to "Assign yourself to #{pluralize_this_issue}", path, method: :post
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_revert_on_current_merge_request?
|
2018-04-06 10:02:36 -04:00
|
|
|
can_collaborate_with_project?(project) && cached_can_be_reverted?
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_cherry_pick_on_current_merge_request?
|
2018-04-06 10:02:36 -04:00
|
|
|
can_collaborate_with_project?(project) && can_be_cherry_picked?
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
def can_push_to_source_branch?
|
2018-02-28 03:06:18 -05:00
|
|
|
return false unless source_branch_exists?
|
|
|
|
|
|
|
|
!!::Gitlab::UserAccess
|
|
|
|
.new(current_user, project: source_project)
|
|
|
|
.can_push_to_branch?(source_branch)
|
2017-12-20 04:01:21 -05:00
|
|
|
end
|
|
|
|
|
2018-06-25 04:59:00 -04:00
|
|
|
def can_remove_source_branch?
|
|
|
|
source_branch_exists? && merge_request.can_remove_source_branch?(current_user)
|
|
|
|
end
|
|
|
|
|
2018-05-21 03:52:24 -04:00
|
|
|
def mergeable_discussions_state
|
|
|
|
# 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)
|
|
|
|
merge_request.mergeable_discussions_state?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def web_url
|
|
|
|
Gitlab::UrlBuilder.build(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def subscribed?
|
|
|
|
merge_request.subscribed?(current_user, merge_request.target_project)
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
private
|
|
|
|
|
2018-02-07 12:30:41 -05:00
|
|
|
def cached_can_be_reverted?
|
2018-02-07 11:32:32 -05:00
|
|
|
strong_memoize(:can_be_reverted) do
|
|
|
|
can_be_reverted?(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 11:23:02 -04:00
|
|
|
def conflicts
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-05-11 11:23:02 -04:00
|
|
|
@conflicts ||= MergeRequests::Conflicts::ListService.new(merge_request)
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-05-11 11:23:02 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def closing_issues
|
2018-07-30 14:52:42 -04:00
|
|
|
@closing_issues ||= visible_closing_issues_for(current_user)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline
|
2017-12-01 08:26:51 -05:00
|
|
|
@pipeline ||= actual_head_pipeline
|
2017-05-09 00:15:34 -04: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
|
|
|
|
end
|