1 # redMine - project management software
\r
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
\r
4 # This program is free software; you can redistribute it and/or
\r
5 # modify it under the terms of the GNU General Public License
\r
6 # as published by the Free Software Foundation; either version 2
\r
7 # of the License, or (at your option) any later version.
\r
9 # This program is distributed in the hope that it will be useful,
\r
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 # GNU General Public License for more details.
\r
14 # You should have received a copy of the GNU General Public License
\r
15 # along with this program; if not, write to the Free Software
\r
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
18 desc 'Mantis migration script'
\r
20 require 'active_record'
\r
24 namespace :redmine do
\r
25 task :migrate_from_mantis => :environment do
\r
27 module MantisMigrate
\r
29 DEFAULT_STATUS = IssueStatus.default
\r
30 assigned_status = IssueStatus.find_by_position(2)
\r
31 resolved_status = IssueStatus.find_by_position(3)
\r
32 feedback_status = IssueStatus.find_by_position(4)
\r
33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
\r
34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
\r
35 20 => feedback_status, # feedback
\r
36 30 => DEFAULT_STATUS, # acknowledged
\r
37 40 => DEFAULT_STATUS, # confirmed
\r
38 50 => assigned_status, # assigned
\r
39 80 => resolved_status, # resolved
\r
40 90 => closed_status # closed
\r
43 priorities = IssuePriority.all
\r
44 DEFAULT_PRIORITY = priorities[2]
\r
45 PRIORITY_MAPPING = {10 => priorities[1], # none
\r
46 20 => priorities[1], # low
\r
47 30 => priorities[2], # normal
\r
48 40 => priorities[3], # high
\r
49 50 => priorities[4], # urgent
\r
50 60 => priorities[5] # immediate
\r
53 TRACKER_BUG = Tracker.find_by_position(1)
\r
54 TRACKER_FEATURE = Tracker.find_by_position(2)
\r
56 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
\r
57 manager_role = roles[0]
\r
58 developer_role = roles[1]
\r
59 DEFAULT_ROLE = roles.last
\r
60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
\r
61 25 => DEFAULT_ROLE, # reporter
\r
62 40 => DEFAULT_ROLE, # updater
\r
63 55 => developer_role, # developer
\r
64 70 => manager_role, # manager
\r
65 90 => manager_role # administrator
\r
68 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
\r
69 1 => 'int', # Numeric
\r
71 3 => 'list', # Enumeration
\r
72 4 => 'string', # Email
\r
73 5 => 'bool', # Checkbox
\r
75 7 => 'list', # Multiselection list
\r
79 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
\r
80 2 => IssueRelation::TYPE_RELATES, # parent of
\r
81 3 => IssueRelation::TYPE_RELATES, # child of
\r
82 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
\r
83 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
\r
86 class MantisUser < ActiveRecord::Base
\r
87 set_table_name :mantis_user_table
\r
90 @firstname = realname.blank? ? username : realname.split.first[0..29]
\r
91 @firstname.gsub!(/[^\w\s\'\-]/i, '')
\r
96 @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
\r
97 @lastname.gsub!(/[^\w\s\'\-]/i, '')
\r
98 @lastname = '-' if @lastname.blank?
\r
103 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
\r
104 !User.find_by_mail(read_attribute(:email))
\r
105 @email = read_attribute(:email)
\r
107 @email = "#{username}@foo.bar"
\r
112 read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
\r
116 class MantisProject < ActiveRecord::Base
\r
117 set_table_name :mantis_project_table
\r
118 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
\r
119 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
\r
120 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
\r
121 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
\r
124 read_attribute(:name)[0..29]
\r
128 read_attribute(:name).underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
\r
132 class MantisVersion < ActiveRecord::Base
\r
133 set_table_name :mantis_project_version_table
\r
136 read_attribute(:version)[0..29]
\r
140 read_attribute(:description)[0..254]
\r
144 class MantisCategory < ActiveRecord::Base
\r
145 set_table_name :mantis_project_category_table
\r
148 class MantisProjectUser < ActiveRecord::Base
\r
149 set_table_name :mantis_project_user_list_table
\r
152 class MantisBug < ActiveRecord::Base
\r
153 set_table_name :mantis_bug_table
\r
154 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
\r
155 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
\r
156 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
\r
157 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
\r
160 class MantisBugText < ActiveRecord::Base
\r
161 set_table_name :mantis_bug_text_table
\r
163 # Adds Mantis steps_to_reproduce and additional_information fields
\r
164 # to description if any
\r
165 def full_description
\r
166 full_description = description
\r
167 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
\r
168 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
\r
173 class MantisBugNote < ActiveRecord::Base
\r
174 set_table_name :mantis_bugnote_table
\r
175 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
\r
176 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
\r
179 class MantisBugNoteText < ActiveRecord::Base
\r
180 set_table_name :mantis_bugnote_text_table
\r
183 class MantisBugFile < ActiveRecord::Base
\r
184 set_table_name :mantis_bug_file_table
\r
190 def original_filename
\r
191 MantisMigrate.encode(filename)
\r
202 @read_finished = true
\r
208 class MantisBugRelationship < ActiveRecord::Base
\r
209 set_table_name :mantis_bug_relationship_table
\r
212 class MantisBugMonitor < ActiveRecord::Base
\r
213 set_table_name :mantis_bug_monitor_table
\r
216 class MantisNews < ActiveRecord::Base
\r
217 set_table_name :mantis_news_table
\r
220 class MantisCustomField < ActiveRecord::Base
\r
221 set_table_name :mantis_custom_field_table
\r
222 set_inheritance_column :none
\r
223 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
\r
224 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
\r
227 read_attribute :type
\r
231 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
\r
235 class MantisCustomFieldProject < ActiveRecord::Base
\r
236 set_table_name :mantis_custom_field_project_table
\r
239 class MantisCustomFieldString < ActiveRecord::Base
\r
240 set_table_name :mantis_custom_field_string_table
\r
247 print "Migrating users"
\r
248 User.delete_all "login <> 'admin'"
\r
251 MantisUser.find(:all).each do |user|
\r
252 u = User.new :firstname => encode(user.firstname),
\r
253 :lastname => encode(user.lastname),
\r
254 :mail => user.email,
\r
255 :last_login_on => user.last_visit
\r
256 u.login = user.username
\r
257 u.password = 'mantis'
\r
258 u.status = User::STATUS_LOCKED if user.enabled != 1
\r
259 u.admin = true if user.access_level == 90
\r
260 next unless u.save!
\r
261 users_migrated += 1
\r
262 users_map[user.id] = u.id
\r
268 print "Migrating projects"
\r
269 Project.destroy_all
\r
272 categories_map = {}
\r
273 MantisProject.find(:all).each do |project|
\r
274 p = Project.new :name => encode(project.name),
\r
275 :description => encode(project.description)
\r
276 p.identifier = project.identifier
\r
278 projects_map[project.id] = p.id
\r
279 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
\r
280 p.trackers << TRACKER_BUG
\r
281 p.trackers << TRACKER_FEATURE
\r
285 project.members.each do |member|
\r
286 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
\r
287 :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
\r
293 project.versions.each do |version|
\r
294 v = Version.new :name => encode(version.version),
\r
295 :description => encode(version.description),
\r
296 :effective_date => version.date_order.to_date
\r
299 versions_map[version.id] = v.id
\r
302 # Project categories
\r
303 project.categories.each do |category|
\r
304 g = IssueCategory.new :name => category.category[0,30]
\r
307 categories_map[category.category] = g.id
\r
313 print "Migrating bugs"
\r
316 keep_bug_ids = (Issue.count == 0)
\r
317 MantisBug.find_each(:batch_size => 200) do |bug|
\r
318 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
\r
319 i = Issue.new :project_id => projects_map[bug.project_id],
\r
320 :subject => encode(bug.summary),
\r
321 :description => encode(bug.bug_text.full_description),
\r
322 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
\r
323 :created_on => bug.date_submitted,
\r
324 :updated_on => bug.last_updated
\r
325 i.author = User.find_by_id(users_map[bug.reporter_id])
\r
326 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
\r
327 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
\r
328 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
\r
329 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
\r
330 i.id = bug.id if keep_bug_ids
\r
332 issues_map[bug.id] = i.id
\r
337 # Redmine checks that the assignee is a project member
\r
338 if (bug.handler_id && users_map[bug.handler_id])
\r
339 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
\r
340 i.save_with_validation(false)
\r
344 bug.bug_notes.each do |note|
\r
345 next unless users_map[note.reporter_id]
\r
346 n = Journal.new :notes => encode(note.bug_note_text.note),
\r
347 :created_on => note.date_submitted
\r
348 n.user = User.find_by_id(users_map[note.reporter_id])
\r
354 bug.bug_files.each do |file|
\r
355 a = Attachment.new :created_on => file.date_added
\r
357 a.author = User.find :first
\r
363 bug.bug_monitors.each do |monitor|
\r
364 next unless users_map[monitor.user_id]
\r
365 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
\r
369 # update issue id sequence if needed (postgresql)
\r
370 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
\r
373 # Bug relationships
\r
374 print "Migrating bug relations"
\r
375 MantisBugRelationship.find(:all).each do |relation|
\r
376 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
\r
377 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
\r
378 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
\r
379 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
\r
387 print "Migrating news"
\r
389 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
\r
390 next unless projects_map[news.project_id]
\r
391 n = News.new :project_id => projects_map[news.project_id],
\r
392 :title => encode(news.headline[0..59]),
\r
393 :description => encode(news.body),
\r
394 :created_on => news.date_posted
\r
395 n.author = User.find_by_id(users_map[news.poster_id])
\r
403 print "Migrating custom fields"
\r
404 IssueCustomField.destroy_all
\r
405 MantisCustomField.find(:all).each do |field|
\r
406 f = IssueCustomField.new :name => field.name[0..29],
\r
407 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
\r
408 :min_length => field.length_min,
\r
409 :max_length => field.length_max,
\r
410 :regexp => field.valid_regexp,
\r
411 :possible_values => field.possible_values.split('|'),
\r
412 :is_required => field.require_report?
\r
416 # Trackers association
\r
417 f.trackers = Tracker.find :all
\r
419 # Projects association
\r
420 field.projects.each do |project|
\r
421 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
\r
425 field.values.each do |value|
\r
426 v = CustomValue.new :custom_field_id => f.id,
\r
427 :value => value.value
\r
428 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
\r
430 end unless f.new_record?
\r
435 puts "Users: #{users_migrated}/#{MantisUser.count}"
\r
436 puts "Projects: #{Project.count}/#{MantisProject.count}"
\r
437 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
\r
438 puts "Versions: #{Version.count}/#{MantisVersion.count}"
\r
439 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
\r
440 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
\r
441 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
\r
442 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
\r
443 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
\r
444 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
\r
445 puts "News: #{News.count}/#{MantisNews.count}"
\r
446 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
\r
449 def self.encoding(charset)
\r
450 @ic = Iconv.new('UTF-8', charset)
\r
451 rescue Iconv::InvalidEncoding
\r
455 def self.establish_connection(params)
\r
456 constants.each do |const|
\r
457 klass = const_get(const)
\r
458 next unless klass.respond_to? 'establish_connection'
\r
459 klass.establish_connection params
\r
463 def self.encode(text)
\r
471 if Redmine::DefaultData::Loader.no_data?
\r
472 puts "Redmine configuration need to be loaded before importing data."
\r
473 puts "Please, run this first:"
\r
475 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
\r
479 puts "WARNING: Your Redmine data will be deleted during this process."
\r
480 print "Are you sure you want to continue ? [y/N] "
\r
482 break unless STDIN.gets.match(/^y$/i)
\r
484 # Default Mantis database settings
\r
485 db_params = {:adapter => 'mysql',
\r
486 :database => 'bugtracker',
\r
487 :host => 'localhost',
\r
488 :username => 'root',
\r
492 puts "Please enter settings for your Mantis database"
\r
493 [:adapter, :host, :database, :username, :password].each do |param|
\r
494 print "#{param} [#{db_params[param]}]: "
\r
495 value = STDIN.gets.chomp!
\r
496 db_params[param] = value unless value.blank?
\r
500 print "encoding [UTF-8]: "
\r
502 encoding = STDIN.gets.chomp!
\r
503 encoding = 'UTF-8' if encoding.blank?
\r
504 break if MantisMigrate.encoding encoding
\r
505 puts "Invalid encoding!"
\r
509 # Make sure bugs can refer bugs in other projects
\r
510 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
\r
512 # Turn off email notifications
\r
513 Setting.notified_events = []
\r
515 MantisMigrate.establish_connection db_params
\r
516 MantisMigrate.migrate
\r