2014-09-02 18:07:02 +05:30
|
|
|
class Projects::MergeRequestsController < Projects::ApplicationController
|
2016-06-02 11:05:42 +05:30
|
|
|
include ToggleSubscriptionAction
|
2016-08-24 12:49:21 +05:30
|
|
|
include DiffForPath
|
2016-06-02 11:05:42 +05:30
|
|
|
include DiffHelper
|
|
|
|
include IssuableActions
|
2017-08-17 22:00:37 +05:30
|
|
|
include RendersNotes
|
2016-06-16 23:09:34 +05:30
|
|
|
include ToggleAwardEmoji
|
2016-09-13 17:45:13 +05:30
|
|
|
include IssuableCollections
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :module_enabled
|
|
|
|
before_action :merge_request, only: [
|
2017-08-17 22:00:37 +05:30
|
|
|
:edit, :update, :show, :diffs, :commits, :conflicts, :conflict_for_path, :pipelines, :merge,
|
|
|
|
:pipeline_status, :ci_environments_status, :toggle_subscription, :cancel_merge_when_pipeline_succeeds, :remove_wip, :resolve_conflicts, :assign_related_issues, :commit_change_content
|
2015-09-11 14:41:01 +05:30
|
|
|
]
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :validates_merge_request, only: [:show, :diffs, :commits, :pipelines]
|
|
|
|
before_action :define_show_vars, only: [:diffs, :commits, :conflicts, :conflict_for_path, :builds, :pipelines]
|
2016-08-24 12:49:21 +05:30
|
|
|
before_action :define_commit_vars, only: [:diffs]
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :ensure_ref_fetched, only: [:show, :diffs, :commits, :builds, :conflicts, :conflict_for_path, :pipelines]
|
|
|
|
before_action :close_merge_request_without_source_project, only: [:show, :diffs, :commits, :builds, :pipelines]
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :check_if_can_be_merged, only: :show
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :apply_diff_view_cookie!, only: [:new_diffs]
|
|
|
|
before_action :build_merge_request, only: [:new, :new_diffs]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow read any merge_request
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_read_merge_request!
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow write(create) merge_request
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_create_merge_request!, only: [:new, :create]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Allow modify merge_request
|
2016-06-02 11:05:42 +05:30
|
|
|
before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :remove_wip, :sort]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :authenticate_user!, only: [:assign_related_issues]
|
|
|
|
|
|
|
|
before_action :authorize_can_resolve_conflicts!, only: [:conflicts, :conflict_for_path, :resolve_conflicts]
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def index
|
2017-08-17 22:00:37 +05:30
|
|
|
@collection_type = "MergeRequest"
|
|
|
|
@merge_requests = merge_requests_collection
|
|
|
|
@merge_requests = @merge_requests.page(params[:page])
|
|
|
|
@merge_requests = @merge_requests.preload(merge_request_diff: :merge_request)
|
|
|
|
@issuable_meta_data = issuable_meta_data(@merge_requests, @collection_type)
|
|
|
|
|
|
|
|
if @merge_requests.out_of_range? && @merge_requests.total_pages != 0
|
|
|
|
return redirect_to url_for(params.merge(page: @merge_requests.total_pages, only_path: true))
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
if params[:label_name].present?
|
|
|
|
labels_params = { project_id: @project.id, title: params[:label_name] }
|
|
|
|
@labels = LabelsFinder.new(current_user, labels_params).execute
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@users = []
|
|
|
|
if params[:assignee_id].present?
|
|
|
|
assignee = User.find_by_id(params[:assignee_id])
|
|
|
|
@users.push(assignee) if assignee
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:author_id].present?
|
|
|
|
author = User.find_by_id(params[:author_id])
|
|
|
|
@users.push(author) if author
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: {
|
2016-06-02 11:05:42 +05:30
|
|
|
html: view_to_html_string("projects/merge_requests/_merge_requests"),
|
|
|
|
labels: @labels.as_json(methods: :text_color)
|
2015-04-26 12:48:37 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
respond_to do |format|
|
2017-08-17 22:00:37 +05:30
|
|
|
format.html do
|
|
|
|
define_discussion_vars
|
|
|
|
define_show_vars
|
|
|
|
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)
|
|
|
|
|
|
|
|
render json: serializer.represent(@merge_request, basic: params[:basic])
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
format.patch do
|
|
|
|
return render_404 unless @merge_request.diff_refs
|
|
|
|
|
|
|
|
send_git_patch @project.repository, @merge_request.diff_refs
|
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
format.diff do
|
|
|
|
return render_404 unless @merge_request.diff_refs
|
|
|
|
|
|
|
|
send_git_diff @project.repository, @merge_request.diff_refs
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def diffs
|
2016-04-02 18:10:28 +05:30
|
|
|
apply_diff_view_cookie!
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
respond_to do |format|
|
2016-08-24 12:49:21 +05:30
|
|
|
format.html { define_discussion_vars }
|
2016-09-13 17:45:13 +05:30
|
|
|
format.json do
|
2017-08-17 22:00:37 +05:30
|
|
|
define_diff_vars
|
|
|
|
define_diff_comment_vars
|
|
|
|
|
|
|
|
@environment = @merge_request.environments_for(current_user).last
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# With an ID param, loads the MR at that ID. Otherwise, accepts the same params as #new
|
|
|
|
# and uses that (unsaved) MR.
|
|
|
|
#
|
|
|
|
def diff_for_path
|
|
|
|
if params[:id]
|
|
|
|
merge_request
|
2017-08-17 22:00:37 +05:30
|
|
|
define_diff_vars
|
2016-08-24 12:49:21 +05:30
|
|
|
define_diff_comment_vars
|
|
|
|
else
|
|
|
|
build_merge_request
|
2017-08-17 22:00:37 +05:30
|
|
|
@compare = @merge_request
|
|
|
|
@diffs = @compare.diffs(diff_options)
|
2016-08-24 12:49:21 +05:30
|
|
|
@diff_notes_disabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
define_commit_vars
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render_diff_for_path(@diffs)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def commits
|
|
|
|
respond_to do |format|
|
2016-08-24 12:49:21 +05:30
|
|
|
format.html do
|
|
|
|
define_discussion_vars
|
|
|
|
|
|
|
|
render 'show'
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
# Get commits from repository
|
|
|
|
# or from cache if already merged
|
|
|
|
@commits = @merge_request.commits
|
|
|
|
@note_counts = Note.where(commit_id: @commits.map(&:id)).
|
|
|
|
group(:commit_id).count
|
|
|
|
|
|
|
|
render json: { html: view_to_html_string('projects/merge_requests/show/_commits') }
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def conflicts
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { define_discussion_vars }
|
|
|
|
|
|
|
|
format.json do
|
2017-08-17 22:00:37 +05:30
|
|
|
if @conflicts_list.can_be_resolved_in_ui?
|
|
|
|
render json: @conflicts_list
|
2016-09-13 17:45:13 +05:30
|
|
|
elsif @merge_request.can_be_merged?
|
|
|
|
render json: {
|
|
|
|
message: 'The merge conflicts for this merge request have already been resolved. Please return to the merge request.',
|
|
|
|
type: 'error'
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
message: 'The merge conflicts for this merge request cannot be resolved through GitLab. Please try to resolve them locally.',
|
|
|
|
type: 'error'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def conflict_for_path
|
2017-08-17 22:00:37 +05:30
|
|
|
return render_404 unless @conflicts_list.can_be_resolved_in_ui?
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
file = @conflicts_list.file_for_path(params[:old_path], params[:new_path])
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
return render_404 unless file
|
|
|
|
|
|
|
|
render json: file, full_content: true
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def resolve_conflicts
|
2017-08-17 22:00:37 +05:30
|
|
|
return render_404 unless @conflicts_list.can_be_resolved_in_ui?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
if @merge_request.can_be_merged?
|
|
|
|
render status: :bad_request, json: { message: 'The merge conflicts for this merge request have already been resolved.' }
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2017-08-17 22:00:37 +05:30
|
|
|
MergeRequests::Conflicts::ResolveService.
|
|
|
|
new(merge_request).
|
|
|
|
execute(current_user, params)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
flash[:notice] = 'All merge conflicts were resolved. The merge request can now be merged.'
|
|
|
|
|
|
|
|
render json: { redirect_to: namespace_project_merge_request_url(@project.namespace, @project, @merge_request, resolved_conflicts: true) }
|
2016-11-03 12:29:30 +05:30
|
|
|
rescue Gitlab::Conflict::ResolutionError => e
|
2016-09-13 17:45:13 +05:30
|
|
|
render status: :bad_request, json: { message: e.message }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipelines
|
|
|
|
@pipelines = @merge_request.all_pipelines
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
define_discussion_vars
|
|
|
|
|
|
|
|
render 'show'
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
format.json do
|
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
|
|
|
|
|
|
|
render json: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
|
|
|
.represent(@pipelines)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def new
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html { define_new_vars }
|
|
|
|
format.json do
|
|
|
|
define_pipelines_vars
|
|
|
|
|
|
|
|
Gitlab::PollingInterval.set_header(response, interval: 10_000)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
pipelines: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
|
|
|
.represent(@pipelines)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def new_diffs
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
define_new_vars
|
2017-08-17 22:00:37 +05:30
|
|
|
@show_changes_tab = true
|
2016-11-03 12:29:30 +05:30
|
|
|
render "new"
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
@diffs = if @merge_request.can_be_created
|
|
|
|
@merge_request.diffs(diff_options)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
@diff_notes_disabled = true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@environment = @merge_request.environments_for(current_user).last
|
|
|
|
|
|
|
|
render json: { html: view_to_html_string('projects/merge_requests/_new_diffs', diffs: @diffs, environment: @environment) }
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@target_branches ||= []
|
|
|
|
@merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
|
|
|
|
|
|
|
|
if @merge_request.valid?
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to(merge_request_path(@merge_request))
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
|
|
|
@source_project = @merge_request.source_project
|
|
|
|
@target_project = @merge_request.target_project
|
|
|
|
render action: "new"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
def edit
|
|
|
|
@source_project = @merge_request.source_project
|
|
|
|
@target_project = @merge_request.target_project
|
|
|
|
@target_branches = @merge_request.target_project.repository.branch_names
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def update
|
|
|
|
@merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
|
|
|
render :edit
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
rescue ActiveRecord::StaleObjectError
|
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-08-17 22:00:37 +05:30
|
|
|
@merge_request = MergeRequests::UpdateService
|
|
|
|
.new(project, current_user, wip_event: 'unwip')
|
|
|
|
.execute(@merge_request)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render json: serializer.represent(@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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def cancel_merge_when_pipeline_succeeds
|
|
|
|
unless @merge_request.can_cancel_merge_when_pipeline_succeeds?(current_user)
|
|
|
|
return access_denied!
|
|
|
|
end
|
|
|
|
|
|
|
|
MergeRequests::MergeWhenPipelineSucceedsService
|
|
|
|
.new(@project, current_user)
|
|
|
|
.cancel(@merge_request)
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
render json: serializer.represent(@merge_request)
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
def merge
|
2015-09-11 14:41:01 +05:30
|
|
|
return access_denied! unless @merge_request.can_be_merged_by?(current_user)
|
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
|
|
|
|
|
|
|
|
def branch_from
|
2016-06-16 23:09:34 +05:30
|
|
|
# This is always source
|
2014-09-02 18:07:02 +05:30
|
|
|
@source_project = @merge_request.nil? ? @project : @merge_request.source_project
|
2016-11-24 13:41:30 +05:30
|
|
|
|
|
|
|
if params[:ref].present?
|
|
|
|
@ref = params[:ref]
|
2017-08-17 22:00:37 +05:30
|
|
|
@commit = @repository.commit("refs/heads/#{@ref}")
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
render layout: false
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def branch_to
|
|
|
|
@target_project = selected_target_project
|
2016-11-24 13:41:30 +05:30
|
|
|
|
|
|
|
if params[:ref].present?
|
|
|
|
@ref = params[:ref]
|
2017-08-17 22:00:37 +05:30
|
|
|
@commit = @target_project.commit("refs/heads/#{@ref}")
|
2016-11-24 13:41:30 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
render layout: false
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update_branches
|
|
|
|
@target_project = selected_target_project
|
|
|
|
@target_branches = @target_project.repository.branch_names
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
render layout: false
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def assign_related_issues
|
|
|
|
result = MergeRequests::AssignIssuesService.new(project, current_user, merge_request: @merge_request).execute
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
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
|
|
|
|
|
|
|
|
redirect_to(merge_request_path(@merge_request))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def pipeline_status
|
|
|
|
render json: PipelineSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
|
|
|
.represent_status(@merge_request.head_pipeline)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def ci_environments_status
|
|
|
|
environments =
|
|
|
|
begin
|
2017-08-17 22:00:37 +05:30
|
|
|
@merge_request.environments_for(current_user).map do |environment|
|
2016-11-03 12:29:30 +05:30
|
|
|
project = environment.project
|
|
|
|
deployment = environment.first_deployment_for(@merge_request.diff_head_commit)
|
|
|
|
|
|
|
|
stop_url =
|
2017-08-17 22:00:37 +05:30
|
|
|
if environment.stop_action? && can?(current_user, :create_deployment, environment)
|
2016-11-03 12:29:30 +05:30
|
|
|
stop_namespace_project_environment_path(project.namespace, project, environment)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
metrics_url =
|
|
|
|
if can?(current_user, :read_environment, environment) && environment.has_metrics?
|
|
|
|
metrics_namespace_project_environment_deployment_path(environment.project.namespace,
|
|
|
|
environment.project,
|
|
|
|
environment,
|
|
|
|
deployment)
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
{
|
|
|
|
id: environment.id,
|
|
|
|
name: environment.name,
|
|
|
|
url: namespace_project_environment_path(project.namespace, project, environment),
|
2017-08-17 22:00:37 +05:30
|
|
|
metrics_url: metrics_url,
|
2016-11-03 12:29:30 +05:30
|
|
|
stop_url: stop_url,
|
|
|
|
external_url: environment.external_url,
|
|
|
|
external_url_formatted: environment.formatted_external_url,
|
|
|
|
deployed_at: deployment.try(:created_at),
|
|
|
|
deployed_at_formatted: deployment.try(:formatted_deployment_time)
|
|
|
|
}
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: environments
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
protected
|
|
|
|
|
|
|
|
def selected_target_project
|
|
|
|
if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
|
|
|
|
@project
|
|
|
|
else
|
|
|
|
@project.forked_project_link.forked_from_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
2016-09-13 17:45:13 +05:30
|
|
|
@issuable = @merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
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
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def authorize_update_merge_request!
|
|
|
|
return render_404 unless can?(current_user, :update_merge_request, @merge_request)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_merge_request!
|
|
|
|
return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def authorize_can_resolve_conflicts!
|
2017-08-17 22:00:37 +05:30
|
|
|
@conflicts_list = MergeRequests::Conflicts::ListService.new(@merge_request)
|
|
|
|
|
|
|
|
return render_404 unless @conflicts_list.can_be_resolved_by?(current_user)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def module_enabled
|
2016-09-29 09:46:39 +05:30
|
|
|
return render_404 unless @project.feature_available?(:merge_requests, current_user)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def validates_merge_request
|
|
|
|
# Show git not found page
|
|
|
|
# if there is no saved commits between source & target branch
|
2017-08-17 22:00:37 +05:30
|
|
|
if @merge_request.has_no_commits?
|
2014-09-02 18:07:02 +05:30
|
|
|
# and if target branch doesn't exist
|
|
|
|
return invalid_mr unless @merge_request.target_branch_exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def define_show_vars
|
|
|
|
@noteable = @merge_request
|
2017-08-17 22:00:37 +05:30
|
|
|
@commits_count = @merge_request.commits_count
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
if @merge_request.locked_long_ago?
|
|
|
|
@merge_request.unlock_mr
|
|
|
|
@merge_request.close
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2016-11-24 13:41:30 +05:30
|
|
|
labels
|
2016-11-03 12:29:30 +05:30
|
|
|
define_pipelines_vars
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# Discussion tab data is rendered on html responses of actions
|
|
|
|
# :show, :diff, :commits, :builds. but not when request the data through AJAX
|
|
|
|
def define_discussion_vars
|
|
|
|
# Build a note object for comment form
|
2016-09-13 17:45:13 +05:30
|
|
|
@note = @project.notes.new(noteable: @merge_request)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
@discussions = @merge_request.discussions
|
2017-08-17 22:00:37 +05:30
|
|
|
@notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes))
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def define_commit_vars
|
|
|
|
@commit = @merge_request.diff_head_commit
|
|
|
|
@base_commit = @merge_request.diff_base_commit || @merge_request.likely_diff_base_commit
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def define_diff_vars
|
|
|
|
@merge_request_diff =
|
|
|
|
if params[:diff_id]
|
|
|
|
@merge_request.merge_request_diffs.viewable.find(params[:diff_id])
|
|
|
|
else
|
|
|
|
@merge_request.merge_request_diff
|
|
|
|
end
|
|
|
|
|
|
|
|
@merge_request_diffs = @merge_request.merge_request_diffs.viewable.select_without_diff
|
|
|
|
@comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
|
|
|
|
|
|
|
|
if params[:start_sha].present?
|
|
|
|
@start_sha = params[:start_sha]
|
|
|
|
@start_version = @comparable_diffs.find { |diff| diff.head_commit_sha == @start_sha }
|
|
|
|
|
|
|
|
unless @start_version
|
|
|
|
@start_sha = @merge_request_diff.head_commit_sha
|
|
|
|
@start_version = @merge_request_diff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@compare =
|
|
|
|
if @start_sha
|
|
|
|
@merge_request_diff.compare_with(@start_sha)
|
|
|
|
else
|
|
|
|
@merge_request_diff
|
|
|
|
end
|
|
|
|
|
|
|
|
@diffs = @compare.diffs(diff_options)
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def define_diff_comment_vars
|
2017-08-17 22:00:37 +05:30
|
|
|
@new_diff_note_attrs = {
|
2016-08-24 12:49:21 +05:30
|
|
|
noteable_type: 'MergeRequest',
|
|
|
|
noteable_id: @merge_request.id
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@diff_notes_disabled = false
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
@use_legacy_diff_notes = !@merge_request.has_complete_diff_refs?
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@grouped_diff_discussions = @merge_request.grouped_diff_discussions(@compare.diff_refs)
|
|
|
|
@notes = prepare_notes_for_rendering(@grouped_diff_discussions.values.flatten.flat_map(&:notes))
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def define_pipelines_vars
|
|
|
|
@pipelines = @merge_request.all_pipelines
|
2017-08-17 22:00:37 +05:30
|
|
|
@pipeline = @merge_request.head_pipeline
|
|
|
|
@statuses_count = @pipeline.present? ? @pipeline.statuses.relevant.count : 0
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def define_new_vars
|
|
|
|
@noteable = @merge_request
|
|
|
|
|
|
|
|
@target_branches = if @merge_request.target_project
|
|
|
|
@merge_request.target_project.repository.branch_names
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
@target_project = merge_request.target_project
|
|
|
|
@source_project = merge_request.source_project
|
|
|
|
@commits = @merge_request.compare_commits.reverse
|
|
|
|
@commit = @merge_request.diff_head_commit
|
|
|
|
@base_commit = @merge_request.diff_base_commit
|
|
|
|
|
|
|
|
@note_counts = Note.where(commit_id: @commits.map(&:id)).
|
|
|
|
group(:commit_id).count
|
|
|
|
|
|
|
|
@labels = LabelsFinder.new(current_user, project_id: @project.id).execute
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@show_changes_tab = params[:show_changes].present?
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
define_pipelines_vars
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def invalid_mr
|
2016-11-03 12:29:30 +05:30
|
|
|
# Render special view for MR with removed target branch
|
2014-09-02 18:07:02 +05:30
|
|
|
render 'invalid'
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params
|
2017-08-17 22:00:37 +05:30
|
|
|
params.require(:merge_request)
|
|
|
|
.permit(merge_request_params_ce)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params_ce
|
|
|
|
[
|
|
|
|
:assignee_id,
|
|
|
|
:description,
|
|
|
|
:force_remove_source_branch,
|
|
|
|
:lock_version,
|
|
|
|
:milestone_id,
|
|
|
|
:source_branch,
|
|
|
|
:source_project_id,
|
|
|
|
:state_event,
|
|
|
|
:target_branch,
|
|
|
|
:target_project_id,
|
|
|
|
:task_num,
|
|
|
|
:title,
|
|
|
|
|
|
|
|
label_ids: []
|
|
|
|
]
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_params
|
|
|
|
params.permit(:should_remove_source_branch, :commit_message)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
# Make sure merge requests created before 8.0
|
|
|
|
# have head file in refs/merge-requests/
|
|
|
|
def ensure_ref_fetched
|
|
|
|
@merge_request.ensure_ref_fetched
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def merge_when_pipeline_succeeds_active?
|
|
|
|
params[:merge_when_pipeline_succeeds].present? &&
|
|
|
|
@merge_request.head_pipeline && @merge_request.head_pipeline.active?
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def build_merge_request
|
|
|
|
params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
|
2016-11-03 12:29:30 +05:30
|
|
|
@merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params.merge(diff_options: diff_options)).execute
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def close_merge_request_without_source_project
|
|
|
|
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
|
|
|
|
|
|
|
|
def check_if_can_be_merged
|
|
|
|
@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!
|
|
|
|
# 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
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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.head_pipeline
|
|
|
|
|
|
|
|
if @merge_request.head_pipeline.active?
|
|
|
|
MergeRequests::MergeWhenPipelineSucceedsService
|
|
|
|
.new(@project, current_user, merge_params)
|
|
|
|
.execute(@merge_request)
|
|
|
|
|
|
|
|
:merge_when_pipeline_succeeds
|
|
|
|
elsif @merge_request.head_pipeline.success?
|
|
|
|
# This can be triggered when a user clicks the auto merge button while
|
|
|
|
# the tests finish at about the same time
|
|
|
|
MergeWorker.perform_async(@merge_request.id, current_user.id, params)
|
|
|
|
|
|
|
|
:success
|
|
|
|
else
|
|
|
|
:failed
|
|
|
|
end
|
|
|
|
else
|
|
|
|
MergeWorker.perform_async(@merge_request.id, current_user.id, params)
|
|
|
|
|
|
|
|
:success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def serializer
|
|
|
|
MergeRequestSerializer.new(current_user: current_user, project: merge_request.project)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|