From c6a0dfee9709a03a994b0295d817b0ab562455c9 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Sun, 28 Oct 2012 00:04:28 +0600 Subject: [PATCH] [refs #90] fixed orphan pulls --- app/helpers/pull_request_helper.rb | 17 ++++++++++----- app/models/pull_request.rb | 10 ++++----- .../projects/pull_requests/show.html.haml | 2 +- ...602_add_helpers_columns_to_pull_request.rb | 14 +++++++++++++ db/schema.rb | 14 +++++++------ lib/tasks/fix_orphan_pulls.rake | 21 +++++++++++++++++++ lib/tasks/git_detach_from_parent.rake | 2 +- 7 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20121027084602_add_helpers_columns_to_pull_request.rb create mode 100644 lib/tasks/fix_orphan_pulls.rake diff --git a/app/helpers/pull_request_helper.rb b/app/helpers/pull_request_helper.rb index db6ee030d..28c6b3e9e 100644 --- a/app/helpers/pull_request_helper.rb +++ b/app/helpers/pull_request_helper.rb @@ -27,20 +27,27 @@ module PullRequestHelper #{show_ref pull, 'from'} \ #{t 'into'} \ #{show_ref pull, 'to'}" - 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) diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index d43e3f325..71d0b7728 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -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 diff --git a/app/views/projects/pull_requests/show.html.haml b/app/views/projects/pull_requests/show.html.haml index 0e525d1cd..c1de1d3b9 100644 --- a/app/views/projects/pull_requests/show.html.haml +++ b/app/views/projects/pull_requests/show.html.haml @@ -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 diff --git a/db/migrate/20121027084602_add_helpers_columns_to_pull_request.rb b/db/migrate/20121027084602_add_helpers_columns_to_pull_request.rb new file mode 100644 index 000000000..13b09c0a3 --- /dev/null +++ b/db/migrate/20121027084602_add_helpers_columns_to_pull_request.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index bd9ee3cdb..d907b04dd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/tasks/fix_orphan_pulls.rake b/lib/tasks/fix_orphan_pulls.rake new file mode 100644 index 000000000..960bffca8 --- /dev/null +++ b/lib/tasks/fix_orphan_pulls.rake @@ -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 + diff --git a/lib/tasks/git_detach_from_parent.rake b/lib/tasks/git_detach_from_parent.rake index 737208c87..fe4e7213f 100644 --- a/lib/tasks/git_detach_from_parent.rake +++ b/lib/tasks/git_detach_from_parent.rake @@ -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!')