[refs #90] fixed orphan pulls

This commit is contained in:
Alexander Machehin 2012-10-28 00:04:28 +06:00
parent ba74024b29
commit c6a0dfee97
7 changed files with 62 additions and 18 deletions

View File

@ -27,20 +27,27 @@ module PullRequestHelper
#{show_ref pull, 'from'}</span> \
#{t 'into'} <span class='label-bootstrap label-info font14'> \
#{show_ref pull, 'to'}</span>"
str << " #{t 'by'} #{link_to pull.user.uname, user_path(pull.user)}" if pull.persisted?
str << " #{t 'by'} #{link_to pull.user.uname, user_path(pull.user)}" if pull.user# pull.persisted?
str.html_safe
end
#helper for helpers
def show_ref pull, which, limit = 30
project, ref = pull.send("#{which}_project"), pull.send("#{which}_ref")
link_to "#{project.owner.uname.truncate limit}/#{project.name.truncate limit}: #{ref.truncate limit}", ref_path(project, ref)
fullname = if which == 'into'
"#{project.owner.uname.truncate limit}/#{project.name.truncate limit}"
elsif which == 'from'
"#{pull.from_project_owner_uname.truncate limit}/#{pull.from_project_name.truncate limit}"
end
link_to "#{fullname}: #{ref.truncate limit}", ref_path(project, ref)
end
def ref_path project, ref
return tree_path(project, ref) if project.repo.branches_and_tags.map(&:name).include? ref
return commit_path(project, ref) if project.repo.commit ref
'#'
if project && project.repo.branches_and_tags.map(&:name).include?(ref)
tree_path(project, ref)
else
'#'
end
end
def ref_selector_options(project, current)

View File

@ -82,12 +82,12 @@ class PullRequest < ActiveRecord::Base
end
def path
filename = [id, from_project.owner.uname, from_project.name].compact.join('-')
filename = [id, from_project_owner_uname, from_project_name].compact.join('-')
File.join(APP_CONFIG['root_path'], 'pull_requests', to_project.owner.uname, to_project.name, filename)
end
def from_branch
if to_project != from_project
if to_project_id != from_project_id
"head_#{from_ref}"
else
from_ref
@ -142,7 +142,7 @@ class PullRequest < ActiveRecord::Base
end
def uniq_merge
if to_project.pull_requests.needed_checking.where(:from_project_id => from_project, :to_ref => to_ref, :from_ref => from_ref).where('pull_requests.id <> :id or :id is null', :id => id).count > 0
if to_project.pull_requests.needed_checking.where(:from_project_id => from_project_id, :to_ref => to_ref, :from_ref => from_ref).where('pull_requests.id <> :id or :id is null', :id => id).count > 0
errors.add(:base_branch, I18n.t('projects.pull_requests.duplicate', :from_ref => from_ref))
end
end
@ -160,7 +160,7 @@ class PullRequest < ActiveRecord::Base
def merge
clone
message = "Merge pull request ##{serial_id} from #{from_project.name_with_owner}:#{from_ref}\r\n #{title}"
message = "Merge pull request ##{serial_id} from #{from_project_owner_uname}/#{from_project_name}:#{from_ref}\r\n #{title}"
%x(cd #{path} && git checkout #{to_ref} && git merge --no-ff #{from_branch} -m '#{message}')
end
@ -183,7 +183,7 @@ class PullRequest < ActiveRecord::Base
Dir.chdir(path) do
system 'git', 'checkout', to_ref
system 'git', 'pull', 'origin', to_ref
if to_project == from_project
if to_project_id == from_project_id
system 'git', 'checkout', from_ref
system 'git', 'pull', 'origin', from_ref
else

View File

@ -1,5 +1,5 @@
-ar = 'activerecord.attributes.pull_requests'
-set_meta_tags :title => [title_object(@project), t('.title', :name => @pull.title.truncate(40), :user => @pull.user.uname)]
-set_meta_tags :title => [title_object(@project), t('.title', :name => @pull.title.truncate(40), :user => @pull.user.try(:uname))]
= render :partial => 'submenu'
%h3.bpadding10
=pull_status_label @pull

View File

@ -0,0 +1,14 @@
class AddHelpersColumnsToPullRequest < ActiveRecord::Migration
def up
add_column :pull_requests, :from_project_owner_uname, :string
add_column :pull_requests, :from_project_name, :string
# includes generate error "undefined method `repo' for nil:NilClass"
# update not orphan pulls. For other need execute a task project:fix_orphan_pulls
PullRequest.joins(:from_project).each {|pull| pull.from_project_name = pull.from_project.name}
end
def down
remove_column :pull_requests, :from_project_owner_uname
remove_column :pull_requests, :from_project_name
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121005100158) do
ActiveRecord::Schema.define(:version => 20121027084602) do
create_table "activity_feeds", :force => true do |t|
t.integer "user_id", :null => false
@ -347,11 +347,13 @@ ActiveRecord::Schema.define(:version => 20121005100158) do
add_index "projects", ["owner_id", "name", "owner_type"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false
create_table "pull_requests", :force => true do |t|
t.integer "issue_id", :null => false
t.integer "to_project_id", :null => false
t.integer "from_project_id", :null => false
t.string "to_ref", :null => false
t.string "from_ref", :null => false
t.integer "issue_id", :null => false
t.integer "to_project_id", :null => false
t.integer "from_project_id", :null => false
t.string "to_ref", :null => false
t.string "from_ref", :null => false
t.string "from_project_owner_uname"
t.string "from_project_name"
end
add_index "pull_requests", ["from_project_id"], :name => "index_pull_requests_on_head_project_id"

View File

@ -0,0 +1,21 @@
namespace :project do
desc 'Fix pull requests where was delete the "from project"'
task :fix_orphan_pulls => :environment do
projects = Project.where('ancestry IS NOT NULL')
say "Pull requests total count is #{PullRequest.count}"
PullRequest.all.each_with_index do |pull, ind|
say "Check pull with id:#{pull.id} (#{ind+1}/#{PullRequest.count})"
unless pull.from_project.present?
print ' its orphan! updating...'
parent_path = File.join(APP_CONFIG['root_path'], 'pull_requests', pull.to_project.owner.uname, pull.to_project.name)
Dir.chdir(parent_path) do
# Get an owner and project name from the pull dir
elements = Dir["#{pull.id}-*"].first.split '-' rescue []
pull.from_project_owner_uname, pull.from_project_name = elements[1], elements[2]
say pull.save(:validate => false) ? 'success' : 'fail!'
end
end
end
end
end

View File

@ -3,7 +3,7 @@ namespace :project do
task :git_detach_from_parent => :environment do
projects = Project.where('ancestry IS NOT NULL')
say "Total count of the forked projects is #{projects.count}"
projects.each_with_index do |project, ind|
Project.all.each_with_index do |project, ind|
Dir.chdir(project.path) do
say "--Start work with #{project.name_with_owner} (#{ind+1}/#{projects.count})--"
say (system('git', 'repack', '-a') ? 'Ok!' : 'Something wrong!')