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