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