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