2012-05-04 11:56:11 +01:00
|
|
|
class Projects::PullRequestsController < Projects::BaseController
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :authenticate_user!
|
|
|
|
skip_before_action :authenticate_user!, only: [:index, :show] if APP_CONFIG['anonymous_access']
|
2012-09-26 18:10:04 +01:00
|
|
|
load_and_authorize_resource :project
|
2012-05-30 19:08:07 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
load_resource :issue, through: :project, find_by: :serial_id, parent: false, except: [:index, :autocomplete_to_project]
|
|
|
|
load_and_authorize_resource instance_name: :pull, through: :issue, singleton: true, except: [:index, :autocomplete_to_project]
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :find_collaborators, only: [:new, :create, :show]
|
2012-04-23 18:06:14 +01:00
|
|
|
|
|
|
|
def new
|
2012-10-03 12:59:18 +01:00
|
|
|
to_project = find_destination_project(false)
|
2015-03-17 22:33:16 +00:00
|
|
|
authorize to_project, :show?
|
2012-07-06 18:30:01 +01:00
|
|
|
|
2012-10-03 12:36:04 +01:00
|
|
|
@pull = to_project.pull_requests.new
|
|
|
|
@pull.issue = to_project.issues.new
|
2012-08-07 17:34:22 +01:00
|
|
|
set_attrs
|
2012-07-06 18:30:01 +01:00
|
|
|
|
2012-10-03 12:59:18 +01:00
|
|
|
if PullRequest.check_ref(@pull, 'to', @pull.to_ref) && PullRequest.check_ref(@pull, 'from', @pull.from_ref) || @pull.uniq_merge
|
2012-09-28 18:45:27 +01:00
|
|
|
flash.now[:warning] = @pull.errors.full_messages.join('. ')
|
2012-05-24 18:10:49 +01:00
|
|
|
else
|
2012-08-07 17:24:47 +01:00
|
|
|
@pull.check(false) # don't make event transaction
|
|
|
|
if @pull.already?
|
|
|
|
@pull.destroy
|
2014-01-21 04:51:49 +00:00
|
|
|
flash.now[:warning] = I18n.t('projects.pull_requests.up_to_date', to_ref: @pull.to_ref, from_ref: @pull.from_ref)
|
2012-08-07 17:24:47 +01:00
|
|
|
else
|
|
|
|
load_diff_commits_data
|
|
|
|
end
|
2012-05-23 17:09:11 +01:00
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2012-07-06 18:30:01 +01:00
|
|
|
unless pull_params
|
|
|
|
redirect :back
|
|
|
|
end
|
2012-10-03 12:59:18 +01:00
|
|
|
to_project = find_destination_project
|
2015-03-17 22:33:16 +00:00
|
|
|
authorize to_project, :show?
|
2012-07-06 18:30:01 +01:00
|
|
|
|
2012-10-03 12:36:04 +01:00
|
|
|
@pull = to_project.pull_requests.new pull_params
|
2015-03-14 22:10:04 +00:00
|
|
|
@pull.issue.assignee_id = (params[:issue] || {})[:assignee_id] if policy(to_project).write?
|
2012-10-03 12:36:04 +01:00
|
|
|
@pull.issue.user, @pull.issue.project, @pull.from_project = current_user, to_project, @project
|
2014-10-20 20:18:34 +01:00
|
|
|
@pull.from_project_owner_uname = @pull.from_project.owner.uname
|
|
|
|
@pull.from_project_name = @pull.from_project.name
|
|
|
|
@pull.issue.new_pull_request = true
|
2012-04-28 18:28:57 +01:00
|
|
|
|
2012-08-07 18:59:36 +01:00
|
|
|
if @pull.valid? # FIXME more clean/clever logics
|
2012-10-02 10:14:31 +01:00
|
|
|
@pull.save # set pull id
|
2013-07-09 20:54:53 +01:00
|
|
|
@pull.reload
|
2012-06-28 14:47:29 +01:00
|
|
|
@pull.check(false) # don't make event transaction
|
2012-08-07 17:34:22 +01:00
|
|
|
if @pull.already?
|
2012-05-23 17:09:11 +01:00
|
|
|
@pull.destroy
|
2014-01-21 04:51:49 +00:00
|
|
|
flash.now[:error] = I18n.t('projects.pull_requests.up_to_date', to_ref: @pull.to_ref, from_ref: @pull.from_ref)
|
2012-05-23 17:09:11 +01:00
|
|
|
render :new
|
|
|
|
else
|
2012-10-05 20:19:05 +01:00
|
|
|
@pull.send(@pull.status == 'blocked' ? 'block' : @pull.status)
|
2012-10-03 12:36:04 +01:00
|
|
|
redirect_to project_pull_request_path(@pull.to_project, @pull)
|
2012-05-23 17:09:11 +01:00
|
|
|
end
|
2012-04-28 18:28:57 +01:00
|
|
|
else
|
2012-09-28 18:45:27 +01:00
|
|
|
flash.now[:error] = t('flash.pull_request.save_error')
|
|
|
|
flash.now[:warning] = @pull.errors.full_messages.join('. ')
|
2012-07-10 17:10:12 +01:00
|
|
|
|
2012-10-03 12:36:04 +01:00
|
|
|
if @pull.errors.try(:messages) && @pull.errors.messages[:to_ref].nil? && @pull.errors.messages[:from_ref].nil?
|
2012-08-07 17:24:47 +01:00
|
|
|
@pull.check(false) # don't make event transaction
|
|
|
|
load_diff_commits_data
|
|
|
|
end
|
2012-04-28 18:28:57 +01:00
|
|
|
render :new
|
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|
|
|
|
|
2013-07-23 13:29:35 +01:00
|
|
|
def merge
|
|
|
|
status = @pull.merge!(current_user) ? 200 : 422
|
2014-01-21 04:51:49 +00:00
|
|
|
render nothing: true, status: status
|
2013-07-23 13:29:35 +01:00
|
|
|
end
|
|
|
|
|
2012-04-23 18:06:14 +01:00
|
|
|
def update
|
2013-07-23 13:29:35 +01:00
|
|
|
status = 422
|
2012-07-12 15:15:28 +01:00
|
|
|
if (action = params[:pull_request_action]) && %w(close reopen).include?(params[:pull_request_action])
|
|
|
|
if @pull.send("can_#{action}?")
|
|
|
|
@pull.set_user_and_time current_user
|
|
|
|
@pull.send(action)
|
|
|
|
@pull.check if @pull.open?
|
2013-07-23 13:29:35 +01:00
|
|
|
status = 200
|
2012-07-12 15:15:28 +01:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
render nothing: true, status: status
|
2012-05-17 17:47:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-12-24 18:41:13 +00:00
|
|
|
if @pull.nil?
|
|
|
|
redirect_to project_issue_path(@project, @issue)
|
2015-02-02 18:21:28 +00:00
|
|
|
return
|
2014-12-24 18:41:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
load_diff_commits_data
|
|
|
|
|
|
|
|
if params[:get_activity] == 'true'
|
|
|
|
render partial: 'activity', layout: false
|
|
|
|
elsif params[:get_diff] == 'true'
|
|
|
|
render partial: 'diff_tab', layout: false
|
|
|
|
elsif params[:get_commits] == 'true'
|
|
|
|
render partial: 'commits_tab', layout: false
|
2013-07-22 17:54:59 +01:00
|
|
|
end
|
2012-05-17 17:47:36 +01:00
|
|
|
end
|
|
|
|
|
2012-10-03 12:36:04 +01:00
|
|
|
def autocomplete_to_project
|
2013-04-03 17:03:52 +01:00
|
|
|
items = []
|
2015-02-10 11:57:16 +00:00
|
|
|
term = params[:query].to_s.strip.downcase
|
2014-08-28 18:13:38 +01:00
|
|
|
[ Project.where(id: @project.pull_requests.last.try(:to_project_id)),
|
2014-06-10 12:31:15 +01:00
|
|
|
@project.ancestors,
|
2015-03-26 00:26:24 +00:00
|
|
|
ProjectPolicy::Scope.new(current_user, Project).membered
|
2014-06-10 12:31:15 +01:00
|
|
|
].each do |p|
|
2013-04-04 14:33:11 +01:00
|
|
|
items.concat p.by_owner_and_name(term)
|
2013-04-03 17:03:52 +01:00
|
|
|
end
|
|
|
|
items = items.uniq{|i| i.id}.select{|e| e.repo.branches.count > 0}
|
2014-01-21 04:51:49 +00:00
|
|
|
render json: json_for_autocomplete_base(items)
|
2012-05-16 17:50:30 +01:00
|
|
|
end
|
|
|
|
|
2012-07-06 18:30:01 +01:00
|
|
|
protected
|
2012-05-16 17:50:30 +01:00
|
|
|
|
2012-07-06 18:30:01 +01:00
|
|
|
def pull_params
|
|
|
|
@pull_params ||= params[:pull_request].presence
|
2012-05-16 17:50:30 +01:00
|
|
|
end
|
|
|
|
|
2012-07-06 18:30:01 +01:00
|
|
|
def json_for_autocomplete_base items
|
|
|
|
items.collect do |project|
|
2015-02-04 18:33:24 +00:00
|
|
|
{id: project.id.to_s, name: project.name_with_owner}
|
2012-05-16 17:50:30 +01:00
|
|
|
end
|
|
|
|
end
|
2012-05-30 19:08:07 +01:00
|
|
|
|
2012-05-31 12:23:30 +01:00
|
|
|
def load_diff_commits_data
|
2012-10-18 13:50:43 +01:00
|
|
|
@commits = @pull.repo.commits_between(@pull.to_commit, @pull.from_commit)
|
2012-09-26 18:00:58 +01:00
|
|
|
@total_commits = @commits.count
|
|
|
|
@commits = @commits.last(100)
|
2012-06-07 18:23:28 +01:00
|
|
|
|
2012-10-26 15:03:46 +01:00
|
|
|
@stats = @pull.diff_stats
|
2012-10-18 13:09:12 +01:00
|
|
|
@comments, @commentable = @issue.comments, @issue
|
2012-05-31 12:23:30 +01:00
|
|
|
end
|
2012-08-07 17:34:22 +01:00
|
|
|
|
2012-10-03 12:59:18 +01:00
|
|
|
def find_destination_project bang=true
|
2015-03-14 22:10:04 +00:00
|
|
|
project = Project.find_by_owner_and_name params[:to_project]
|
2012-10-03 12:59:18 +01:00
|
|
|
raise ActiveRecord::RecordNotFound if bang && !project
|
2014-06-10 12:31:15 +01:00
|
|
|
project || @project.pull_requests.last.try(:to_project) || @project.root
|
2012-10-01 18:43:15 +01:00
|
|
|
end
|
|
|
|
|
2012-08-07 17:34:22 +01:00
|
|
|
def set_attrs
|
|
|
|
if pull_params && pull_params[:issue_attributes]
|
|
|
|
@pull.issue.title = pull_params[:issue_attributes][:title].presence
|
|
|
|
@pull.issue.body = pull_params[:issue_attributes][:body].presence
|
|
|
|
end
|
2012-10-03 12:36:04 +01:00
|
|
|
@pull.from_project = @project
|
2013-03-07 12:22:48 +00:00
|
|
|
@pull.to_ref = (pull_params[:to_ref].presence if pull_params) || @pull.to_project.default_head
|
2013-01-31 16:26:49 +00:00
|
|
|
@pull.from_ref = params[:treeish].presence || (pull_params[:from_ref].presence if pull_params) || @pull.from_project.default_head(params[:treeish])
|
2012-10-27 19:44:42 +01:00
|
|
|
@pull.from_project_owner_uname = @pull.from_project.owner.uname
|
|
|
|
@pull.from_project_name = @pull.from_project.name
|
2012-08-07 17:34:22 +01:00
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|