2017-06-13 18:12:31 -04:00
|
|
|
class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationController
|
2016-03-15 12:18:47 -04:00
|
|
|
include ToggleSubscriptionAction
|
2016-03-21 09:12:52 -04:00
|
|
|
include IssuableActions
|
2017-03-30 23:06:09 -04:00
|
|
|
include RendersNotes
|
2017-08-23 12:53:29 -04:00
|
|
|
include RendersCommits
|
2016-04-16 15:09:08 -04:00
|
|
|
include ToggleAwardEmoji
|
2016-07-23 19:28:12 -04:00
|
|
|
include IssuableCollections
|
2016-03-03 12:38:44 -05:00
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
skip_before_action :merge_request, only: [:index, :bulk_update]
|
2018-01-15 10:21:04 -05:00
|
|
|
before_action :whitelist_query_limiting, only: [:assign_related_issues, :update]
|
2017-11-01 13:35:14 -04:00
|
|
|
before_action :authorize_update_issuable!, only: [:close, :edit, :update, :remove_wip, :sort]
|
2017-11-07 08:34:12 -05:00
|
|
|
before_action :set_issuables_index, only: [:index]
|
2016-10-07 12:16:42 -04:00
|
|
|
before_action :authenticate_user!, only: [:assign_related_issues]
|
2017-12-20 04:01:21 -05:00
|
|
|
before_action :check_user_can_push_to_source_branch!, only: [:rebase]
|
2016-10-07 12:16:42 -04:00
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
def index
|
2017-11-07 08:34:12 -05:00
|
|
|
@merge_requests = @issuables
|
2017-02-15 17:13:53 -05:00
|
|
|
|
2015-03-12 19:20:58 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: {
|
2016-04-20 08:55:19 -04:00
|
|
|
html: view_to_html_string("projects/merge_requests/_merge_requests"),
|
2016-04-20 12:00:12 -04:00
|
|
|
labels: @labels.as_json(methods: :text_color)
|
2015-03-12 19:20:58 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2017-06-13 18:12:31 -04:00
|
|
|
validates_merge_request
|
|
|
|
close_merge_request_without_source_project
|
|
|
|
check_if_can_be_merged
|
|
|
|
|
2017-09-19 06:55:37 -04:00
|
|
|
# Return if the response has already been rendered
|
|
|
|
return if response_body
|
|
|
|
|
2011-11-28 15:16:57 -05:00
|
|
|
respond_to do |format|
|
2017-05-09 00:15:34 -04:00
|
|
|
format.html do
|
2017-06-13 18:12:31 -04:00
|
|
|
# Build a note object for comment form
|
|
|
|
@note = @project.notes.new(noteable: @merge_request)
|
|
|
|
|
|
|
|
@noteable = @merge_request
|
|
|
|
@commits_count = @merge_request.commits_count
|
|
|
|
|
2017-07-29 11:04:42 -04:00
|
|
|
@discussions = @merge_request.discussions
|
|
|
|
@notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)
|
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
labels
|
|
|
|
|
|
|
|
set_pipeline_variables
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2018-02-07 05:29:01 -05:00
|
|
|
render
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
2016-06-28 12:25:32 -04:00
|
|
|
|
2016-07-03 17:01:13 -04:00
|
|
|
format.json do
|
2017-05-18 16:14:48 -04:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
|
|
|
|
2017-10-31 12:15:03 -04:00
|
|
|
render json: serializer.represent(@merge_request, serializer: params[:serializer])
|
2016-07-03 17:01:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-10 08:57:50 -04:00
|
|
|
format.patch do
|
2016-07-03 17:01:13 -04:00
|
|
|
return render_404 unless @merge_request.diff_refs
|
|
|
|
|
|
|
|
send_git_patch @project.repository, @merge_request.diff_refs
|
2016-06-10 08:57:50 -04:00
|
|
|
end
|
2016-07-03 17:01:13 -04:00
|
|
|
|
2016-05-12 14:50:49 -04:00
|
|
|
format.diff do
|
2016-06-08 08:30:15 -04:00
|
|
|
return render_404 unless @merge_request.diff_refs
|
2016-05-12 14:50:49 -04:00
|
|
|
|
2016-06-08 08:30:15 -04:00
|
|
|
send_git_diff @project.repository, @merge_request.diff_refs
|
2016-05-12 14:50:49 -04:00
|
|
|
end
|
2012-11-22 15:50:36 -05:00
|
|
|
end
|
2012-07-04 18:26:23 -04:00
|
|
|
end
|
|
|
|
|
2015-05-29 00:09:28 -04:00
|
|
|
def commits
|
2017-06-13 18:12:31 -04:00
|
|
|
# Get commits from repository
|
|
|
|
# or from cache if already merged
|
2017-11-10 14:57:11 -05:00
|
|
|
@commits =
|
|
|
|
prepare_commits_for_rendering(@merge_request.commits.with_pipeline_status)
|
2016-08-04 09:56:54 -04:00
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
render json: { html: view_to_html_string('projects/merge_requests/_commits') }
|
2016-07-27 12:54:04 -04:00
|
|
|
end
|
|
|
|
|
2016-07-22 20:28:39 -04:00
|
|
|
def pipelines
|
2016-08-12 10:22:48 -04:00
|
|
|
@pipelines = @merge_request.all_pipelines
|
2016-07-25 20:30:52 -04:00
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
2017-04-05 10:36:14 -04:00
|
|
|
|
2017-07-14 11:52:54 -04:00
|
|
|
render json: {
|
|
|
|
pipelines: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
|
|
|
.represent(@pipelines),
|
|
|
|
count: {
|
|
|
|
all: @pipelines.count
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
2015-12-08 07:04:24 -05:00
|
|
|
def edit
|
2017-06-13 18:12:31 -04:00
|
|
|
define_edit_vars
|
2015-12-08 07:04:24 -05:00
|
|
|
end
|
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
def update
|
2017-06-13 18:12:31 -04:00
|
|
|
@merge_request = ::MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
|
2014-02-18 09:24:19 -05:00
|
|
|
|
2017-01-16 13:43:03 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if @merge_request.valid?
|
|
|
|
redirect_to([@merge_request.target_project.namespace.becomes(Namespace), @merge_request.target_project, @merge_request])
|
|
|
|
else
|
2017-06-13 18:12:31 -04:00
|
|
|
define_edit_vars
|
|
|
|
|
2017-01-16 13:43:03 -05:00
|
|
|
render :edit
|
2015-03-06 08:03:41 -05:00
|
|
|
end
|
2014-02-18 09:24:19 -05:00
|
|
|
end
|
2017-01-16 13:43:03 -05:00
|
|
|
|
|
|
|
format.json do
|
2017-03-18 00:23:15 -04:00
|
|
|
render json: @merge_request.to_json(include: { milestone: {}, assignee: { only: [:name, :username], methods: [:avatar_url] }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
|
2017-01-16 13:43:03 -05:00
|
|
|
end
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
2016-08-01 11:34:17 -04:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
2017-06-13 18:12:31 -04:00
|
|
|
define_edit_vars if request.format.html?
|
|
|
|
|
2017-01-16 13:43:03 -05:00
|
|
|
render_conflict_response
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
2016-02-26 22:34:32 -05:00
|
|
|
def remove_wip
|
2017-06-13 18:12:31 -04:00
|
|
|
@merge_request = ::MergeRequests::UpdateService
|
2017-05-09 00:15:34 -04:00
|
|
|
.new(project, current_user, wip_event: 'unwip')
|
|
|
|
.execute(@merge_request)
|
2016-02-26 22:34:32 -05:00
|
|
|
|
2017-12-07 16:56:59 -05:00
|
|
|
render json: serialize_widget(@merge_request)
|
2016-02-26 22:34:32 -05:00
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def commit_change_content
|
|
|
|
render partial: 'projects/merge_requests/widget/commit_change_content', layout: false
|
2012-04-16 15:08:03 -04:00
|
|
|
end
|
|
|
|
|
2017-02-17 08:56:13 -05:00
|
|
|
def cancel_merge_when_pipeline_succeeds
|
|
|
|
unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
|
2016-10-27 08:04:43 -04:00
|
|
|
return access_denied!
|
|
|
|
end
|
2015-11-02 11:27:38 -05:00
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
::MergeRequests::MergeWhenPipelineSucceedsService
|
2017-02-22 17:54:59 -05:00
|
|
|
.new(@project, current_user)
|
|
|
|
.cancel(@merge_request)
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2017-12-07 16:56:59 -05:00
|
|
|
render json: serialize_widget(@merge_request)
|
2015-11-02 11:27:38 -05:00
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def merge
|
2015-06-11 12:45:12 -04:00
|
|
|
return access_denied! unless @merge_request.can_be_merged_by?(current_user)
|
2013-02-27 13:20:31 -05:00
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
status = merge!
|
2015-11-02 11:27:38 -05:00
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
if @merge_request.merge_error
|
|
|
|
render json: { status: status, merge_error: @merge_request.merge_error }
|
2012-04-16 16:05:08 -04:00
|
|
|
else
|
2017-05-09 00:15:34 -04:00
|
|
|
render json: { status: status }
|
2012-04-16 16:05:08 -04:00
|
|
|
end
|
2012-03-29 11:03:05 -04:00
|
|
|
end
|
|
|
|
|
2016-08-08 18:30:01 -04:00
|
|
|
def assign_related_issues
|
2017-06-13 18:12:31 -04:00
|
|
|
result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
|
2016-08-08 18:30:01 -04:00
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
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-08-08 18:30:01 -04:00
|
|
|
end
|
2017-06-13 18:12:31 -04:00
|
|
|
|
|
|
|
redirect_to(merge_request_path(@merge_request))
|
2016-08-08 18:30:01 -04:00
|
|
|
end
|
|
|
|
|
2017-03-17 02:58:12 -04:00
|
|
|
def pipeline_status
|
2017-02-27 14:17:21 -05:00
|
|
|
render json: PipelineSerializer
|
2017-05-09 00:15:34 -04:00
|
|
|
.new(project: @project, current_user: @current_user)
|
2017-03-10 12:44:41 -05:00
|
|
|
.represent_status(@merge_request.head_pipeline)
|
2017-02-27 14:17:21 -05:00
|
|
|
end
|
|
|
|
|
2016-10-07 18:10:06 -04:00
|
|
|
def ci_environments_status
|
2016-10-12 09:21:01 -04:00
|
|
|
environments =
|
2016-10-10 14:38:48 -04:00
|
|
|
begin
|
2017-02-06 19:06:46 -05:00
|
|
|
@merge_request.environments_for(current_user).map do |environment|
|
2016-10-13 08:23:18 -04:00
|
|
|
project = environment.project
|
2016-10-10 14:38:48 -04:00
|
|
|
deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
|
|
|
|
|
2016-10-18 05:22:22 -04:00
|
|
|
stop_url =
|
2017-02-07 07:30:33 -05:00
|
|
|
if environment.stop_action? && can?(current_user, :create_deployment, environment)
|
2017-06-29 13:06:35 -04:00
|
|
|
stop_project_environment_path(project, environment)
|
2016-10-18 05:22:22 -04:00
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
metrics_url =
|
|
|
|
if can?(current_user, :read_environment, environment) && environment.has_metrics?
|
2017-06-29 13:06:35 -04:00
|
|
|
metrics_project_environment_deployment_path(environment.project, environment, deployment)
|
2017-05-09 00:15:34 -04:00
|
|
|
end
|
2017-08-15 13:44:37 -04:00
|
|
|
|
|
|
|
metrics_monitoring_url =
|
2017-07-26 05:31:17 -04:00
|
|
|
if can?(current_user, :read_environment, environment)
|
|
|
|
environment_metrics_path(environment)
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2016-10-12 09:21:01 -04:00
|
|
|
{
|
2016-10-10 14:38:48 -04:00
|
|
|
id: environment.id,
|
2016-10-12 09:21:01 -04:00
|
|
|
name: environment.name,
|
2017-06-29 13:06:35 -04:00
|
|
|
url: project_environment_path(project, environment),
|
2017-05-09 00:15:34 -04:00
|
|
|
metrics_url: metrics_url,
|
2017-07-26 05:31:17 -04:00
|
|
|
metrics_monitoring_url: metrics_monitoring_url,
|
2016-10-18 05:22:22 -04:00
|
|
|
stop_url: stop_url,
|
2016-10-10 14:38:48 -04:00
|
|
|
external_url: environment.external_url,
|
2016-10-12 09:21:01 -04:00
|
|
|
external_url_formatted: environment.formatted_external_url,
|
|
|
|
deployed_at: deployment.try(:created_at),
|
|
|
|
deployed_at_formatted: deployment.try(:formatted_deployment_time)
|
2016-10-10 14:38:48 -04:00
|
|
|
}
|
|
|
|
end.compact
|
2016-10-04 10:03:13 -04:00
|
|
|
end
|
2016-10-05 07:52:15 -04:00
|
|
|
|
2016-10-10 14:38:48 -04:00
|
|
|
render json: environments
|
2012-12-10 22:14:05 -05:00
|
|
|
end
|
|
|
|
|
2017-12-20 04:01:21 -05:00
|
|
|
def rebase
|
|
|
|
RebaseWorker.perform_async(@merge_request.id, current_user.id)
|
|
|
|
|
|
|
|
render nothing: true, status: 200
|
|
|
|
end
|
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
protected
|
|
|
|
|
2016-03-15 12:18:47 -04:00
|
|
|
alias_method :subscribable_resource, :merge_request
|
2016-03-21 09:12:52 -04:00
|
|
|
alias_method :issuable, :merge_request
|
2016-04-16 15:09:08 -04:00
|
|
|
alias_method :awardable, :merge_request
|
2011-12-15 16:57:46 -05:00
|
|
|
|
2012-06-29 14:55:22 -04:00
|
|
|
def validates_merge_request
|
2013-07-31 12:31:34 -04:00
|
|
|
# Show git not found page
|
|
|
|
# if there is no saved commits between source & target branch
|
2016-12-01 06:17:30 -05:00
|
|
|
if @merge_request.has_no_commits?
|
2013-12-12 04:34:42 -05:00
|
|
|
# and if target branch doesn't exist
|
|
|
|
return invalid_mr unless @merge_request.target_branch_exists?
|
2013-07-31 12:31:34 -04:00
|
|
|
end
|
2012-06-29 14:55:22 -04:00
|
|
|
end
|
|
|
|
|
2013-11-27 03:17:29 -05:00
|
|
|
def invalid_mr
|
2016-09-26 07:49:20 -04:00
|
|
|
# Render special view for MR with removed target branch
|
2013-11-27 03:17:29 -05:00
|
|
|
render 'invalid'
|
|
|
|
end
|
|
|
|
|
2015-11-02 11:27:38 -05:00
|
|
|
def merge_params
|
2017-06-21 11:30:45 -04:00
|
|
|
params.permit(merge_params_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_params_attributes
|
|
|
|
[:should_remove_source_branch, :commit_message]
|
2015-11-02 11:27:38 -05:00
|
|
|
end
|
|
|
|
|
2017-02-17 08:56:13 -05:00
|
|
|
def merge_when_pipeline_succeeds_active?
|
|
|
|
params[:merge_when_pipeline_succeeds].present? &&
|
2016-11-28 05:13:32 -05:00
|
|
|
@merge_request.head_pipeline && @merge_request.head_pipeline.active?
|
2016-06-24 12:13:56 -04:00
|
|
|
end
|
2016-06-28 12:25:32 -04:00
|
|
|
|
2016-09-26 07:49:20 -04:00
|
|
|
def close_merge_request_without_source_project
|
|
|
|
if !@merge_request.source_project && @merge_request.open?
|
|
|
|
@merge_request.close
|
|
|
|
end
|
|
|
|
end
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-18 16:14:48 -04:00
|
|
|
def check_if_can_be_merged
|
|
|
|
@merge_request.check_if_can_be_merged
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def merge!
|
|
|
|
# Disable the CI check if merge_when_pipeline_succeeds is enabled since we have
|
|
|
|
# to wait until CI completes to know
|
|
|
|
unless @merge_request.mergeable?(skip_ci_check: merge_when_pipeline_succeeds_active?)
|
|
|
|
return :failed
|
|
|
|
end
|
|
|
|
|
|
|
|
return :sha_mismatch if params[:sha] != @merge_request.diff_head_sha
|
|
|
|
|
|
|
|
@merge_request.update(merge_error: nil)
|
|
|
|
|
|
|
|
if params[:merge_when_pipeline_succeeds].present?
|
2017-12-01 08:26:51 -05:00
|
|
|
return :failed unless @merge_request.actual_head_pipeline
|
2017-05-09 00:15:34 -04:00
|
|
|
|
2017-12-01 08:26:51 -05:00
|
|
|
if @merge_request.actual_head_pipeline.active?
|
2017-06-13 18:12:31 -04:00
|
|
|
::MergeRequests::MergeWhenPipelineSucceedsService
|
2017-05-09 00:15:34 -04:00
|
|
|
.new(@project, current_user, merge_params)
|
|
|
|
.execute(@merge_request)
|
|
|
|
|
|
|
|
:merge_when_pipeline_succeeds
|
2017-12-01 08:26:51 -05:00
|
|
|
elsif @merge_request.actual_head_pipeline.success?
|
2017-05-09 00:15:34 -04:00
|
|
|
# This can be triggered when a user clicks the auto merge button while
|
|
|
|
# the tests finish at about the same time
|
2017-08-28 14:55:25 -04:00
|
|
|
@merge_request.merge_async(current_user.id, params)
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
:success
|
|
|
|
else
|
|
|
|
:failed
|
|
|
|
end
|
|
|
|
else
|
2017-08-28 14:55:25 -04:00
|
|
|
@merge_request.merge_async(current_user.id, params)
|
2017-05-09 00:15:34 -04:00
|
|
|
|
|
|
|
:success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 16:56:59 -05:00
|
|
|
def serialize_widget(merge_request)
|
|
|
|
serializer.represent(merge_request, serializer: 'widget')
|
|
|
|
end
|
|
|
|
|
2017-05-09 00:15:34 -04:00
|
|
|
def serializer
|
|
|
|
MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
|
|
|
|
end
|
2017-06-13 18:12:31 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
2017-11-07 08:34:12 -05:00
|
|
|
|
2018-02-20 07:33:49 -05:00
|
|
|
def finder_type
|
|
|
|
MergeRequestsFinder
|
2017-11-07 08:34:12 -05:00
|
|
|
end
|
2017-12-20 04:01:21 -05:00
|
|
|
|
|
|
|
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-01-15 10:21:04 -05:00
|
|
|
|
|
|
|
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
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|