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