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