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