##// END OF EJS Templates
Trac importer improvements (by Mat Trudel):...
Jean-Philippe Lang -
r918:3937caeb3c8f
parent child
Show More
@@ -1,462 +1,499
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'active_record'
18 require 'active_record'
19 require 'iconv'
19 require 'iconv'
20 require 'pp'
20 require 'pp'
21
21
22 namespace :redmine do
22 namespace :redmine do
23 desc 'Trac migration script'
23 desc 'Trac migration script'
24 task :migrate_from_trac => :environment do
24 task :migrate_from_trac => :environment do
25
25
26 module TracMigrate
26 module TracMigrate
27 TICKET_MAP = [];
27
28
28 DEFAULT_STATUS = IssueStatus.default
29 DEFAULT_STATUS = IssueStatus.default
29 assigned_status = IssueStatus.find_by_position(2)
30 assigned_status = IssueStatus.find_by_position(2)
30 resolved_status = IssueStatus.find_by_position(3)
31 resolved_status = IssueStatus.find_by_position(3)
31 feedback_status = IssueStatus.find_by_position(4)
32 feedback_status = IssueStatus.find_by_position(4)
32 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
33 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
34 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
34 'reopened' => feedback_status,
35 'reopened' => feedback_status,
35 'assigned' => assigned_status,
36 'assigned' => assigned_status,
36 'closed' => closed_status
37 'closed' => closed_status
37 }
38 }
38
39
39 priorities = Enumeration.get_values('IPRI')
40 priorities = Enumeration.get_values('IPRI')
40 DEFAULT_PRIORITY = priorities[0]
41 DEFAULT_PRIORITY = priorities[0]
41 PRIORITY_MAPPING = {'lowest' => priorities[0],
42 PRIORITY_MAPPING = {'lowest' => priorities[0],
42 'low' => priorities[0],
43 'low' => priorities[0],
43 'normal' => priorities[1],
44 'normal' => priorities[1],
44 'high' => priorities[2],
45 'high' => priorities[2],
45 'highest' => priorities[3]
46 'highest' => priorities[3]
46 }
47 }
47
48
48 TRACKER_BUG = Tracker.find_by_position(1)
49 TRACKER_BUG = Tracker.find_by_position(1)
49 TRACKER_FEATURE = Tracker.find_by_position(2)
50 TRACKER_FEATURE = Tracker.find_by_position(2)
50 DEFAULT_TRACKER = TRACKER_BUG
51 DEFAULT_TRACKER = TRACKER_BUG
51 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
52 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
52 'enhancement' => TRACKER_FEATURE,
53 'enhancement' => TRACKER_FEATURE,
53 'task' => TRACKER_FEATURE,
54 'task' => TRACKER_FEATURE,
54 'patch' =>TRACKER_FEATURE
55 'patch' =>TRACKER_FEATURE
55 }
56 }
56
57
57 DEFAULT_ROLE = Role.find_by_position(3)
58 DEFAULT_ROLE = Role.find_by_position(3)
58 manager_role = Role.find_by_position(1)
59 manager_role = Role.find_by_position(1)
59 developer_role = Role.find_by_position(2)
60 developer_role = Role.find_by_position(2)
60 ROLE_MAPPING = {'admin' => manager_role,
61 ROLE_MAPPING = {'admin' => manager_role,
61 'developer' => developer_role
62 'developer' => developer_role
62 }
63 }
63
64
64 class TracComponent < ActiveRecord::Base
65 class TracComponent < ActiveRecord::Base
65 set_table_name :component
66 set_table_name :component
66 end
67 end
67
68
68 class TracMilestone < ActiveRecord::Base
69 class TracMilestone < ActiveRecord::Base
69 set_table_name :milestone
70 set_table_name :milestone
70
71
71 def due
72 def due
72 if read_attribute(:due) > 0
73 if read_attribute(:due) > 0
73 Time.at(read_attribute(:due)).to_date
74 Time.at(read_attribute(:due)).to_date
74 else
75 else
75 nil
76 nil
76 end
77 end
77 end
78 end
78 end
79 end
79
80
80 class TracTicketCustom < ActiveRecord::Base
81 class TracTicketCustom < ActiveRecord::Base
81 set_table_name :ticket_custom
82 set_table_name :ticket_custom
82 end
83 end
83
84
84 class TracAttachment < ActiveRecord::Base
85 class TracAttachment < ActiveRecord::Base
85 set_table_name :attachment
86 set_table_name :attachment
86 set_inheritance_column :none
87 set_inheritance_column :none
87
88
88 def time; Time.at(read_attribute(:time)) end
89 def time; Time.at(read_attribute(:time)) end
89
90
90 def original_filename
91 def original_filename
91 filename
92 filename
92 end
93 end
93
94
94 def content_type
95 def content_type
95 Redmine::MimeType.of(filename) || ''
96 Redmine::MimeType.of(filename) || ''
96 end
97 end
97
98
98 def exist?
99 def exist?
99 File.file? trac_fullpath
100 File.file? trac_fullpath
100 end
101 end
101
102
102 def read
103 def read
103 File.open("#{trac_fullpath}", 'rb').read
104 File.open("#{trac_fullpath}", 'rb').read
104 end
105 end
105
106
106 private
107 private
107 def trac_fullpath
108 def trac_fullpath
108 attachment_type = read_attribute(:type)
109 attachment_type = read_attribute(:type)
109 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
110 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
110 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
111 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
111 end
112 end
112 end
113 end
113
114
114 class TracTicket < ActiveRecord::Base
115 class TracTicket < ActiveRecord::Base
115 set_table_name :ticket
116 set_table_name :ticket
116 set_inheritance_column :none
117 set_inheritance_column :none
117
118
118 # ticket changes: only migrate status changes and comments
119 # ticket changes: only migrate status changes and comments
119 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
120 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
120 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
121 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
121 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
122 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
122
123
123 def ticket_type
124 def ticket_type
124 read_attribute(:type)
125 read_attribute(:type)
125 end
126 end
126
127
127 def summary
128 def summary
128 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
129 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
129 end
130 end
130
131
131 def description
132 def description
132 read_attribute(:description).blank? ? summary : read_attribute(:description)
133 read_attribute(:description).blank? ? summary : read_attribute(:description)
133 end
134 end
134
135
135 def time; Time.at(read_attribute(:time)) end
136 def time; Time.at(read_attribute(:time)) end
136 end
137 end
137
138
138 class TracTicketChange < ActiveRecord::Base
139 class TracTicketChange < ActiveRecord::Base
139 set_table_name :ticket_change
140 set_table_name :ticket_change
140
141
141 def time; Time.at(read_attribute(:time)) end
142 def time; Time.at(read_attribute(:time)) end
142 end
143 end
143
144
144 class TracWikiPage < ActiveRecord::Base
145 class TracWikiPage < ActiveRecord::Base
145 set_table_name :wiki
146 set_table_name :wiki
146 end
147 end
147
148
148 class TracPermission < ActiveRecord::Base
149 class TracPermission < ActiveRecord::Base
149 set_table_name :permission
150 set_table_name :permission
150 end
151 end
151
152
152 def self.find_or_create_user(username, project_member = false)
153 def self.find_or_create_user(username, project_member = false)
153 u = User.find_by_login(username)
154 u = User.find_by_login(username)
154 if !u
155 if !u
155 # Create a new user if not found
156 # Create a new user if not found
156 mail = username[0,limit_for(User, 'mail')]
157 mail = username[0,limit_for(User, 'mail')]
157 mail = "#{mail}@foo.bar" unless mail.include?("@")
158 mail = "#{mail}@foo.bar" unless mail.include?("@")
158 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
159 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
159 :lastname => '-',
160 :lastname => '-',
160 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
161 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
161 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
162 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
162 u.password = 'trac'
163 u.password = 'trac'
163 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
164 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
164 # finally, a default user is used if the new user is not valid
165 # finally, a default user is used if the new user is not valid
165 u = User.find(:first) unless u.save
166 u = User.find(:first) unless u.save
166 end
167 end
167 # Make sure he is a member of the project
168 # Make sure he is a member of the project
168 if project_member && !u.member_of?(@target_project)
169 if project_member && !u.member_of?(@target_project)
169 role = DEFAULT_ROLE
170 role = DEFAULT_ROLE
170 if u.admin
171 if u.admin
171 role = ROLE_MAPPING['admin']
172 role = ROLE_MAPPING['admin']
172 elsif TracPermission.find_by_username_and_action(username, 'developer')
173 elsif TracPermission.find_by_username_and_action(username, 'developer')
173 role = ROLE_MAPPING['developer']
174 role = ROLE_MAPPING['developer']
174 end
175 end
175 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
176 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
176 u.reload
177 u.reload
177 end
178 end
178 u
179 u
179 end
180 end
180
181
181 # Basic wiki syntax conversion
182 # Basic wiki syntax conversion
182 def self.convert_wiki_text(text)
183 def self.convert_wiki_text(text)
183 # Titles
184 # Titles
184 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"}
185 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
185 # Links
186 # External Links
186 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
187 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
188 # Internal Links
189 text = text.gsub(/[[BR]]/, "\n") # This has to go before the rules below
190 text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
191 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
192 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
193 text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
187 # Revisions links
194 # Revisions links
188 text = text.gsub(/\[(\d+)\]/, 'r\1')
195 text = text.gsub(/\[(\d+)\]/, 'r\1')
196 # Ticket number re-writing
197 text = text.gsub(/#(\d+)/) do |s|
198 TICKET_MAP[$1.to_i] ||= $1
199 "\##{TICKET_MAP[$1.to_i]}"
200 end
201 # Preformatted blocks
202 text = text.gsub(/\{\{\{/, '<pre>')
203 text = text.gsub(/\}\}\}/, '</pre>')
204 # Highlighting
205 text = text.gsub(/'''''([^\s])/, '_*\1')
206 text = text.gsub(/([^\s])'''''/, '\1*_')
207 text = text.gsub(/'''/, '*')
208 text = text.gsub(/''/, '_')
209 text = text.gsub(/__/, '+')
210 text = text.gsub(/~~/, '-')
211 text = text.gsub(/`/, '@')
212 text = text.gsub(/,,/, '~')
213 # Lists
214 text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
215
216
189 text
217 text
190 end
218 end
191
219
192 def self.migrate
220 def self.migrate
193 establish_connection({:adapter => trac_adapter,
221 establish_connection({:adapter => trac_adapter,
194 :database => trac_db_path})
222 :database => trac_db_path})
195
223
196 # Quick database test before clearing Redmine data
224 # Quick database test
197 TracComponent.count
225 TracComponent.count
198
226
199 puts "Deleting data"
200 CustomField.destroy_all
201 Issue.destroy_all
202 IssueCategory.destroy_all
203 Version.destroy_all
204 User.destroy_all "login <> 'admin'"
205
206 migrated_components = 0
227 migrated_components = 0
207 migrated_milestones = 0
228 migrated_milestones = 0
208 migrated_tickets = 0
229 migrated_tickets = 0
209 migrated_custom_values = 0
230 migrated_custom_values = 0
210 migrated_ticket_attachments = 0
231 migrated_ticket_attachments = 0
211 migrated_wiki_edits = 0
232 migrated_wiki_edits = 0
212
233
213 # Components
234 # Components
214 print "Migrating components"
235 print "Migrating components"
215 issues_category_map = {}
236 issues_category_map = {}
216 TracComponent.find(:all).each do |component|
237 TracComponent.find(:all).each do |component|
217 print '.'
238 print '.'
239 STDOUT.flush
218 c = IssueCategory.new :project => @target_project,
240 c = IssueCategory.new :project => @target_project,
219 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
241 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
220 next unless c.save
242 next unless c.save
221 issues_category_map[component.name] = c
243 issues_category_map[component.name] = c
222 migrated_components += 1
244 migrated_components += 1
223 end
245 end
224 puts
246 puts
225
247
226 # Milestones
248 # Milestones
227 print "Migrating milestones"
249 print "Migrating milestones"
228 version_map = {}
250 version_map = {}
229 TracMilestone.find(:all).each do |milestone|
251 TracMilestone.find(:all).each do |milestone|
230 print '.'
252 print '.'
253 STDOUT.flush
231 v = Version.new :project => @target_project,
254 v = Version.new :project => @target_project,
232 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
255 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
233 :description => encode(milestone.description[0, limit_for(Version, 'description')]),
256 :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]),
234 :effective_date => milestone.due
257 :effective_date => milestone.due
235 next unless v.save
258 next unless v.save
236 version_map[milestone.name] = v
259 version_map[milestone.name] = v
237 migrated_milestones += 1
260 migrated_milestones += 1
238 end
261 end
239 puts
262 puts
240
263
241 # Custom fields
264 # Custom fields
242 # TODO: read trac.ini instead
265 # TODO: read trac.ini instead
243 print "Migrating custom fields"
266 print "Migrating custom fields"
244 custom_field_map = {}
267 custom_field_map = {}
245 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
268 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
246 print '.'
269 print '.'
247 f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
270 STDOUT.flush
248 :field_format => 'string'
271 # Redmine custom field name
249 next unless f.save
272 field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
273 # Find if the custom already exists in Redmine
274 f = IssueCustomField.find_by_name(field_name)
275 # Or create a new one
276 f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
277 :field_format => 'string')
278
279 next if f.new_record?
250 f.trackers = Tracker.find(:all)
280 f.trackers = Tracker.find(:all)
251 f.projects << @target_project
281 f.projects << @target_project
252 custom_field_map[field.name] = f
282 custom_field_map[field.name] = f
253 end
283 end
254 puts
284 puts
255
285
256 # Trac 'resolution' field as a Redmine custom field
286 # Trac 'resolution' field as a Redmine custom field
257 r = IssueCustomField.new :name => 'Resolution',
287 r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
288 r = IssueCustomField.new(:name => 'Resolution',
258 :field_format => 'list',
289 :field_format => 'list',
259 :is_filter => true
290 :is_filter => true) if r.nil?
260 r.trackers = Tracker.find(:all)
291 r.trackers = Tracker.find(:all)
261 r.projects << @target_project
292 r.projects << @target_project
262 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
293 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
263 custom_field_map['resolution'] = r if r.save
294 custom_field_map['resolution'] = r if r.save
264
295
265 # Tickets
296 # Tickets
266 print "Migrating tickets"
297 print "Migrating tickets"
267 TracTicket.find(:all).each do |ticket|
298 TracTicket.find(:all, :order => 'id ASC').each do |ticket|
268 print '.'
299 print '.'
300 STDOUT.flush
269 i = Issue.new :project => @target_project,
301 i = Issue.new :project => @target_project,
270 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
302 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
271 :description => convert_wiki_text(encode(ticket.description)),
303 :description => convert_wiki_text(encode(ticket.description)),
272 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
304 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
273 :created_on => ticket.time
305 :created_on => ticket.time
274 i.author = find_or_create_user(ticket.reporter)
306 i.author = find_or_create_user(ticket.reporter)
275 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
307 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
276 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
308 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
277 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
309 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
278 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
310 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
279 i.id = ticket.id
280 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
311 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
312 i.id = ticket.id unless Issue.exists?(ticket.id)
281 next unless i.save
313 next unless i.save
314 TICKET_MAP[ticket.id] = i.id
282 migrated_tickets += 1
315 migrated_tickets += 1
283
316
284 # Owner
317 # Owner
285 unless ticket.owner.blank?
318 unless ticket.owner.blank?
286 i.assigned_to = find_or_create_user(ticket.owner, true)
319 i.assigned_to = find_or_create_user(ticket.owner, true)
287 i.save
320 i.save
288 end
321 end
289
322
290 # Comments and status/resolution changes
323 # Comments and status/resolution changes
291 ticket.changes.group_by(&:time).each do |time, changeset|
324 ticket.changes.group_by(&:time).each do |time, changeset|
292 status_change = changeset.select {|change| change.field == 'status'}.first
325 status_change = changeset.select {|change| change.field == 'status'}.first
293 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
326 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
294 comment_change = changeset.select {|change| change.field == 'comment'}.first
327 comment_change = changeset.select {|change| change.field == 'comment'}.first
295
328
296 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
329 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
297 :created_on => time
330 :created_on => time
298 n.user = find_or_create_user(changeset.first.author)
331 n.user = find_or_create_user(changeset.first.author)
299 n.journalized = i
332 n.journalized = i
300 if status_change &&
333 if status_change &&
301 STATUS_MAPPING[status_change.oldvalue] &&
334 STATUS_MAPPING[status_change.oldvalue] &&
302 STATUS_MAPPING[status_change.newvalue] &&
335 STATUS_MAPPING[status_change.newvalue] &&
303 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
336 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
304 n.details << JournalDetail.new(:property => 'attr',
337 n.details << JournalDetail.new(:property => 'attr',
305 :prop_key => 'status_id',
338 :prop_key => 'status_id',
306 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
339 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
307 :value => STATUS_MAPPING[status_change.newvalue].id)
340 :value => STATUS_MAPPING[status_change.newvalue].id)
308 end
341 end
309 if resolution_change
342 if resolution_change
310 n.details << JournalDetail.new(:property => 'cf',
343 n.details << JournalDetail.new(:property => 'cf',
311 :prop_key => custom_field_map['resolution'].id,
344 :prop_key => custom_field_map['resolution'].id,
312 :old_value => resolution_change.oldvalue,
345 :old_value => resolution_change.oldvalue,
313 :value => resolution_change.newvalue)
346 :value => resolution_change.newvalue)
314 end
347 end
315 n.save unless n.details.empty? && n.notes.blank?
348 n.save unless n.details.empty? && n.notes.blank?
316 end
349 end
317
350
318 # Attachments
351 # Attachments
319 ticket.attachments.each do |attachment|
352 ticket.attachments.each do |attachment|
320 next unless attachment.exist?
353 next unless attachment.exist?
321 a = Attachment.new :created_on => attachment.time
354 a = Attachment.new :created_on => attachment.time
322 a.file = attachment
355 a.file = attachment
323 a.author = find_or_create_user(attachment.author)
356 a.author = find_or_create_user(attachment.author)
324 a.container = i
357 a.container = i
325 migrated_ticket_attachments += 1 if a.save
358 migrated_ticket_attachments += 1 if a.save
326 end
359 end
327
360
328 # Custom fields
361 # Custom fields
329 ticket.customs.each do |custom|
362 ticket.customs.each do |custom|
363 next if custom_field_map[custom.name].nil?
330 v = CustomValue.new :custom_field => custom_field_map[custom.name],
364 v = CustomValue.new :custom_field => custom_field_map[custom.name],
331 :value => custom.value
365 :value => custom.value
332 v.customized = i
366 v.customized = i
333 next unless v.save
367 next unless v.save
334 migrated_custom_values += 1
368 migrated_custom_values += 1
335 end
369 end
336 end
370 end
337 puts
371 puts
338
372
339 # Wiki
373 # Wiki
340 print "Migrating wiki"
374 print "Migrating wiki"
341 @target_project.wiki.destroy if @target_project.wiki
375 @target_project.wiki.destroy if @target_project.wiki
342 @target_project.reload
376 @target_project.reload
343 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
377 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
344 if wiki.save
378 if wiki.save
345 TracWikiPage.find(:all, :order => 'name, version').each do |page|
379 TracWikiPage.find(:all, :order => 'name, version').each do |page|
346 print '.'
380 print '.'
381 STDOUT.flush
347 p = wiki.find_or_new_page(page.name)
382 p = wiki.find_or_new_page(page.name)
348 p.content = WikiContent.new(:page => p) if p.new_record?
383 p.content = WikiContent.new(:page => p) if p.new_record?
349 p.content.text = page.text
384 p.content.text = page.text
350 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
385 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
351 p.content.comments = page.comment
386 p.content.comments = page.comment
352 p.new_record? ? p.save : p.content.save
387 p.new_record? ? p.save : p.content.save
353 migrated_wiki_edits += 1 unless p.content.new_record?
388 migrated_wiki_edits += 1 unless p.content.new_record?
354 end
389 end
355
390
356 wiki.reload
391 wiki.reload
357 wiki.pages.each do |page|
392 wiki.pages.each do |page|
358 page.content.text = convert_wiki_text(page.content.text)
393 page.content.text = convert_wiki_text(page.content.text)
359 page.content.save
394 page.content.save
360 end
395 end
361 end
396 end
362 puts
397 puts
363
398
364 puts
399 puts
365 puts "Components: #{migrated_components}/#{TracComponent.count}"
400 puts "Components: #{migrated_components}/#{TracComponent.count}"
366 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
401 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
367 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
402 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
368 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
403 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
369 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
404 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
370 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
405 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
371 end
406 end
372
407
373 def self.limit_for(klass, attribute)
408 def self.limit_for(klass, attribute)
374 klass.columns_hash[attribute.to_s].limit
409 klass.columns_hash[attribute.to_s].limit
375 end
410 end
376
411
377 def self.encoding(charset)
412 def self.encoding(charset)
378 @ic = Iconv.new('UTF-8', charset)
413 @ic = Iconv.new('UTF-8', charset)
379 rescue Iconv::InvalidEncoding
414 rescue Iconv::InvalidEncoding
380 puts "Invalid encoding!"
415 puts "Invalid encoding!"
381 return false
416 return false
382 end
417 end
383
418
384 def self.set_trac_directory(path)
419 def self.set_trac_directory(path)
385 @trac_directory = path
420 @trac_directory = path
386 raise "This directory doesn't exist!" unless File.directory?(path)
421 raise "This directory doesn't exist!" unless File.directory?(path)
387 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
422 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
388 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
423 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
389 @trac_directory
424 @trac_directory
390 rescue Exception => e
425 rescue Exception => e
391 puts e
426 puts e
392 return false
427 return false
393 end
428 end
394
429
395 def self.trac_directory
430 def self.trac_directory
396 @trac_directory
431 @trac_directory
397 end
432 end
398
433
399 def self.set_trac_adapter(adapter)
434 def self.set_trac_adapter(adapter)
400 return false unless %w(sqlite sqlite3).include?(adapter)
435 return false unless %w(sqlite sqlite3).include?(adapter)
401 @trac_adapter = adapter
436 @trac_adapter = adapter
402 end
437 end
403
438
404 def self.trac_adapter; @trac_adapter end
439 def self.trac_adapter; @trac_adapter end
405 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
440 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
406 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
441 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
407
442
408 def self.target_project_identifier(identifier)
443 def self.target_project_identifier(identifier)
409 project = Project.find_by_identifier(identifier)
444 project = Project.find_by_identifier(identifier)
410 if !project
445 if !project
411 # create the target project
446 # create the target project
412 project = Project.new :name => identifier.humanize,
447 project = Project.new :name => identifier.humanize,
413 :description => identifier.humanize
448 :description => identifier.humanize
414 project.identifier = identifier
449 project.identifier = identifier
415 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
450 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
416 # enable issues and wiki for the created project
451 # enable issues and wiki for the created project
417 project.enabled_module_names = ['issue_tracking', 'wiki']
452 project.enabled_module_names = ['issue_tracking', 'wiki']
453 project.trackers << TRACKER_BUG
454 project.trackers << TRACKER_FEATURE
418 end
455 end
419 @target_project = project.new_record? ? nil : project
456 @target_project = project.new_record? ? nil : project
420 end
457 end
421
458
422 def self.establish_connection(params)
459 def self.establish_connection(params)
423 constants.each do |const|
460 constants.each do |const|
424 klass = const_get(const)
461 klass = const_get(const)
425 next unless klass.respond_to? 'establish_connection'
462 next unless klass.respond_to? 'establish_connection'
426 klass.establish_connection params
463 klass.establish_connection params
427 end
464 end
428 end
465 end
429
466
430 private
467 private
431 def self.encode(text)
468 def self.encode(text)
432 @ic.iconv text
469 @ic.iconv text
433 rescue
470 rescue
434 text
471 text
435 end
472 end
436 end
473 end
437
474
438 puts
475 puts
439 puts "WARNING: Your Redmine data will be deleted during this process."
476 puts "WARNING: Your Redmine install will have a new project added during this process."
440 print "Are you sure you want to continue ? [y/N] "
477 print "Are you sure you want to continue ? [y/N] "
441 break unless STDIN.gets.match(/^y$/i)
478 break unless STDIN.gets.match(/^y$/i)
442 puts
479 puts
443
480
444 def prompt(text, options = {}, &block)
481 def prompt(text, options = {}, &block)
445 default = options[:default] || ''
482 default = options[:default] || ''
446 while true
483 while true
447 print "#{text} [#{default}]: "
484 print "#{text} [#{default}]: "
448 value = STDIN.gets.chomp!
485 value = STDIN.gets.chomp!
449 value = default if value.blank?
486 value = default if value.blank?
450 break if yield value
487 break if yield value
451 end
488 end
452 end
489 end
453
490
454 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
491 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
455 prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
492 prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
456 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
493 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
457 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
494 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
458 puts
495 puts
459
496
460 TracMigrate.migrate
497 TracMigrate.migrate
461 end
498 end
462 end
499 end
General Comments 0
You need to be logged in to leave comments. Login now