[refs #90] try to prevent create same pull

This commit is contained in:
Alexander Machehin 2012-05-21 19:24:16 +06:00
parent 6b6887250b
commit d643bb3cd0
6 changed files with 57 additions and 27 deletions

View File

@ -10,7 +10,6 @@ class Projects::PullRequestsController < Projects::BaseController
def new
@pull = PullRequest.default_base_project(@project).pull_requests.new
FileUtils.rm_rf @pull.path
#@pull.build_issue
@pull.issue = @project.issues.new
@pull.head_project = @project

View File

@ -8,7 +8,10 @@ class PullRequest < ActiveRecord::Base
delegate :user, :title, :body, :serial_id, :assignee, :state, :to => :issue, :allow_nil => true
accepts_nested_attributes_for :issue
#attr_accessible #FIXME disable for development
scope :needed_checking, where(:state => ['open', 'blocked', 'ready'])
#validate :uniq_merge
before_create :clean_dir
scope :needed_checking, includes(:issue).where(:issues => {:state => ['open', 'blocked', 'ready', 'already']})
state_machine :initial => :open do
#after_transition [:ready, :blocked] => [:merged, :closed] do |pull, transition|
@ -79,19 +82,6 @@ class PullRequest < ActiveRecord::Base
project.is_root? ? project : project.root
end
def clean #FIXME move to protected
Dir.chdir(path) do
base_project.branches.each {|branch| system 'git', 'checkout', branch.name}
system 'git', 'checkout', base_ref
base_project.branches.each do |branch|
system 'git', 'branch', '-D', branch.name unless [base_ref, head_branch].include? branch.name
end
base_project.tags.each do |tag|
system 'git', 'tag', '-d', tag.name unless [base_ref, head_branch].include? tag.name
end
end
end
def path
filename = [id, base_ref, head_project.owner.uname, head_project.name, head_ref].compact.join('-')
@ -143,4 +133,28 @@ class PullRequest < ActiveRecord::Base
end
# TODO catch errors
end
def clean
Dir.chdir(path) do
base_project.branches.each {|branch| system 'git', 'checkout', branch.name}
system 'git', 'checkout', base_ref
base_project.branches.each do |branch|
system 'git', 'branch', '-D', branch.name unless [base_ref, head_branch].include? branch.name
end
base_project.tags.each do |tag|
system 'git', 'tag', '-d', tag.name unless [base_ref, head_branch].include? tag.name
end
end
end
def uniq_merge
if base_project.pull_requests.needed_checking.where('pull_requests.id != ?', id).count >= 1
errors.add(:head_ref, t('projects.pull_requests.duplicate', :head_ref => head_ref))
end
end
def clean_dir
FileUtils.rm_rf path
end
end

View File

@ -33,6 +33,11 @@
.leftlist=issue.label :title, t('activerecord.attributes.issue.body'), :class => :label
.rightlist=issue.text_area :body
.both
.leftlist for debug
.rightlist=@pull.state
.both
.leftlist
\ 
.rightlist=f.submit t('.submit'), :class => 'btn btn-primary disabled', 'data-loading-text' => t('layout.processing'), :id => 'create_pull'

View File

@ -3,13 +3,14 @@ en:
pull_requests:
new:
new: 'Create a pull request into <span class="label label-info font14">%{base}</span> from <span class="label label-info font14">%{head}</span>'
base_ref: 'Base'
head_ref: 'Head'
base_ref: Base
head_ref: Head
refs: 'branch · tag · commit'
base_project: 'Base project'
head_project: 'Head project'
submit: 'Send pull request'
base_project: Base project
head_project: Head project
submit: Send pull request
merge: Merge
duplicate: There's already a pull request for %{head_ref}
pull_requests:
tabs:

View File

@ -3,13 +3,14 @@ ru:
pull_requests:
new:
new: 'Создать запрос на слияние в <span class="label label-info font14">%{base}</span> из <span class="label label-info font14">%{head}</span>'
base_ref: 'База'
head_ref: 'Источник'
base_ref: База
head_ref: Источник
refs: 'branch · tag · commit'
base_project: 'Базовый проект'
head_project: 'Проект-источник'
submit: 'Создать запрос на слияние'
base_project: Базовый проект
head_project: Проект-источник
submit: Создать запрос на слияние
merge: Слияние
duplicate: Уже существует запрос на слияние %{head_ref}
pull_requests:
tabs:

View File

@ -23,7 +23,7 @@ describe PullRequest do
context 'for owner user' do
before (:all) do
stub_rsync_methods
stub_symlink_methods
@user = FactoryGirl.create(:user)
set_data_for_pull
@pull = @project.pull_requests.new(:issue_attributes => {:title => 'test', :body => 'testing'})
@ -74,10 +74,20 @@ describe PullRequest do
@other_pull.state.should == 'already'
end
end
it "should not be created same pull" do
@same_pull = @project.pull_requests.new(:issue_attributes => {:title => 'same', :body => 'testing'})
@same_pull.issue.user, @same_pull.issue.project = @user, @same_pull.base_project
@same_pull.base_ref = 'master'
@same_pull.head_project, @same_pull.head_ref = @project, 'non_conflicts'
@same_pull.save
@project.pull_requests.includes(:issue).where(:issues => {:title => @same_pull.title}).count.should == 0
end
end
before(:all) do
stub_rsync_methods
stub_symlink_methods
Platform.delete_all
User.delete_all
Repository.delete_all