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