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