rosa-build/app/controllers/projects/pull_requests_controller.rb

125 lines
4.2 KiB
Ruby
Raw Normal View History

# -*- encoding : utf-8 -*-
2012-05-04 11:56:11 +01:00
class Projects::PullRequestsController < Projects::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:index, :show] if APP_CONFIG['anonymous_access']
load_resource :project
2012-07-06 18:30:01 +01:00
load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :parent => false, :except => :autocomplete_base_project
before_filter :load_pull, :except => :autocomplete_base_project
def new
2012-07-06 18:30:01 +01:00
base_project = (Project.find(params[:base_project_id]) if params[:base_project_id]) || PullRequest.default_base_project(@project)
authorize! :read, base_project
@pull = base_project.pull_requests.new
@pull.issue = base_project.issues.new
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
2012-05-24 18:10:49 +01:00
end
2012-04-28 18:28:57 +01:00
@pull.head_project = @project
2012-07-06 18:30:01 +01:00
@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
2012-06-28 14:47:29 +01:00
@pull.check(false) # don't make event transaction
if @pull.status == 'already'
@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
load_diff_commits_data
end
end
def create
2012-07-06 18:30:01 +01:00
unless pull_params
raise 'expect pull_request params' # for debug
redirect :back
end
base_project = Project.find(params[:base_project_id])
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
if @pull.save
2012-06-28 14:47:29 +01:00
@pull.check(false) # don't make event transaction
if @pull.status == 'already'
@pull.destroy
flash[:error] = I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_ref)
render :new
else
2012-06-27 11:47:08 +01:00
@pull.save
redirect_to project_pull_request_path(@project, @pull)
end
2012-04-28 18:28:57 +01:00
else
flash[:error] = t('flash.pull_request.save_error')
flash[:warning] = @pull.errors.full_messages.join('. ')
2012-07-06 18:30:01 +01:00
@commits = @diff = @stats = nil
2012-04-28 18:28:57 +01:00
render :new
end
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
end
2012-05-17 17:47:36 +01:00
def merge
@pull.check
2012-06-27 11:47:08 +01:00
load_diff_commits_data
unless @pull.merge!(current_user)
flash[:error] = t('flash.pull_request.save_error')
flash[:warning] = @pull.errors.full_messages.join('. ')
end
render :show
2012-05-17 17:47:36 +01:00
end
def show
load_diff_commits_data
2012-05-17 17:47:36 +01:00
end
2012-07-06 18:30:01 +01:00
def autocomplete_base_project
#Maybe slow? ILIKE?
2012-05-14 18:09:18 +01:00
items = Project.accessible_by(current_ability, :membered)
2012-05-16 17:50:30 +01:00
items << PullRequest.default_base_project(@project)
2012-07-06 18:30:01 +01:00
logger.debug "items.count is #{items.count}"
items.select! {|e| Regexp.new(params[:term].downcase).match e.name.downcase}
2012-05-16 17:50:30 +01:00
items.uniq!
2012-07-06 18:30:01 +01:00
render :json => json_for_autocomplete_base(items)#, :full_name, [:branches])
2012-05-14 18:09:18 +01:00
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|
hash = {"id" => project.id.to_s, "label" => project.full_name, "value" => project.full_name}
hash[:refs] = project.branches_and_tags.map &:name
hash
2012-05-16 17:50:30 +01:00
end
end
def load_pull
2012-07-06 18:30:01 +01:00
@issue ||= @issues.first #FIXME! merge action create @issues?
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
else
@pull_requests = @project.pull_requests
end
end
def load_diff_commits_data
repo = Grit::Repo.new(@pull.path)
2012-06-07 18:23:28 +01:00
@base_commit = @pull.common_ancestor
@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
end
end