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

116 lines
4.0 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
load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :parent => false
before_filter :load_pull
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
@pull.status = @pull.soft_check
if @pull.status == 'already'
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-04-28 18:28:57 +01:00
@pull = @project.pull_requests.new(params[:pull_request]) # FIXME need validation!
@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
@pull.check
if @pull.status == 'already'
@pull.destroy
@pull.errors.add(:head_ref, I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_ref))
flash[:notice] = t('flash.pull_request.saved')
flash[:warning] = @pull.errors.full_messages.join('. ')
render :new
else
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-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
@pull.merge! current_user
2012-05-17 17:47:36 +01:00
redirect_to :show
end
def show
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
def load_pull
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