@@ -1,487 +1,491 | |||||
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 | DEFAULT_ROLE = Role.find_by_position(3) |
|
56 | roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') | |
57 |
manager_role = |
|
57 | manager_role = roles[0] | |
58 |
developer_role = |
|
58 | developer_role = roles[1] | |
|
59 | DEFAULT_ROLE = roles.last | |||
59 | ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer |
|
60 | ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer | |
60 | 25 => DEFAULT_ROLE, # reporter |
|
61 | 25 => DEFAULT_ROLE, # reporter | |
61 | 40 => DEFAULT_ROLE, # updater |
|
62 | 40 => DEFAULT_ROLE, # updater | |
62 | 55 => developer_role, # developer |
|
63 | 55 => developer_role, # developer | |
63 | 70 => manager_role, # manager |
|
64 | 70 => manager_role, # manager | |
64 | 90 => manager_role # administrator |
|
65 | 90 => manager_role # administrator | |
65 | } |
|
66 | } | |
66 |
|
67 | |||
67 | CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String |
|
68 | CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String | |
68 | 1 => 'int', # Numeric |
|
69 | 1 => 'int', # Numeric | |
69 | 2 => 'int', # Float |
|
70 | 2 => 'int', # Float | |
70 | 3 => 'list', # Enumeration |
|
71 | 3 => 'list', # Enumeration | |
71 | 4 => 'string', # Email |
|
72 | 4 => 'string', # Email | |
72 | 5 => 'bool', # Checkbox |
|
73 | 5 => 'bool', # Checkbox | |
73 | 6 => 'list', # List |
|
74 | 6 => 'list', # List | |
74 | 7 => 'list', # Multiselection list |
|
75 | 7 => 'list', # Multiselection list | |
75 | 8 => 'date', # Date |
|
76 | 8 => 'date', # Date | |
76 | } |
|
77 | } | |
77 |
|
78 | |||
78 | RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to |
|
79 | RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to | |
79 | 2 => IssueRelation::TYPE_RELATES, # parent of |
|
80 | 2 => IssueRelation::TYPE_RELATES, # parent of | |
80 | 3 => IssueRelation::TYPE_RELATES, # child of |
|
81 | 3 => IssueRelation::TYPE_RELATES, # child of | |
81 | 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of |
|
82 | 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of | |
82 | 4 => IssueRelation::TYPE_DUPLICATES # has duplicate |
|
83 | 4 => IssueRelation::TYPE_DUPLICATES # has duplicate | |
83 | } |
|
84 | } | |
84 |
|
85 | |||
85 | class MantisUser < ActiveRecord::Base |
|
86 | class MantisUser < ActiveRecord::Base | |
86 | set_table_name :mantis_user_table |
|
87 | set_table_name :mantis_user_table | |
87 |
|
88 | |||
88 | def firstname |
|
89 | def firstname | |
89 | realname.blank? ? username : realname.split.first[0..29] |
|
90 | realname.blank? ? username : realname.split.first[0..29] | |
90 | end |
|
91 | end | |
91 |
|
92 | |||
92 | def lastname |
|
93 | def lastname | |
93 | realname.blank? ? username : realname.split[1..-1].join(' ')[0..29] |
|
94 | realname.blank? ? username : realname.split[1..-1].join(' ')[0..29] | |
94 | end |
|
95 | end | |
95 |
|
96 | |||
96 | def email |
|
97 | def email | |
97 | 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) | |
98 | read_attribute(:email) |
|
99 | read_attribute(:email) | |
99 | else |
|
100 | else | |
100 | "#{username}@foo.bar" |
|
101 | "#{username}@foo.bar" | |
101 | end |
|
102 | end | |
102 | end |
|
103 | end | |
103 |
|
104 | |||
104 | def username |
|
105 | def username | |
105 | read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-') |
|
106 | read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-') | |
106 | end |
|
107 | end | |
107 | end |
|
108 | end | |
108 |
|
109 | |||
109 | class MantisProject < ActiveRecord::Base |
|
110 | class MantisProject < ActiveRecord::Base | |
110 | set_table_name :mantis_project_table |
|
111 | set_table_name :mantis_project_table | |
111 | has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id |
|
112 | has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id | |
112 | has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id |
|
113 | has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id | |
113 | has_many :news, :class_name => "MantisNews", :foreign_key => :project_id |
|
114 | has_many :news, :class_name => "MantisNews", :foreign_key => :project_id | |
114 | has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id |
|
115 | has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id | |
115 |
|
116 | |||
116 | def name |
|
117 | def name | |
117 | read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-') |
|
118 | read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-') | |
118 | end |
|
119 | end | |
119 |
|
120 | |||
120 | def description |
|
121 | def description | |
121 | read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254] |
|
122 | read_attribute(:description).blank? ? read_attribute(:name) : read_attribute(:description)[0..254] | |
122 | end |
|
123 | end | |
123 |
|
124 | |||
124 | def identifier |
|
125 | def identifier | |
125 | read_attribute(:name).underscore[0..11].gsub(/[^a-z0-9\-]/, '-') |
|
126 | read_attribute(:name).underscore[0..11].gsub(/[^a-z0-9\-]/, '-') | |
126 | end |
|
127 | end | |
127 | end |
|
128 | end | |
128 |
|
129 | |||
129 | class MantisVersion < ActiveRecord::Base |
|
130 | class MantisVersion < ActiveRecord::Base | |
130 | set_table_name :mantis_project_version_table |
|
131 | set_table_name :mantis_project_version_table | |
131 |
|
132 | |||
132 | def version |
|
133 | def version | |
133 | read_attribute(:version)[0..29] |
|
134 | read_attribute(:version)[0..29] | |
134 | end |
|
135 | end | |
135 |
|
136 | |||
136 | def description |
|
137 | def description | |
137 | read_attribute(:description)[0..254] |
|
138 | read_attribute(:description)[0..254] | |
138 | end |
|
139 | end | |
139 | end |
|
140 | end | |
140 |
|
141 | |||
141 | class MantisCategory < ActiveRecord::Base |
|
142 | class MantisCategory < ActiveRecord::Base | |
142 | set_table_name :mantis_project_category_table |
|
143 | set_table_name :mantis_project_category_table | |
143 | end |
|
144 | end | |
144 |
|
145 | |||
145 | class MantisProjectUser < ActiveRecord::Base |
|
146 | class MantisProjectUser < ActiveRecord::Base | |
146 | set_table_name :mantis_project_user_list_table |
|
147 | set_table_name :mantis_project_user_list_table | |
147 | end |
|
148 | end | |
148 |
|
149 | |||
149 | class MantisBug < ActiveRecord::Base |
|
150 | class MantisBug < ActiveRecord::Base | |
150 | set_table_name :mantis_bug_table |
|
151 | set_table_name :mantis_bug_table | |
151 | belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id |
|
152 | belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id | |
152 | has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id |
|
153 | has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id | |
153 | has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id |
|
154 | has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id | |
154 | has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id |
|
155 | has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id | |
155 | end |
|
156 | end | |
156 |
|
157 | |||
157 | class MantisBugText < ActiveRecord::Base |
|
158 | class MantisBugText < ActiveRecord::Base | |
158 | set_table_name :mantis_bug_text_table |
|
159 | set_table_name :mantis_bug_text_table | |
159 |
|
160 | |||
160 | # Adds Mantis steps_to_reproduce and additional_information fields |
|
161 | # Adds Mantis steps_to_reproduce and additional_information fields | |
161 | # to description if any |
|
162 | # to description if any | |
162 | def full_description |
|
163 | def full_description | |
163 | full_description = description |
|
164 | full_description = description | |
164 | full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank? |
|
165 | full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank? | |
165 | full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank? |
|
166 | full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank? | |
166 | full_description |
|
167 | full_description | |
167 | end |
|
168 | end | |
168 | end |
|
169 | end | |
169 |
|
170 | |||
170 | class MantisBugNote < ActiveRecord::Base |
|
171 | class MantisBugNote < ActiveRecord::Base | |
171 | set_table_name :mantis_bugnote_table |
|
172 | set_table_name :mantis_bugnote_table | |
172 | belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id |
|
173 | belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id | |
173 | belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id |
|
174 | belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id | |
174 | end |
|
175 | end | |
175 |
|
176 | |||
176 | class MantisBugNoteText < ActiveRecord::Base |
|
177 | class MantisBugNoteText < ActiveRecord::Base | |
177 | set_table_name :mantis_bugnote_text_table |
|
178 | set_table_name :mantis_bugnote_text_table | |
178 | end |
|
179 | end | |
179 |
|
180 | |||
180 | class MantisBugFile < ActiveRecord::Base |
|
181 | class MantisBugFile < ActiveRecord::Base | |
181 | set_table_name :mantis_bug_file_table |
|
182 | set_table_name :mantis_bug_file_table | |
182 |
|
183 | |||
183 | def size |
|
184 | def size | |
184 | filesize |
|
185 | filesize | |
185 | end |
|
186 | end | |
186 |
|
187 | |||
187 | def original_filename |
|
188 | def original_filename | |
188 | filename |
|
189 | filename | |
189 | end |
|
190 | end | |
190 |
|
191 | |||
191 | def content_type |
|
192 | def content_type | |
192 | file_type |
|
193 | file_type | |
193 | end |
|
194 | end | |
194 |
|
195 | |||
195 | def read |
|
196 | def read | |
196 | content |
|
197 | content | |
197 | end |
|
198 | end | |
198 | end |
|
199 | end | |
199 |
|
200 | |||
200 | class MantisBugRelationship < ActiveRecord::Base |
|
201 | class MantisBugRelationship < ActiveRecord::Base | |
201 | set_table_name :mantis_bug_relationship_table |
|
202 | set_table_name :mantis_bug_relationship_table | |
202 | end |
|
203 | end | |
203 |
|
204 | |||
204 | class MantisBugMonitor < ActiveRecord::Base |
|
205 | class MantisBugMonitor < ActiveRecord::Base | |
205 | set_table_name :mantis_bug_monitor_table |
|
206 | set_table_name :mantis_bug_monitor_table | |
206 | end |
|
207 | end | |
207 |
|
208 | |||
208 | class MantisNews < ActiveRecord::Base |
|
209 | class MantisNews < ActiveRecord::Base | |
209 | set_table_name :mantis_news_table |
|
210 | set_table_name :mantis_news_table | |
210 | end |
|
211 | end | |
211 |
|
212 | |||
212 | class MantisCustomField < ActiveRecord::Base |
|
213 | class MantisCustomField < ActiveRecord::Base | |
213 | set_table_name :mantis_custom_field_table |
|
214 | set_table_name :mantis_custom_field_table | |
214 | set_inheritance_column :none |
|
215 | set_inheritance_column :none | |
215 | has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id |
|
216 | has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id | |
216 | has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id |
|
217 | has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id | |
217 |
|
218 | |||
218 | def format |
|
219 | def format | |
219 | read_attribute :type |
|
220 | read_attribute :type | |
220 | end |
|
221 | end | |
221 |
|
222 | |||
222 | def name |
|
223 | def name | |
223 | read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-') |
|
224 | read_attribute(:name)[0..29].gsub(/[^\w\s\'\-]/, '-') | |
224 | end |
|
225 | end | |
225 | end |
|
226 | end | |
226 |
|
227 | |||
227 | class MantisCustomFieldProject < ActiveRecord::Base |
|
228 | class MantisCustomFieldProject < ActiveRecord::Base | |
228 | set_table_name :mantis_custom_field_project_table |
|
229 | set_table_name :mantis_custom_field_project_table | |
229 | end |
|
230 | end | |
230 |
|
231 | |||
231 | class MantisCustomFieldString < ActiveRecord::Base |
|
232 | class MantisCustomFieldString < ActiveRecord::Base | |
232 | set_table_name :mantis_custom_field_string_table |
|
233 | set_table_name :mantis_custom_field_string_table | |
233 | end |
|
234 | end | |
234 |
|
235 | |||
235 |
|
236 | |||
236 | def self.migrate |
|
237 | def self.migrate | |
237 |
|
238 | |||
238 | # Users |
|
239 | # Users | |
239 | print "Migrating users" |
|
240 | print "Migrating users" | |
240 | User.delete_all "login <> 'admin'" |
|
241 | User.delete_all "login <> 'admin'" | |
241 | users_map = {} |
|
242 | users_map = {} | |
242 | users_migrated = 0 |
|
243 | users_migrated = 0 | |
243 | MantisUser.find(:all).each do |user| |
|
244 | MantisUser.find(:all).each do |user| | |
244 | u = User.new :firstname => encode(user.firstname), |
|
245 | u = User.new :firstname => encode(user.firstname), | |
245 | :lastname => encode(user.lastname), |
|
246 | :lastname => encode(user.lastname), | |
246 | :mail => user.email, |
|
247 | :mail => user.email, | |
247 | :last_login_on => user.last_visit |
|
248 | :last_login_on => user.last_visit | |
248 | u.login = user.username |
|
249 | u.login = user.username | |
249 | u.password = 'mantis' |
|
250 | u.password = 'mantis' | |
250 | u.status = User::STATUS_LOCKED if user.enabled != 1 |
|
251 | u.status = User::STATUS_LOCKED if user.enabled != 1 | |
251 | u.admin = true if user.access_level == 90 |
|
252 | u.admin = true if user.access_level == 90 | |
252 | next unless u.save |
|
253 | next unless u.save | |
253 | users_migrated += 1 |
|
254 | users_migrated += 1 | |
254 | users_map[user.id] = u.id |
|
255 | users_map[user.id] = u.id | |
255 | print '.' |
|
256 | print '.' | |
256 | end |
|
257 | end | |
257 | puts |
|
258 | puts | |
258 |
|
259 | |||
259 | # Projects |
|
260 | # Projects | |
260 | print "Migrating projects" |
|
261 | print "Migrating projects" | |
261 | Project.destroy_all |
|
262 | Project.destroy_all | |
262 | projects_map = {} |
|
263 | projects_map = {} | |
263 | versions_map = {} |
|
264 | versions_map = {} | |
264 | categories_map = {} |
|
265 | categories_map = {} | |
265 | MantisProject.find(:all).each do |project| |
|
266 | MantisProject.find(:all).each do |project| | |
266 | p = Project.new :name => encode(project.name), |
|
267 | p = Project.new :name => encode(project.name), | |
267 | :description => encode(project.description) |
|
268 | :description => encode(project.description) | |
268 | p.identifier = project.identifier |
|
269 | p.identifier = project.identifier | |
269 | next unless p.save |
|
270 | next unless p.save | |
270 | projects_map[project.id] = p.id |
|
271 | projects_map[project.id] = p.id | |
|
272 | p.enabled_module_names = ['issue_tracking', 'news', 'wiki'] | |||
|
273 | p.trackers << TRACKER_BUG | |||
|
274 | p.trackers << TRACKER_FEATURE | |||
271 | print '.' |
|
275 | print '.' | |
272 |
|
276 | |||
273 | # Project members |
|
277 | # Project members | |
274 | project.members.each do |member| |
|
278 | project.members.each do |member| | |
275 | m = Member.new :user => User.find_by_id(users_map[member.user_id]), |
|
279 | m = Member.new :user => User.find_by_id(users_map[member.user_id]), | |
276 | :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE |
|
280 | :role => ROLE_MAPPING[member.access_level] || DEFAULT_ROLE | |
277 | m.project = p |
|
281 | m.project = p | |
278 | m.save |
|
282 | m.save | |
279 | end |
|
283 | end | |
280 |
|
284 | |||
281 | # Project versions |
|
285 | # Project versions | |
282 | project.versions.each do |version| |
|
286 | project.versions.each do |version| | |
283 | v = Version.new :name => encode(version.version), |
|
287 | v = Version.new :name => encode(version.version), | |
284 | :description => encode(version.description), |
|
288 | :description => encode(version.description), | |
285 | :effective_date => version.date_order.to_date |
|
289 | :effective_date => version.date_order.to_date | |
286 | v.project = p |
|
290 | v.project = p | |
287 | v.save |
|
291 | v.save | |
288 | versions_map[version.id] = v.id |
|
292 | versions_map[version.id] = v.id | |
289 | end |
|
293 | end | |
290 |
|
294 | |||
291 | # Project categories |
|
295 | # Project categories | |
292 | project.categories.each do |category| |
|
296 | project.categories.each do |category| | |
293 | g = IssueCategory.new :name => category.category[0,30] |
|
297 | g = IssueCategory.new :name => category.category[0,30] | |
294 | g.project = p |
|
298 | g.project = p | |
295 | g.save |
|
299 | g.save | |
296 | categories_map[category.category] = g.id |
|
300 | categories_map[category.category] = g.id | |
297 | end |
|
301 | end | |
298 | end |
|
302 | end | |
299 | puts |
|
303 | puts | |
300 |
|
304 | |||
301 | # Bugs |
|
305 | # Bugs | |
302 | print "Migrating bugs" |
|
306 | print "Migrating bugs" | |
303 | Issue.destroy_all |
|
307 | Issue.destroy_all | |
304 | issues_map = {} |
|
308 | issues_map = {} | |
305 | MantisBug.find(:all).each do |bug| |
|
309 | MantisBug.find(:all).each do |bug| | |
306 | next unless projects_map[bug.project_id] && users_map[bug.reporter_id] |
|
310 | next unless projects_map[bug.project_id] && users_map[bug.reporter_id] | |
307 | i = Issue.new :project_id => projects_map[bug.project_id], |
|
311 | i = Issue.new :project_id => projects_map[bug.project_id], | |
308 | :subject => encode(bug.summary), |
|
312 | :subject => encode(bug.summary), | |
309 | :description => encode(bug.bug_text.full_description), |
|
313 | :description => encode(bug.bug_text.full_description), | |
310 | :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY, |
|
314 | :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY, | |
311 | :created_on => bug.date_submitted, |
|
315 | :created_on => bug.date_submitted, | |
312 | :updated_on => bug.last_updated |
|
316 | :updated_on => bug.last_updated | |
313 | i.author = User.find_by_id(users_map[bug.reporter_id]) |
|
317 | 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? |
|
318 | 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? |
|
319 | 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 |
|
320 | i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS | |
317 | i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG) |
|
321 | i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG) | |
318 | next unless i.save |
|
322 | next unless i.save | |
319 | issues_map[bug.id] = i.id |
|
323 | issues_map[bug.id] = i.id | |
320 | print '.' |
|
324 | print '.' | |
321 |
|
325 | |||
322 | # Assignee |
|
326 | # Assignee | |
323 | # Redmine checks that the assignee is a project member |
|
327 | # Redmine checks that the assignee is a project member | |
324 | if (bug.handler_id && users_map[bug.handler_id]) |
|
328 | if (bug.handler_id && users_map[bug.handler_id]) | |
325 | i.assigned_to = User.find_by_id(users_map[bug.handler_id]) |
|
329 | i.assigned_to = User.find_by_id(users_map[bug.handler_id]) | |
326 | i.save_with_validation(false) |
|
330 | i.save_with_validation(false) | |
327 | end |
|
331 | end | |
328 |
|
332 | |||
329 | # Bug notes |
|
333 | # Bug notes | |
330 | bug.bug_notes.each do |note| |
|
334 | bug.bug_notes.each do |note| | |
331 | next unless users_map[note.reporter_id] |
|
335 | next unless users_map[note.reporter_id] | |
332 | n = Journal.new :notes => encode(note.bug_note_text.note), |
|
336 | n = Journal.new :notes => encode(note.bug_note_text.note), | |
333 | :created_on => note.date_submitted |
|
337 | :created_on => note.date_submitted | |
334 | n.user = User.find_by_id(users_map[note.reporter_id]) |
|
338 | n.user = User.find_by_id(users_map[note.reporter_id]) | |
335 | n.journalized = i |
|
339 | n.journalized = i | |
336 | n.save |
|
340 | n.save | |
337 | end |
|
341 | end | |
338 |
|
342 | |||
339 | # Bug files |
|
343 | # Bug files | |
340 | bug.bug_files.each do |file| |
|
344 | bug.bug_files.each do |file| | |
341 | a = Attachment.new :created_on => file.date_added |
|
345 | a = Attachment.new :created_on => file.date_added | |
342 | a.file = file |
|
346 | a.file = file | |
343 | a.author = User.find :first |
|
347 | a.author = User.find :first | |
344 | a.container = i |
|
348 | a.container = i | |
345 | a.save |
|
349 | a.save | |
346 | end |
|
350 | end | |
347 |
|
351 | |||
348 | # Bug monitors |
|
352 | # Bug monitors | |
349 | bug.bug_monitors.each do |monitor| |
|
353 | bug.bug_monitors.each do |monitor| | |
350 | next unless users_map[monitor.user_id] |
|
354 | next unless users_map[monitor.user_id] | |
351 | i.add_watcher(User.find_by_id(users_map[monitor.user_id])) |
|
355 | i.add_watcher(User.find_by_id(users_map[monitor.user_id])) | |
352 | end |
|
356 | end | |
353 | end |
|
357 | end | |
354 | puts |
|
358 | puts | |
355 |
|
359 | |||
356 | # Bug relationships |
|
360 | # Bug relationships | |
357 | print "Migrating bug relations" |
|
361 | print "Migrating bug relations" | |
358 | MantisBugRelationship.find(:all).each do |relation| |
|
362 | MantisBugRelationship.find(:all).each do |relation| | |
359 | next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id] |
|
363 | 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] |
|
364 | 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]) |
|
365 | 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]) |
|
366 | r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id]) | |
363 | pp r unless r.save |
|
367 | pp r unless r.save | |
364 | print '.' |
|
368 | print '.' | |
365 | end |
|
369 | end | |
366 | puts |
|
370 | puts | |
367 |
|
371 | |||
368 | # News |
|
372 | # News | |
369 | print "Migrating news" |
|
373 | print "Migrating news" | |
370 | News.destroy_all |
|
374 | News.destroy_all | |
371 | MantisNews.find(:all, :conditions => 'project_id > 0').each do |news| |
|
375 | MantisNews.find(:all, :conditions => 'project_id > 0').each do |news| | |
372 | next unless projects_map[news.project_id] |
|
376 | next unless projects_map[news.project_id] | |
373 | n = News.new :project_id => projects_map[news.project_id], |
|
377 | n = News.new :project_id => projects_map[news.project_id], | |
374 | :title => encode(news.headline[0..59]), |
|
378 | :title => encode(news.headline[0..59]), | |
375 | :description => encode(news.body), |
|
379 | :description => encode(news.body), | |
376 | :created_on => news.date_posted |
|
380 | :created_on => news.date_posted | |
377 | n.author = User.find_by_id(users_map[news.poster_id]) |
|
381 | n.author = User.find_by_id(users_map[news.poster_id]) | |
378 | n.save |
|
382 | n.save | |
379 | print '.' |
|
383 | print '.' | |
380 | end |
|
384 | end | |
381 | puts |
|
385 | puts | |
382 |
|
386 | |||
383 | # Custom fields |
|
387 | # Custom fields | |
384 | print "Migrating custom fields" |
|
388 | print "Migrating custom fields" | |
385 | IssueCustomField.destroy_all |
|
389 | IssueCustomField.destroy_all | |
386 | MantisCustomField.find(:all).each do |field| |
|
390 | MantisCustomField.find(:all).each do |field| | |
387 | f = IssueCustomField.new :name => field.name[0..29], |
|
391 | f = IssueCustomField.new :name => field.name[0..29], | |
388 | :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format], |
|
392 | :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format], | |
389 | :min_length => field.length_min, |
|
393 | :min_length => field.length_min, | |
390 | :max_length => field.length_max, |
|
394 | :max_length => field.length_max, | |
391 | :regexp => field.valid_regexp, |
|
395 | :regexp => field.valid_regexp, | |
392 | :possible_values => field.possible_values.split('|'), |
|
396 | :possible_values => field.possible_values.split('|'), | |
393 | :is_required => field.require_report? |
|
397 | :is_required => field.require_report? | |
394 | next unless f.save |
|
398 | next unless f.save | |
395 | print '.' |
|
399 | print '.' | |
396 |
|
400 | |||
397 | # Trackers association |
|
401 | # Trackers association | |
398 | f.trackers = Tracker.find :all |
|
402 | f.trackers = Tracker.find :all | |
399 |
|
403 | |||
400 | # Projects association |
|
404 | # Projects association | |
401 | field.projects.each do |project| |
|
405 | field.projects.each do |project| | |
402 | f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id] |
|
406 | f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id] | |
403 | end |
|
407 | end | |
404 |
|
408 | |||
405 | # Values |
|
409 | # Values | |
406 | field.values.each do |value| |
|
410 | field.values.each do |value| | |
407 | v = CustomValue.new :custom_field_id => f.id, |
|
411 | v = CustomValue.new :custom_field_id => f.id, | |
408 | :value => value.value |
|
412 | :value => value.value | |
409 | v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id] |
|
413 | v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id] | |
410 | v.save |
|
414 | v.save | |
411 | end unless f.new_record? |
|
415 | end unless f.new_record? | |
412 | end |
|
416 | end | |
413 | puts |
|
417 | puts | |
414 |
|
418 | |||
415 | puts |
|
419 | puts | |
416 | puts "Users: #{users_migrated}/#{MantisUser.count}" |
|
420 | puts "Users: #{users_migrated}/#{MantisUser.count}" | |
417 | puts "Projects: #{Project.count}/#{MantisProject.count}" |
|
421 | puts "Projects: #{Project.count}/#{MantisProject.count}" | |
418 | puts "Memberships: #{Member.count}/#{MantisProjectUser.count}" |
|
422 | puts "Memberships: #{Member.count}/#{MantisProjectUser.count}" | |
419 | puts "Versions: #{Version.count}/#{MantisVersion.count}" |
|
423 | puts "Versions: #{Version.count}/#{MantisVersion.count}" | |
420 | puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}" |
|
424 | puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}" | |
421 | puts "Bugs: #{Issue.count}/#{MantisBug.count}" |
|
425 | puts "Bugs: #{Issue.count}/#{MantisBug.count}" | |
422 | puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}" |
|
426 | puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}" | |
423 | puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}" |
|
427 | puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}" | |
424 | puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}" |
|
428 | puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}" | |
425 | puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}" |
|
429 | puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}" | |
426 | puts "News: #{News.count}/#{MantisNews.count}" |
|
430 | puts "News: #{News.count}/#{MantisNews.count}" | |
427 | puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}" |
|
431 | puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}" | |
428 | end |
|
432 | end | |
429 |
|
433 | |||
430 | def self.encoding(charset) |
|
434 | def self.encoding(charset) | |
431 | @ic = Iconv.new('UTF-8', charset) |
|
435 | @ic = Iconv.new('UTF-8', charset) | |
432 | rescue Iconv::InvalidEncoding |
|
436 | rescue Iconv::InvalidEncoding | |
433 | return false |
|
437 | return false | |
434 | end |
|
438 | end | |
435 |
|
439 | |||
436 | def self.establish_connection(params) |
|
440 | def self.establish_connection(params) | |
437 | constants.each do |const| |
|
441 | constants.each do |const| | |
438 | klass = const_get(const) |
|
442 | klass = const_get(const) | |
439 | next unless klass.respond_to? 'establish_connection' |
|
443 | next unless klass.respond_to? 'establish_connection' | |
440 | klass.establish_connection params |
|
444 | klass.establish_connection params | |
441 | end |
|
445 | end | |
442 | end |
|
446 | end | |
443 |
|
447 | |||
444 | private |
|
448 | private | |
445 | def self.encode(text) |
|
449 | def self.encode(text) | |
446 | @ic.iconv text |
|
450 | @ic.iconv text | |
447 | rescue |
|
451 | rescue | |
448 | text |
|
452 | text | |
449 | end |
|
453 | end | |
450 | end |
|
454 | end | |
451 |
|
455 | |||
452 | puts |
|
456 | puts | |
453 | puts "WARNING: Your Redmine data will be deleted during this process." |
|
457 | puts "WARNING: Your Redmine data will be deleted during this process." | |
454 | print "Are you sure you want to continue ? [y/N] " |
|
458 | print "Are you sure you want to continue ? [y/N] " | |
455 | break unless STDIN.gets.match(/^y$/i) |
|
459 | break unless STDIN.gets.match(/^y$/i) | |
456 |
|
460 | |||
457 | # Default Mantis database settings |
|
461 | # Default Mantis database settings | |
458 | db_params = {:adapter => 'mysql', |
|
462 | db_params = {:adapter => 'mysql', | |
459 | :database => 'bugtracker', |
|
463 | :database => 'bugtracker', | |
460 | :host => 'localhost', |
|
464 | :host => 'localhost', | |
461 | :username => 'root', |
|
465 | :username => 'root', | |
462 | :password => '' } |
|
466 | :password => '' } | |
463 |
|
467 | |||
464 | puts |
|
468 | puts | |
465 | puts "Please enter settings for your Mantis database" |
|
469 | puts "Please enter settings for your Mantis database" | |
466 | [:adapter, :host, :database, :username, :password].each do |param| |
|
470 | [:adapter, :host, :database, :username, :password].each do |param| | |
467 | print "#{param} [#{db_params[param]}]: " |
|
471 | print "#{param} [#{db_params[param]}]: " | |
468 | value = STDIN.gets.chomp! |
|
472 | value = STDIN.gets.chomp! | |
469 | db_params[param] = value unless value.blank? |
|
473 | db_params[param] = value unless value.blank? | |
470 | end |
|
474 | end | |
471 |
|
475 | |||
472 | while true |
|
476 | while true | |
473 | print "encoding [UTF-8]: " |
|
477 | print "encoding [UTF-8]: " | |
474 | encoding = STDIN.gets.chomp! |
|
478 | encoding = STDIN.gets.chomp! | |
475 | encoding = 'UTF-8' if encoding.blank? |
|
479 | encoding = 'UTF-8' if encoding.blank? | |
476 | break if MantisMigrate.encoding encoding |
|
480 | break if MantisMigrate.encoding encoding | |
477 | puts "Invalid encoding!" |
|
481 | puts "Invalid encoding!" | |
478 | end |
|
482 | end | |
479 | puts |
|
483 | puts | |
480 |
|
484 | |||
481 | # Make sure bugs can refer bugs in other projects |
|
485 | # Make sure bugs can refer bugs in other projects | |
482 | Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations' |
|
486 | Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations' | |
483 |
|
487 | |||
484 | MantisMigrate.establish_connection db_params |
|
488 | MantisMigrate.establish_connection db_params | |
485 | MantisMigrate.migrate |
|
489 | MantisMigrate.migrate | |
486 | end |
|
490 | end | |
487 | end |
|
491 | end |
@@ -1,499 +1,500 | |||||
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 | require 'active_record' |
|
18 | require 'active_record' | |
19 | require 'iconv' |
|
19 | require 'iconv' | |
20 | require 'pp' |
|
20 | require 'pp' | |
21 |
|
21 | |||
22 | namespace :redmine do |
|
22 | namespace :redmine do | |
23 | desc 'Trac migration script' |
|
23 | desc 'Trac migration script' | |
24 | task :migrate_from_trac => :environment do |
|
24 | task :migrate_from_trac => :environment do | |
25 |
|
25 | |||
26 | module TracMigrate |
|
26 | module TracMigrate | |
27 | TICKET_MAP = []; |
|
27 | TICKET_MAP = []; | |
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 = {'new' => DEFAULT_STATUS, |
|
34 | STATUS_MAPPING = {'new' => DEFAULT_STATUS, | |
35 | 'reopened' => feedback_status, |
|
35 | 'reopened' => feedback_status, | |
36 | 'assigned' => assigned_status, |
|
36 | 'assigned' => assigned_status, | |
37 | 'closed' => closed_status |
|
37 | 'closed' => closed_status | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | priorities = Enumeration.get_values('IPRI') |
|
40 | priorities = Enumeration.get_values('IPRI') | |
41 | DEFAULT_PRIORITY = priorities[0] |
|
41 | DEFAULT_PRIORITY = priorities[0] | |
42 | PRIORITY_MAPPING = {'lowest' => priorities[0], |
|
42 | PRIORITY_MAPPING = {'lowest' => priorities[0], | |
43 | 'low' => priorities[0], |
|
43 | 'low' => priorities[0], | |
44 | 'normal' => priorities[1], |
|
44 | 'normal' => priorities[1], | |
45 | 'high' => priorities[2], |
|
45 | 'high' => priorities[2], | |
46 | 'highest' => priorities[3] |
|
46 | 'highest' => priorities[3] | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | TRACKER_BUG = Tracker.find_by_position(1) |
|
49 | TRACKER_BUG = Tracker.find_by_position(1) | |
50 | TRACKER_FEATURE = Tracker.find_by_position(2) |
|
50 | TRACKER_FEATURE = Tracker.find_by_position(2) | |
51 | DEFAULT_TRACKER = TRACKER_BUG |
|
51 | DEFAULT_TRACKER = TRACKER_BUG | |
52 | TRACKER_MAPPING = {'defect' => TRACKER_BUG, |
|
52 | TRACKER_MAPPING = {'defect' => TRACKER_BUG, | |
53 | 'enhancement' => TRACKER_FEATURE, |
|
53 | 'enhancement' => TRACKER_FEATURE, | |
54 | 'task' => TRACKER_FEATURE, |
|
54 | 'task' => TRACKER_FEATURE, | |
55 | 'patch' =>TRACKER_FEATURE |
|
55 | 'patch' =>TRACKER_FEATURE | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | DEFAULT_ROLE = Role.find_by_position(3) |
|
58 | roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') | |
59 |
manager_role = |
|
59 | manager_role = roles[0] | |
60 |
developer_role = |
|
60 | developer_role = roles[1] | |
|
61 | DEFAULT_ROLE = roles.last | |||
61 | ROLE_MAPPING = {'admin' => manager_role, |
|
62 | ROLE_MAPPING = {'admin' => manager_role, | |
62 | 'developer' => developer_role |
|
63 | 'developer' => developer_role | |
63 | } |
|
64 | } | |
64 |
|
65 | |||
65 | class TracComponent < ActiveRecord::Base |
|
66 | class TracComponent < ActiveRecord::Base | |
66 | set_table_name :component |
|
67 | set_table_name :component | |
67 | end |
|
68 | end | |
68 |
|
69 | |||
69 | class TracMilestone < ActiveRecord::Base |
|
70 | class TracMilestone < ActiveRecord::Base | |
70 | set_table_name :milestone |
|
71 | set_table_name :milestone | |
71 |
|
72 | |||
72 | def due |
|
73 | def due | |
73 | if read_attribute(:due) > 0 |
|
74 | if read_attribute(:due) > 0 | |
74 | Time.at(read_attribute(:due)).to_date |
|
75 | Time.at(read_attribute(:due)).to_date | |
75 | else |
|
76 | else | |
76 | nil |
|
77 | nil | |
77 | end |
|
78 | end | |
78 | end |
|
79 | end | |
79 | end |
|
80 | end | |
80 |
|
81 | |||
81 | class TracTicketCustom < ActiveRecord::Base |
|
82 | class TracTicketCustom < ActiveRecord::Base | |
82 | set_table_name :ticket_custom |
|
83 | set_table_name :ticket_custom | |
83 | end |
|
84 | end | |
84 |
|
85 | |||
85 | class TracAttachment < ActiveRecord::Base |
|
86 | class TracAttachment < ActiveRecord::Base | |
86 | set_table_name :attachment |
|
87 | set_table_name :attachment | |
87 | set_inheritance_column :none |
|
88 | set_inheritance_column :none | |
88 |
|
89 | |||
89 | def time; Time.at(read_attribute(:time)) end |
|
90 | def time; Time.at(read_attribute(:time)) end | |
90 |
|
91 | |||
91 | def original_filename |
|
92 | def original_filename | |
92 | filename |
|
93 | filename | |
93 | end |
|
94 | end | |
94 |
|
95 | |||
95 | def content_type |
|
96 | def content_type | |
96 | Redmine::MimeType.of(filename) || '' |
|
97 | Redmine::MimeType.of(filename) || '' | |
97 | end |
|
98 | end | |
98 |
|
99 | |||
99 | def exist? |
|
100 | def exist? | |
100 | File.file? trac_fullpath |
|
101 | File.file? trac_fullpath | |
101 | end |
|
102 | end | |
102 |
|
103 | |||
103 | def read |
|
104 | def read | |
104 | File.open("#{trac_fullpath}", 'rb').read |
|
105 | File.open("#{trac_fullpath}", 'rb').read | |
105 | end |
|
106 | end | |
106 |
|
107 | |||
107 | private |
|
108 | private | |
108 | def trac_fullpath |
|
109 | def trac_fullpath | |
109 | attachment_type = read_attribute(:type) |
|
110 | attachment_type = read_attribute(:type) | |
110 | trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } |
|
111 | trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } | |
111 | "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}" |
|
112 | "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}" | |
112 | end |
|
113 | end | |
113 | end |
|
114 | end | |
114 |
|
115 | |||
115 | class TracTicket < ActiveRecord::Base |
|
116 | class TracTicket < ActiveRecord::Base | |
116 | set_table_name :ticket |
|
117 | set_table_name :ticket | |
117 | set_inheritance_column :none |
|
118 | set_inheritance_column :none | |
118 |
|
119 | |||
119 | # ticket changes: only migrate status changes and comments |
|
120 | # ticket changes: only migrate status changes and comments | |
120 | has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket |
|
121 | has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket | |
121 | has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'" |
|
122 | has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'" | |
122 | has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket |
|
123 | has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket | |
123 |
|
124 | |||
124 | def ticket_type |
|
125 | def ticket_type | |
125 | read_attribute(:type) |
|
126 | read_attribute(:type) | |
126 | end |
|
127 | end | |
127 |
|
128 | |||
128 | def summary |
|
129 | def summary | |
129 | read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) |
|
130 | read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) | |
130 | end |
|
131 | end | |
131 |
|
132 | |||
132 | def description |
|
133 | def description | |
133 | read_attribute(:description).blank? ? summary : read_attribute(:description) |
|
134 | read_attribute(:description).blank? ? summary : read_attribute(:description) | |
134 | end |
|
135 | end | |
135 |
|
136 | |||
136 | def time; Time.at(read_attribute(:time)) end |
|
137 | def time; Time.at(read_attribute(:time)) end | |
137 | end |
|
138 | end | |
138 |
|
139 | |||
139 | class TracTicketChange < ActiveRecord::Base |
|
140 | class TracTicketChange < ActiveRecord::Base | |
140 | set_table_name :ticket_change |
|
141 | set_table_name :ticket_change | |
141 |
|
142 | |||
142 | def time; Time.at(read_attribute(:time)) end |
|
143 | def time; Time.at(read_attribute(:time)) end | |
143 | end |
|
144 | end | |
144 |
|
145 | |||
145 | class TracWikiPage < ActiveRecord::Base |
|
146 | class TracWikiPage < ActiveRecord::Base | |
146 | set_table_name :wiki |
|
147 | set_table_name :wiki | |
147 | end |
|
148 | end | |
148 |
|
149 | |||
149 | class TracPermission < ActiveRecord::Base |
|
150 | class TracPermission < ActiveRecord::Base | |
150 | set_table_name :permission |
|
151 | set_table_name :permission | |
151 | end |
|
152 | end | |
152 |
|
153 | |||
153 | def self.find_or_create_user(username, project_member = false) |
|
154 | def self.find_or_create_user(username, project_member = false) | |
154 | u = User.find_by_login(username) |
|
155 | u = User.find_by_login(username) | |
155 | if !u |
|
156 | if !u | |
156 | # Create a new user if not found |
|
157 | # Create a new user if not found | |
157 | mail = username[0,limit_for(User, 'mail')] |
|
158 | mail = username[0,limit_for(User, 'mail')] | |
158 | mail = "#{mail}@foo.bar" unless mail.include?("@") |
|
159 | mail = "#{mail}@foo.bar" unless mail.include?("@") | |
159 | u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'), |
|
160 | u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'), | |
160 | :lastname => '-', |
|
161 | :lastname => '-', | |
161 | :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-') |
|
162 | :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-') | |
162 | u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') |
|
163 | u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') | |
163 | u.password = 'trac' |
|
164 | u.password = 'trac' | |
164 | u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') |
|
165 | u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') | |
165 | # finally, a default user is used if the new user is not valid |
|
166 | # finally, a default user is used if the new user is not valid | |
166 | u = User.find(:first) unless u.save |
|
167 | u = User.find(:first) unless u.save | |
167 | end |
|
168 | end | |
168 | # Make sure he is a member of the project |
|
169 | # Make sure he is a member of the project | |
169 | if project_member && !u.member_of?(@target_project) |
|
170 | if project_member && !u.member_of?(@target_project) | |
170 | role = DEFAULT_ROLE |
|
171 | role = DEFAULT_ROLE | |
171 | if u.admin |
|
172 | if u.admin | |
172 | role = ROLE_MAPPING['admin'] |
|
173 | role = ROLE_MAPPING['admin'] | |
173 | elsif TracPermission.find_by_username_and_action(username, 'developer') |
|
174 | elsif TracPermission.find_by_username_and_action(username, 'developer') | |
174 | role = ROLE_MAPPING['developer'] |
|
175 | role = ROLE_MAPPING['developer'] | |
175 | end |
|
176 | end | |
176 |
Member.create(:user => u, :project => @target_project, :role => |
|
177 | Member.create(:user => u, :project => @target_project, :role => role) | |
177 | u.reload |
|
178 | u.reload | |
178 | end |
|
179 | end | |
179 | u |
|
180 | u | |
180 | end |
|
181 | end | |
181 |
|
182 | |||
182 | # Basic wiki syntax conversion |
|
183 | # Basic wiki syntax conversion | |
183 | def self.convert_wiki_text(text) |
|
184 | def self.convert_wiki_text(text) | |
184 | # Titles |
|
185 | # Titles | |
185 | text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"} |
|
186 | text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"} | |
186 | # External Links |
|
187 | # External Links | |
187 | text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"} |
|
188 | text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"} | |
188 | # Internal Links |
|
189 | # Internal Links | |
189 | text = text.gsub(/[[BR]]/, "\n") # This has to go before the rules below |
|
190 | text = text.gsub(/[[BR]]/, "\n") # This has to go before the rules below | |
190 | text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} |
|
191 | text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} | |
191 | text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} |
|
192 | text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} | |
192 | text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} |
|
193 | text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} | |
193 | text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} |
|
194 | text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} | |
194 | # Revisions links |
|
195 | # Revisions links | |
195 | text = text.gsub(/\[(\d+)\]/, 'r\1') |
|
196 | text = text.gsub(/\[(\d+)\]/, 'r\1') | |
196 | # Ticket number re-writing |
|
197 | # Ticket number re-writing | |
197 | text = text.gsub(/#(\d+)/) do |s| |
|
198 | text = text.gsub(/#(\d+)/) do |s| | |
198 | TICKET_MAP[$1.to_i] ||= $1 |
|
199 | TICKET_MAP[$1.to_i] ||= $1 | |
199 | "\##{TICKET_MAP[$1.to_i]}" |
|
200 | "\##{TICKET_MAP[$1.to_i]}" | |
200 | end |
|
201 | end | |
201 | # Preformatted blocks |
|
202 | # Preformatted blocks | |
202 | text = text.gsub(/\{\{\{/, '<pre>') |
|
203 | text = text.gsub(/\{\{\{/, '<pre>') | |
203 | text = text.gsub(/\}\}\}/, '</pre>') |
|
204 | text = text.gsub(/\}\}\}/, '</pre>') | |
204 | # Highlighting |
|
205 | # Highlighting | |
205 | text = text.gsub(/'''''([^\s])/, '_*\1') |
|
206 | text = text.gsub(/'''''([^\s])/, '_*\1') | |
206 | text = text.gsub(/([^\s])'''''/, '\1*_') |
|
207 | text = text.gsub(/([^\s])'''''/, '\1*_') | |
207 | text = text.gsub(/'''/, '*') |
|
208 | text = text.gsub(/'''/, '*') | |
208 | text = text.gsub(/''/, '_') |
|
209 | text = text.gsub(/''/, '_') | |
209 | text = text.gsub(/__/, '+') |
|
210 | text = text.gsub(/__/, '+') | |
210 | text = text.gsub(/~~/, '-') |
|
211 | text = text.gsub(/~~/, '-') | |
211 | text = text.gsub(/`/, '@') |
|
212 | text = text.gsub(/`/, '@') | |
212 | text = text.gsub(/,,/, '~') |
|
213 | text = text.gsub(/,,/, '~') | |
213 | # Lists |
|
214 | # Lists | |
214 | text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "} |
|
215 | text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "} | |
215 |
|
216 | |||
216 |
|
217 | |||
217 | text |
|
218 | text | |
218 | end |
|
219 | end | |
219 |
|
220 | |||
220 | def self.migrate |
|
221 | def self.migrate | |
221 | establish_connection({:adapter => trac_adapter, |
|
222 | establish_connection({:adapter => trac_adapter, | |
222 | :database => trac_db_path}) |
|
223 | :database => trac_db_path}) | |
223 |
|
224 | |||
224 | # Quick database test |
|
225 | # Quick database test | |
225 | TracComponent.count |
|
226 | TracComponent.count | |
226 |
|
227 | |||
227 | migrated_components = 0 |
|
228 | migrated_components = 0 | |
228 | migrated_milestones = 0 |
|
229 | migrated_milestones = 0 | |
229 | migrated_tickets = 0 |
|
230 | migrated_tickets = 0 | |
230 | migrated_custom_values = 0 |
|
231 | migrated_custom_values = 0 | |
231 | migrated_ticket_attachments = 0 |
|
232 | migrated_ticket_attachments = 0 | |
232 | migrated_wiki_edits = 0 |
|
233 | migrated_wiki_edits = 0 | |
233 |
|
234 | |||
234 | # Components |
|
235 | # Components | |
235 | print "Migrating components" |
|
236 | print "Migrating components" | |
236 | issues_category_map = {} |
|
237 | issues_category_map = {} | |
237 | TracComponent.find(:all).each do |component| |
|
238 | TracComponent.find(:all).each do |component| | |
238 | print '.' |
|
239 | print '.' | |
239 | STDOUT.flush |
|
240 | STDOUT.flush | |
240 | c = IssueCategory.new :project => @target_project, |
|
241 | c = IssueCategory.new :project => @target_project, | |
241 | :name => encode(component.name[0, limit_for(IssueCategory, 'name')]) |
|
242 | :name => encode(component.name[0, limit_for(IssueCategory, 'name')]) | |
242 | next unless c.save |
|
243 | next unless c.save | |
243 | issues_category_map[component.name] = c |
|
244 | issues_category_map[component.name] = c | |
244 | migrated_components += 1 |
|
245 | migrated_components += 1 | |
245 | end |
|
246 | end | |
246 | puts |
|
247 | puts | |
247 |
|
248 | |||
248 | # Milestones |
|
249 | # Milestones | |
249 | print "Migrating milestones" |
|
250 | print "Migrating milestones" | |
250 | version_map = {} |
|
251 | version_map = {} | |
251 | TracMilestone.find(:all).each do |milestone| |
|
252 | TracMilestone.find(:all).each do |milestone| | |
252 | print '.' |
|
253 | print '.' | |
253 | STDOUT.flush |
|
254 | STDOUT.flush | |
254 | v = Version.new :project => @target_project, |
|
255 | v = Version.new :project => @target_project, | |
255 | :name => encode(milestone.name[0, limit_for(Version, 'name')]), |
|
256 | :name => encode(milestone.name[0, limit_for(Version, 'name')]), | |
256 | :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]), |
|
257 | :description => encode(milestone.description.to_s[0, limit_for(Version, 'description')]), | |
257 | :effective_date => milestone.due |
|
258 | :effective_date => milestone.due | |
258 | next unless v.save |
|
259 | next unless v.save | |
259 | version_map[milestone.name] = v |
|
260 | version_map[milestone.name] = v | |
260 | migrated_milestones += 1 |
|
261 | migrated_milestones += 1 | |
261 | end |
|
262 | end | |
262 | puts |
|
263 | puts | |
263 |
|
264 | |||
264 | # Custom fields |
|
265 | # Custom fields | |
265 | # TODO: read trac.ini instead |
|
266 | # TODO: read trac.ini instead | |
266 | print "Migrating custom fields" |
|
267 | print "Migrating custom fields" | |
267 | custom_field_map = {} |
|
268 | custom_field_map = {} | |
268 | TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field| |
|
269 | TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field| | |
269 | print '.' |
|
270 | print '.' | |
270 | STDOUT.flush |
|
271 | STDOUT.flush | |
271 | # Redmine custom field name |
|
272 | # Redmine custom field name | |
272 | field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize |
|
273 | field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize | |
273 | # Find if the custom already exists in Redmine |
|
274 | # Find if the custom already exists in Redmine | |
274 | f = IssueCustomField.find_by_name(field_name) |
|
275 | f = IssueCustomField.find_by_name(field_name) | |
275 | # Or create a new one |
|
276 | # Or create a new one | |
276 | f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize, |
|
277 | f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize, | |
277 | :field_format => 'string') |
|
278 | :field_format => 'string') | |
278 |
|
279 | |||
279 | next if f.new_record? |
|
280 | next if f.new_record? | |
280 | f.trackers = Tracker.find(:all) |
|
281 | f.trackers = Tracker.find(:all) | |
281 | f.projects << @target_project |
|
282 | f.projects << @target_project | |
282 | custom_field_map[field.name] = f |
|
283 | custom_field_map[field.name] = f | |
283 | end |
|
284 | end | |
284 | puts |
|
285 | puts | |
285 |
|
286 | |||
286 | # Trac 'resolution' field as a Redmine custom field |
|
287 | # Trac 'resolution' field as a Redmine custom field | |
287 | r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" }) |
|
288 | r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" }) | |
288 | r = IssueCustomField.new(:name => 'Resolution', |
|
289 | r = IssueCustomField.new(:name => 'Resolution', | |
289 | :field_format => 'list', |
|
290 | :field_format => 'list', | |
290 | :is_filter => true) if r.nil? |
|
291 | :is_filter => true) if r.nil? | |
291 | r.trackers = Tracker.find(:all) |
|
292 | r.trackers = Tracker.find(:all) | |
292 | r.projects << @target_project |
|
293 | r.projects << @target_project | |
293 | r.possible_values = %w(fixed invalid wontfix duplicate worksforme) |
|
294 | r.possible_values = %w(fixed invalid wontfix duplicate worksforme) | |
294 | custom_field_map['resolution'] = r if r.save |
|
295 | custom_field_map['resolution'] = r if r.save | |
295 |
|
296 | |||
296 | # Tickets |
|
297 | # Tickets | |
297 | print "Migrating tickets" |
|
298 | print "Migrating tickets" | |
298 | TracTicket.find(:all, :order => 'id ASC').each do |ticket| |
|
299 | TracTicket.find(:all, :order => 'id ASC').each do |ticket| | |
299 | print '.' |
|
300 | print '.' | |
300 | STDOUT.flush |
|
301 | STDOUT.flush | |
301 | i = Issue.new :project => @target_project, |
|
302 | i = Issue.new :project => @target_project, | |
302 | :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]), |
|
303 | :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]), | |
303 | :description => convert_wiki_text(encode(ticket.description)), |
|
304 | :description => convert_wiki_text(encode(ticket.description)), | |
304 | :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY, |
|
305 | :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY, | |
305 | :created_on => ticket.time |
|
306 | :created_on => ticket.time | |
306 | i.author = find_or_create_user(ticket.reporter) |
|
307 | i.author = find_or_create_user(ticket.reporter) | |
307 | i.category = issues_category_map[ticket.component] unless ticket.component.blank? |
|
308 | i.category = issues_category_map[ticket.component] unless ticket.component.blank? | |
308 | i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank? |
|
309 | i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank? | |
309 | i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS |
|
310 | i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS | |
310 | i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER |
|
311 | i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER | |
311 | i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank? |
|
312 | i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank? | |
312 | i.id = ticket.id unless Issue.exists?(ticket.id) |
|
313 | i.id = ticket.id unless Issue.exists?(ticket.id) | |
313 | next unless i.save |
|
314 | next unless i.save | |
314 | TICKET_MAP[ticket.id] = i.id |
|
315 | TICKET_MAP[ticket.id] = i.id | |
315 | migrated_tickets += 1 |
|
316 | migrated_tickets += 1 | |
316 |
|
317 | |||
317 | # Owner |
|
318 | # Owner | |
318 | unless ticket.owner.blank? |
|
319 | unless ticket.owner.blank? | |
319 | i.assigned_to = find_or_create_user(ticket.owner, true) |
|
320 | i.assigned_to = find_or_create_user(ticket.owner, true) | |
320 | i.save |
|
321 | i.save | |
321 | end |
|
322 | end | |
322 |
|
323 | |||
323 | # Comments and status/resolution changes |
|
324 | # Comments and status/resolution changes | |
324 | ticket.changes.group_by(&:time).each do |time, changeset| |
|
325 | ticket.changes.group_by(&:time).each do |time, changeset| | |
325 | status_change = changeset.select {|change| change.field == 'status'}.first |
|
326 | status_change = changeset.select {|change| change.field == 'status'}.first | |
326 | resolution_change = changeset.select {|change| change.field == 'resolution'}.first |
|
327 | resolution_change = changeset.select {|change| change.field == 'resolution'}.first | |
327 | comment_change = changeset.select {|change| change.field == 'comment'}.first |
|
328 | comment_change = changeset.select {|change| change.field == 'comment'}.first | |
328 |
|
329 | |||
329 | n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''), |
|
330 | n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''), | |
330 | :created_on => time |
|
331 | :created_on => time | |
331 | n.user = find_or_create_user(changeset.first.author) |
|
332 | n.user = find_or_create_user(changeset.first.author) | |
332 | n.journalized = i |
|
333 | n.journalized = i | |
333 | if status_change && |
|
334 | if status_change && | |
334 | STATUS_MAPPING[status_change.oldvalue] && |
|
335 | STATUS_MAPPING[status_change.oldvalue] && | |
335 | STATUS_MAPPING[status_change.newvalue] && |
|
336 | STATUS_MAPPING[status_change.newvalue] && | |
336 | (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue]) |
|
337 | (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue]) | |
337 | n.details << JournalDetail.new(:property => 'attr', |
|
338 | n.details << JournalDetail.new(:property => 'attr', | |
338 | :prop_key => 'status_id', |
|
339 | :prop_key => 'status_id', | |
339 | :old_value => STATUS_MAPPING[status_change.oldvalue].id, |
|
340 | :old_value => STATUS_MAPPING[status_change.oldvalue].id, | |
340 | :value => STATUS_MAPPING[status_change.newvalue].id) |
|
341 | :value => STATUS_MAPPING[status_change.newvalue].id) | |
341 | end |
|
342 | end | |
342 | if resolution_change |
|
343 | if resolution_change | |
343 | n.details << JournalDetail.new(:property => 'cf', |
|
344 | n.details << JournalDetail.new(:property => 'cf', | |
344 | :prop_key => custom_field_map['resolution'].id, |
|
345 | :prop_key => custom_field_map['resolution'].id, | |
345 | :old_value => resolution_change.oldvalue, |
|
346 | :old_value => resolution_change.oldvalue, | |
346 | :value => resolution_change.newvalue) |
|
347 | :value => resolution_change.newvalue) | |
347 | end |
|
348 | end | |
348 | n.save unless n.details.empty? && n.notes.blank? |
|
349 | n.save unless n.details.empty? && n.notes.blank? | |
349 | end |
|
350 | end | |
350 |
|
351 | |||
351 | # Attachments |
|
352 | # Attachments | |
352 | ticket.attachments.each do |attachment| |
|
353 | ticket.attachments.each do |attachment| | |
353 | next unless attachment.exist? |
|
354 | next unless attachment.exist? | |
354 | a = Attachment.new :created_on => attachment.time |
|
355 | a = Attachment.new :created_on => attachment.time | |
355 | a.file = attachment |
|
356 | a.file = attachment | |
356 | a.author = find_or_create_user(attachment.author) |
|
357 | a.author = find_or_create_user(attachment.author) | |
357 | a.container = i |
|
358 | a.container = i | |
358 | migrated_ticket_attachments += 1 if a.save |
|
359 | migrated_ticket_attachments += 1 if a.save | |
359 | end |
|
360 | end | |
360 |
|
361 | |||
361 | # Custom fields |
|
362 | # Custom fields | |
362 | ticket.customs.each do |custom| |
|
363 | ticket.customs.each do |custom| | |
363 | next if custom_field_map[custom.name].nil? |
|
364 | next if custom_field_map[custom.name].nil? | |
364 | v = CustomValue.new :custom_field => custom_field_map[custom.name], |
|
365 | v = CustomValue.new :custom_field => custom_field_map[custom.name], | |
365 | :value => custom.value |
|
366 | :value => custom.value | |
366 | v.customized = i |
|
367 | v.customized = i | |
367 | next unless v.save |
|
368 | next unless v.save | |
368 | migrated_custom_values += 1 |
|
369 | migrated_custom_values += 1 | |
369 | end |
|
370 | end | |
370 | end |
|
371 | end | |
371 | puts |
|
372 | puts | |
372 |
|
373 | |||
373 | # Wiki |
|
374 | # Wiki | |
374 | print "Migrating wiki" |
|
375 | print "Migrating wiki" | |
375 | @target_project.wiki.destroy if @target_project.wiki |
|
376 | @target_project.wiki.destroy if @target_project.wiki | |
376 | @target_project.reload |
|
377 | @target_project.reload | |
377 | wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart') |
|
378 | wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart') | |
378 | if wiki.save |
|
379 | if wiki.save | |
379 | TracWikiPage.find(:all, :order => 'name, version').each do |page| |
|
380 | TracWikiPage.find(:all, :order => 'name, version').each do |page| | |
380 | print '.' |
|
381 | print '.' | |
381 | STDOUT.flush |
|
382 | STDOUT.flush | |
382 | p = wiki.find_or_new_page(page.name) |
|
383 | p = wiki.find_or_new_page(page.name) | |
383 | p.content = WikiContent.new(:page => p) if p.new_record? |
|
384 | p.content = WikiContent.new(:page => p) if p.new_record? | |
384 | p.content.text = page.text |
|
385 | p.content.text = page.text | |
385 | p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac' |
|
386 | p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac' | |
386 | p.content.comments = page.comment |
|
387 | p.content.comments = page.comment | |
387 | p.new_record? ? p.save : p.content.save |
|
388 | p.new_record? ? p.save : p.content.save | |
388 | migrated_wiki_edits += 1 unless p.content.new_record? |
|
389 | migrated_wiki_edits += 1 unless p.content.new_record? | |
389 | end |
|
390 | end | |
390 |
|
391 | |||
391 | wiki.reload |
|
392 | wiki.reload | |
392 | wiki.pages.each do |page| |
|
393 | wiki.pages.each do |page| | |
393 | page.content.text = convert_wiki_text(page.content.text) |
|
394 | page.content.text = convert_wiki_text(page.content.text) | |
394 | page.content.save |
|
395 | page.content.save | |
395 | end |
|
396 | end | |
396 | end |
|
397 | end | |
397 | puts |
|
398 | puts | |
398 |
|
399 | |||
399 | puts |
|
400 | puts | |
400 | puts "Components: #{migrated_components}/#{TracComponent.count}" |
|
401 | puts "Components: #{migrated_components}/#{TracComponent.count}" | |
401 | puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}" |
|
402 | puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}" | |
402 | puts "Tickets: #{migrated_tickets}/#{TracTicket.count}" |
|
403 | puts "Tickets: #{migrated_tickets}/#{TracTicket.count}" | |
403 | puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s |
|
404 | puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s | |
404 | puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}" |
|
405 | puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}" | |
405 | puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}" |
|
406 | puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}" | |
406 | end |
|
407 | end | |
407 |
|
408 | |||
408 | def self.limit_for(klass, attribute) |
|
409 | def self.limit_for(klass, attribute) | |
409 | klass.columns_hash[attribute.to_s].limit |
|
410 | klass.columns_hash[attribute.to_s].limit | |
410 | end |
|
411 | end | |
411 |
|
412 | |||
412 | def self.encoding(charset) |
|
413 | def self.encoding(charset) | |
413 | @ic = Iconv.new('UTF-8', charset) |
|
414 | @ic = Iconv.new('UTF-8', charset) | |
414 | rescue Iconv::InvalidEncoding |
|
415 | rescue Iconv::InvalidEncoding | |
415 | puts "Invalid encoding!" |
|
416 | puts "Invalid encoding!" | |
416 | return false |
|
417 | return false | |
417 | end |
|
418 | end | |
418 |
|
419 | |||
419 | def self.set_trac_directory(path) |
|
420 | def self.set_trac_directory(path) | |
420 | @trac_directory = path |
|
421 | @trac_directory = path | |
421 | raise "This directory doesn't exist!" unless File.directory?(path) |
|
422 | raise "This directory doesn't exist!" unless File.directory?(path) | |
422 | raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path) |
|
423 | raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path) | |
423 | raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory) |
|
424 | raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory) | |
424 | @trac_directory |
|
425 | @trac_directory | |
425 | rescue Exception => e |
|
426 | rescue Exception => e | |
426 | puts e |
|
427 | puts e | |
427 | return false |
|
428 | return false | |
428 | end |
|
429 | end | |
429 |
|
430 | |||
430 | def self.trac_directory |
|
431 | def self.trac_directory | |
431 | @trac_directory |
|
432 | @trac_directory | |
432 | end |
|
433 | end | |
433 |
|
434 | |||
434 | def self.set_trac_adapter(adapter) |
|
435 | def self.set_trac_adapter(adapter) | |
435 | return false unless %w(sqlite sqlite3).include?(adapter) |
|
436 | return false unless %w(sqlite sqlite3).include?(adapter) | |
436 | @trac_adapter = adapter |
|
437 | @trac_adapter = adapter | |
437 | end |
|
438 | end | |
438 |
|
439 | |||
439 | def self.trac_adapter; @trac_adapter end |
|
440 | def self.trac_adapter; @trac_adapter end | |
440 | def self.trac_db_path; "#{trac_directory}/db/trac.db" end |
|
441 | def self.trac_db_path; "#{trac_directory}/db/trac.db" end | |
441 | def self.trac_attachments_directory; "#{trac_directory}/attachments" end |
|
442 | def self.trac_attachments_directory; "#{trac_directory}/attachments" end | |
442 |
|
443 | |||
443 | def self.target_project_identifier(identifier) |
|
444 | def self.target_project_identifier(identifier) | |
444 | project = Project.find_by_identifier(identifier) |
|
445 | project = Project.find_by_identifier(identifier) | |
445 | if !project |
|
446 | if !project | |
446 | # create the target project |
|
447 | # create the target project | |
447 | project = Project.new :name => identifier.humanize, |
|
448 | project = Project.new :name => identifier.humanize, | |
448 | :description => identifier.humanize |
|
449 | :description => identifier.humanize | |
449 | project.identifier = identifier |
|
450 | project.identifier = identifier | |
450 | puts "Unable to create a project with identifier '#{identifier}'!" unless project.save |
|
451 | puts "Unable to create a project with identifier '#{identifier}'!" unless project.save | |
451 | # enable issues and wiki for the created project |
|
452 | # enable issues and wiki for the created project | |
452 | project.enabled_module_names = ['issue_tracking', 'wiki'] |
|
453 | project.enabled_module_names = ['issue_tracking', 'wiki'] | |
453 | project.trackers << TRACKER_BUG |
|
454 | project.trackers << TRACKER_BUG | |
454 | project.trackers << TRACKER_FEATURE |
|
455 | project.trackers << TRACKER_FEATURE | |
455 | end |
|
456 | end | |
456 | @target_project = project.new_record? ? nil : project |
|
457 | @target_project = project.new_record? ? nil : project | |
457 | end |
|
458 | end | |
458 |
|
459 | |||
459 | def self.establish_connection(params) |
|
460 | def self.establish_connection(params) | |
460 | constants.each do |const| |
|
461 | constants.each do |const| | |
461 | klass = const_get(const) |
|
462 | klass = const_get(const) | |
462 | next unless klass.respond_to? 'establish_connection' |
|
463 | next unless klass.respond_to? 'establish_connection' | |
463 | klass.establish_connection params |
|
464 | klass.establish_connection params | |
464 | end |
|
465 | end | |
465 | end |
|
466 | end | |
466 |
|
467 | |||
467 | private |
|
468 | private | |
468 | def self.encode(text) |
|
469 | def self.encode(text) | |
469 | @ic.iconv text |
|
470 | @ic.iconv text | |
470 | rescue |
|
471 | rescue | |
471 | text |
|
472 | text | |
472 | end |
|
473 | end | |
473 | end |
|
474 | end | |
474 |
|
475 | |||
475 | puts |
|
476 | puts | |
476 | puts "WARNING: Your Redmine install will have a new project added during this process." |
|
477 | puts "WARNING: Your Redmine install will have a new project added during this process." | |
477 | print "Are you sure you want to continue ? [y/N] " |
|
478 | print "Are you sure you want to continue ? [y/N] " | |
478 | break unless STDIN.gets.match(/^y$/i) |
|
479 | break unless STDIN.gets.match(/^y$/i) | |
479 | puts |
|
480 | puts | |
480 |
|
481 | |||
481 | def prompt(text, options = {}, &block) |
|
482 | def prompt(text, options = {}, &block) | |
482 | default = options[:default] || '' |
|
483 | default = options[:default] || '' | |
483 | while true |
|
484 | while true | |
484 | print "#{text} [#{default}]: " |
|
485 | print "#{text} [#{default}]: " | |
485 | value = STDIN.gets.chomp! |
|
486 | value = STDIN.gets.chomp! | |
486 | value = default if value.blank? |
|
487 | value = default if value.blank? | |
487 | break if yield value |
|
488 | break if yield value | |
488 | end |
|
489 | end | |
489 | end |
|
490 | end | |
490 |
|
491 | |||
491 | prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory} |
|
492 | prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory} | |
492 | prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter} |
|
493 | prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter} | |
493 | prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding} |
|
494 | prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding} | |
494 | prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier} |
|
495 | prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier} | |
495 | puts |
|
496 | puts | |
496 |
|
497 | |||
497 | TracMigrate.migrate |
|
498 | TracMigrate.migrate | |
498 | end |
|
499 | end | |
499 | end |
|
500 | end |
@@ -1,49 +1,54 | |||||
1 | require File.dirname(__FILE__) + '/../test_helper' |
|
1 | require File.dirname(__FILE__) + '/../test_helper' | |
2 | require 'search_controller' |
|
2 | require 'search_controller' | |
3 |
|
3 | |||
4 | # Re-raise errors caught by the controller. |
|
4 | # Re-raise errors caught by the controller. | |
5 | class SearchController; def rescue_action(e) raise e end; end |
|
5 | class SearchController; def rescue_action(e) raise e end; end | |
6 |
|
6 | |||
7 | class SearchControllerTest < Test::Unit::TestCase |
|
7 | class SearchControllerTest < Test::Unit::TestCase | |
8 | fixtures :projects, :issues |
|
8 | fixtures :projects, :issues | |
9 |
|
9 | |||
10 | def setup |
|
10 | def setup | |
11 | @controller = SearchController.new |
|
11 | @controller = SearchController.new | |
12 | @request = ActionController::TestRequest.new |
|
12 | @request = ActionController::TestRequest.new | |
13 | @response = ActionController::TestResponse.new |
|
13 | @response = ActionController::TestResponse.new | |
14 | User.current = nil |
|
14 | User.current = nil | |
15 | end |
|
15 | end | |
16 |
|
16 | |||
17 | def test_search_for_projects |
|
17 | def test_search_for_projects | |
18 | get :index |
|
18 | get :index | |
19 | assert_response :success |
|
19 | assert_response :success | |
20 | assert_template 'index' |
|
20 | assert_template 'index' | |
21 |
|
21 | |||
22 | get :index, :q => "cook" |
|
22 | get :index, :q => "cook" | |
23 | assert_response :success |
|
23 | assert_response :success | |
24 | assert_template 'index' |
|
24 | assert_template 'index' | |
25 | assert assigns(:results).include?(Project.find(1)) |
|
25 | assert assigns(:results).include?(Project.find(1)) | |
26 | end |
|
26 | end | |
27 |
|
27 | |||
28 | def test_search_in_project |
|
28 | def test_search_in_project | |
29 | get :index, :id => 1 |
|
29 | get :index, :id => 1 | |
30 | assert_response :success |
|
30 | assert_response :success | |
31 | assert_template 'index' |
|
31 | assert_template 'index' | |
32 | assert_not_nil assigns(:project) |
|
32 | assert_not_nil assigns(:project) | |
33 |
|
33 | |||
34 | get :index, :id => 1, :q => "can" |
|
34 | get :index, :id => 1, :q => "can" | |
35 | assert_response :success |
|
35 | assert_response :success | |
36 | assert_template 'index' |
|
36 | assert_template 'index' | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_quick_jump_to_issue |
|
39 | def test_quick_jump_to_issue | |
40 | # issue of a public project |
|
40 | # issue of a public project | |
41 | get :index, :q => "3" |
|
41 | get :index, :q => "3" | |
42 | assert_redirected_to 'issues/show/3' |
|
42 | assert_redirected_to 'issues/show/3' | |
43 |
|
43 | |||
44 | # issue of a private project |
|
44 | # issue of a private project | |
45 | get :index, :q => "4" |
|
45 | get :index, :q => "4" | |
46 | assert_response :success |
|
46 | assert_response :success | |
47 | assert_template 'index' |
|
47 | assert_template 'index' | |
48 | end |
|
48 | end | |
|
49 | ||||
|
50 | def test_tokens_with_quotes | |||
|
51 | get :index, :id => 1, :q => '"good bye" hello "bye bye"' | |||
|
52 | assert_equal ["good bye", "hello", "bye bye"], assigns(:tokens) | |||
|
53 | end | |||
49 | end |
|
54 | end |
General Comments 0
You need to be logged in to leave comments.
Login now