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