##// END OF EJS Templates
Mantis importer:...
Jean-Philippe Lang -
r1086:7a1e928b8db0
parent child
Show More
@@ -1,491 +1,486
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 desc 'Mantis migration script'
19 19
20 20 require 'active_record'
21 21 require 'iconv'
22 22 require 'pp'
23 23
24 24 namespace :redmine do
25 25 task :migrate_from_mantis => :environment do
26 26
27 27 module MantisMigrate
28 28
29 29 DEFAULT_STATUS = IssueStatus.default
30 30 assigned_status = IssueStatus.find_by_position(2)
31 31 resolved_status = IssueStatus.find_by_position(3)
32 32 feedback_status = IssueStatus.find_by_position(4)
33 33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
34 34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
35 35 20 => feedback_status, # feedback
36 36 30 => DEFAULT_STATUS, # acknowledged
37 37 40 => DEFAULT_STATUS, # confirmed
38 38 50 => assigned_status, # assigned
39 39 80 => resolved_status, # resolved
40 40 90 => closed_status # closed
41 41 }
42 42
43 43 priorities = Enumeration.get_values('IPRI')
44 44 DEFAULT_PRIORITY = priorities[2]
45 45 PRIORITY_MAPPING = {10 => priorities[1], # none
46 46 20 => priorities[1], # low
47 47 30 => priorities[2], # normal
48 48 40 => priorities[3], # high
49 49 50 => priorities[4], # urgent
50 50 60 => priorities[5] # immediate
51 51 }
52 52
53 53 TRACKER_BUG = Tracker.find_by_position(1)
54 54 TRACKER_FEATURE = Tracker.find_by_position(2)
55 55
56 56 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
57 57 manager_role = roles[0]
58 58 developer_role = roles[1]
59 59 DEFAULT_ROLE = roles.last
60 60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
61 61 25 => DEFAULT_ROLE, # reporter
62 62 40 => DEFAULT_ROLE, # updater
63 63 55 => developer_role, # developer
64 64 70 => manager_role, # manager
65 65 90 => manager_role # administrator
66 66 }
67 67
68 68 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
69 69 1 => 'int', # Numeric
70 70 2 => 'int', # Float
71 71 3 => 'list', # Enumeration
72 72 4 => 'string', # Email
73 73 5 => 'bool', # Checkbox
74 74 6 => 'list', # List
75 75 7 => 'list', # Multiselection list
76 76 8 => 'date', # Date
77 77 }
78 78
79 79 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
80 80 2 => IssueRelation::TYPE_RELATES, # parent of
81 81 3 => IssueRelation::TYPE_RELATES, # child of
82 82 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
83 83 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
84 84 }
85 85
86 86 class MantisUser < ActiveRecord::Base
87 87 set_table_name :mantis_user_table
88 88
89 89 def firstname
90 90 realname.blank? ? username : realname.split.first[0..29]
91 91 end
92 92
93 93 def lastname
94 94 realname.blank? ? username : realname.split[1..-1].join(' ')[0..29]
95 95 end
96 96
97 97 def email
98 98 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
99 99 read_attribute(:email)
100 100 else
101 101 "#{username}@foo.bar"
102 102 end
103 103 end
104 104
105 105 def username
106 106 read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
107 107 end
108 108 end
109 109
110 110 class MantisProject < ActiveRecord::Base
111 111 set_table_name :mantis_project_table
112 112 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
113 113 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
114 114 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
115 115 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
116 116
117 117 def name
118 118 read_attribute(:name)[0..29]
119 119 end
120 120
121 def description
122 read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254]
123 end
124
125 121 def identifier
126 122 read_attribute(:name).underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
127 123 end
128 124 end
129 125
130 126 class MantisVersion < ActiveRecord::Base
131 127 set_table_name :mantis_project_version_table
132 128
133 129 def version
134 130 read_attribute(:version)[0..29]
135 131 end
136 132
137 133 def description
138 134 read_attribute(:description)[0..254]
139 135 end
140 136 end
141 137
142 138 class MantisCategory < ActiveRecord::Base
143 139 set_table_name :mantis_project_category_table
144 140 end
145 141
146 142 class MantisProjectUser < ActiveRecord::Base
147 143 set_table_name :mantis_project_user_list_table
148 144 end
149 145
150 146 class MantisBug < ActiveRecord::Base
151 147 set_table_name :mantis_bug_table
152 148 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
153 149 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
154 150 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
155 151 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
156 152 end
157 153
158 154 class MantisBugText < ActiveRecord::Base
159 155 set_table_name :mantis_bug_text_table
160 156
161 157 # Adds Mantis steps_to_reproduce and additional_information fields
162 158 # to description if any
163 159 def full_description
164 160 full_description = description
165 161 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
166 162 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
167 163 full_description
168 164 end
169 165 end
170 166
171 167 class MantisBugNote < ActiveRecord::Base
172 168 set_table_name :mantis_bugnote_table
173 169 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
174 170 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
175 171 end
176 172
177 173 class MantisBugNoteText < ActiveRecord::Base
178 174 set_table_name :mantis_bugnote_text_table
179 175 end
180 176
181 177 class MantisBugFile < ActiveRecord::Base
182 178 set_table_name :mantis_bug_file_table
183 179
184 180 def size
185 181 filesize
186 182 end
187 183
188 184 def original_filename
189 filename
185 MantisMigrate.encode(filename)
190 186 end
191 187
192 188 def content_type
193 189 file_type
194 190 end
195 191
196 192 def read
197 193 content
198 194 end
199 195 end
200 196
201 197 class MantisBugRelationship < ActiveRecord::Base
202 198 set_table_name :mantis_bug_relationship_table
203 199 end
204 200
205 201 class MantisBugMonitor < ActiveRecord::Base
206 202 set_table_name :mantis_bug_monitor_table
207 203 end
208 204
209 205 class MantisNews < ActiveRecord::Base
210 206 set_table_name :mantis_news_table
211 207 end
212 208
213 209 class MantisCustomField < ActiveRecord::Base
214 210 set_table_name :mantis_custom_field_table
215 211 set_inheritance_column :none
216 212 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
217 213 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
218 214
219 215 def format
220 216 read_attribute :type
221 217 end
222 218
223 219 def name
224 220 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
225 221 end
226 222 end
227 223
228 224 class MantisCustomFieldProject < ActiveRecord::Base
229 225 set_table_name :mantis_custom_field_project_table
230 226 end
231 227
232 228 class MantisCustomFieldString < ActiveRecord::Base
233 229 set_table_name :mantis_custom_field_string_table
234 230 end
235 231
236 232
237 233 def self.migrate
238 234
239 235 # Users
240 236 print "Migrating users"
241 237 User.delete_all "login <> 'admin'"
242 238 users_map = {}
243 239 users_migrated = 0
244 240 MantisUser.find(:all).each do |user|
245 241 u = User.new :firstname => encode(user.firstname),
246 242 :lastname => encode(user.lastname),
247 243 :mail => user.email,
248 244 :last_login_on => user.last_visit
249 245 u.login = user.username
250 246 u.password = 'mantis'
251 247 u.status = User::STATUS_LOCKED if user.enabled != 1
252 248 u.admin = true if user.access_level == 90
253 249 next unless u.save
254 250 users_migrated += 1
255 251 users_map[user.id] = u.id
256 252 print '.'
257 253 end
258 254 puts
259 255
260 256 # Projects
261 257 print "Migrating projects"
262 258 Project.destroy_all
263 259 projects_map = {}
264 260 versions_map = {}
265 261 categories_map = {}
266 262 MantisProject.find(:all).each do |project|
267 263 p = Project.new :name => encode(project.name),
268 264 :description => encode(project.description)
269 265 p.identifier = project.identifier
270 266 next unless p.save
271 267 projects_map[project.id] = p.id
272 268 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
273 269 p.trackers << TRACKER_BUG
274 270 p.trackers << TRACKER_FEATURE
275 271 print '.'
276 272
277 273 # Project members
278 274 project.members.each do |member|
279 275 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
280 276 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
281 277 m.project = p
282 278 m.save
283 279 end
284 280
285 281 # Project versions
286 282 project.versions.each do |version|
287 283 v = Version.new :name => encode(version.version),
288 284 :description => encode(version.description),
289 285 :effective_date => version.date_order.to_date
290 286 v.project = p
291 287 v.save
292 288 versions_map[version.id] = v.id
293 289 end
294 290
295 291 # Project categories
296 292 project.categories.each do |category|
297 293 g = IssueCategory.new :name => category.category[0,30]
298 294 g.project = p
299 295 g.save
300 296 categories_map[category.category] = g.id
301 297 end
302 298 end
303 299 puts
304 300
305 301 # Bugs
306 302 print "Migrating bugs"
307 303 Issue.destroy_all
308 304 issues_map = {}
309 305 MantisBug.find(:all).each do |bug|
310 306 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
311 307 i = Issue.new :project_id => projects_map[bug.project_id],
312 308 :subject => encode(bug.summary),
313 309 :description => encode(bug.bug_text.full_description),
314 310 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
315 311 :created_on => bug.date_submitted,
316 312 :updated_on => bug.last_updated
317 313 i.author = User.find_by_id(users_map[bug.reporter_id])
318 314 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
319 315 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
320 316 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
321 317 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
322 318 next unless i.save
323 319 issues_map[bug.id] = i.id
324 320 print '.'
325 321
326 322 # Assignee
327 323 # Redmine checks that the assignee is a project member
328 324 if (bug.handler_id && users_map[bug.handler_id])
329 325 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
330 326 i.save_with_validation(false)
331 327 end
332 328
333 329 # Bug notes
334 330 bug.bug_notes.each do |note|
335 331 next unless users_map[note.reporter_id]
336 332 n = Journal.new :notes => encode(note.bug_note_text.note),
337 333 :created_on => note.date_submitted
338 334 n.user = User.find_by_id(users_map[note.reporter_id])
339 335 n.journalized = i
340 336 n.save
341 337 end
342 338
343 339 # Bug files
344 340 bug.bug_files.each do |file|
345 341 a = Attachment.new :created_on => file.date_added
346 342 a.file = file
347 343 a.author = User.find :first
348 344 a.container = i
349 345 a.save
350 346 end
351 347
352 348 # Bug monitors
353 349 bug.bug_monitors.each do |monitor|
354 350 next unless users_map[monitor.user_id]
355 351 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
356 352 end
357 353 end
358 354 puts
359 355
360 356 # Bug relationships
361 357 print "Migrating bug relations"
362 358 MantisBugRelationship.find(:all).each do |relation|
363 359 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
364 360 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
365 361 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
366 362 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
367 363 pp r unless r.save
368 364 print '.'
369 365 end
370 366 puts
371 367
372 368 # News
373 369 print "Migrating news"
374 370 News.destroy_all
375 371 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
376 372 next unless projects_map[news.project_id]
377 373 n = News.new :project_id => projects_map[news.project_id],
378 374 :title => encode(news.headline[0..59]),
379 375 :description => encode(news.body),
380 376 :created_on => news.date_posted
381 377 n.author = User.find_by_id(users_map[news.poster_id])
382 378 n.save
383 379 print '.'
384 380 end
385 381 puts
386 382
387 383 # Custom fields
388 384 print "Migrating custom fields"
389 385 IssueCustomField.destroy_all
390 386 MantisCustomField.find(:all).each do |field|
391 387 f = IssueCustomField.new :name => field.name[0..29],
392 388 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
393 389 :min_length => field.length_min,
394 390 :max_length => field.length_max,
395 391 :regexp => field.valid_regexp,
396 392 :possible_values => field.possible_values.split('|'),
397 393 :is_required => field.require_report?
398 394 next unless f.save
399 395 print '.'
400 396
401 397 # Trackers association
402 398 f.trackers = Tracker.find :all
403 399
404 400 # Projects association
405 401 field.projects.each do |project|
406 402 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
407 403 end
408 404
409 405 # Values
410 406 field.values.each do |value|
411 407 v = CustomValue.new :custom_field_id => f.id,
412 408 :value => value.value
413 409 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
414 410 v.save
415 411 end unless f.new_record?
416 412 end
417 413 puts
418 414
419 415 puts
420 416 puts "Users: #{users_migrated}/#{MantisUser.count}"
421 417 puts "Projects: #{Project.count}/#{MantisProject.count}"
422 418 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
423 419 puts "Versions: #{Version.count}/#{MantisVersion.count}"
424 420 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
425 421 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
426 422 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
427 423 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
428 424 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
429 425 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
430 426 puts "News: #{News.count}/#{MantisNews.count}"
431 427 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
432 428 end
433 429
434 430 def self.encoding(charset)
435 431 @ic = Iconv.new('UTF-8', charset)
436 432 rescue Iconv::InvalidEncoding
437 433 return false
438 434 end
439 435
440 436 def self.establish_connection(params)
441 437 constants.each do |const|
442 438 klass = const_get(const)
443 439 next unless klass.respond_to? 'establish_connection'
444 440 klass.establish_connection params
445 441 end
446 442 end
447 443
448 private
449 444 def self.encode(text)
450 445 @ic.iconv text
451 446 rescue
452 447 text
453 448 end
454 449 end
455 450
456 451 puts
457 452 puts "WARNING: Your Redmine data will be deleted during this process."
458 453 print "Are you sure you want to continue ? [y/N] "
459 454 break unless STDIN.gets.match(/^y$/i)
460 455
461 456 # Default Mantis database settings
462 457 db_params = {:adapter => 'mysql',
463 458 :database => 'bugtracker',
464 459 :host => 'localhost',
465 460 :username => 'root',
466 461 :password => '' }
467 462
468 463 puts
469 464 puts "Please enter settings for your Mantis database"
470 465 [:adapter, :host, :database, :username, :password].each do |param|
471 466 print "#{param} [#{db_params[param]}]: "
472 467 value = STDIN.gets.chomp!
473 468 db_params[param] = value unless value.blank?
474 469 end
475 470
476 471 while true
477 472 print "encoding [UTF-8]: "
478 473 encoding = STDIN.gets.chomp!
479 474 encoding = 'UTF-8' if encoding.blank?
480 475 break if MantisMigrate.encoding encoding
481 476 puts "Invalid encoding!"
482 477 end
483 478 puts
484 479
485 480 # Make sure bugs can refer bugs in other projects
486 481 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
487 482
488 483 MantisMigrate.establish_connection db_params
489 484 MantisMigrate.migrate
490 485 end
491 486 end
General Comments 0
You need to be logged in to leave comments. Login now