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-04-23 18:06:14 +01:00
|
|
|
load_resource :project
|
2012-05-30 19:08:07 +01:00
|
|
|
|
|
|
|
load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :parent => false
|
|
|
|
before_filter :load_pull
|
2012-04-23 18:06:14 +01:00
|
|
|
|
|
|
|
def new
|
2012-04-28 18:28:57 +01:00
|
|
|
@pull = PullRequest.default_base_project(@project).pull_requests.new
|
|
|
|
@pull.issue = @project.issues.new
|
2012-05-24 18:10:49 +01:00
|
|
|
if params[:pull_request] && params[:pull_request][:issue_attributes]
|
|
|
|
@pull.issue.title = params[:pull_request][:issue_attributes][:title].presence
|
|
|
|
@pull.issue.body = params[:pull_request][:issue_attributes][:body].presence
|
|
|
|
end
|
2012-04-28 18:28:57 +01:00
|
|
|
@pull.head_project = @project
|
2012-05-24 18:10:49 +01:00
|
|
|
@pull.base_ref = (params[:pull_request][:base_ref].presence if params[:pull_request]) || @pull.base_project.default_branch
|
|
|
|
@pull.head_ref = params[:treeish].presence || (params[:pull_request][:head_ref].presence if params[:pull_request]) || @pull.head_project.default_branch
|
2012-05-29 11:09:37 +01:00
|
|
|
@pull.status = @pull.soft_check
|
|
|
|
if @pull.status == 'already'
|
2012-06-09 10:09:38 +01:00
|
|
|
@pull.destroy
|
2012-05-24 18:10:49 +01:00
|
|
|
flash[:warning] = I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_ref)
|
|
|
|
else
|
2012-05-31 12:23:30 +01:00
|
|
|
load_diff_commits_data
|
2012-05-23 17:09:11 +01:00
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2012-06-13 18:52:11 +01:00
|
|
|
@pull = @project.pull_requests.new(params[:pull_request])
|
2012-04-28 18:28:57 +01:00
|
|
|
@pull.issue.user, @pull.issue.project = current_user, @pull.base_project
|
2012-05-16 17:50:30 +01:00
|
|
|
@pull.base_project, @pull.head_project = PullRequest.default_base_project(@project), @project
|
2012-04-28 18:28:57 +01:00
|
|
|
|
|
|
|
if @pull.save
|
2012-06-09 10:09:38 +01:00
|
|
|
@pull.status = @pull.soft_check
|
2012-05-29 11:09:37 +01:00
|
|
|
if @pull.status == 'already'
|
2012-05-23 17:09:11 +01:00
|
|
|
@pull.destroy
|
2012-06-09 10:09:38 +01:00
|
|
|
flash[: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
|
|
|
|
redirect_to project_pull_request_path(@project, @pull)
|
|
|
|
end
|
2012-04-28 18:28:57 +01:00
|
|
|
else
|
2012-05-23 17:09:11 +01:00
|
|
|
flash[:error] = t('flash.pull_request.save_error')
|
|
|
|
flash[:warning] = @pull.errors.full_messages.join('. ')
|
2012-04-28 18:28:57 +01:00
|
|
|
render :new
|
|
|
|
end
|
2012-04-23 18:06:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-05-31 17:56:27 +01:00
|
|
|
render :nothing => true, :status => (@pull.update_attributes(params[:pull_request]) ? 200 : 500), :layout => false
|
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
|
|
|
|
@pull.merge! current_user
|
2012-05-17 17:47:36 +01:00
|
|
|
redirect_to :show
|
|
|
|
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-05-14 18:09:18 +01:00
|
|
|
def autocomplete_base_project_name
|
|
|
|
items = Project.accessible_by(current_ability, :membered)
|
2012-05-16 17:50:30 +01:00
|
|
|
items << PullRequest.default_base_project(@project)
|
|
|
|
items.uniq!
|
2012-05-14 18:09:18 +01:00
|
|
|
render :json => json_for_autocomplete(items, 'full_name')
|
|
|
|
end
|
|
|
|
|
2012-05-16 17:50:30 +01:00
|
|
|
def autocomplete_head_project_name
|
|
|
|
items = Project.accessible_by(current_ability, :membered)
|
|
|
|
render :json => json_for_autocomplete(items, 'full_name')
|
|
|
|
end
|
|
|
|
|
|
|
|
def autocomplete_base_ref
|
|
|
|
project = PullRequest.default_base_project(@project)
|
|
|
|
items = (project.branches + project.tags).select {|e| Regexp.new(params[:term].downcase).match e.name.downcase}
|
|
|
|
render :json => json_for_autocomplete_ref(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
def autocomplete_head_ref
|
|
|
|
items = (@project.branches + @project.tags).select {|e| Regexp.new(params[:term].downcase).match e.name.downcase}
|
|
|
|
render :json => json_for_autocomplete_ref(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def json_for_autocomplete_ref(items)
|
|
|
|
items.collect do |item|
|
|
|
|
{"id" => item.name, "label" => item.name, "value" => item.name}
|
|
|
|
end
|
|
|
|
end
|
2012-05-30 19:08:07 +01:00
|
|
|
|
|
|
|
def load_pull
|
2012-06-25 17:04:43 +01:00
|
|
|
@issue ||= @issues.first #FIXME!
|
2012-05-30 19:08:07 +01:00
|
|
|
if params[:action].to_sym != :index
|
2012-05-31 17:56:27 +01:00
|
|
|
@pull = @project.pull_requests.joins(:issue).where(:issues => {:id => @issue.id}).readonly(false).first
|
2012-05-30 19:08:07 +01:00
|
|
|
else
|
|
|
|
@pull_requests = @project.pull_requests
|
|
|
|
end
|
|
|
|
end
|
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-06-07 18:23:28 +01:00
|
|
|
@commits = repo.commits_between repo.commits(@pull.base_ref).first, @head_commit
|
|
|
|
|
|
|
|
@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-04-23 18:06:14 +01:00
|
|
|
end
|