2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationController
|
2016-06-02 11:05:42 +05:30
|
|
|
include ToggleSubscriptionAction
|
|
|
|
include IssuableActions
|
2017-08-17 22:00:37 +05:30
|
|
|
include RendersNotes
|
2018-03-17 18:26:18 +05:30
|
|
|
include RendersCommits
|
2016-06-16 23:09:34 +05:30
|
|
|
include ToggleAwardEmoji
|
2016-09-13 17:45:13 +05:30
|
|
|
include IssuableCollections
|
2019-03-02 22:35:43 +05:30
|
|
|
include RecordUserLastActivity
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
skip_before_action :merge_request, only: [:index, :bulk_update]
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :whitelist_query_limiting, only: [:assign_related_issues, :update]
|
|
|
|
before_action :authorize_update_issuable!, only: [:close, :edit, :update, :remove_wip, :sort]
|
2019-09-04 21:01:54 +05:30
|
|
|
before_action :authorize_test_reports!, only: [:test_reports]
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :set_issuables_index, only: [:index]
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :authenticate_user!, only: [:assign_related_issues]
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :check_user_can_push_to_source_branch!, only: [:rebase]
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
around_action :allow_gitaly_ref_name_caching, only: [:index, :show]
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2018-03-17 18:26:18 +05:30
|
|
|
@merge_requests = @issuables
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: {
|
2019-02-15 15:39:39 +05:30
|
|
|
html: view_to_html_string("projects/merge_requests/_merge_requests")
|
2015-04-26 12:48:37 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-11-08 19:23:39 +05:30
|
|
|
close_merge_request_if_no_source_project
|
|
|
|
mark_merge_request_mergeable
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
respond_to do |format|
|
2017-08-17 22:00:37 +05:30
|
|
|
format.html do
|
2018-11-08 19:23:39 +05:30
|
|
|
# use next to appease Rubocop
|
|
|
|
next render('invalid') if target_branch_missing?
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Build a note object for comment form
|
|
|
|
@note = @project.notes.new(noteable: @merge_request)
|
|
|
|
|
|
|
|
@noteable = @merge_request
|
|
|
|
@commits_count = @merge_request.commits_count
|
2019-02-15 15:39:39 +05:30
|
|
|
@issuable_sidebar = serializer.represent(@merge_request, serializer: 'sidebar')
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
set_pipeline_variables
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
render
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
format.json do
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
render json: serializer.represent(@merge_request, serializer: params[:serializer])
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
format.patch do
|
2018-10-15 14:42:47 +05:30
|
|
|
break render_404 unless @merge_request.diff_refs
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
send_git_patch @project.repository, @merge_request.diff_refs
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
format.diff do
|
2018-10-15 14:42:47 +05:30
|
|
|
break render_404 unless @merge_request.diff_refs
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
send_git_diff @project.repository, @merge_request.diff_refs
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def commits
|
2017-09-10 17:25:29 +05:30
|
|
|
# Get commits from repository
|
|
|
|
# or from cache if already merged
|
2018-03-17 18:26:18 +05:30
|
|
|
@commits =
|
2018-11-20 20:47:30 +05:30
|
|
|
set_commits_for_rendering(@merge_request.commits.with_pipeline_status)
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
render json: { html: view_to_html_string('projects/merge_requests/_commits') }
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def pipelines
|
2019-07-31 22:56:46 +05:30
|
|
|
set_pipeline_variables
|
|
|
|
@pipelines = @pipelines.page(params[:page]).per(30)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
render json: {
|
|
|
|
pipelines: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
2018-12-13 13:39:08 +05:30
|
|
|
.with_pagination(request, response)
|
2017-09-10 17:25:29 +05:30
|
|
|
.represent(@pipelines),
|
|
|
|
count: {
|
|
|
|
all: @pipelines.count
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def test_reports
|
2019-07-07 11:18:12 +05:30
|
|
|
reports_response(@merge_request.compare_test_reports)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def edit
|
2017-09-10 17:25:29 +05:30
|
|
|
define_edit_vars
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def update
|
2017-09-10 17:25:29 +05:30
|
|
|
@merge_request = ::MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2019-02-15 15:39:39 +05:30
|
|
|
if @merge_request.errors.present?
|
2017-09-10 17:25:29 +05:30
|
|
|
define_edit_vars
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render :edit
|
2019-02-15 15:39:39 +05:30
|
|
|
else
|
|
|
|
redirect_to project_merge_request_path(@merge_request.target_project, @merge_request)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
format.json do
|
2019-02-15 15:39:39 +05:30
|
|
|
if merge_request.errors.present?
|
|
|
|
render json: @merge_request.errors, status: :bad_request
|
|
|
|
else
|
|
|
|
render json: serializer.represent(@merge_request, serializer: 'basic')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
rescue ActiveRecord::StaleObjectError
|
2017-09-10 17:25:29 +05:30
|
|
|
define_edit_vars if request.format.html?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render_conflict_response
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def remove_wip
|
2017-09-10 17:25:29 +05:30
|
|
|
@merge_request = ::MergeRequests::UpdateService
|
2017-08-17 22:00:37 +05:30
|
|
|
.new(project, current_user, wip_event: 'unwip')
|
|
|
|
.execute(@merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
render json: serialize_widget(@merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def commit_change_content
|
|
|
|
render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def cancel_auto_merge
|
|
|
|
unless @merge_request.can_cancel_auto_merge?(current_user)
|
2017-08-17 22:00:37 +05:30
|
|
|
return access_denied!
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
AutoMergeService.new(project, current_user).cancel(@merge_request)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
render json: serialize_widget(@merge_request)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def merge
|
2018-12-13 13:39:08 +05:30
|
|
|
access_check_result = merge_access_check
|
|
|
|
|
|
|
|
return access_check_result if access_check_result
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
status = merge!
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if @merge_request.merge_error
|
|
|
|
render json: { status: status, merge_error: @merge_request.merge_error }
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
render json: { status: status }
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def assign_related_issues
|
2017-09-10 17:25:29 +05:30
|
|
|
result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
case result[:count]
|
|
|
|
when 0
|
|
|
|
flash[:error] = "Failed to assign you issues related to the merge request"
|
|
|
|
when 1
|
|
|
|
flash[:notice] = "1 issue has been assigned to you"
|
|
|
|
else
|
|
|
|
flash[:notice] = "#{result[:count]} issues have been assigned to you"
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
redirect_to(merge_request_path(@merge_request))
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def pipeline_status
|
|
|
|
render json: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
2019-09-04 21:01:54 +05:30
|
|
|
.represent_status(head_pipeline)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def ci_environments_status
|
2018-12-13 13:39:08 +05:30
|
|
|
environments = if ci_environments_status_on_merge_result?
|
|
|
|
EnvironmentStatus.after_merge_request(@merge_request, current_user)
|
|
|
|
else
|
|
|
|
EnvironmentStatus.for_merge_request(@merge_request, current_user)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
render json: EnvironmentStatusSerializer.new(current_user: current_user).represent(environments)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def rebase
|
|
|
|
RebaseWorker.perform_async(@merge_request.id, current_user.id)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def discussions
|
|
|
|
merge_request.preload_discussions_diff_highlight
|
|
|
|
|
|
|
|
super
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
protected
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
alias_method :subscribable_resource, :merge_request
|
|
|
|
alias_method :issuable, :merge_request
|
2016-06-16 23:09:34 +05:30
|
|
|
alias_method :awardable, :merge_request
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def issuable_sorting_field
|
|
|
|
MergeRequest::SORTING_PREFERENCE_FIELD
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def merge_params
|
2017-09-10 17:25:29 +05:30
|
|
|
params.permit(merge_params_attributes)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def merge_params_attributes
|
2019-09-04 21:01:54 +05:30
|
|
|
[:should_remove_source_branch, :commit_message, :squash_commit_message, :squash, :auto_merge_strategy]
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def auto_merge_requested?
|
|
|
|
# Support params[:merge_when_pipeline_succeeds] during the transition period
|
|
|
|
params[:auto_merge_strategy].present? || params[:merge_when_pipeline_succeeds].present?
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def close_merge_request_if_no_source_project
|
2017-08-17 22:00:37 +05:30
|
|
|
if !@merge_request.source_project && @merge_request.open?
|
|
|
|
@merge_request.close
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def head_pipeline
|
|
|
|
strong_memoize(:head_pipeline) do
|
|
|
|
pipeline = @merge_request.head_pipeline
|
|
|
|
pipeline if can?(current_user, :read_pipeline, pipeline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def ci_environments_status_on_merge_result?
|
|
|
|
params[:environment_target] == 'merge_commit'
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def target_branch_missing?
|
|
|
|
@merge_request.has_no_commits? && !@merge_request.target_branch_exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_merge_request_mergeable
|
2017-08-17 22:00:37 +05:30
|
|
|
@merge_request.check_if_can_be_merged
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def merge!
|
2019-09-04 21:01:54 +05:30
|
|
|
# Disable the CI check if auto_merge_strategy is specified since we have
|
2017-08-17 22:00:37 +05:30
|
|
|
# to wait until CI completes to know
|
2019-09-04 21:01:54 +05:30
|
|
|
unless @merge_request.mergeable?(skip_ci_check: auto_merge_requested?)
|
2017-08-17 22:00:37 +05:30
|
|
|
return :failed
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
merge_service = ::MergeRequests::MergeService.new(@project, current_user, merge_params)
|
|
|
|
|
|
|
|
unless merge_service.hooks_validation_pass?(@merge_request)
|
|
|
|
return :hook_validation_error
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
return :sha_mismatch if params[:sha] != @merge_request.diff_head_sha
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
@merge_request.update(merge_error: nil, squash: merge_params.fetch(:squash, false))
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
if auto_merge_requested?
|
|
|
|
if merge_request.auto_merge_enabled?
|
|
|
|
# TODO: We should have a dedicated endpoint for updating merge params.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/63130.
|
|
|
|
AutoMergeService.new(project, current_user, merge_params).update(merge_request)
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2019-09-04 21:01:54 +05:30
|
|
|
AutoMergeService.new(project, current_user, merge_params)
|
|
|
|
.execute(merge_request,
|
|
|
|
params[:auto_merge_strategy] || AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
else
|
2018-11-08 19:23:39 +05:30
|
|
|
@merge_request.merge_async(current_user.id, merge_params)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
:success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def serialize_widget(merge_request)
|
|
|
|
serializer.represent(merge_request, serializer: 'widget')
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def serializer
|
|
|
|
MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def define_edit_vars
|
|
|
|
@source_project = @merge_request.source_project
|
|
|
|
@target_project = @merge_request.target_project
|
|
|
|
@target_branches = @merge_request.target_project.repository.branch_names
|
2018-12-05 23:21:45 +05:30
|
|
|
@noteable = @merge_request
|
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
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def finder_type
|
|
|
|
MergeRequestsFinder
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def check_user_can_push_to_source_branch!
|
|
|
|
return access_denied! unless @merge_request.source_branch_exists?
|
|
|
|
|
|
|
|
access_check = ::Gitlab::UserAccess
|
|
|
|
.new(current_user, project: @merge_request.source_project)
|
|
|
|
.can_push_to_branch?(@merge_request.source_branch)
|
|
|
|
|
|
|
|
access_denied! unless access_check
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def merge_access_check
|
|
|
|
access_denied! unless @merge_request.can_be_merged_by?(current_user)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def whitelist_query_limiting
|
|
|
|
# Also see https://gitlab.com/gitlab-org/gitlab-ce/issues/42441
|
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42438')
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
def reports_response(report_comparison)
|
|
|
|
case report_comparison[:status]
|
|
|
|
when :parsing
|
|
|
|
::Gitlab::PollingInterval.set_header(response, interval: 3000)
|
|
|
|
|
|
|
|
render json: '', status: :no_content
|
|
|
|
when :parsed
|
|
|
|
render json: report_comparison[:data].to_json, status: :ok
|
|
|
|
when :error
|
|
|
|
render json: { status_reason: report_comparison[:status_reason] }, status: :bad_request
|
|
|
|
else
|
|
|
|
render json: { status_reason: 'Unknown error' }, status: :internal_server_error
|
|
|
|
end
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
def authorize_test_reports!
|
|
|
|
# MergeRequest#actual_head_pipeline is the pipeline accessed in MergeRequest#compare_reports.
|
|
|
|
return render_404 unless can?(current_user, :read_build, merge_request.actual_head_pipeline)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|