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