##// END OF EJS Templates
Updates mantis importer for new project name/identifier max lengths (#6446)....
Jean-Philippe Lang -
r4289:97a9210483c1
parent child
Show More
@@ -1,518 +1,514
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 = IssuePriority.all
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 @firstname = realname.blank? ? username : realname.split.first[0..29]
91 91 @firstname.gsub!(/[^\w\s\'\-]/i, '')
92 92 @firstname
93 93 end
94 94
95 95 def lastname
96 96 @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
97 97 @lastname.gsub!(/[^\w\s\'\-]/i, '')
98 98 @lastname = '-' if @lastname.blank?
99 99 @lastname
100 100 end
101 101
102 102 def email
103 103 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
104 104 !User.find_by_mail(read_attribute(:email))
105 105 @email = read_attribute(:email)
106 106 else
107 107 @email = "#{username}@foo.bar"
108 108 end
109 109 end
110 110
111 111 def username
112 112 read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
113 113 end
114 114 end
115 115
116 116 class MantisProject < ActiveRecord::Base
117 117 set_table_name :mantis_project_table
118 118 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
119 119 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
120 120 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
121 121 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
122 122
123 def name
124 read_attribute(:name)[0..29]
125 end
126
127 123 def identifier
128 read_attribute(:name).underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
124 read_attribute(:name).gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
129 125 end
130 126 end
131 127
132 128 class MantisVersion < ActiveRecord::Base
133 129 set_table_name :mantis_project_version_table
134 130
135 131 def version
136 132 read_attribute(:version)[0..29]
137 133 end
138 134
139 135 def description
140 136 read_attribute(:description)[0..254]
141 137 end
142 138 end
143 139
144 140 class MantisCategory < ActiveRecord::Base
145 141 set_table_name :mantis_project_category_table
146 142 end
147 143
148 144 class MantisProjectUser < ActiveRecord::Base
149 145 set_table_name :mantis_project_user_list_table
150 146 end
151 147
152 148 class MantisBug < ActiveRecord::Base
153 149 set_table_name :mantis_bug_table
154 150 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
155 151 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
156 152 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
157 153 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
158 154 end
159 155
160 156 class MantisBugText < ActiveRecord::Base
161 157 set_table_name :mantis_bug_text_table
162 158
163 159 # Adds Mantis steps_to_reproduce and additional_information fields
164 160 # to description if any
165 161 def full_description
166 162 full_description = description
167 163 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
168 164 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
169 165 full_description
170 166 end
171 167 end
172 168
173 169 class MantisBugNote < ActiveRecord::Base
174 170 set_table_name :mantis_bugnote_table
175 171 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
176 172 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
177 173 end
178 174
179 175 class MantisBugNoteText < ActiveRecord::Base
180 176 set_table_name :mantis_bugnote_text_table
181 177 end
182 178
183 179 class MantisBugFile < ActiveRecord::Base
184 180 set_table_name :mantis_bug_file_table
185 181
186 182 def size
187 183 filesize
188 184 end
189 185
190 186 def original_filename
191 187 MantisMigrate.encode(filename)
192 188 end
193 189
194 190 def content_type
195 191 file_type
196 192 end
197 193
198 194 def read(*args)
199 195 if @read_finished
200 196 nil
201 197 else
202 198 @read_finished = true
203 199 content
204 200 end
205 201 end
206 202 end
207 203
208 204 class MantisBugRelationship < ActiveRecord::Base
209 205 set_table_name :mantis_bug_relationship_table
210 206 end
211 207
212 208 class MantisBugMonitor < ActiveRecord::Base
213 209 set_table_name :mantis_bug_monitor_table
214 210 end
215 211
216 212 class MantisNews < ActiveRecord::Base
217 213 set_table_name :mantis_news_table
218 214 end
219 215
220 216 class MantisCustomField < ActiveRecord::Base
221 217 set_table_name :mantis_custom_field_table
222 218 set_inheritance_column :none
223 219 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
224 220 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
225 221
226 222 def format
227 223 read_attribute :type
228 224 end
229 225
230 226 def name
231 227 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
232 228 end
233 229 end
234 230
235 231 class MantisCustomFieldProject < ActiveRecord::Base
236 232 set_table_name :mantis_custom_field_project_table
237 233 end
238 234
239 235 class MantisCustomFieldString < ActiveRecord::Base
240 236 set_table_name :mantis_custom_field_string_table
241 237 end
242 238
243 239
244 240 def self.migrate
245 241
246 242 # Users
247 243 print "Migrating users"
248 244 User.delete_all "login <> 'admin'"
249 245 users_map = {}
250 246 users_migrated = 0
251 247 MantisUser.find(:all).each do |user|
252 248 u = User.new :firstname => encode(user.firstname),
253 249 :lastname => encode(user.lastname),
254 250 :mail => user.email,
255 251 :last_login_on => user.last_visit
256 252 u.login = user.username
257 253 u.password = 'mantis'
258 254 u.status = User::STATUS_LOCKED if user.enabled != 1
259 255 u.admin = true if user.access_level == 90
260 256 next unless u.save!
261 257 users_migrated += 1
262 258 users_map[user.id] = u.id
263 259 print '.'
264 260 end
265 261 puts
266 262
267 263 # Projects
268 264 print "Migrating projects"
269 265 Project.destroy_all
270 266 projects_map = {}
271 267 versions_map = {}
272 268 categories_map = {}
273 269 MantisProject.find(:all).each do |project|
274 270 p = Project.new :name => encode(project.name),
275 271 :description => encode(project.description)
276 272 p.identifier = project.identifier
277 273 next unless p.save
278 274 projects_map[project.id] = p.id
279 275 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
280 276 p.trackers << TRACKER_BUG
281 277 p.trackers << TRACKER_FEATURE
282 278 print '.'
283 279
284 280 # Project members
285 281 project.members.each do |member|
286 282 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
287 283 :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
288 284 m.project = p
289 285 m.save
290 286 end
291 287
292 288 # Project versions
293 289 project.versions.each do |version|
294 290 v = Version.new :name => encode(version.version),
295 291 :description => encode(version.description),
296 292 :effective_date => version.date_order.to_date
297 293 v.project = p
298 294 v.save
299 295 versions_map[version.id] = v.id
300 296 end
301 297
302 298 # Project categories
303 299 project.categories.each do |category|
304 300 g = IssueCategory.new :name => category.category[0,30]
305 301 g.project = p
306 302 g.save
307 303 categories_map[category.category] = g.id
308 304 end
309 305 end
310 306 puts
311 307
312 308 # Bugs
313 309 print "Migrating bugs"
314 310 Issue.destroy_all
315 311 issues_map = {}
316 312 keep_bug_ids = (Issue.count == 0)
317 313 MantisBug.find_each(:batch_size => 200) do |bug|
318 314 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
319 315 i = Issue.new :project_id => projects_map[bug.project_id],
320 316 :subject => encode(bug.summary),
321 317 :description => encode(bug.bug_text.full_description),
322 318 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
323 319 :created_on => bug.date_submitted,
324 320 :updated_on => bug.last_updated
325 321 i.author = User.find_by_id(users_map[bug.reporter_id])
326 322 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
327 323 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
328 324 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
329 325 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
330 326 i.id = bug.id if keep_bug_ids
331 327 next unless i.save
332 328 issues_map[bug.id] = i.id
333 329 print '.'
334 330 STDOUT.flush
335 331
336 332 # Assignee
337 333 # Redmine checks that the assignee is a project member
338 334 if (bug.handler_id && users_map[bug.handler_id])
339 335 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
340 336 i.save_with_validation(false)
341 337 end
342 338
343 339 # Bug notes
344 340 bug.bug_notes.each do |note|
345 341 next unless users_map[note.reporter_id]
346 342 n = Journal.new :notes => encode(note.bug_note_text.note),
347 343 :created_on => note.date_submitted
348 344 n.user = User.find_by_id(users_map[note.reporter_id])
349 345 n.journalized = i
350 346 n.save
351 347 end
352 348
353 349 # Bug files
354 350 bug.bug_files.each do |file|
355 351 a = Attachment.new :created_on => file.date_added
356 352 a.file = file
357 353 a.author = User.find :first
358 354 a.container = i
359 355 a.save
360 356 end
361 357
362 358 # Bug monitors
363 359 bug.bug_monitors.each do |monitor|
364 360 next unless users_map[monitor.user_id]
365 361 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
366 362 end
367 363 end
368 364
369 365 # update issue id sequence if needed (postgresql)
370 366 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
371 367 puts
372 368
373 369 # Bug relationships
374 370 print "Migrating bug relations"
375 371 MantisBugRelationship.find(:all).each do |relation|
376 372 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
377 373 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
378 374 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
379 375 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
380 376 pp r unless r.save
381 377 print '.'
382 378 STDOUT.flush
383 379 end
384 380 puts
385 381
386 382 # News
387 383 print "Migrating news"
388 384 News.destroy_all
389 385 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
390 386 next unless projects_map[news.project_id]
391 387 n = News.new :project_id => projects_map[news.project_id],
392 388 :title => encode(news.headline[0..59]),
393 389 :description => encode(news.body),
394 390 :created_on => news.date_posted
395 391 n.author = User.find_by_id(users_map[news.poster_id])
396 392 n.save
397 393 print '.'
398 394 STDOUT.flush
399 395 end
400 396 puts
401 397
402 398 # Custom fields
403 399 print "Migrating custom fields"
404 400 IssueCustomField.destroy_all
405 401 MantisCustomField.find(:all).each do |field|
406 402 f = IssueCustomField.new :name => field.name[0..29],
407 403 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
408 404 :min_length => field.length_min,
409 405 :max_length => field.length_max,
410 406 :regexp => field.valid_regexp,
411 407 :possible_values => field.possible_values.split('|'),
412 408 :is_required => field.require_report?
413 409 next unless f.save
414 410 print '.'
415 411 STDOUT.flush
416 412 # Trackers association
417 413 f.trackers = Tracker.find :all
418 414
419 415 # Projects association
420 416 field.projects.each do |project|
421 417 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
422 418 end
423 419
424 420 # Values
425 421 field.values.each do |value|
426 422 v = CustomValue.new :custom_field_id => f.id,
427 423 :value => value.value
428 424 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
429 425 v.save
430 426 end unless f.new_record?
431 427 end
432 428 puts
433 429
434 430 puts
435 431 puts "Users: #{users_migrated}/#{MantisUser.count}"
436 432 puts "Projects: #{Project.count}/#{MantisProject.count}"
437 433 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
438 434 puts "Versions: #{Version.count}/#{MantisVersion.count}"
439 435 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
440 436 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
441 437 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
442 438 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
443 439 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
444 440 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
445 441 puts "News: #{News.count}/#{MantisNews.count}"
446 442 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
447 443 end
448 444
449 445 def self.encoding(charset)
450 446 @ic = Iconv.new('UTF-8', charset)
451 447 rescue Iconv::InvalidEncoding
452 448 return false
453 449 end
454 450
455 451 def self.establish_connection(params)
456 452 constants.each do |const|
457 453 klass = const_get(const)
458 454 next unless klass.respond_to? 'establish_connection'
459 455 klass.establish_connection params
460 456 end
461 457 end
462 458
463 459 def self.encode(text)
464 460 @ic.iconv text
465 461 rescue
466 462 text
467 463 end
468 464 end
469 465
470 466 puts
471 467 if Redmine::DefaultData::Loader.no_data?
472 468 puts "Redmine configuration need to be loaded before importing data."
473 469 puts "Please, run this first:"
474 470 puts
475 471 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
476 472 exit
477 473 end
478 474
479 475 puts "WARNING: Your Redmine data will be deleted during this process."
480 476 print "Are you sure you want to continue ? [y/N] "
481 477 STDOUT.flush
482 478 break unless STDIN.gets.match(/^y$/i)
483 479
484 480 # Default Mantis database settings
485 481 db_params = {:adapter => 'mysql',
486 482 :database => 'bugtracker',
487 483 :host => 'localhost',
488 484 :username => 'root',
489 485 :password => '' }
490 486
491 487 puts
492 488 puts "Please enter settings for your Mantis database"
493 489 [:adapter, :host, :database, :username, :password].each do |param|
494 490 print "#{param} [#{db_params[param]}]: "
495 491 value = STDIN.gets.chomp!
496 492 db_params[param] = value unless value.blank?
497 493 end
498 494
499 495 while true
500 496 print "encoding [UTF-8]: "
501 497 STDOUT.flush
502 498 encoding = STDIN.gets.chomp!
503 499 encoding = 'UTF-8' if encoding.blank?
504 500 break if MantisMigrate.encoding encoding
505 501 puts "Invalid encoding!"
506 502 end
507 503 puts
508 504
509 505 # Make sure bugs can refer bugs in other projects
510 506 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
511 507
512 508 # Turn off email notifications
513 509 Setting.notified_events = []
514 510
515 511 MantisMigrate.establish_connection db_params
516 512 MantisMigrate.migrate
517 513 end
518 514 end
General Comments 0
You need to be logged in to leave comments. Login now