##// END OF EJS Templates
Fixed Mantis importer: projects trackers and modules assignment...
Jean-Philippe Lang -
r923:2cf11bd64e7b
parent child
Show More
@@ -1,487 +1,491
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 namespace :redmine do
25 25 task :migrate_from_mantis => :environment do
26 26
27 27 module MantisMigrate
28 28
29 29 DEFAULT_STATUS = IssueStatus.default
30 30 assigned_status = IssueStatus.find_by_position(2)
31 31 resolved_status = IssueStatus.find_by_position(3)
32 32 feedback_status = IssueStatus.find_by_position(4)
33 33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
34 34 STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
35 35 20 => feedback_status, # feedback
36 36 30 => DEFAULT_STATUS, # acknowledged
37 37 40 => DEFAULT_STATUS, # confirmed
38 38 50 => assigned_status, # assigned
39 39 80 => resolved_status, # resolved
40 40 90 => closed_status # closed
41 41 }
42 42
43 43 priorities = Enumeration.get_values('IPRI')
44 44 DEFAULT_PRIORITY = priorities[2]
45 45 PRIORITY_MAPPING = {10 => priorities[1], # none
46 46 20 => priorities[1], # low
47 47 30 => priorities[2], # normal
48 48 40 => priorities[3], # high
49 49 50 => priorities[4], # urgent
50 50 60 => priorities[5] # immediate
51 51 }
52 52
53 53 TRACKER_BUG = Tracker.find_by_position(1)
54 54 TRACKER_FEATURE = Tracker.find_by_position(2)
55 55
56 DEFAULT_ROLE = Role.find_by_position(3)
57 manager_role = Role.find_by_position(1)
58 developer_role = Role.find_by_position(2)
56 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
57 manager_role = roles[0]
58 developer_role = roles[1]
59 DEFAULT_ROLE = roles.last
59 60 ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
60 61 25 => DEFAULT_ROLE, # reporter
61 62 40 => DEFAULT_ROLE, # updater
62 63 55 => developer_role, # developer
63 64 70 => manager_role, # manager
64 65 90 => manager_role # administrator
65 66 }
66 67
67 68 CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
68 69 1 => 'int', # Numeric
69 70 2 => 'int', # Float
70 71 3 => 'list', # Enumeration
71 72 4 => 'string', # Email
72 73 5 => 'bool', # Checkbox
73 74 6 => 'list', # List
74 75 7 => 'list', # Multiselection list
75 76 8 => 'date', # Date
76 77 }
77 78
78 79 RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
79 80 2 => IssueRelation::TYPE_RELATES, # parent of
80 81 3 => IssueRelation::TYPE_RELATES, # child of
81 82 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
82 83 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
83 84 }
84 85
85 86 class MantisUser < ActiveRecord::Base
86 87 set_table_name :mantis_user_table
87 88
88 89 def firstname
89 90 realname.blank? ? username : realname.split.first[0..29]
90 91 end
91 92
92 93 def lastname
93 94 realname.blank? ? username : realname.split[1..-1].join(' ')[0..29]
94 95 end
95 96
96 97 def email
97 98 if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
98 99 read_attribute(:email)
99 100 else
100 101 "#{username}@foo.bar"
101 102 end
102 103 end
103 104
104 105 def username
105 106 read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
106 107 end
107 108 end
108 109
109 110 class MantisProject < ActiveRecord::Base
110 111 set_table_name :mantis_project_table
111 112 has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
112 113 has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
113 114 has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
114 115 has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
115 116
116 117 def name
117 118 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
118 119 end
119 120
120 121 def description
121 122 read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254]
122 123 end
123 124
124 125 def identifier
125 126 read_attribute(:name).underscore[0..11].gsub(/[^a-z0-9\-]/, '-')
126 127 end
127 128 end
128 129
129 130 class MantisVersion < ActiveRecord::Base
130 131 set_table_name :mantis_project_version_table
131 132
132 133 def version
133 134 read_attribute(:version)[0..29]
134 135 end
135 136
136 137 def description
137 138 read_attribute(:description)[0..254]
138 139 end
139 140 end
140 141
141 142 class MantisCategory < ActiveRecord::Base
142 143 set_table_name :mantis_project_category_table
143 144 end
144 145
145 146 class MantisProjectUser < ActiveRecord::Base
146 147 set_table_name :mantis_project_user_list_table
147 148 end
148 149
149 150 class MantisBug < ActiveRecord::Base
150 151 set_table_name :mantis_bug_table
151 152 belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
152 153 has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
153 154 has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
154 155 has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
155 156 end
156 157
157 158 class MantisBugText < ActiveRecord::Base
158 159 set_table_name :mantis_bug_text_table
159 160
160 161 # Adds Mantis steps_to_reproduce and additional_information fields
161 162 # to description if any
162 163 def full_description
163 164 full_description = description
164 165 full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
165 166 full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
166 167 full_description
167 168 end
168 169 end
169 170
170 171 class MantisBugNote < ActiveRecord::Base
171 172 set_table_name :mantis_bugnote_table
172 173 belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
173 174 belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
174 175 end
175 176
176 177 class MantisBugNoteText < ActiveRecord::Base
177 178 set_table_name :mantis_bugnote_text_table
178 179 end
179 180
180 181 class MantisBugFile < ActiveRecord::Base
181 182 set_table_name :mantis_bug_file_table
182 183
183 184 def size
184 185 filesize
185 186 end
186 187
187 188 def original_filename
188 189 filename
189 190 end
190 191
191 192 def content_type
192 193 file_type
193 194 end
194 195
195 196 def read
196 197 content
197 198 end
198 199 end
199 200
200 201 class MantisBugRelationship < ActiveRecord::Base
201 202 set_table_name :mantis_bug_relationship_table
202 203 end
203 204
204 205 class MantisBugMonitor < ActiveRecord::Base
205 206 set_table_name :mantis_bug_monitor_table
206 207 end
207 208
208 209 class MantisNews < ActiveRecord::Base
209 210 set_table_name :mantis_news_table
210 211 end
211 212
212 213 class MantisCustomField < ActiveRecord::Base
213 214 set_table_name :mantis_custom_field_table
214 215 set_inheritance_column :none
215 216 has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
216 217 has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
217 218
218 219 def format
219 220 read_attribute :type
220 221 end
221 222
222 223 def name
223 224 read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-')
224 225 end
225 226 end
226 227
227 228 class MantisCustomFieldProject < ActiveRecord::Base
228 229 set_table_name :mantis_custom_field_project_table
229 230 end
230 231
231 232 class MantisCustomFieldString < ActiveRecord::Base
232 233 set_table_name :mantis_custom_field_string_table
233 234 end
234 235
235 236
236 237 def self.migrate
237 238
238 239 # Users
239 240 print "Migrating users"
240 241 User.delete_all "login <> 'admin'"
241 242 users_map = {}
242 243 users_migrated = 0
243 244 MantisUser.find(:all).each do |user|
244 245 u = User.new :firstname => encode(user.firstname),
245 246 :lastname => encode(user.lastname),
246 247 :mail => user.email,
247 248 :last_login_on => user.last_visit
248 249 u.login = user.username
249 250 u.password = 'mantis'
250 251 u.status = User::STATUS_LOCKED if user.enabled != 1
251 252 u.admin = true if user.access_level == 90
252 253 next unless u.save
253 254 users_migrated += 1
254 255 users_map[user.id] = u.id
255 256 print '.'
256 257 end
257 258 puts
258 259
259 260 # Projects
260 261 print "Migrating projects"
261 262 Project.destroy_all
262 263 projects_map = {}
263 264 versions_map = {}
264 265 categories_map = {}
265 266 MantisProject.find(:all).each do |project|
266 267 p = Project.new :name => encode(project.name),
267 268 :description => encode(project.description)
268 269 p.identifier = project.identifier
269 270 next unless p.save
270 271 projects_map[project.id] = p.id
272 p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
273 p.trackers << TRACKER_BUG
274 p.trackers << TRACKER_FEATURE
271 275 print '.'
272 276
273 277 # Project members
274 278 project.members.each do |member|
275 279 m = Member.new :user => User.find_by_id(users_map[member.user_id]),
276 280 :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE
277 281 m.project = p
278 282 m.save
279 283 end
280 284
281 285 # Project versions
282 286 project.versions.each do |version|
283 287 v = Version.new :name => encode(version.version),
284 288 :description => encode(version.description),
285 289 :effective_date => version.date_order.to_date
286 290 v.project = p
287 291 v.save
288 292 versions_map[version.id] = v.id
289 293 end
290 294
291 295 # Project categories
292 296 project.categories.each do |category|
293 297 g = IssueCategory.new :name => category.category[0,30]
294 298 g.project = p
295 299 g.save
296 300 categories_map[category.category] = g.id
297 301 end
298 302 end
299 303 puts
300 304
301 305 # Bugs
302 306 print "Migrating bugs"
303 307 Issue.destroy_all
304 308 issues_map = {}
305 309 MantisBug.find(:all).each do |bug|
306 310 next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
307 311 i = Issue.new :project_id => projects_map[bug.project_id],
308 312 :subject => encode(bug.summary),
309 313 :description => encode(bug.bug_text.full_description),
310 314 :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
311 315 :created_on => bug.date_submitted,
312 316 :updated_on => bug.last_updated
313 317 i.author = User.find_by_id(users_map[bug.reporter_id])
314 318 i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
315 319 i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
316 320 i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
317 321 i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
318 322 next unless i.save
319 323 issues_map[bug.id] = i.id
320 324 print '.'
321 325
322 326 # Assignee
323 327 # Redmine checks that the assignee is a project member
324 328 if (bug.handler_id && users_map[bug.handler_id])
325 329 i.assigned_to = User.find_by_id(users_map[bug.handler_id])
326 330 i.save_with_validation(false)
327 331 end
328 332
329 333 # Bug notes
330 334 bug.bug_notes.each do |note|
331 335 next unless users_map[note.reporter_id]
332 336 n = Journal.new :notes => encode(note.bug_note_text.note),
333 337 :created_on => note.date_submitted
334 338 n.user = User.find_by_id(users_map[note.reporter_id])
335 339 n.journalized = i
336 340 n.save
337 341 end
338 342
339 343 # Bug files
340 344 bug.bug_files.each do |file|
341 345 a = Attachment.new :created_on => file.date_added
342 346 a.file = file
343 347 a.author = User.find :first
344 348 a.container = i
345 349 a.save
346 350 end
347 351
348 352 # Bug monitors
349 353 bug.bug_monitors.each do |monitor|
350 354 next unless users_map[monitor.user_id]
351 355 i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
352 356 end
353 357 end
354 358 puts
355 359
356 360 # Bug relationships
357 361 print "Migrating bug relations"
358 362 MantisBugRelationship.find(:all).each do |relation|
359 363 next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
360 364 r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
361 365 r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
362 366 r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
363 367 pp r unless r.save
364 368 print '.'
365 369 end
366 370 puts
367 371
368 372 # News
369 373 print "Migrating news"
370 374 News.destroy_all
371 375 MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
372 376 next unless projects_map[news.project_id]
373 377 n = News.new :project_id => projects_map[news.project_id],
374 378 :title => encode(news.headline[0..59]),
375 379 :description => encode(news.body),
376 380 :created_on => news.date_posted
377 381 n.author = User.find_by_id(users_map[news.poster_id])
378 382 n.save
379 383 print '.'
380 384 end
381 385 puts
382 386
383 387 # Custom fields
384 388 print "Migrating custom fields"
385 389 IssueCustomField.destroy_all
386 390 MantisCustomField.find(:all).each do |field|
387 391 f = IssueCustomField.new :name => field.name[0..29],
388 392 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
389 393 :min_length => field.length_min,
390 394 :max_length => field.length_max,
391 395 :regexp => field.valid_regexp,
392 396 :possible_values => field.possible_values.split('|'),
393 397 :is_required => field.require_report?
394 398 next unless f.save
395 399 print '.'
396 400
397 401 # Trackers association
398 402 f.trackers = Tracker.find :all
399 403
400 404 # Projects association
401 405 field.projects.each do |project|
402 406 f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
403 407 end
404 408
405 409 # Values
406 410 field.values.each do |value|
407 411 v = CustomValue.new :custom_field_id => f.id,
408 412 :value => value.value
409 413 v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
410 414 v.save
411 415 end unless f.new_record?
412 416 end
413 417 puts
414 418
415 419 puts
416 420 puts "Users: #{users_migrated}/#{MantisUser.count}"
417 421 puts "Projects: #{Project.count}/#{MantisProject.count}"
418 422 puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
419 423 puts "Versions: #{Version.count}/#{MantisVersion.count}"
420 424 puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
421 425 puts "Bugs: #{Issue.count}/#{MantisBug.count}"
422 426 puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
423 427 puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
424 428 puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
425 429 puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
426 430 puts "News: #{News.count}/#{MantisNews.count}"
427 431 puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
428 432 end
429 433
430 434 def self.encoding(charset)
431 435 @ic = Iconv.new('UTF-8', charset)
432 436 rescue Iconv::InvalidEncoding
433 437 return false
434 438 end
435 439
436 440 def self.establish_connection(params)
437 441 constants.each do |const|
438 442 klass = const_get(const)
439 443 next unless klass.respond_to? 'establish_connection'
440 444 klass.establish_connection params
441 445 end
442 446 end
443 447
444 448 private
445 449 def self.encode(text)
446 450 @ic.iconv text
447 451 rescue
448 452 text
449 453 end
450 454 end
451 455
452 456 puts
453 457 puts "WARNING: Your Redmine data will be deleted during this process."
454 458 print "Are you sure you want to continue ? [y/N] "
455 459 break unless STDIN.gets.match(/^y$/i)
456 460
457 461 # Default Mantis database settings
458 462 db_params = {:adapter => 'mysql',
459 463 :database => 'bugtracker',
460 464 :host => 'localhost',
461 465 :username => 'root',
462 466 :password => '' }
463 467
464 468 puts
465 469 puts "Please enter settings for your Mantis database"
466 470 [:adapter, :host, :database, :username, :password].each do |param|
467 471 print "#{param} [#{db_params[param]}]: "
468 472 value = STDIN.gets.chomp!
469 473 db_params[param] = value unless value.blank?
470 474 end
471 475
472 476 while true
473 477 print "encoding [UTF-8]: "
474 478 encoding = STDIN.gets.chomp!
475 479 encoding = 'UTF-8' if encoding.blank?
476 480 break if MantisMigrate.encoding encoding
477 481 puts "Invalid encoding!"
478 482 end
479 483 puts
480 484
481 485 # Make sure bugs can refer bugs in other projects
482 486 Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
483 487
484 488 MantisMigrate.establish_connection db_params
485 489 MantisMigrate.migrate
486 490 end
487 491 end
@@ -1,499 +1,500
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 require 'active_record'
19 19 require 'iconv'
20 20 require 'pp'
21 21
22 22 namespace :redmine do
23 23 desc 'Trac migration script'
24 24 task :migrate_from_trac => :environment do
25 25
26 26 module TracMigrate
27 27 TICKET_MAP = [];
28 28
29 29 DEFAULT_STATUS = IssueStatus.default
30 30 assigned_status = IssueStatus.find_by_position(2)
31 31 resolved_status = IssueStatus.find_by_position(3)
32 32 feedback_status = IssueStatus.find_by_position(4)
33 33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
34 34 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
35 35 'reopened' => feedback_status,
36 36 'assigned' => assigned_status,
37 37 'closed' => closed_status
38 38 }
39 39
40 40 priorities = Enumeration.get_values('IPRI')
41 41 DEFAULT_PRIORITY = priorities[0]
42 42 PRIORITY_MAPPING = {'lowest' => priorities[0],
43 43 'low' => priorities[0],
44 44 'normal' => priorities[1],
45 45 'high' => priorities[2],
46 46 'highest' => priorities[3]
47 47 }
48 48
49 49 TRACKER_BUG = Tracker.find_by_position(1)
50 50 TRACKER_FEATURE = Tracker.find_by_position(2)
51 51 DEFAULT_TRACKER = TRACKER_BUG
52 52 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
53 53 'enhancement' => TRACKER_FEATURE,
54 54 'task' => TRACKER_FEATURE,
55 55 'patch' =>TRACKER_FEATURE
56 56 }
57 57
58 DEFAULT_ROLE = Role.find_by_position(3)
59 manager_role = Role.find_by_position(1)
60 developer_role = Role.find_by_position(2)
58 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
59 manager_role = roles[0]
60 developer_role = roles[1]
61 DEFAULT_ROLE = roles.last
61 62 ROLE_MAPPING = {'admin' => manager_role,
62 63 'developer' => developer_role
63 64 }
64 65
65 66 class TracComponent < ActiveRecord::Base
66 67 set_table_name :component
67 68 end
68 69
69 70 class TracMilestone < ActiveRecord::Base
70 71 set_table_name :milestone
71 72
72 73 def due
73 74 if read_attribute(:due) > 0
74 75 Time.at(read_attribute(:due)).to_date
75 76 else
76 77 nil
77 78 end
78 79 end
79 80 end
80 81
81 82 class TracTicketCustom < ActiveRecord::Base
82 83 set_table_name :ticket_custom
83 84 end
84 85
85 86 class TracAttachment < ActiveRecord::Base
86 87 set_table_name :attachment
87 88 set_inheritance_column :none
88 89
89 90 def time; Time.at(read_attribute(:time)) end
90 91
91 92 def original_filename
92 93 filename
93 94 end
94 95
95 96 def content_type
96 97 Redmine::MimeType.of(filename) || ''
97 98 end
98 99
99 100 def exist?
100 101 File.file? trac_fullpath
101 102 end
102 103
103 104 def read
104 105 File.open("#{trac_fullpath}", 'rb').read
105 106 end
106 107
107 108 private
108 109 def trac_fullpath
109 110 attachment_type = read_attribute(:type)
110 111 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
111 112 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
112 113 end
113 114 end
114 115
115 116 class TracTicket < ActiveRecord::Base
116 117 set_table_name :ticket
117 118 set_inheritance_column :none
118 119
119 120 # ticket changes: only migrate status changes and comments
120 121 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
121 122 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
122 123 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
123 124
124 125 def ticket_type
125 126 read_attribute(:type)
126 127 end
127 128
128 129 def summary
129 130 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
130 131 end
131 132
132 133 def description
133 134 read_attribute(:description).blank? ? summary : read_attribute(:description)
134 135 end
135 136
136 137 def time; Time.at(read_attribute(:time)) end
137 138 end
138 139
139 140 class TracTicketChange < ActiveRecord::Base
140 141 set_table_name :ticket_change
141 142
142 143 def time; Time.at(read_attribute(:time)) end
143 144 end
144 145
145 146 class TracWikiPage < ActiveRecord::Base
146 147 set_table_name :wiki
147 148 end
148 149
149 150 class TracPermission < ActiveRecord::Base
150 151 set_table_name :permission
151 152 end
152 153
153 154 def self.find_or_create_user(username, project_member = false)
154 155 u = User.find_by_login(username)
155 156 if !u
156 157 # Create a new user if not found
157 158 mail = username[0,limit_for(User, 'mail')]
158 159 mail = "#{mail}@foo.bar" unless mail.include?("@")
159 160 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
160 161 :lastname => '-',
161 162 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
162 163 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
163 164 u.password = 'trac'
164 165 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
165 166 # finally, a default user is used if the new user is not valid
166 167 u = User.find(:first) unless u.save
167 168 end
168 169 # Make sure he is a member of the project
169 170 if project_member && !u.member_of?(@target_project)
170 171 role = DEFAULT_ROLE
171 172 if u.admin
172 173 role = ROLE_MAPPING['admin']
173 174 elsif TracPermission.find_by_username_and_action(username, 'developer')
174 175 role = ROLE_MAPPING['developer']
175 176 end
176 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
177 Member.create(:user => u, :project => @target_project, :role => role)
177 178 u.reload
178 179 end
179 180 u
180 181 end
181 182
182 183 # Basic wiki syntax conversion
183 184 def self.convert_wiki_text(text)
184 185 # Titles
185 186 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
186 187 # External Links
187 188 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
188 189 # Internal Links
189 190 text = text.gsub(/[[BR]]/, "\n") # This has to go before the rules below
190 191 text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
191 192 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
192 193 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
193 194 text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
194 195 # Revisions links
195 196 text = text.gsub(/\[(\d+)\]/, 'r\1')
196 197 # Ticket number re-writing
197 198 text = text.gsub(/#(\d+)/) do |s|
198 199 TICKET_MAP[$1.to_i] ||= $1
199 200 "\##{TICKET_MAP[$1.to_i]}"
200 201 end
201 202 # Preformatted blocks
202 203 text = text.gsub(/\{\{\{/, '<pre>')
203 204 text = text.gsub(/\}\}\}/, '</pre>')
204 205 # Highlighting
205 206 text = text.gsub(/'''''([^\s])/, '_*\1')
206 207 text = text.gsub(/([^\s])'''''/, '\1*_')
207 208 text = text.gsub(/'''/, '*')
208 209 text = text.gsub(/''/, '_')
209 210 text = text.gsub(/__/, '+')
210 211 text = text.gsub(/~~/, '-')
211 212 text = text.gsub(/`/, '@')
212 213 text = text.gsub(/,,/, '~')
213 214 # Lists
214 215 text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
215 216
216 217
217 218 text
218 219 end
219 220
220 221 def self.migrate
221 222 establish_connection({:adapter => trac_adapter,
222 223 :database => trac_db_path})
223 224
224 225 # Quick database test
225 226 TracComponent.count
226 227
227 228 migrated_components = 0
228 229 migrated_milestones = 0
229 230 migrated_tickets = 0
230 231 migrated_custom_values = 0
231 232 migrated_ticket_attachments = 0
232 233 migrated_wiki_edits = 0
233 234
234 235 # Components
235 236 print "Migrating components"
236 237 issues_category_map = {}
237 238 TracComponent.find(:all).each do |component|
238 239 print '.'
239 240 STDOUT.flush
240 241 c = IssueCategory.new :project => @target_project,
241 242 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
242 243 next unless c.save
243 244 issues_category_map[component.name] = c
244 245 migrated_components += 1
245 246 end
246 247 puts
247 248
248 249 # Milestones
249 250 print "Migrating milestones"
250 251 version_map = {}
251 252 TracMilestone.find(:all).each do |milestone|
252 253 print '.'
253 254 STDOUT.flush
254 255 v = Version.new :project => @target_project,
255 256 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
256 257 :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]),
257 258 :effective_date => milestone.due
258 259 next unless v.save
259 260 version_map[milestone.name] = v
260 261 migrated_milestones += 1
261 262 end
262 263 puts
263 264
264 265 # Custom fields
265 266 # TODO: read trac.ini instead
266 267 print "Migrating custom fields"
267 268 custom_field_map = {}
268 269 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
269 270 print '.'
270 271 STDOUT.flush
271 272 # Redmine custom field name
272 273 field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
273 274 # Find if the custom already exists in Redmine
274 275 f = IssueCustomField.find_by_name(field_name)
275 276 # Or create a new one
276 277 f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
277 278 :field_format => 'string')
278 279
279 280 next if f.new_record?
280 281 f.trackers = Tracker.find(:all)
281 282 f.projects << @target_project
282 283 custom_field_map[field.name] = f
283 284 end
284 285 puts
285 286
286 287 # Trac 'resolution' field as a Redmine custom field
287 288 r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
288 289 r = IssueCustomField.new(:name => 'Resolution',
289 290 :field_format => 'list',
290 291 :is_filter => true) if r.nil?
291 292 r.trackers = Tracker.find(:all)
292 293 r.projects << @target_project
293 294 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
294 295 custom_field_map['resolution'] = r if r.save
295 296
296 297 # Tickets
297 298 print "Migrating tickets"
298 299 TracTicket.find(:all, :order => 'id ASC').each do |ticket|
299 300 print '.'
300 301 STDOUT.flush
301 302 i = Issue.new :project => @target_project,
302 303 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
303 304 :description => convert_wiki_text(encode(ticket.description)),
304 305 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
305 306 :created_on => ticket.time
306 307 i.author = find_or_create_user(ticket.reporter)
307 308 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
308 309 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
309 310 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
310 311 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
311 312 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
312 313 i.id = ticket.id unless Issue.exists?(ticket.id)
313 314 next unless i.save
314 315 TICKET_MAP[ticket.id] = i.id
315 316 migrated_tickets += 1
316 317
317 318 # Owner
318 319 unless ticket.owner.blank?
319 320 i.assigned_to = find_or_create_user(ticket.owner, true)
320 321 i.save
321 322 end
322 323
323 324 # Comments and status/resolution changes
324 325 ticket.changes.group_by(&:time).each do |time, changeset|
325 326 status_change = changeset.select {|change| change.field == 'status'}.first
326 327 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
327 328 comment_change = changeset.select {|change| change.field == 'comment'}.first
328 329
329 330 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
330 331 :created_on => time
331 332 n.user = find_or_create_user(changeset.first.author)
332 333 n.journalized = i
333 334 if status_change &&
334 335 STATUS_MAPPING[status_change.oldvalue] &&
335 336 STATUS_MAPPING[status_change.newvalue] &&
336 337 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
337 338 n.details << JournalDetail.new(:property => 'attr',
338 339 :prop_key => 'status_id',
339 340 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
340 341 :value => STATUS_MAPPING[status_change.newvalue].id)
341 342 end
342 343 if resolution_change
343 344 n.details << JournalDetail.new(:property => 'cf',
344 345 :prop_key => custom_field_map['resolution'].id,
345 346 :old_value => resolution_change.oldvalue,
346 347 :value => resolution_change.newvalue)
347 348 end
348 349 n.save unless n.details.empty? && n.notes.blank?
349 350 end
350 351
351 352 # Attachments
352 353 ticket.attachments.each do |attachment|
353 354 next unless attachment.exist?
354 355 a = Attachment.new :created_on => attachment.time
355 356 a.file = attachment
356 357 a.author = find_or_create_user(attachment.author)
357 358 a.container = i
358 359 migrated_ticket_attachments += 1 if a.save
359 360 end
360 361
361 362 # Custom fields
362 363 ticket.customs.each do |custom|
363 364 next if custom_field_map[custom.name].nil?
364 365 v = CustomValue.new :custom_field => custom_field_map[custom.name],
365 366 :value => custom.value
366 367 v.customized = i
367 368 next unless v.save
368 369 migrated_custom_values += 1
369 370 end
370 371 end
371 372 puts
372 373
373 374 # Wiki
374 375 print "Migrating wiki"
375 376 @target_project.wiki.destroy if @target_project.wiki
376 377 @target_project.reload
377 378 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
378 379 if wiki.save
379 380 TracWikiPage.find(:all, :order => 'name, version').each do |page|
380 381 print '.'
381 382 STDOUT.flush
382 383 p = wiki.find_or_new_page(page.name)
383 384 p.content = WikiContent.new(:page => p) if p.new_record?
384 385 p.content.text = page.text
385 386 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
386 387 p.content.comments = page.comment
387 388 p.new_record? ? p.save : p.content.save
388 389 migrated_wiki_edits += 1 unless p.content.new_record?
389 390 end
390 391
391 392 wiki.reload
392 393 wiki.pages.each do |page|
393 394 page.content.text = convert_wiki_text(page.content.text)
394 395 page.content.save
395 396 end
396 397 end
397 398 puts
398 399
399 400 puts
400 401 puts "Components: #{migrated_components}/#{TracComponent.count}"
401 402 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
402 403 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
403 404 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
404 405 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
405 406 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
406 407 end
407 408
408 409 def self.limit_for(klass, attribute)
409 410 klass.columns_hash[attribute.to_s].limit
410 411 end
411 412
412 413 def self.encoding(charset)
413 414 @ic = Iconv.new('UTF-8', charset)
414 415 rescue Iconv::InvalidEncoding
415 416 puts "Invalid encoding!"
416 417 return false
417 418 end
418 419
419 420 def self.set_trac_directory(path)
420 421 @trac_directory = path
421 422 raise "This directory doesn't exist!" unless File.directory?(path)
422 423 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
423 424 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
424 425 @trac_directory
425 426 rescue Exception => e
426 427 puts e
427 428 return false
428 429 end
429 430
430 431 def self.trac_directory
431 432 @trac_directory
432 433 end
433 434
434 435 def self.set_trac_adapter(adapter)
435 436 return false unless %w(sqlite sqlite3).include?(adapter)
436 437 @trac_adapter = adapter
437 438 end
438 439
439 440 def self.trac_adapter; @trac_adapter end
440 441 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
441 442 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
442 443
443 444 def self.target_project_identifier(identifier)
444 445 project = Project.find_by_identifier(identifier)
445 446 if !project
446 447 # create the target project
447 448 project = Project.new :name => identifier.humanize,
448 449 :description => identifier.humanize
449 450 project.identifier = identifier
450 451 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
451 452 # enable issues and wiki for the created project
452 453 project.enabled_module_names = ['issue_tracking', 'wiki']
453 454 project.trackers << TRACKER_BUG
454 455 project.trackers << TRACKER_FEATURE
455 456 end
456 457 @target_project = project.new_record? ? nil : project
457 458 end
458 459
459 460 def self.establish_connection(params)
460 461 constants.each do |const|
461 462 klass = const_get(const)
462 463 next unless klass.respond_to? 'establish_connection'
463 464 klass.establish_connection params
464 465 end
465 466 end
466 467
467 468 private
468 469 def self.encode(text)
469 470 @ic.iconv text
470 471 rescue
471 472 text
472 473 end
473 474 end
474 475
475 476 puts
476 477 puts "WARNING: Your Redmine install will have a new project added during this process."
477 478 print "Are you sure you want to continue ? [y/N] "
478 479 break unless STDIN.gets.match(/^y$/i)
479 480 puts
480 481
481 482 def prompt(text, options = {}, &block)
482 483 default = options[:default] || ''
483 484 while true
484 485 print "#{text} [#{default}]: "
485 486 value = STDIN.gets.chomp!
486 487 value = default if value.blank?
487 488 break if yield value
488 489 end
489 490 end
490 491
491 492 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
492 493 prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
493 494 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
494 495 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
495 496 puts
496 497
497 498 TracMigrate.migrate
498 499 end
499 500 end
@@ -1,49 +1,54
1 1 require File.dirname(__FILE__) + '/../test_helper'
2 2 require 'search_controller'
3 3
4 4 # Re-raise errors caught by the controller.
5 5 class SearchController; def rescue_action(e) raise e end; end
6 6
7 7 class SearchControllerTest < Test::Unit::TestCase
8 8 fixtures :projects, :issues
9 9
10 10 def setup
11 11 @controller = SearchController.new
12 12 @request = ActionController::TestRequest.new
13 13 @response = ActionController::TestResponse.new
14 14 User.current = nil
15 15 end
16 16
17 17 def test_search_for_projects
18 18 get :index
19 19 assert_response :success
20 20 assert_template 'index'
21 21
22 22 get :index, :q => "cook"
23 23 assert_response :success
24 24 assert_template 'index'
25 25 assert assigns(:results).include?(Project.find(1))
26 26 end
27 27
28 28 def test_search_in_project
29 29 get :index, :id => 1
30 30 assert_response :success
31 31 assert_template 'index'
32 32 assert_not_nil assigns(:project)
33 33
34 34 get :index, :id => 1, :q => "can"
35 35 assert_response :success
36 36 assert_template 'index'
37 37 end
38 38
39 39 def test_quick_jump_to_issue
40 40 # issue of a public project
41 41 get :index, :q => "3"
42 42 assert_redirected_to 'issues/show/3'
43 43
44 44 # issue of a private project
45 45 get :index, :q => "4"
46 46 assert_response :success
47 47 assert_template 'index'
48 48 end
49
50 def test_tokens_with_quotes
51 get :index, :id => 1, :q => '"good bye" hello "bye bye"'
52 assert_equal ["good bye", "hello", "bye bye"], assigns(:tokens)
53 end
49 54 end
General Comments 0
You need to be logged in to leave comments. Login now