debian-mirror-gitlab/app/helpers/compare_helper.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
2.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module CompareHelper
2022-08-27 11:52:29 +05:30
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-09-11 14:41:01 +05:30
from.present? &&
to.present? &&
from != to &&
2021-04-17 20:07:23 +05:30
can?(current_user, :create_merge_request_from, source_project) &&
can?(current_user, :create_merge_request_in, target_project) &&
2022-08-27 11:52:29 +05:30
target_project.repository.branch_exists?(to) &&
source_project.repository.branch_exists?(from)
2014-09-02 18:07:02 +05:30
end
2022-08-27 11:52:29 +05:30
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-09-10 17:25:29 +05:30
project_new_merge_request_path(
2022-08-27 11:52:29 +05:30
source_project,
merge_request: merge_request_params.merge(mr_params)
2015-04-26 12:48:37 +05:30
)
2014-09-02 18:07:02 +05:30
end
2021-04-17 20:07:23 +05:30
def target_projects(source_project)
MergeRequestTargetProjectFinder
.new(current_user: current_user, source_project: source_project, project_feature: :repository)
.execute(include_routes: true)
end
def project_compare_selector_data(project, merge_request, params)
{
project_compare_index_path: project_compare_index_path(project),
2022-08-27 11:52:29 +05:30
source_project: { id: project.id, name: project.full_path }.to_json,
target_project: { id: @target_project.id, name: @target_project.full_path }.to_json,
source_project_refs_path: refs_project_path(project),
target_project_refs_path: refs_project_path(@target_project),
2021-04-17 20:07:23 +05:30
params_from: params[:from],
2022-11-25 23:54:43 +05:30
params_to: params[:to],
straight: params[:straight]
2021-04-17 20:07:23 +05:30
}.tap do |data|
2022-08-27 11:52:29 +05:30
data[:projects_from] = target_projects(project).map do |target_project|
{ id: target_project.id, name: target_project.full_path }
end.to_json
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-04-17 20:07:23 +05:30
end
end
2014-09-02 18:07:02 +05:30
end