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