2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-12 13:15:10 -04:00
|
|
|
module CompareHelper
|
2022-07-25 17:08:32 -04:00
|
|
|
def create_mr_button?(source_project:, from:, to: nil, target_project: nil)
|
|
|
|
target_project ||= source_project.default_merge_request_target
|
|
|
|
to ||= target_project.default_branch
|
|
|
|
|
2015-05-09 17:51:50 -04:00
|
|
|
from.present? &&
|
2015-05-09 16:26:25 -04:00
|
|
|
to.present? &&
|
|
|
|
from != to &&
|
2021-02-22 10:10:48 -05:00
|
|
|
can?(current_user, :create_merge_request_from, source_project) &&
|
|
|
|
can?(current_user, :create_merge_request_in, target_project) &&
|
2022-07-25 17:08:32 -04:00
|
|
|
target_project.repository.branch_exists?(to) &&
|
|
|
|
source_project.repository.branch_exists?(from)
|
2013-09-12 13:15:10 -04:00
|
|
|
end
|
|
|
|
|
2022-07-25 17:08:32 -04:00
|
|
|
def create_mr_path(from:, source_project:, to: nil, target_project: nil, mr_params: {})
|
|
|
|
merge_request_params = {
|
|
|
|
source_branch: from
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_request_params[:target_project_id] = target_project.id if target_project
|
|
|
|
merge_request_params[:target_branch] = to if to
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
project_new_merge_request_path(
|
2022-07-25 17:08:32 -04:00
|
|
|
source_project,
|
|
|
|
merge_request: merge_request_params.merge(mr_params)
|
2015-01-24 13:02:58 -05:00
|
|
|
)
|
2013-09-12 13:15:10 -04:00
|
|
|
end
|
2021-02-22 10:10:48 -05:00
|
|
|
|
|
|
|
def target_projects(source_project)
|
|
|
|
MergeRequestTargetProjectFinder
|
|
|
|
.new(current_user: current_user, source_project: source_project, project_feature: :repository)
|
|
|
|
.execute(include_routes: true)
|
|
|
|
end
|
2021-02-24 16:11:16 -05:00
|
|
|
|
|
|
|
def project_compare_selector_data(project, merge_request, params)
|
|
|
|
{
|
|
|
|
project_compare_index_path: project_compare_index_path(project),
|
2022-11-09 10:07:50 -05:00
|
|
|
source_project: { id: project.id, name: project.full_path }.to_json,
|
|
|
|
target_project: { id: @target_project.id, name: @target_project.full_path }.to_json,
|
2022-07-25 17:08:32 -04:00
|
|
|
source_project_refs_path: refs_project_path(project),
|
|
|
|
target_project_refs_path: refs_project_path(@target_project),
|
2021-02-24 16:11:16 -05:00
|
|
|
params_from: params[:from],
|
2022-09-29 08:08:49 -04:00
|
|
|
params_to: params[:to],
|
|
|
|
straight: params[:straight]
|
2021-02-24 16:11:16 -05:00
|
|
|
}.tap do |data|
|
2022-11-09 10:07:50 -05:00
|
|
|
data[:projects_from] = target_projects(project).map do |target_project|
|
2022-07-25 17:08:32 -04:00
|
|
|
{ id: target_project.id, name: target_project.full_path }
|
2022-11-09 10:07:50 -05:00
|
|
|
end.to_json
|
2022-07-25 17:08:32 -04:00
|
|
|
|
|
|
|
data[:project_merge_request_path] =
|
|
|
|
if merge_request.present?
|
|
|
|
project_merge_request_path(project, merge_request)
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
# The `from` and `to` params are inverted in the compare page. The route is `/compare/:from...:to`, but the UI
|
|
|
|
# correctly shows `:to` as the "Source" (i.e. the `from` for MR), and `:from` as "Target" (i.e. the `to` for MR).
|
|
|
|
data[:create_mr_path] =
|
|
|
|
if create_mr_button?(from: params[:to], to: params[:from], source_project: project, target_project: @target_project)
|
|
|
|
create_mr_path(from: params[:to], to: params[:from], source_project: project, target_project: @target_project)
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2021-02-24 16:11:16 -05:00
|
|
|
end
|
|
|
|
end
|
2013-09-12 13:15:10 -04:00
|
|
|
end
|