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