2013-06-23 12:47:22 -04:00
|
|
|
class Projects::MergeRequestsController < Projects::ApplicationController
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :module_enabled
|
2015-05-29 00:09:28 -04:00
|
|
|
before_action :merge_request, only: [
|
2015-08-11 08:33:31 -04:00
|
|
|
:edit, :update, :show, :diffs, :commits, :merge, :merge_check,
|
2015-05-29 00:09:28 -04:00
|
|
|
:ci_status, :toggle_subscription
|
|
|
|
]
|
|
|
|
before_action :closes_issues, only: [:edit, :update, :show, :diffs, :commits]
|
|
|
|
before_action :validates_merge_request, only: [:show, :diffs, :commits]
|
|
|
|
before_action :define_show_vars, only: [:show, :diffs, :commits]
|
2015-09-21 10:25:59 -04:00
|
|
|
before_action :ensure_ref_fetched, only: [:show, :commits, :diffs]
|
2011-12-15 16:57:46 -05:00
|
|
|
|
|
|
|
# Allow read any merge_request
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_read_merge_request!
|
2011-12-15 16:57:46 -05:00
|
|
|
|
|
|
|
# Allow write(create) merge_request
|
2015-06-26 10:44:21 -04:00
|
|
|
before_action :authorize_create_merge_request!, only: [:new, :create]
|
2011-12-15 16:57:46 -05:00
|
|
|
|
|
|
|
# Allow modify merge_request
|
2015-06-26 10:44:21 -04:00
|
|
|
before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :sort]
|
2011-12-15 16:57:46 -05:00
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
def index
|
2015-03-12 19:20:58 -04:00
|
|
|
terms = params['issue_search']
|
2014-12-24 04:04:33 -05:00
|
|
|
@merge_requests = get_merge_requests_collection
|
2015-05-17 13:32:58 -04:00
|
|
|
|
|
|
|
if terms.present?
|
|
|
|
if terms =~ /\A[#!](\d+)\z/
|
|
|
|
@merge_requests = @merge_requests.where(iid: $1)
|
|
|
|
else
|
|
|
|
@merge_requests = @merge_requests.full_search(terms)
|
|
|
|
end
|
|
|
|
end
|
2015-05-29 00:09:28 -04:00
|
|
|
|
2015-03-12 18:37:00 -04:00
|
|
|
@merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE)
|
2015-03-12 19:20:58 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: view_to_html_string("projects/merge_requests/_merge_requests")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-06-10 02:50:27 -04:00
|
|
|
@note_counts = Note.where(commit_id: @merge_request.commits.map(&:id)).
|
2015-02-05 23:29:41 -05:00
|
|
|
group(:commit_id).count
|
2014-07-15 11:28:21 -04:00
|
|
|
|
2011-11-28 15:16:57 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2015-01-09 22:10:01 -05:00
|
|
|
format.json { render json: @merge_request }
|
2013-04-25 10:15:33 -04:00
|
|
|
format.diff { render text: @merge_request.to_diff(current_user) }
|
|
|
|
format.patch { render text: @merge_request.to_patch(current_user) }
|
2012-11-22 15:50:36 -05:00
|
|
|
end
|
2012-07-04 18:26:23 -04:00
|
|
|
end
|
|
|
|
|
2011-11-28 15:16:57 -05:00
|
|
|
def diffs
|
2011-11-29 13:06:37 -05:00
|
|
|
@commit = @merge_request.last_commit
|
2015-10-20 08:23:56 -04:00
|
|
|
@first_commit = @merge_request.first_commit
|
|
|
|
|
2012-10-29 10:55:37 -04:00
|
|
|
@comments_allowed = @reply_allowed = true
|
2014-07-15 11:28:21 -04:00
|
|
|
@comments_target = {
|
|
|
|
noteable_type: 'MergeRequest',
|
|
|
|
noteable_id: @merge_request.id
|
|
|
|
}
|
2012-02-24 15:19:47 -05:00
|
|
|
@line_notes = @merge_request.notes.where("line_code is not null")
|
2013-09-04 03:33:09 -04:00
|
|
|
|
2013-12-02 07:27:36 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json { render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") } }
|
|
|
|
end
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
2015-05-29 00:09:28 -04:00
|
|
|
def commits
|
2015-06-17 16:12:28 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render 'show' }
|
|
|
|
format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_commits') } }
|
|
|
|
end
|
2015-05-29 00:09:28 -04:00
|
|
|
end
|
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
def new
|
2014-07-15 08:34:06 -04:00
|
|
|
params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
|
|
|
|
@merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
|
|
|
|
|
|
|
|
@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
|
2015-10-20 08:23:56 -04:00
|
|
|
@commit = @merge_request.last_commit
|
|
|
|
@first_commit = @merge_request.first_commit
|
2014-07-15 08:34:06 -04:00
|
|
|
@diffs = @merge_request.compare_diffs
|
|
|
|
@note_counts = Note.where(commit_id: @commits.map(&:id)).
|
|
|
|
group(:commit_id).count
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2013-06-03 16:20:50 -04:00
|
|
|
@source_project = @merge_request.source_project
|
|
|
|
@target_project = @merge_request.target_project
|
2013-04-25 10:15:33 -04:00
|
|
|
@target_branches = @merge_request.target_project.repository.branch_names
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2013-04-25 10:15:33 -04:00
|
|
|
@target_branches ||= []
|
2014-06-26 07:30:07 -04:00
|
|
|
@merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
|
2014-04-02 14:51:53 -04:00
|
|
|
|
|
|
|
if @merge_request.valid?
|
2015-03-11 19:07:33 -04:00
|
|
|
redirect_to(merge_request_path(@merge_request))
|
2012-06-13 02:03:53 -04:00
|
|
|
else
|
2013-06-08 11:51:18 -04:00
|
|
|
@source_project = @merge_request.source_project
|
|
|
|
@target_project = @merge_request.target_project
|
|
|
|
render action: "new"
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-06-26 07:30:07 -04:00
|
|
|
@merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
|
2014-02-18 09:24:19 -05:00
|
|
|
|
2014-04-02 14:51:53 -04:00
|
|
|
if @merge_request.valid?
|
2014-02-18 09:24:19 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
format.html do
|
2015-01-24 13:02:58 -05:00
|
|
|
redirect_to([@merge_request.target_project.namespace.becomes(Namespace),
|
2015-03-11 19:07:33 -04:00
|
|
|
@merge_request.target_project, @merge_request])
|
2014-02-18 09:24:19 -05:00
|
|
|
end
|
2015-03-06 08:03:41 -05:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
saved: @merge_request.valid?,
|
|
|
|
assignee_avatar_url: @merge_request.assignee.try(:avatar_url)
|
|
|
|
}
|
|
|
|
end
|
2014-02-18 09:24:19 -05:00
|
|
|
end
|
2012-06-13 02:03:53 -04:00
|
|
|
else
|
2013-06-22 08:08:11 -04:00
|
|
|
render "edit"
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def merge_check
|
2012-10-09 15:09:46 -04:00
|
|
|
if @merge_request.unchecked?
|
2012-04-16 15:08:03 -04:00
|
|
|
@merge_request.check_if_can_be_merged
|
|
|
|
end
|
2015-01-09 22:10:01 -05:00
|
|
|
|
2015-06-11 09:14:50 -04:00
|
|
|
closes_issues
|
|
|
|
|
2015-06-11 12:35:43 -04:00
|
|
|
render partial: "projects/merge_requests/widget/show.html.haml", layout: false
|
2012-04-16 15:08:03 -04: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
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
if @merge_request.mergeable?
|
2015-10-01 02:31:48 -04:00
|
|
|
@merge_request.update(merge_error: nil)
|
2015-08-11 08:33:31 -04:00
|
|
|
MergeWorker.perform_async(@merge_request.id, current_user.id, params)
|
2012-04-16 16:05:08 -04:00
|
|
|
@status = true
|
|
|
|
else
|
|
|
|
@status = false
|
|
|
|
end
|
2012-03-29 11:03:05 -04:00
|
|
|
end
|
|
|
|
|
2012-03-13 17:54:49 -04:00
|
|
|
def branch_from
|
2013-04-25 10:15:33 -04:00
|
|
|
#This is always source
|
2013-06-03 16:20:50 -04:00
|
|
|
@source_project = @merge_request.nil? ? @project : @merge_request.source_project
|
2013-08-08 08:14:59 -04:00
|
|
|
@commit = @repository.commit(params[:ref]) if params[:ref].present?
|
2012-03-13 17:54:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def branch_to
|
2013-04-25 10:15:33 -04:00
|
|
|
@target_project = selected_target_project
|
2015-04-21 09:13:40 -04:00
|
|
|
@commit = @target_project.commit(params[:ref]) if params[:ref].present?
|
2012-03-13 17:54:49 -04:00
|
|
|
end
|
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
def update_branches
|
|
|
|
@target_project = selected_target_project
|
2013-06-08 11:51:18 -04:00
|
|
|
@target_branches = @target_project.repository.branch_names
|
2014-01-23 03:08:26 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
2012-12-10 22:14:05 -05:00
|
|
|
def ci_status
|
2014-10-02 11:17:47 -04:00
|
|
|
ci_service = @merge_request.source_project.ci_service
|
2015-03-05 09:58:04 -05:00
|
|
|
status = ci_service.commit_status(merge_request.last_commit.sha, merge_request.source_branch)
|
2014-10-02 11:17:47 -04:00
|
|
|
|
|
|
|
if ci_service.respond_to?(:commit_coverage)
|
2015-03-05 09:58:04 -05:00
|
|
|
coverage = ci_service.commit_coverage(merge_request.last_commit.sha, merge_request.source_branch)
|
2014-10-02 11:17:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
response = {
|
|
|
|
status: status,
|
|
|
|
coverage: coverage
|
|
|
|
}
|
2012-12-10 22:14:05 -05:00
|
|
|
|
|
|
|
render json: response
|
|
|
|
end
|
|
|
|
|
2015-03-16 11:20:17 -04:00
|
|
|
def toggle_subscription
|
|
|
|
@merge_request.toggle_subscription(current_user)
|
2015-04-16 08:03:37 -04:00
|
|
|
|
2015-03-15 12:17:12 -04:00
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
protected
|
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
def selected_target_project
|
2014-01-31 06:24:38 -05:00
|
|
|
if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
|
|
|
|
@project
|
|
|
|
else
|
|
|
|
@project.forked_project_link.forked_from_project
|
|
|
|
end
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
2011-11-28 02:39:43 -05:00
|
|
|
def merge_request
|
2014-01-19 13:55:59 -05:00
|
|
|
@merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|
2011-12-15 16:57:46 -05:00
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
def closes_issues
|
|
|
|
@closes_issues ||= @merge_request.closes_issues
|
|
|
|
end
|
|
|
|
|
2015-06-26 10:44:21 -04:00
|
|
|
def authorize_update_merge_request!
|
2015-06-26 09:55:56 -04:00
|
|
|
return render_404 unless can?(current_user, :update_merge_request, @merge_request)
|
2011-12-15 16:57:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_merge_request!
|
2012-02-21 17:31:18 -05:00
|
|
|
return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
|
2011-12-15 16:57:46 -05:00
|
|
|
end
|
2012-02-06 12:40:32 -05:00
|
|
|
|
|
|
|
def module_enabled
|
|
|
|
return render_404 unless @project.merge_requests_enabled
|
|
|
|
end
|
2012-06-29 14:55:22 -04:00
|
|
|
|
|
|
|
def validates_merge_request
|
2013-12-12 04:34:42 -05:00
|
|
|
# If source project was removed (Ex. mr from fork to origin)
|
|
|
|
return invalid_mr unless @merge_request.source_project
|
|
|
|
|
2013-07-31 12:31:34 -04:00
|
|
|
# Show git not found page
|
|
|
|
# if there is no saved commits between source & target branch
|
|
|
|
if @merge_request.commits.blank?
|
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
|
|
|
|
2013-12-12 04:34:42 -05:00
|
|
|
# or if source branch doesn't exist
|
|
|
|
return invalid_mr unless @merge_request.source_branch_exists?
|
2013-07-31 12:31:34 -04:00
|
|
|
end
|
2012-06-29 14:55:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def define_show_vars
|
2015-10-12 05:54:46 -04:00
|
|
|
@participants = @merge_request.participants(current_user)
|
2015-06-24 06:56:10 -04:00
|
|
|
|
2012-06-29 14:55:22 -04:00
|
|
|
# Build a note object for comment form
|
2012-08-10 18:07:50 -04:00
|
|
|
@note = @project.notes.new(noteable: @merge_request)
|
2013-12-25 06:32:43 -05:00
|
|
|
@notes = @merge_request.mr_and_commit_notes.inc_author.fresh
|
|
|
|
@discussions = Note.discussions_from_notes(@notes)
|
2013-12-25 15:32:48 -05:00
|
|
|
@noteable = @merge_request
|
2012-06-29 14:55:22 -04:00
|
|
|
|
2012-10-09 15:09:46 -04:00
|
|
|
# Get commits from repository
|
2012-06-29 14:55:22 -04:00
|
|
|
# or from cache if already merged
|
|
|
|
@commits = @merge_request.commits
|
2013-02-27 13:20:31 -05:00
|
|
|
|
2014-01-22 08:20:40 -05:00
|
|
|
@merge_request_diff = @merge_request.merge_request_diff
|
2015-10-12 06:04:20 -04:00
|
|
|
|
2014-12-05 09:02:08 -05:00
|
|
|
if @merge_request.locked_long_ago?
|
|
|
|
@merge_request.unlock_mr
|
|
|
|
@merge_request.close
|
|
|
|
end
|
2013-02-27 13:20:31 -05:00
|
|
|
end
|
|
|
|
|
2013-11-27 03:17:29 -05:00
|
|
|
def invalid_mr
|
|
|
|
# Render special view for MR with removed source or target branch
|
|
|
|
render 'invalid'
|
|
|
|
end
|
|
|
|
|
2014-06-26 07:30:07 -04:00
|
|
|
def merge_request_params
|
|
|
|
params.require(:merge_request).permit(
|
|
|
|
:title, :assignee_id, :source_project_id, :source_branch,
|
|
|
|
:target_project_id, :target_branch, :milestone_id,
|
2014-10-05 01:53:44 -04:00
|
|
|
:state_event, :description, :task_num, label_ids: []
|
2014-06-26 07:30:07 -04:00
|
|
|
)
|
|
|
|
end
|
2015-09-21 10:25:59 -04:00
|
|
|
|
|
|
|
# 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
|
2011-11-28 02:39:43 -05:00
|
|
|
end
|