remove trailing white-spaces from lib/tasks/migrate_from_mantis.rake

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9584 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2012-04-29 12:21:18 +00:00
parent d2378ea641
commit 6a1a4e006d

View File

@ -1,16 +1,16 @@
# redMine - project management software # Redmine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang # Copyright (C) 2006-2012 Jean-Philippe Lang
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -23,9 +23,9 @@ require 'pp'
namespace :redmine do namespace :redmine do
task :migrate_from_mantis => :environment do task :migrate_from_mantis => :environment do
module MantisMigrate module MantisMigrate
DEFAULT_STATUS = IssueStatus.default DEFAULT_STATUS = IssueStatus.default
assigned_status = IssueStatus.find_by_position(2) assigned_status = IssueStatus.find_by_position(2)
resolved_status = IssueStatus.find_by_position(3) resolved_status = IssueStatus.find_by_position(3)
@ -39,7 +39,7 @@ task :migrate_from_mantis => :environment do
80 => resolved_status, # resolved 80 => resolved_status, # resolved
90 => closed_status # closed 90 => closed_status # closed
} }
priorities = IssuePriority.all priorities = IssuePriority.all
DEFAULT_PRIORITY = priorities[2] DEFAULT_PRIORITY = priorities[2]
PRIORITY_MAPPING = {10 => priorities[1], # none PRIORITY_MAPPING = {10 => priorities[1], # none
@ -49,10 +49,10 @@ task :migrate_from_mantis => :environment do
50 => priorities[4], # urgent 50 => priorities[4], # urgent
60 => priorities[5] # immediate 60 => priorities[5] # immediate
} }
TRACKER_BUG = Tracker.find_by_position(1) TRACKER_BUG = Tracker.find_by_position(1)
TRACKER_FEATURE = Tracker.find_by_position(2) TRACKER_FEATURE = Tracker.find_by_position(2)
roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
manager_role = roles[0] manager_role = roles[0]
developer_role = roles[1] developer_role = roles[1]
@ -64,7 +64,7 @@ task :migrate_from_mantis => :environment do
70 => manager_role, # manager 70 => manager_role, # manager
90 => manager_role # administrator 90 => manager_role # administrator
} }
CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
1 => 'int', # Numeric 1 => 'int', # Numeric
2 => 'int', # Float 2 => 'int', # Float
@ -75,28 +75,28 @@ task :migrate_from_mantis => :environment do
7 => 'list', # Multiselection list 7 => 'list', # Multiselection list
8 => 'date', # Date 8 => 'date', # Date
} }
RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
2 => IssueRelation::TYPE_RELATES, # parent of 2 => IssueRelation::TYPE_RELATES, # parent of
3 => IssueRelation::TYPE_RELATES, # child of 3 => IssueRelation::TYPE_RELATES, # child of
0 => IssueRelation::TYPE_DUPLICATES, # duplicate of 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
4 => IssueRelation::TYPE_DUPLICATES # has duplicate 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
} }
class MantisUser < ActiveRecord::Base class MantisUser < ActiveRecord::Base
self.table_name = :mantis_user_table self.table_name = :mantis_user_table
def firstname def firstname
@firstname = realname.blank? ? username : realname.split.first[0..29] @firstname = realname.blank? ? username : realname.split.first[0..29]
@firstname @firstname
end end
def lastname def lastname
@lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29] @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
@lastname = '-' if @lastname.blank? @lastname = '-' if @lastname.blank?
@lastname @lastname
end end
def email def email
if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) && if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
!User.find_by_mail(read_attribute(:email)) !User.find_by_mail(read_attribute(:email))
@ -105,44 +105,44 @@ task :migrate_from_mantis => :environment do
@email = "#{username}@foo.bar" @email = "#{username}@foo.bar"
end end
end end
def username def username
read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-') read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
end end
end end
class MantisProject < ActiveRecord::Base class MantisProject < ActiveRecord::Base
self.table_name = :mantis_project_table self.table_name = :mantis_project_table
has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
has_many :news, :class_name => "MantisNews", :foreign_key => :project_id has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
def identifier def identifier
read_attribute(:name).gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH) read_attribute(:name).gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
end end
end end
class MantisVersion < ActiveRecord::Base class MantisVersion < ActiveRecord::Base
self.table_name = :mantis_project_version_table self.table_name = :mantis_project_version_table
def version def version
read_attribute(:version)[0..29] read_attribute(:version)[0..29]
end end
def description def description
read_attribute(:description)[0..254] read_attribute(:description)[0..254]
end end
end end
class MantisCategory < ActiveRecord::Base class MantisCategory < ActiveRecord::Base
self.table_name = :mantis_project_category_table self.table_name = :mantis_project_category_table
end end
class MantisProjectUser < ActiveRecord::Base class MantisProjectUser < ActiveRecord::Base
self.table_name = :mantis_project_user_list_table self.table_name = :mantis_project_user_list_table
end end
class MantisBug < ActiveRecord::Base class MantisBug < ActiveRecord::Base
self.table_name = :mantis_bug_table self.table_name = :mantis_bug_table
belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
@ -150,10 +150,10 @@ task :migrate_from_mantis => :environment do
has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
end end
class MantisBugText < ActiveRecord::Base class MantisBugText < ActiveRecord::Base
self.table_name = :mantis_bug_text_table self.table_name = :mantis_bug_text_table
# Adds Mantis steps_to_reproduce and additional_information fields # Adds Mantis steps_to_reproduce and additional_information fields
# to description if any # to description if any
def full_description def full_description
@ -163,32 +163,32 @@ task :migrate_from_mantis => :environment do
full_description full_description
end end
end end
class MantisBugNote < ActiveRecord::Base class MantisBugNote < ActiveRecord::Base
self.table_name = :mantis_bugnote_table self.table_name = :mantis_bugnote_table
belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
end end
class MantisBugNoteText < ActiveRecord::Base class MantisBugNoteText < ActiveRecord::Base
self.table_name = :mantis_bugnote_text_table self.table_name = :mantis_bugnote_text_table
end end
class MantisBugFile < ActiveRecord::Base class MantisBugFile < ActiveRecord::Base
self.table_name = :mantis_bug_file_table self.table_name = :mantis_bug_file_table
def size def size
filesize filesize
end end
def original_filename def original_filename
MantisMigrate.encode(filename) MantisMigrate.encode(filename)
end end
def content_type def content_type
file_type file_type
end end
def read(*args) def read(*args)
if @read_finished if @read_finished
nil nil
@ -198,52 +198,51 @@ task :migrate_from_mantis => :environment do
end end
end end
end end
class MantisBugRelationship < ActiveRecord::Base class MantisBugRelationship < ActiveRecord::Base
self.table_name = :mantis_bug_relationship_table self.table_name = :mantis_bug_relationship_table
end end
class MantisBugMonitor < ActiveRecord::Base class MantisBugMonitor < ActiveRecord::Base
self.table_name = :mantis_bug_monitor_table self.table_name = :mantis_bug_monitor_table
end end
class MantisNews < ActiveRecord::Base class MantisNews < ActiveRecord::Base
self.table_name = :mantis_news_table self.table_name = :mantis_news_table
end end
class MantisCustomField < ActiveRecord::Base class MantisCustomField < ActiveRecord::Base
self.table_name = :mantis_custom_field_table self.table_name = :mantis_custom_field_table
set_inheritance_column :none set_inheritance_column :none
has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
def format def format
read_attribute :type read_attribute :type
end end
def name def name
read_attribute(:name)[0..29] read_attribute(:name)[0..29]
end end
end end
class MantisCustomFieldProject < ActiveRecord::Base class MantisCustomFieldProject < ActiveRecord::Base
self.table_name = :mantis_custom_field_project_table self.table_name = :mantis_custom_field_project_table
end end
class MantisCustomFieldString < ActiveRecord::Base class MantisCustomFieldString < ActiveRecord::Base
self.table_name = :mantis_custom_field_string_table self.table_name = :mantis_custom_field_string_table
end end
def self.migrate def self.migrate
# Users # Users
print "Migrating users" print "Migrating users"
User.delete_all "login <> 'admin'" User.delete_all "login <> 'admin'"
users_map = {} users_map = {}
users_migrated = 0 users_migrated = 0
MantisUser.find(:all).each do |user| MantisUser.find(:all).each do |user|
u = User.new :firstname => encode(user.firstname), u = User.new :firstname => encode(user.firstname),
:lastname => encode(user.lastname), :lastname => encode(user.lastname),
:mail => user.email, :mail => user.email,
:last_login_on => user.last_visit :last_login_on => user.last_visit
@ -257,7 +256,7 @@ task :migrate_from_mantis => :environment do
print '.' print '.'
end end
puts puts
# Projects # Projects
print "Migrating projects" print "Migrating projects"
Project.destroy_all Project.destroy_all
@ -265,7 +264,7 @@ task :migrate_from_mantis => :environment do
versions_map = {} versions_map = {}
categories_map = {} categories_map = {}
MantisProject.find(:all).each do |project| MantisProject.find(:all).each do |project|
p = Project.new :name => encode(project.name), p = Project.new :name => encode(project.name),
:description => encode(project.description) :description => encode(project.description)
p.identifier = project.identifier p.identifier = project.identifier
next unless p.save next unless p.save
@ -302,7 +301,7 @@ task :migrate_from_mantis => :environment do
end end
end end
puts puts
# Bugs # Bugs
print "Migrating bugs" print "Migrating bugs"
Issue.destroy_all Issue.destroy_all
@ -310,7 +309,7 @@ task :migrate_from_mantis => :environment do
keep_bug_ids = (Issue.count == 0) keep_bug_ids = (Issue.count == 0)
MantisBug.find_each(:batch_size => 200) do |bug| MantisBug.find_each(:batch_size => 200) do |bug|
next unless projects_map[bug.project_id] && users_map[bug.reporter_id] next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
i = Issue.new :project_id => projects_map[bug.project_id], i = Issue.new :project_id => projects_map[bug.project_id],
:subject => encode(bug.summary), :subject => encode(bug.summary),
:description => encode(bug.bug_text.full_description), :description => encode(bug.bug_text.full_description),
:priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY, :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
@ -332,7 +331,7 @@ task :migrate_from_mantis => :environment do
if (bug.handler_id && users_map[bug.handler_id]) if (bug.handler_id && users_map[bug.handler_id])
i.assigned_to = User.find_by_id(users_map[bug.handler_id]) i.assigned_to = User.find_by_id(users_map[bug.handler_id])
i.save(:validate => false) i.save(:validate => false)
end end
# Bug notes # Bug notes
bug.bug_notes.each do |note| bug.bug_notes.each do |note|
@ -352,18 +351,18 @@ task :migrate_from_mantis => :environment do
a.container = i a.container = i
a.save a.save
end end
# Bug monitors # Bug monitors
bug.bug_monitors.each do |monitor| bug.bug_monitors.each do |monitor|
next unless users_map[monitor.user_id] next unless users_map[monitor.user_id]
i.add_watcher(User.find_by_id(users_map[monitor.user_id])) i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
end end
end end
# update issue id sequence if needed (postgresql) # update issue id sequence if needed (postgresql)
Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!') Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
puts puts
# Bug relationships # Bug relationships
print "Migrating bug relations" print "Migrating bug relations"
MantisBugRelationship.find(:all).each do |relation| MantisBugRelationship.find(:all).each do |relation|
@ -376,7 +375,7 @@ task :migrate_from_mantis => :environment do
STDOUT.flush STDOUT.flush
end end
puts puts
# News # News
print "Migrating news" print "Migrating news"
News.destroy_all News.destroy_all
@ -392,7 +391,7 @@ task :migrate_from_mantis => :environment do
STDOUT.flush STDOUT.flush
end end
puts puts
# Custom fields # Custom fields
print "Migrating custom fields" print "Migrating custom fields"
IssueCustomField.destroy_all IssueCustomField.destroy_all
@ -409,12 +408,12 @@ task :migrate_from_mantis => :environment do
STDOUT.flush STDOUT.flush
# Trackers association # Trackers association
f.trackers = Tracker.find :all f.trackers = Tracker.find :all
# Projects association # Projects association
field.projects.each do |project| field.projects.each do |project|
f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id] f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
end end
# Values # Values
field.values.each do |value| field.values.each do |value|
v = CustomValue.new :custom_field_id => f.id, v = CustomValue.new :custom_field_id => f.id,
@ -424,7 +423,7 @@ task :migrate_from_mantis => :environment do
end unless f.new_record? end unless f.new_record?
end end
puts puts
puts puts
puts "Users: #{users_migrated}/#{MantisUser.count}" puts "Users: #{users_migrated}/#{MantisUser.count}"
puts "Projects: #{Project.count}/#{MantisProject.count}" puts "Projects: #{Project.count}/#{MantisProject.count}"
@ -439,13 +438,13 @@ task :migrate_from_mantis => :environment do
puts "News: #{News.count}/#{MantisNews.count}" puts "News: #{News.count}/#{MantisNews.count}"
puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}" puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
end end
def self.encoding(charset) def self.encoding(charset)
@ic = Iconv.new('UTF-8', charset) @ic = Iconv.new('UTF-8', charset)
rescue Iconv::InvalidEncoding rescue Iconv::InvalidEncoding
return false return false
end end
def self.establish_connection(params) def self.establish_connection(params)
constants.each do |const| constants.each do |const|
klass = const_get(const) klass = const_get(const)
@ -453,14 +452,14 @@ task :migrate_from_mantis => :environment do
klass.establish_connection params klass.establish_connection params
end end
end end
def self.encode(text) def self.encode(text)
@ic.iconv text @ic.iconv text
rescue rescue
text text
end end
end end
puts puts
if Redmine::DefaultData::Loader.no_data? if Redmine::DefaultData::Loader.no_data?
puts "Redmine configuration need to be loaded before importing data." puts "Redmine configuration need to be loaded before importing data."
@ -469,27 +468,27 @@ task :migrate_from_mantis => :environment do
puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\"" puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
exit exit
end end
puts "WARNING: Your Redmine data will be deleted during this process." puts "WARNING: Your Redmine data will be deleted during this process."
print "Are you sure you want to continue ? [y/N] " print "Are you sure you want to continue ? [y/N] "
STDOUT.flush STDOUT.flush
break unless STDIN.gets.match(/^y$/i) break unless STDIN.gets.match(/^y$/i)
# Default Mantis database settings # Default Mantis database settings
db_params = {:adapter => 'mysql2', db_params = {:adapter => 'mysql2',
:database => 'bugtracker', :database => 'bugtracker',
:host => 'localhost', :host => 'localhost',
:username => 'root', :username => 'root',
:password => '' } :password => '' }
puts puts
puts "Please enter settings for your Mantis database" puts "Please enter settings for your Mantis database"
[:adapter, :host, :database, :username, :password].each do |param| [:adapter, :host, :database, :username, :password].each do |param|
print "#{param} [#{db_params[param]}]: " print "#{param} [#{db_params[param]}]: "
value = STDIN.gets.chomp! value = STDIN.gets.chomp!
db_params[param] = value unless value.blank? db_params[param] = value unless value.blank?
end end
while true while true
print "encoding [UTF-8]: " print "encoding [UTF-8]: "
STDOUT.flush STDOUT.flush
@ -499,13 +498,13 @@ task :migrate_from_mantis => :environment do
puts "Invalid encoding!" puts "Invalid encoding!"
end end
puts puts
# Make sure bugs can refer bugs in other projects # Make sure bugs can refer bugs in other projects
Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations' Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
# Turn off email notifications # Turn off email notifications
Setting.notified_events = [] Setting.notified_events = []
MantisMigrate.establish_connection db_params MantisMigrate.establish_connection db_params
MantisMigrate.migrate MantisMigrate.migrate
end end