debian-mirror-gitlab/app/controllers/projects/merge_requests/creations_controller.rb

148 lines
4.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
class Projects::MergeRequests::CreationsController < Projects::MergeRequests::ApplicationController
include DiffForPath
include DiffHelper
2018-03-17 18:26:18 +05:30
include RendersCommits
2017-09-10 17:25:29 +05:30
skip_before_action :merge_request
2018-05-09 12:01:36 +05:30
before_action :authorize_create_merge_request_from!
2017-09-10 17:25:29 +05:30
before_action :apply_diff_view_cookie!, only: [:diffs, :diff_for_path]
before_action :build_merge_request, except: [:create]
2022-01-26 12:08:38 +05:30
urgency :low, [
:new,
:create,
:pipelines,
:diffs,
:branch_from,
:branch_to
]
2017-09-10 17:25:29 +05:30
def new
2020-03-13 15:44:24 +05:30
define_new_vars
2017-09-10 17:25:29 +05:30
end
def create
2021-06-08 01:23:25 +05:30
@merge_request = ::MergeRequests::CreateService.new(project: project, current_user: current_user, params: merge_request_params).execute
2017-09-10 17:25:29 +05:30
if @merge_request.valid?
2019-10-12 21:52:04 +05:30
incr_count_webide_merge_request
2017-09-10 17:25:29 +05:30
redirect_to(merge_request_path(@merge_request))
else
@source_project = @merge_request.source_project
@target_project = @merge_request.target_project
define_new_vars
render action: "new"
end
end
def pipelines
2020-07-28 23:09:34 +05:30
@pipelines = Ci::PipelinesForMergeRequestFinder.new(@merge_request, current_user).execute
2017-09-10 17:25:29 +05:30
Gitlab::PollingInterval.set_header(response, interval: 10_000)
render json: {
pipelines: PipelineSerializer
2020-07-28 23:09:34 +05:30
.new(project: @project, current_user: current_user)
2017-09-10 17:25:29 +05:30
.represent(@pipelines)
}
end
def diffs
2018-03-17 18:26:18 +05:30
@diffs = @merge_request.diffs(diff_options) if @merge_request.can_be_created
2017-09-10 17:25:29 +05:30
@diff_notes_disabled = true
2022-03-02 08:16:31 +05:30
render json: { html: view_to_html_string('projects/merge_requests/creations/_diffs', diffs: @diffs) }
2017-09-10 17:25:29 +05:30
end
def diff_for_path
@diffs = @merge_request.diffs(diff_options)
@diff_notes_disabled = true
render_diff_for_path(@diffs)
end
def branch_from
# This is always source
@source_project = @merge_request.nil? ? @project : @merge_request.source_project
if params[:ref].present?
@ref = params[:ref]
2018-03-17 18:26:18 +05:30
@commit = @repository.commit(Gitlab::Git::BRANCH_REF_PREFIX + @ref)
2017-09-10 17:25:29 +05:30
end
render layout: false
end
def branch_to
@target_project = selected_target_project
2022-04-01 21:47:47 +05:30
if @target_project && params[:ref].present? && Ability.allowed?(current_user, :create_merge_request_in, @target_project)
2017-09-10 17:25:29 +05:30
@ref = params[:ref]
2018-03-17 18:26:18 +05:30
@commit = @target_project.commit(Gitlab::Git::BRANCH_REF_PREFIX + @ref)
2017-09-10 17:25:29 +05:30
end
render layout: false
end
private
def build_merge_request
params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
2019-07-07 11:18:12 +05:30
2019-12-04 20:38:33 +05:30
# Gitaly N+1 issue: https://gitlab.com/gitlab-org/gitlab-foss/issues/58096
2019-07-07 11:18:12 +05:30
Gitlab::GitalyClient.allow_n_plus_1_calls do
2021-06-08 01:23:25 +05:30
@merge_request = ::MergeRequests::BuildService.new(project: project, current_user: current_user, params: merge_request_params.merge(diff_options: diff_options)).execute
2019-07-07 11:18:12 +05:30
end
2017-09-10 17:25:29 +05:30
end
def define_new_vars
@noteable = @merge_request
@target_project = @merge_request.target_project
@source_project = @merge_request.source_project
2019-12-26 22:10:19 +05:30
@commits =
set_commits_for_rendering(
@merge_request.recent_commits.with_latest_pipeline(@merge_request.source_branch),
commits_count: @merge_request.commits_count
)
2017-09-10 17:25:29 +05:30
@commit = @merge_request.diff_head_commit
2018-11-08 19:23:39 +05:30
# FIXME: We have to assign a presenter to another instance variable
# due to class_name checks being made with issuable classes
@mr_presenter = @merge_request.present(current_user: current_user)
2017-09-10 17:25:29 +05:30
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
set_pipeline_variables
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def selected_target_project
2021-04-29 21:17:54 +05:30
return @project unless @project.forked?
if params[:target_project_id].present?
return @project if @project.id.to_s == params[:target_project_id]
2018-03-17 18:26:18 +05:30
MergeRequestTargetProjectFinder.new(current_user: current_user, source_project: @project)
2018-03-27 19:54:05 +05:30
.find_by(id: params[:target_project_id])
2017-09-10 17:25:29 +05:30
else
2021-04-29 21:17:54 +05:30
@project.default_merge_request_target
2017-09-10 17:25:29 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
def incr_count_webide_merge_request
return if params[:nav_source] != 'webide'
Gitlab::UsageDataCounters::WebIdeCounter.increment_merge_requests_count
end
2017-09-10 17:25:29 +05:30
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
Projects::MergeRequests::CreationsController.prepend_mod