gitlab-org--gitlab-foss/app/controllers/projects/merge_requests_controller.rb

345 lines
9.9 KiB
Ruby
Raw Normal View History

2017-06-13 18:12:31 -04:00
class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationController
include ToggleSubscriptionAction
2016-03-21 09:12:52 -04:00
include IssuableActions
2017-03-30 23:06:09 -04:00
include RendersNotes
include RendersCommits
2016-04-16 15:09:08 -04:00
include ToggleAwardEmoji
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]
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]
before_action :authenticate_user!, only: [:assign_related_issues]
before_action :check_user_can_push_to_source_branch!, only: [:rebase]
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
respond_to do |format|
format.html
format.json do
render json: {
html: view_to_html_string("projects/merge_requests/_merge_requests"),
labels: @labels.as_json(methods: :text_color)
}
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
@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-07-03 17:01:13 -04:00
format.json do
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
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
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
end
2012-07-04 18:26:23 -04:00
end
def commits
2017-06-13 18:12:31 -04:00
# Get commits from repository
# or from cache if already merged
@commits =
prepare_commits_for_rendering(@merge_request.commits.with_pipeline_status)
2017-06-13 18:12:31 -04:00
render json: { html: view_to_html_string('projects/merge_requests/_commits') }
end
def pipelines
@pipelines = @merge_request.all_pipelines
2017-06-13 18:12:31 -04:00
Gitlab::PollingInterval.set_header(response, interval: 10_000)
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)
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
render :edit
end
end
format.json do
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])
end
2011-11-28 02:39:43 -05:00
end
rescue ActiveRecord::StaleObjectError
2017-06-13 18:12:31 -04:00
define_edit_vars if request.format.html?
render_conflict_response
2011-11-28 02:39:43 -05:00
end
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)
render json: serialize_widget(@merge_request)
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
def cancel_merge_when_pipeline_succeeds
unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
return access_denied!
end
2015-11-02 11:27:38 -05:00
2017-06-13 18:12:31 -04:00
::MergeRequests::MergeWhenPipelineSucceedsService
.new(@project, current_user)
.cancel(@merge_request)
2017-05-09 00:15:34 -04:00
render json: serialize_widget(@merge_request)
2015-11-02 11:27:38 -05:00
end
def merge
return access_denied! unless @merge_request.can_be_merged_by?(current_user)
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 }
else
2017-05-09 00:15:34 -04:00
render json: { status: status }
end
2012-03-29 11:03:05 -04:00
end
def assign_related_issues
2017-06-13 18:12:31 -04:00
result = ::MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
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"
end
2017-06-13 18:12:31 -04:00
redirect_to(merge_request_path(@merge_request))
end
def pipeline_status
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)
end
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)
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?
metrics_project_environment_deployment_path(environment.project, environment, deployment)
2017-05-09 00:15:34 -04:00
end
metrics_monitoring_url =
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,
url: project_environment_path(project, environment),
2017-05-09 00:15:34 -04:00
metrics_url: metrics_url,
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-10 14:38:48 -04:00
render json: environments
end
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
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
def validates_merge_request
# Show git not found page
# if there is no saved commits between source & target branch
if @merge_request.has_no_commits?
# and if target branch doesn't exist
return invalid_mr unless @merge_request.target_branch_exists?
end
end
def invalid_mr
# Render special view for MR with removed target branch
render 'invalid'
end
2015-11-02 11:27:38 -05:00
def merge_params
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
def merge_when_pipeline_succeeds_active?
params[:merge_when_pipeline_succeeds].present? &&
@merge_request.head_pipeline && @merge_request.head_pipeline.active?
end
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
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?
return :failed unless @merge_request.actual_head_pipeline
2017-05-09 00:15:34 -04: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
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
@merge_request.merge_async(current_user.id, params)
2017-05-09 00:15:34 -04:00
:success
else
:failed
end
else
@merge_request.merge_async(current_user.id, params)
2017-05-09 00:15:34 -04:00
:success
end
end
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
def finder_type
MergeRequestsFinder
2017-11-07 08:34:12 -05:00
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
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