2012-04-23 18:06:14 +01:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-05-04 11:56:11 +01:00
|
|
|
class Projects::PullRequestsController < Projects::BaseController
|
2012-04-23 18:06:14 +01:00
|
|
|
before_filter :authenticate_user!
|
2012-05-30 19:08:07 +01:00
|
|
|
skip_before_filter :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
|
|
|
|
2012-09-26 18:09:29 +01:00
|
|
|
load_resource :issue, :through => :project, :find_by => :serial_id, :parent => false, :except => [:index, :autocomplete_base_project]
|
|
|
|
load_and_authorize_resource :instance_name => :pull, :through => :issue, :singleton => true, :except => [:index, :autocomplete_base_project]
|
2012-04-23 18:06:14 +01:00
|
|
|
|
|
|
|
def new
|
2012-10-01 18:43:15 +01:00
|
|
|
base_project = set_base_project(false)
|
2012-07-06 18:30:01 +01:00
|
|
|
authorize! :read, base_project
|
|
|
|
|
|
|
|
@pull = base_project.pull_requests.new
|
|
|
|
@pull.issue = base_project.issues.new
|
2012-08-07 17:34:22 +01:00
|
|
|
set_attrs
|
2012-07-06 18:30:01 +01:00
|
|
|
|
2012-08-07 19:06:49 +01:00
|
|
|
if PullRequest.check_ref(@pull, 'base', @pull.base_ref) && PullRequest.check_ref(@pull, 'head', @pull.head_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
|
2012-09-28 18:45:27 +01:00
|
|
|
flash.now[:warning] = I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_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
|
|
|
|
raise 'expect pull_request params' # for debug
|
|
|
|
redirect :back
|
|
|
|
end
|
2012-10-01 18:43:15 +01:00
|
|
|
base_project = set_base_project
|
2012-07-06 18:30:01 +01:00
|
|
|
authorize! :read, base_project
|
|
|
|
|
|
|
|
@pull = base_project.pull_requests.new pull_params
|
|
|
|
@pull.issue.user, @pull.issue.project, @pull.head_project = current_user, base_project, @project
|
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
|
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
|
2012-09-28 18:45:27 +01:00
|
|
|
flash.now[:error] = I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_ref)
|
2012-05-23 17:09:11 +01:00
|
|
|
render :new
|
|
|
|
else
|
2012-10-02 10:14:31 +01:00
|
|
|
@pull.send(@pull.status)
|
2012-07-10 16:48:11 +01:00
|
|
|
redirect_to project_pull_request_path(@pull.base_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-08-07 17:24:47 +01:00
|
|
|
if @pull.errors.try(:messages) && @pull.errors.messages[:base_ref].nil? && @pull.errors.messages[:head_ref].nil?
|
|
|
|
@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
|
|
|
|
|
|
|
|
def update
|
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?
|
|
|
|
end
|
|
|
|
redirect_to project_pull_request_path(@pull.base_project, @pull)
|
|
|
|
else
|
|
|
|
render :nothing => true, :status => (@pull.update_attributes(params[:pull_request]) ? 200 : 500), :layout => false
|
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|
|
|
|
|
2012-05-17 17:47:36 +01:00
|
|
|
def merge
|
2012-05-30 19:08:07 +01:00
|
|
|
@pull.check
|
2012-06-27 11:47:08 +01:00
|
|
|
unless @pull.merge!(current_user)
|
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-06-27 11:47:08 +01:00
|
|
|
end
|
2012-07-10 17:58:39 +01:00
|
|
|
redirect_to project_pull_request_path(@pull.base_project, @pull)
|
2012-05-17 17:47:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2012-05-31 12:23:30 +01:00
|
|
|
load_diff_commits_data
|
2012-05-17 17:47:36 +01:00
|
|
|
end
|
|
|
|
|
2012-09-25 21:50:40 +01:00
|
|
|
def index(status = 200)
|
2012-09-26 12:46:23 +01:00
|
|
|
@issues_with_pull_request = @project.issues.joins(:pull_request)
|
|
|
|
@issues_with_pull_request = @issues_with_pull_request.search(params[:search_pull_request])
|
2012-09-25 21:50:40 +01:00
|
|
|
|
2012-09-26 12:46:23 +01:00
|
|
|
@opened_issues, @closed_issues = @issues_with_pull_request.not_closed_or_merged.count, @issues_with_pull_request.closed_or_merged.count
|
2012-09-25 21:50:40 +01:00
|
|
|
if params[:status] == 'closed'
|
2012-09-26 12:46:23 +01:00
|
|
|
@issues_with_pull_request, @status = @issues_with_pull_request.closed_or_merged, params[:status]
|
2012-09-25 21:50:40 +01:00
|
|
|
else
|
2012-09-26 12:46:23 +01:00
|
|
|
@issues_with_pull_request, @status = @issues_with_pull_request.not_closed_or_merged, 'open'
|
2012-09-25 21:50:40 +01:00
|
|
|
end
|
|
|
|
|
2012-09-26 12:46:23 +01:00
|
|
|
@issues_with_pull_request = @issues_with_pull_request.
|
|
|
|
includes(:assignee, :user, :pull_request).def_order.uniq.
|
|
|
|
paginate :per_page => 10, :page => params[:page]
|
2012-09-25 21:50:40 +01:00
|
|
|
if status == 200
|
2012-09-26 12:46:23 +01:00
|
|
|
render 'index', :layout => request.xhr? ? 'with_sidebar' : 'application'
|
2012-09-25 21:50:40 +01:00
|
|
|
else
|
|
|
|
render :status => status, :nothing => true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-06 18:30:01 +01:00
|
|
|
def autocomplete_base_project
|
2012-09-28 16:39:45 +01:00
|
|
|
items = Project.accessible_by(current_ability, :membered) | [@project.root]
|
|
|
|
items.select! {|e| Regexp.new(params[:term].downcase).match(e.name_with_owner.downcase) && e.repo.branches.count > 0}
|
2012-08-10 17:37:21 +01: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|
|
2012-08-10 17:37:21 +01:00
|
|
|
hash = {"id" => project.id.to_s, "label" => project.name_with_owner, "value" => project.name_with_owner}
|
2012-10-01 17:03:08 +01:00
|
|
|
hash[:get_refs_url] = project_refs_list_path(project)
|
2012-07-06 18:30:01 +01:00
|
|
|
hash
|
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
|
|
|
|
repo = Grit::Repo.new(@pull.path)
|
2012-06-07 18:23:28 +01:00
|
|
|
@base_commit = @pull.common_ancestor
|
2012-05-31 12:23:30 +01:00
|
|
|
@head_commit = repo.commits(@pull.head_branch).first
|
|
|
|
|
2012-09-26 18:00:58 +01:00
|
|
|
@commits = repo.commits_between(repo.commits(@pull.base_ref).first, @head_commit)
|
|
|
|
@total_commits = @commits.count
|
|
|
|
@commits = @commits.last(100)
|
2012-06-07 18:23:28 +01:00
|
|
|
|
|
|
|
@diff = @pull.diff repo, @base_commit, @head_commit
|
|
|
|
@stats = @pull.diff_stats repo, @base_commit, @head_commit
|
2012-05-31 12:23:30 +01:00
|
|
|
end
|
2012-08-07 17:34:22 +01:00
|
|
|
|
2012-10-01 18:43:15 +01:00
|
|
|
def set_base_project bang=true
|
|
|
|
args = params[:base_project].try(:split, '/') || []
|
|
|
|
if bang
|
|
|
|
raise ActiveRecord::RecordNotFound if args.length != 2
|
|
|
|
Project.find_by_owner_and_name! *args
|
|
|
|
else
|
|
|
|
return @project.root if args.length != 2
|
|
|
|
Project.find_by_owner_and_name(*args) || @project.root
|
|
|
|
end
|
|
|
|
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
|
|
|
|
@pull.head_project = @project
|
|
|
|
@pull.base_ref = (pull_params[:base_ref].presence if pull_params) || @pull.base_project.default_branch
|
|
|
|
@pull.head_ref = params[:treeish].presence || (pull_params[:head_ref].presence if pull_params) || @pull.head_project.default_branch
|
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|