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