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