Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: desc 'Mantis migration script' Chris@0: Chris@0: require 'active_record' Chris@0: require 'iconv' Chris@0: require 'pp' Chris@0: Chris@0: namespace :redmine do Chris@0: task :migrate_from_mantis => :environment do Chris@0: Chris@0: module MantisMigrate Chris@0: Chris@0: DEFAULT_STATUS = IssueStatus.default Chris@0: assigned_status = IssueStatus.find_by_position(2) Chris@0: resolved_status = IssueStatus.find_by_position(3) Chris@0: feedback_status = IssueStatus.find_by_position(4) Chris@0: closed_status = IssueStatus.find :first, :conditions => { :is_closed => true } Chris@0: STATUS_MAPPING = {10 => DEFAULT_STATUS, # new Chris@0: 20 => feedback_status, # feedback Chris@0: 30 => DEFAULT_STATUS, # acknowledged Chris@0: 40 => DEFAULT_STATUS, # confirmed Chris@0: 50 => assigned_status, # assigned Chris@0: 80 => resolved_status, # resolved Chris@0: 90 => closed_status # closed Chris@0: } Chris@0: Chris@0: priorities = IssuePriority.all Chris@0: DEFAULT_PRIORITY = priorities[2] Chris@0: PRIORITY_MAPPING = {10 => priorities[1], # none Chris@0: 20 => priorities[1], # low Chris@0: 30 => priorities[2], # normal Chris@0: 40 => priorities[3], # high Chris@0: 50 => priorities[4], # urgent Chris@0: 60 => priorities[5] # immediate Chris@0: } Chris@0: Chris@0: TRACKER_BUG = Tracker.find_by_position(1) Chris@0: TRACKER_FEATURE = Tracker.find_by_position(2) Chris@0: Chris@0: roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') Chris@0: manager_role = roles[0] Chris@0: developer_role = roles[1] Chris@0: DEFAULT_ROLE = roles.last Chris@0: ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer Chris@0: 25 => DEFAULT_ROLE, # reporter Chris@0: 40 => DEFAULT_ROLE, # updater Chris@0: 55 => developer_role, # developer Chris@0: 70 => manager_role, # manager Chris@0: 90 => manager_role # administrator Chris@0: } Chris@0: Chris@0: CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String Chris@0: 1 => 'int', # Numeric Chris@0: 2 => 'int', # Float Chris@0: 3 => 'list', # Enumeration Chris@0: 4 => 'string', # Email Chris@0: 5 => 'bool', # Checkbox Chris@0: 6 => 'list', # List Chris@0: 7 => 'list', # Multiselection list Chris@0: 8 => 'date', # Date Chris@0: } Chris@0: Chris@0: RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to Chris@0: 2 => IssueRelation::TYPE_RELATES, # parent of Chris@0: 3 => IssueRelation::TYPE_RELATES, # child of Chris@0: 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of Chris@0: 4 => IssueRelation::TYPE_DUPLICATES # has duplicate Chris@0: } Chris@0: Chris@0: class MantisUser < ActiveRecord::Base Chris@0: set_table_name :mantis_user_table Chris@0: Chris@0: def firstname Chris@0: @firstname = realname.blank? ? username : realname.split.first[0..29] Chris@0: @firstname.gsub!(/[^\w\s\'\-]/i, '') Chris@0: @firstname Chris@0: end Chris@0: Chris@0: def lastname Chris@0: @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29] Chris@0: @lastname.gsub!(/[^\w\s\'\-]/i, '') Chris@0: @lastname = '-' if @lastname.blank? Chris@0: @lastname Chris@0: end Chris@0: Chris@0: def email Chris@0: if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) && Chris@0: !User.find_by_mail(read_attribute(:email)) Chris@0: @email = read_attribute(:email) Chris@0: else Chris@0: @email = "#{username}@foo.bar" Chris@0: end Chris@0: end Chris@0: Chris@0: def username Chris@0: read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-') Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisProject < ActiveRecord::Base Chris@0: set_table_name :mantis_project_table Chris@0: has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id Chris@0: has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id Chris@0: has_many :news, :class_name => "MantisNews", :foreign_key => :project_id Chris@0: has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id Chris@0: Chris@0: def identifier chris@37: read_attribute(:name).gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH) Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisVersion < ActiveRecord::Base Chris@0: set_table_name :mantis_project_version_table Chris@0: Chris@0: def version Chris@0: read_attribute(:version)[0..29] Chris@0: end Chris@0: Chris@0: def description Chris@0: read_attribute(:description)[0..254] Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisCategory < ActiveRecord::Base Chris@0: set_table_name :mantis_project_category_table Chris@0: end Chris@0: Chris@0: class MantisProjectUser < ActiveRecord::Base Chris@0: set_table_name :mantis_project_user_list_table Chris@0: end Chris@0: Chris@0: class MantisBug < ActiveRecord::Base Chris@0: set_table_name :mantis_bug_table Chris@0: belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id Chris@0: has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id Chris@0: has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id Chris@0: has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id Chris@0: end Chris@0: Chris@0: class MantisBugText < ActiveRecord::Base Chris@0: set_table_name :mantis_bug_text_table Chris@0: Chris@0: # Adds Mantis steps_to_reproduce and additional_information fields Chris@0: # to description if any Chris@0: def full_description Chris@0: full_description = description Chris@0: full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank? Chris@0: full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank? Chris@0: full_description Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisBugNote < ActiveRecord::Base Chris@0: set_table_name :mantis_bugnote_table Chris@0: belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id Chris@0: belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id Chris@0: end Chris@0: Chris@0: class MantisBugNoteText < ActiveRecord::Base Chris@0: set_table_name :mantis_bugnote_text_table Chris@0: end Chris@0: Chris@0: class MantisBugFile < ActiveRecord::Base Chris@0: set_table_name :mantis_bug_file_table Chris@0: Chris@0: def size Chris@0: filesize Chris@0: end Chris@0: Chris@0: def original_filename Chris@0: MantisMigrate.encode(filename) Chris@0: end Chris@0: Chris@0: def content_type Chris@0: file_type Chris@0: end Chris@0: Chris@0: def read(*args) Chris@0: if @read_finished Chris@0: nil Chris@0: else Chris@0: @read_finished = true Chris@0: content Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisBugRelationship < ActiveRecord::Base Chris@0: set_table_name :mantis_bug_relationship_table Chris@0: end Chris@0: Chris@0: class MantisBugMonitor < ActiveRecord::Base Chris@0: set_table_name :mantis_bug_monitor_table Chris@0: end Chris@0: Chris@0: class MantisNews < ActiveRecord::Base Chris@0: set_table_name :mantis_news_table Chris@0: end Chris@0: Chris@0: class MantisCustomField < ActiveRecord::Base Chris@0: set_table_name :mantis_custom_field_table Chris@0: set_inheritance_column :none Chris@0: has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id Chris@0: has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id Chris@0: Chris@0: def format Chris@0: read_attribute :type Chris@0: end Chris@0: Chris@0: def name Chris@0: read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-') Chris@0: end Chris@0: end Chris@0: Chris@0: class MantisCustomFieldProject < ActiveRecord::Base Chris@0: set_table_name :mantis_custom_field_project_table Chris@0: end Chris@0: Chris@0: class MantisCustomFieldString < ActiveRecord::Base Chris@0: set_table_name :mantis_custom_field_string_table Chris@0: end Chris@0: Chris@0: Chris@0: def self.migrate Chris@0: Chris@0: # Users Chris@0: print "Migrating users" Chris@0: User.delete_all "login <> 'admin'" Chris@0: users_map = {} Chris@0: users_migrated = 0 Chris@0: MantisUser.find(:all).each do |user| Chris@0: u = User.new :firstname => encode(user.firstname), Chris@0: :lastname => encode(user.lastname), Chris@0: :mail => user.email, Chris@0: :last_login_on => user.last_visit Chris@0: u.login = user.username Chris@0: u.password = 'mantis' Chris@0: u.status = User::STATUS_LOCKED if user.enabled != 1 Chris@0: u.admin = true if user.access_level == 90 Chris@0: next unless u.save! Chris@0: users_migrated += 1 Chris@0: users_map[user.id] = u.id Chris@0: print '.' Chris@0: end Chris@0: puts Chris@0: Chris@0: # Projects Chris@0: print "Migrating projects" Chris@0: Project.destroy_all Chris@0: projects_map = {} Chris@0: versions_map = {} Chris@0: categories_map = {} Chris@0: MantisProject.find(:all).each do |project| Chris@0: p = Project.new :name => encode(project.name), Chris@0: :description => encode(project.description) Chris@0: p.identifier = project.identifier Chris@0: next unless p.save Chris@0: projects_map[project.id] = p.id Chris@0: p.enabled_module_names = ['issue_tracking', 'news', 'wiki'] Chris@0: p.trackers << TRACKER_BUG Chris@0: p.trackers << TRACKER_FEATURE Chris@0: print '.' Chris@0: Chris@0: # Project members Chris@0: project.members.each do |member| Chris@0: m = Member.new :user => User.find_by_id(users_map[member.user_id]), Chris@0: :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE] Chris@0: m.project = p Chris@0: m.save Chris@0: end Chris@0: Chris@0: # Project versions Chris@0: project.versions.each do |version| Chris@0: v = Version.new :name => encode(version.version), Chris@0: :description => encode(version.description), Chris@0: :effective_date => version.date_order.to_date Chris@0: v.project = p Chris@0: v.save Chris@0: versions_map[version.id] = v.id Chris@0: end Chris@0: Chris@0: # Project categories Chris@0: project.categories.each do |category| Chris@0: g = IssueCategory.new :name => category.category[0,30] Chris@0: g.project = p Chris@0: g.save Chris@0: categories_map[category.category] = g.id Chris@0: end Chris@0: end Chris@0: puts Chris@0: Chris@0: # Bugs Chris@0: print "Migrating bugs" Chris@0: Issue.destroy_all Chris@0: issues_map = {} Chris@0: keep_bug_ids = (Issue.count == 0) Chris@0: MantisBug.find_each(:batch_size => 200) do |bug| Chris@0: next unless projects_map[bug.project_id] && users_map[bug.reporter_id] Chris@0: i = Issue.new :project_id => projects_map[bug.project_id], Chris@0: :subject => encode(bug.summary), Chris@0: :description => encode(bug.bug_text.full_description), Chris@0: :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY, Chris@0: :created_on => bug.date_submitted, Chris@0: :updated_on => bug.last_updated Chris@0: i.author = User.find_by_id(users_map[bug.reporter_id]) Chris@0: i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank? Chris@0: i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank? Chris@0: i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS Chris@0: i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG) Chris@0: i.id = bug.id if keep_bug_ids Chris@0: next unless i.save Chris@0: issues_map[bug.id] = i.id Chris@0: print '.' Chris@0: STDOUT.flush Chris@0: Chris@0: # Assignee Chris@0: # Redmine checks that the assignee is a project member Chris@0: if (bug.handler_id && users_map[bug.handler_id]) Chris@0: i.assigned_to = User.find_by_id(users_map[bug.handler_id]) Chris@0: i.save_with_validation(false) Chris@0: end Chris@0: Chris@0: # Bug notes Chris@0: bug.bug_notes.each do |note| Chris@0: next unless users_map[note.reporter_id] Chris@0: n = Journal.new :notes => encode(note.bug_note_text.note), Chris@0: :created_on => note.date_submitted Chris@0: n.user = User.find_by_id(users_map[note.reporter_id]) Chris@0: n.journalized = i Chris@0: n.save Chris@0: end Chris@0: Chris@0: # Bug files Chris@0: bug.bug_files.each do |file| Chris@0: a = Attachment.new :created_on => file.date_added Chris@0: a.file = file Chris@0: a.author = User.find :first Chris@0: a.container = i Chris@0: a.save Chris@0: end Chris@0: Chris@0: # Bug monitors Chris@0: bug.bug_monitors.each do |monitor| Chris@0: next unless users_map[monitor.user_id] Chris@0: i.add_watcher(User.find_by_id(users_map[monitor.user_id])) Chris@0: end Chris@0: end Chris@0: Chris@0: # update issue id sequence if needed (postgresql) Chris@0: Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!') Chris@0: puts Chris@0: Chris@0: # Bug relationships Chris@0: print "Migrating bug relations" Chris@0: MantisBugRelationship.find(:all).each do |relation| Chris@0: next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id] Chris@0: r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type] Chris@0: r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id]) Chris@0: r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id]) Chris@0: pp r unless r.save Chris@0: print '.' Chris@0: STDOUT.flush Chris@0: end Chris@0: puts Chris@0: Chris@0: # News Chris@0: print "Migrating news" Chris@0: News.destroy_all Chris@0: MantisNews.find(:all, :conditions => 'project_id > 0').each do |news| Chris@0: next unless projects_map[news.project_id] Chris@0: n = News.new :project_id => projects_map[news.project_id], Chris@0: :title => encode(news.headline[0..59]), Chris@0: :description => encode(news.body), Chris@0: :created_on => news.date_posted Chris@0: n.author = User.find_by_id(users_map[news.poster_id]) Chris@0: n.save Chris@0: print '.' Chris@0: STDOUT.flush Chris@0: end Chris@0: puts Chris@0: Chris@0: # Custom fields Chris@0: print "Migrating custom fields" Chris@0: IssueCustomField.destroy_all Chris@0: MantisCustomField.find(:all).each do |field| Chris@0: f = IssueCustomField.new :name => field.name[0..29], Chris@0: :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format], Chris@0: :min_length => field.length_min, Chris@0: :max_length => field.length_max, Chris@0: :regexp => field.valid_regexp, Chris@0: :possible_values => field.possible_values.split('|'), Chris@0: :is_required => field.require_report? Chris@0: next unless f.save Chris@0: print '.' Chris@0: STDOUT.flush Chris@0: # Trackers association Chris@0: f.trackers = Tracker.find :all Chris@0: Chris@0: # Projects association Chris@0: field.projects.each do |project| Chris@0: f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id] Chris@0: end Chris@0: Chris@0: # Values Chris@0: field.values.each do |value| Chris@0: v = CustomValue.new :custom_field_id => f.id, Chris@0: :value => value.value Chris@0: v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id] Chris@0: v.save Chris@0: end unless f.new_record? Chris@0: end Chris@0: puts Chris@0: Chris@0: puts Chris@0: puts "Users: #{users_migrated}/#{MantisUser.count}" Chris@0: puts "Projects: #{Project.count}/#{MantisProject.count}" Chris@0: puts "Memberships: #{Member.count}/#{MantisProjectUser.count}" Chris@0: puts "Versions: #{Version.count}/#{MantisVersion.count}" Chris@0: puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}" Chris@0: puts "Bugs: #{Issue.count}/#{MantisBug.count}" Chris@0: puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}" Chris@0: puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}" Chris@0: puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}" Chris@0: puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}" Chris@0: puts "News: #{News.count}/#{MantisNews.count}" Chris@0: puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}" Chris@0: end Chris@0: Chris@0: def self.encoding(charset) Chris@0: @ic = Iconv.new('UTF-8', charset) Chris@0: rescue Iconv::InvalidEncoding Chris@0: return false Chris@0: end Chris@0: Chris@0: def self.establish_connection(params) Chris@0: constants.each do |const| Chris@0: klass = const_get(const) Chris@0: next unless klass.respond_to? 'establish_connection' Chris@0: klass.establish_connection params Chris@0: end Chris@0: end Chris@0: Chris@0: def self.encode(text) Chris@0: @ic.iconv text Chris@0: rescue Chris@0: text Chris@0: end Chris@0: end Chris@0: Chris@0: puts Chris@0: if Redmine::DefaultData::Loader.no_data? Chris@0: puts "Redmine configuration need to be loaded before importing data." Chris@0: puts "Please, run this first:" Chris@0: puts Chris@0: puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\"" Chris@0: exit Chris@0: end Chris@0: Chris@0: puts "WARNING: Your Redmine data will be deleted during this process." Chris@0: print "Are you sure you want to continue ? [y/N] " Chris@0: STDOUT.flush Chris@0: break unless STDIN.gets.match(/^y$/i) Chris@0: Chris@0: # Default Mantis database settings Chris@0: db_params = {:adapter => 'mysql', Chris@0: :database => 'bugtracker', Chris@0: :host => 'localhost', Chris@0: :username => 'root', Chris@0: :password => '' } Chris@0: Chris@0: puts Chris@0: puts "Please enter settings for your Mantis database" Chris@0: [:adapter, :host, :database, :username, :password].each do |param| Chris@0: print "#{param} [#{db_params[param]}]: " Chris@0: value = STDIN.gets.chomp! Chris@0: db_params[param] = value unless value.blank? Chris@0: end Chris@0: Chris@0: while true Chris@0: print "encoding [UTF-8]: " Chris@0: STDOUT.flush Chris@0: encoding = STDIN.gets.chomp! Chris@0: encoding = 'UTF-8' if encoding.blank? Chris@0: break if MantisMigrate.encoding encoding Chris@0: puts "Invalid encoding!" Chris@0: end Chris@0: puts Chris@0: Chris@0: # Make sure bugs can refer bugs in other projects Chris@0: Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations' Chris@0: Chris@0: # Turn off email notifications Chris@0: Setting.notified_events = [] Chris@0: Chris@0: MantisMigrate.establish_connection db_params Chris@0: MantisMigrate.migrate Chris@0: end Chris@0: end