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