##// END OF EJS Templates
Trac importer now migrates status changes....
Jean-Philippe Lang -
r682:72b5cd388977
parent child
Show More
@@ -1,428 +1,435
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
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 = {'new' => DEFAULT_STATUS,
34 34 'reopened' => feedback_status,
35 35 'assigned' => assigned_status,
36 36 'closed' => closed_status
37 37 }
38 38
39 39 priorities = Enumeration.get_values('IPRI')
40 40 DEFAULT_PRIORITY = priorities[0]
41 41 PRIORITY_MAPPING = {'lowest' => priorities[0],
42 42 'low' => priorities[0],
43 43 'normal' => priorities[1],
44 44 'high' => priorities[2],
45 45 'highest' => priorities[3]
46 46 }
47 47
48 48 TRACKER_BUG = Tracker.find_by_position(1)
49 49 TRACKER_FEATURE = Tracker.find_by_position(2)
50 50 DEFAULT_TRACKER = TRACKER_BUG
51 51 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
52 52 'enhancement' => TRACKER_FEATURE,
53 53 'task' => TRACKER_FEATURE,
54 54 'patch' =>TRACKER_FEATURE
55 55 }
56 56
57 57 DEFAULT_ROLE = Role.find_by_position(3)
58 58 manager_role = Role.find_by_position(1)
59 59 developer_role = Role.find_by_position(2)
60 60 ROLE_MAPPING = {'admin' => manager_role,
61 61 'developer' => developer_role
62 62 }
63 63
64 64 class TracComponent < ActiveRecord::Base
65 65 set_table_name :component
66 66 end
67 67
68 68 class TracMilestone < ActiveRecord::Base
69 69 set_table_name :milestone
70 70
71 71 def due
72 72 if read_attribute(:due) > 0
73 73 Time.at(read_attribute(:due)).to_date
74 74 else
75 75 nil
76 76 end
77 77 end
78 78 end
79 79
80 80 class TracTicketCustom < ActiveRecord::Base
81 81 set_table_name :ticket_custom
82 82 end
83 83
84 84 class TracTicket < ActiveRecord::Base
85 85 set_table_name :ticket
86 86 set_inheritance_column :none
87 87
88 has_many :comments, :class_name => "TracTicketChange", :foreign_key => :ticket, :conditions => "field = 'comment'"
88 # ticket changes: only migrate status changes and comments
89 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket, :conditions => "field = 'comment' OR field='status'"
89 90 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "type = 'ticket'"
90 91 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
91 92
92 93 def ticket_type
93 94 read_attribute(:type)
94 95 end
95 96
96 97 def summary
97 98 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
98 99 end
99 100
100 101 def description
101 102 read_attribute(:description).blank? ? summary : read_attribute(:description)
102 103 end
103 104
104 105 def time; Time.at(read_attribute(:time)) end
105 106 end
106 107
107 108 class TracTicketChange < ActiveRecord::Base
108 109 set_table_name :ticket_change
109 110
110 111 def time; Time.at(read_attribute(:time)) end
111 112 end
112 113
113 114 class TracAttachment < ActiveRecord::Base
114 115 set_table_name :attachment
115 116 set_inheritance_column :none
116 117
117 118 def time; Time.at(read_attribute(:time)) end
118 119
119 120 def original_filename
120 121 filename
121 122 end
122 123
123 124 def content_type
124 125 Redmine::MimeType.of(filename) || ''
125 126 end
126 127
127 128 def exist?
128 129 File.file? trac_fullpath
129 130 end
130 131
131 132 def read
132 133 File.open("#{trac_fullpath}", 'rb').read
133 134 end
134 135
135 136 private
136 137 def trac_fullpath
137 138 attachment_type = read_attribute(:type)
138 139 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
139 140 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
140 141 end
141 142 end
142 143
143 144 class TracWikiPage < ActiveRecord::Base
144 145 set_table_name :wiki
145 146 end
146 147
147 148 class TracPermission < ActiveRecord::Base
148 149 set_table_name :permission
149 150 end
150 151
151 152 def self.find_or_create_user(username, project_member = false)
152 153 u = User.find_by_login(username)
153 154 if !u
154 155 # Create a new user if not found
155 156 mail = username[0,limit_for(User, 'mail')]
156 157 mail = "#{mail}@foo.bar" unless mail.include?("@")
157 158 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
158 159 :lastname => '-',
159 160 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
160 161 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
161 162 u.password = 'trac'
162 163 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
163 164 # finally, a default user is used if the new user is not valid
164 165 u = User.find(:first) unless u.save
165 166 end
166 167 # Make sure he is a member of the project
167 168 if project_member && !u.member_of?(@target_project)
168 169 role = DEFAULT_ROLE
169 170 if u.admin
170 171 role = ROLE_MAPPING['admin']
171 172 elsif TracPermission.find_by_username_and_action(username, 'developer')
172 173 role = ROLE_MAPPING['developer']
173 174 end
174 175 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
175 176 u.reload
176 177 end
177 178 u
178 179 end
179 180
180 181 # Basic wiki syntax conversion
181 182 def self.convert_wiki_text(text)
182 183 # Titles
183 184 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"}
184 185 # Links
185 186 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
186 187 # Revisions links
187 188 text = text.gsub(/\[(\d+)\]/, 'r\1')
188 189 text
189 190 end
190 191
191 192 def self.migrate
192 193 # Quick database test before clearing Redmine data
193 194 TracComponent.count
194 195
195 196 puts "Deleting data"
196 197 CustomField.destroy_all
197 198 Issue.destroy_all
198 199 IssueCategory.destroy_all
199 200 Version.destroy_all
200 201 User.destroy_all "login <> 'admin'"
201 202
202 203 migrated_components = 0
203 204 migrated_milestones = 0
204 205 migrated_tickets = 0
205 migrated_ticket_comments = 0
206 206 migrated_custom_values = 0
207 trac_ticket_comments = 0
208 207 migrated_ticket_attachments = 0
209 208 migrated_wiki_edits = 0
210 209
211 210 # Components
212 211 print "Migrating components"
213 212 issues_category_map = {}
214 213 TracComponent.find(:all).each do |component|
215 214 print '.'
216 215 c = IssueCategory.new :project => @target_project,
217 216 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
218 217 next unless c.save
219 218 issues_category_map[component.name] = c
220 219 migrated_components += 1
221 220 end
222 221 puts
223 222
224 223 # Milestones
225 224 print "Migrating milestones"
226 225 version_map = {}
227 226 TracMilestone.find(:all).each do |milestone|
228 227 print '.'
229 228 v = Version.new :project => @target_project,
230 229 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
231 230 :description => encode(milestone.description[0, limit_for(Version, 'description')]),
232 231 :effective_date => milestone.due
233 232 next unless v.save
234 233 version_map[milestone.name] = v
235 234 migrated_milestones += 1
236 235 end
237 236 puts
238 237
239 238 # Custom fields
240 239 # TODO: read trac.ini instead
241 print "Custom fields"
240 print "Migrating custom fields"
242 241 custom_field_map = {}
243 242 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
244 243 print '.'
245 f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]),
246 :field_format => 'string',
247 :is_for_all => true
244 f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
245 :field_format => 'string'
248 246 next unless f.save
249 247 f.trackers = Tracker.find(:all)
248 f.projects << @target_project
250 249 custom_field_map[field.name] = f
251 250 end
252 251 puts
253 252
254 253 # Tickets
255 254 print "Migrating tickets"
256 255 TracTicket.find(:all).each do |ticket|
257 256 print '.'
258 257 i = Issue.new :project => @target_project,
259 258 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
260 259 :description => convert_wiki_text(encode(ticket.description)),
261 260 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
262 261 :created_on => ticket.time
263 262 i.author = find_or_create_user(ticket.reporter)
264 263 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
265 264 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
266 265 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
267 266 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
268 267 i.id = ticket.id
269 268 next unless i.save
270 269 migrated_tickets += 1
271 270
272 271 # Owner
273 unless ticket.owner.blank?
274 i.assigned_to = find_or_create_user(ticket.owner, true)
275 i.save
276 end
272 unless ticket.owner.blank?
273 i.assigned_to = find_or_create_user(ticket.owner, true)
274 i.save
275 end
277 276
278 # Comments
279 # TODO: migrate status changes history
280 ticket.comments.each do |comment|
281 next if comment.newvalue.blank?
282 trac_ticket_comments += 1
283 n = Journal.new :notes => convert_wiki_text(encode(comment.newvalue)),
284 :created_on => comment.time
285 n.user = find_or_create_user(comment.author)
277 # Comments and status changes
278 ticket.changes.group_by(&:time).each do |time, changeset|
279 status_change = changeset.select {|change| change.field == 'status'}.first
280 comment_change = changeset.select {|change| change.field == 'comment'}.first
281
282 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
283 :created_on => time
284 n.user = find_or_create_user(changeset.first.author)
286 285 n.journalized = i
287 migrated_ticket_comments += 1 if n.save
286 if status_change &&
287 STATUS_MAPPING[status_change.oldvalue] &&
288 STATUS_MAPPING[status_change.newvalue] &&
289 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
290 n.details << JournalDetail.new(:property => 'attr',
291 :prop_key => 'status_id',
292 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
293 :value => STATUS_MAPPING[status_change.newvalue].id)
294 end
295 n.save
288 296 end
289 297
290 298 # Attachments
291 299 ticket.attachments.each do |attachment|
292 300 next unless attachment.exist?
293 301 a = Attachment.new :created_on => attachment.time
294 302 a.file = attachment
295 303 a.author = find_or_create_user(attachment.author)
296 304 a.container = i
297 305 migrated_ticket_attachments += 1 if a.save
298 306 end
299 307
300 308 # Custom fields
301 309 ticket.customs.each do |custom|
302 310 v = CustomValue.new :custom_field => custom_field_map[custom.name],
303 311 :value => custom.value
304 312 v.customized = i
305 313 next unless v.save
306 314 migrated_custom_values += 1
307 315 end
308 316 end
309 317 puts
310 318
311 319 # Wiki
312 320 print "Migrating wiki"
313 321 @target_project.wiki.destroy if @target_project.wiki
314 322 @target_project.reload
315 323 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
316 324 if wiki.save
317 325 TracWikiPage.find(:all, :order => 'name, version').each do |page|
318 326 print '.'
319 327 p = wiki.find_or_new_page(page.name)
320 328 p.content = WikiContent.new(:page => p) if p.new_record?
321 329 p.content.text = page.text
322 330 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
323 331 p.content.comments = page.comment
324 332 p.new_record? ? p.save : p.content.save
325 333 migrated_wiki_edits += 1 unless p.content.new_record?
326 334 end
327 335
328 336 wiki.reload
329 337 wiki.pages.each do |page|
330 338 page.content.text = convert_wiki_text(page.content.text)
331 339 page.content.save
332 340 end
333 341 end
334 342 puts
335 343
336 344 puts
337 345 puts "Components: #{migrated_components}/#{TracComponent.count}"
338 346 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
339 347 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
340 puts "Ticket comments: #{migrated_ticket_comments}/#{trac_ticket_comments}"
341 348 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
342 349 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
343 350 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
344 351 end
345 352
346 353 def self.limit_for(klass, attribute)
347 354 klass.columns_hash[attribute.to_s].limit
348 355 end
349 356
350 357 def self.encoding(charset)
351 358 @ic = Iconv.new('UTF-8', charset)
352 359 rescue Iconv::InvalidEncoding
353 360 puts "Invalid encoding!"
354 361 return false
355 362 end
356 363
357 364 def self.set_trac_directory(path)
358 365 @trac_directory = path
359 366 raise "This directory doesn't exist!" unless File.directory?(path)
360 367 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
361 368 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
362 369 @trac_directory
363 370 rescue Exception => e
364 371 puts e
365 372 return false
366 373 end
367 374
368 375 def self.trac_directory
369 376 @trac_directory
370 377 end
371 378
372 379 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
373 380 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
374 381
375 382 def self.target_project_identifier(identifier)
376 383 project = Project.find_by_identifier(identifier)
377 384 if !project
378 385 # create the target project
379 386 project = Project.new :name => identifier.humanize,
380 387 :description => identifier.humanize
381 388 project.identifier = identifier
382 389 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
383 390 end
384 391 @target_project = project.new_record? ? nil : project
385 392 end
386 393
387 394 def self.establish_connection(params)
388 395 constants.each do |const|
389 396 klass = const_get(const)
390 397 next unless klass.respond_to? 'establish_connection'
391 398 klass.establish_connection params
392 399 end
393 400 end
394 401
395 402 private
396 403 def self.encode(text)
397 404 @ic.iconv text
398 405 rescue
399 406 text
400 407 end
401 408 end
402 409
403 410 puts
404 411 puts "WARNING: Your Redmine data will be deleted during this process."
405 412 print "Are you sure you want to continue ? [y/N] "
406 413 break unless STDIN.gets.match(/^y$/i)
407 414 puts
408 415
409 416 def prompt(text, options = {}, &block)
410 417 default = options[:default] || ''
411 418 while true
412 419 print "#{text} [#{default}]: "
413 420 value = STDIN.gets.chomp!
414 421 value = default if value.blank?
415 422 break if yield value
416 423 end
417 424 end
418 425
419 426 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
420 427 prompt('Database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
421 428 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
422 429 puts
423 430
424 431 TracMigrate.establish_connection({:adapter => 'sqlite',
425 432 :database => "#{TracMigrate.trac_db_path}"})
426 433 TracMigrate.migrate
427 434 end
428 435 end
General Comments 0
You need to be logged in to leave comments. Login now