2013-02-21 09:44:33 +00:00
|
|
|
require 'gitlab/satellite/satellite'
|
|
|
|
|
2013-06-23 16:47:22 +00:00
|
|
|
class Projects::MergeRequestsController < Projects::ApplicationController
|
2012-02-06 17:40:32 +00:00
|
|
|
before_filter :module_enabled
|
2012-12-19 03:14:05 +00:00
|
|
|
before_filter :merge_request, only: [:edit, :update, :show, :commits, :diffs, :automerge, :automerge_check, :ci_status]
|
2012-11-22 20:50:36 +00:00
|
|
|
before_filter :validates_merge_request, only: [:show, :diffs]
|
2012-08-10 22:07:50 +00:00
|
|
|
before_filter :define_show_vars, only: [:show, :diffs]
|
2011-12-15 21:57:46 +00:00
|
|
|
|
|
|
|
# Allow read any merge_request
|
|
|
|
before_filter :authorize_read_merge_request!
|
|
|
|
|
|
|
|
# Allow write(create) merge_request
|
2012-08-10 22:07:50 +00:00
|
|
|
before_filter :authorize_write_merge_request!, only: [:new, :create]
|
2011-12-15 21:57:46 +00:00
|
|
|
|
|
|
|
# Allow modify merge_request
|
2012-08-10 22:07:50 +00:00
|
|
|
before_filter :authorize_modify_merge_request!, only: [:close, :edit, :update, :sort]
|
2011-12-15 21:57:46 +00:00
|
|
|
|
2011-11-28 07:39:43 +00:00
|
|
|
def index
|
2012-10-09 19:09:46 +00:00
|
|
|
@merge_requests = MergeRequestsLoadContext.new(project, current_user, params).execute
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2011-11-28 20:16:57 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2012-02-24 07:16:06 +00:00
|
|
|
format.js
|
2011-11-28 20:16:57 +00:00
|
|
|
|
2013-04-25 14:15:33 +00:00
|
|
|
format.diff { render text: @merge_request.to_diff(current_user) }
|
|
|
|
format.patch { render text: @merge_request.to_patch(current_user) }
|
2012-11-22 20:50:36 +00:00
|
|
|
end
|
2012-07-04 22:26:23 +00:00
|
|
|
end
|
|
|
|
|
2011-11-28 20:16:57 +00:00
|
|
|
def diffs
|
2011-11-29 18:06:37 +00:00
|
|
|
@commit = @merge_request.last_commit
|
2012-02-24 20:19:47 +00:00
|
|
|
|
2012-10-29 14:55:37 +00:00
|
|
|
@comments_allowed = @reply_allowed = true
|
2013-04-25 14:15:33 +00:00
|
|
|
@comments_target = {noteable_type: 'MergeRequest',
|
|
|
|
noteable_id: @merge_request.id}
|
2012-02-24 20:19:47 +00:00
|
|
|
@line_notes = @merge_request.notes.where("line_code is not null")
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2012-06-12 14:43:16 +00:00
|
|
|
@merge_request = @project.merge_requests.new(params[:merge_request])
|
2013-04-25 14:15:33 +00:00
|
|
|
|
|
|
|
if params[:merge_request] && params[:merge_request][:source_project_id]
|
|
|
|
@merge_request.source_project = Project.find_by_id(params[:merge_request][:source_project_id])
|
|
|
|
else
|
|
|
|
@merge_request.source_project = @project
|
|
|
|
end
|
|
|
|
if params[:merge_request] && params[:merge_request][:target_project_id]
|
|
|
|
@merge_request.target_project = Project.find_by_id(params[:merge_request][:target_project_id])
|
|
|
|
end
|
|
|
|
@target_branches = @merge_request.target_project.nil? ? [] : @merge_request.target_project.repository.branch_names
|
|
|
|
@merge_request
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2013-04-25 14:15:33 +00:00
|
|
|
@target_branches = @merge_request.target_project.repository.branch_names
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@merge_request = @project.merge_requests.new(params[:merge_request])
|
|
|
|
@merge_request.author = current_user
|
2013-04-25 14:15:33 +00:00
|
|
|
@merge_request.source_project_id = params[:merge_request][:source_project_id].to_i
|
|
|
|
@merge_request.target_project_id = params[:merge_request][:target_project_id].to_i
|
|
|
|
@target_branches ||= []
|
2012-06-13 06:03:53 +00:00
|
|
|
if @merge_request.save
|
|
|
|
@merge_request.reload_code
|
2013-04-25 14:15:33 +00:00
|
|
|
redirect_to [@merge_request.target_project, @merge_request], notice: 'Merge request was successfully created.'
|
2012-06-13 06:03:53 +00:00
|
|
|
else
|
2013-06-22 12:08:11 +00:00
|
|
|
render "new"
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-08-10 22:07:50 +00:00
|
|
|
if @merge_request.update_attributes(params[:merge_request].merge(author_id_of_changes: current_user.id))
|
2012-06-13 06:03:53 +00:00
|
|
|
@merge_request.reload_code
|
|
|
|
@merge_request.mark_as_unchecked
|
|
|
|
redirect_to [@project, @merge_request], notice: 'Merge request was successfully updated.'
|
|
|
|
else
|
2013-06-22 12:08:11 +00:00
|
|
|
render "edit"
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-16 19:08:03 +00:00
|
|
|
def automerge_check
|
2012-10-09 19:09:46 +00:00
|
|
|
if @merge_request.unchecked?
|
2012-04-16 19:08:03 +00:00
|
|
|
@merge_request.check_if_can_be_merged
|
|
|
|
end
|
2013-02-26 08:47:27 +00:00
|
|
|
render json: {merge_status: @merge_request.merge_status_name}
|
2013-01-05 21:09:26 +00:00
|
|
|
rescue Gitlab::SatelliteNotExistError
|
2013-02-15 08:27:13 +00:00
|
|
|
render json: {merge_status: :no_satellite}
|
2012-04-16 19:08:03 +00:00
|
|
|
end
|
|
|
|
|
2012-03-29 15:03:05 +00:00
|
|
|
def automerge
|
2013-02-27 18:20:31 +00:00
|
|
|
return access_denied! unless allowed_to_merge?
|
|
|
|
|
2013-02-18 08:40:56 +00:00
|
|
|
if @merge_request.opened? && @merge_request.can_be_merged?
|
2012-06-03 18:29:20 +00:00
|
|
|
@merge_request.should_remove_source_branch = params[:should_remove_source_branch]
|
2012-04-16 20:05:08 +00:00
|
|
|
@merge_request.automerge!(current_user)
|
|
|
|
@status = true
|
|
|
|
else
|
|
|
|
@status = false
|
|
|
|
end
|
2012-03-29 15:03:05 +00:00
|
|
|
end
|
|
|
|
|
2012-03-13 21:54:49 +00:00
|
|
|
def branch_from
|
2013-04-25 14:15:33 +00:00
|
|
|
#This is always source
|
2013-01-04 06:43:25 +00:00
|
|
|
@commit = @repository.commit(params[:ref])
|
2012-03-13 21:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def branch_to
|
2013-04-25 14:15:33 +00:00
|
|
|
@target_project = selected_target_project
|
|
|
|
@commit = @target_project.repository.commit(params[:ref])
|
2012-03-13 21:54:49 +00:00
|
|
|
end
|
|
|
|
|
2013-04-25 14:15:33 +00:00
|
|
|
def update_branches
|
|
|
|
@target_project = selected_target_project
|
|
|
|
@target_branches = (@target_project.repository.branch_names).unshift("Select branch")
|
|
|
|
@target_branches
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-12-11 03:14:05 +00:00
|
|
|
def ci_status
|
|
|
|
status = project.gitlab_ci_service.commit_status(merge_request.last_commit.sha)
|
2013-04-25 14:15:33 +00:00
|
|
|
response = {status: status}
|
2012-12-11 03:14:05 +00:00
|
|
|
|
|
|
|
render json: response
|
|
|
|
end
|
|
|
|
|
2011-11-28 07:39:43 +00:00
|
|
|
protected
|
|
|
|
|
2013-04-25 14:15:33 +00:00
|
|
|
def selected_target_project
|
|
|
|
((@project.id.to_s == params[:target_project_id]) || @project.forked_project_link.nil?) ? @project : @project.forked_project_link.forked_from_project
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-11-28 07:39:43 +00:00
|
|
|
def merge_request
|
|
|
|
@merge_request ||= @project.merge_requests.find(params[:id])
|
|
|
|
end
|
2011-12-15 21:57:46 +00:00
|
|
|
|
|
|
|
def authorize_modify_merge_request!
|
2012-02-21 22:31:18 +00:00
|
|
|
return render_404 unless can?(current_user, :modify_merge_request, @merge_request)
|
2011-12-15 21:57:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_merge_request!
|
2012-02-21 22:31:18 +00:00
|
|
|
return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
|
2011-12-15 21:57:46 +00:00
|
|
|
end
|
2012-02-06 17:40:32 +00:00
|
|
|
|
|
|
|
def module_enabled
|
|
|
|
return render_404 unless @project.merge_requests_enabled
|
|
|
|
end
|
2012-06-29 18:55:22 +00:00
|
|
|
|
|
|
|
def validates_merge_request
|
2013-03-17 19:46:54 +00:00
|
|
|
# Show git not found page if target branch doesn't exist
|
2013-04-25 14:15:33 +00:00
|
|
|
return invalid_mr unless @merge_request.target_project.repository.branch_names.include?(@merge_request.target_branch)
|
2012-06-29 18:55:22 +00:00
|
|
|
|
2013-03-17 19:46:54 +00:00
|
|
|
# Show git not found page if source branch doesn't exist
|
2012-06-29 18:55:22 +00:00
|
|
|
# and there is no saved commits between source & target branch
|
2013-04-25 14:15:33 +00:00
|
|
|
return invalid_mr if !@merge_request.source_project.repository.branch_names.include?(@merge_request.source_branch) && @merge_request.commits.blank?
|
2012-06-29 18:55:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def define_show_vars
|
|
|
|
# Build a note object for comment form
|
2012-08-10 22:07:50 +00:00
|
|
|
@note = @project.notes.new(noteable: @merge_request)
|
2012-06-29 18:55:22 +00:00
|
|
|
|
2012-10-09 19:09:46 +00:00
|
|
|
# Get commits from repository
|
2012-06-29 18:55:22 +00:00
|
|
|
# or from cache if already merged
|
|
|
|
@commits = @merge_request.commits
|
2013-02-27 18:20:31 +00:00
|
|
|
|
|
|
|
@allowed_to_merge = allowed_to_merge?
|
|
|
|
@show_merge_controls = @merge_request.opened? && @commits.any? && @allowed_to_merge
|
2013-04-03 19:14:07 +00:00
|
|
|
|
|
|
|
@target_type = :merge_request
|
|
|
|
@target_id = @merge_request.id
|
2013-02-27 18:20:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_to_merge?
|
|
|
|
action = if project.protected_branch?(@merge_request.target_branch)
|
|
|
|
:push_code_to_protected_branches
|
|
|
|
else
|
|
|
|
:push_code
|
|
|
|
end
|
|
|
|
|
|
|
|
can?(current_user, action, @project)
|
2012-06-29 18:55:22 +00:00
|
|
|
end
|
2013-03-20 18:22:29 +00:00
|
|
|
|
|
|
|
def invalid_mr
|
|
|
|
# Render special view for MR with removed source or target branch
|
|
|
|
render 'invalid'
|
|
|
|
end
|
2011-11-28 07:39:43 +00:00
|
|
|
end
|