##// END OF EJS Templates
Trac importer: user can now choose between sqlite and sqlite3 adapter for Trac database....
Jean-Philippe Lang -
r881:b0bb1baeaac6
parent child
Show More
@@ -1,72 +1,73
1 <% if @query.new_record? %>
1 <% if @query.new_record? %>
2 <h2><%=l(:label_issue_plural)%></h2>
2 <h2><%=l(:label_issue_plural)%></h2>
3 <% set_html_title l(:label_issue_plural) %>
3 <% set_html_title l(:label_issue_plural) %>
4
4
5 <% form_tag({ :controller => 'queries', :action => 'new', :project_id => @project }, :id => 'query_form') do %>
5 <% form_tag({ :controller => 'queries', :action => 'new' }, :id => 'query_form') do %>
6 <%= hidden_field_tag('project_id', @project.id) if @project %>
6 <%= render :partial => 'queries/filters', :locals => {:query => @query} %>
7 <%= render :partial => 'queries/filters', :locals => {:query => @query} %>
7 <div class="contextual">
8 <div class="contextual">
8 <%= link_to_remote l(:button_apply),
9 <%= link_to_remote l(:button_apply),
9 { :url => { :set_filter => 1 },
10 { :url => { :set_filter => 1 },
10 :update => "content",
11 :update => "content",
11 :with => "Form.serialize('query_form')"
12 :with => "Form.serialize('query_form')"
12 }, :class => 'icon icon-edit' %>
13 }, :class => 'icon icon-edit' %>
13
14
14 <%= link_to_remote l(:button_clear),
15 <%= link_to_remote l(:button_clear),
15 { :url => { :set_filter => 1 },
16 { :url => { :set_filter => 1 },
16 :update => "content",
17 :update => "content",
17 }, :class => 'icon icon-reload' %>
18 }, :class => 'icon icon-reload' %>
18
19
19 <% if current_role && current_role.allowed_to?(:save_queries) %>
20 <% if current_role && current_role.allowed_to?(:save_queries) %>
20 <%= link_to l(:button_save), {}, :onclick => "$('query_form').submit(); return false;", :class => 'icon icon-save' %>
21 <%= link_to l(:button_save), {}, :onclick => "$('query_form').submit(); return false;", :class => 'icon icon-save' %>
21 <% end %>
22 <% end %>
22 </div>
23 </div>
23 <br />
24 <br />
24 &nbsp;
25 &nbsp;
25 <% end %>
26 <% end %>
26 <% else %>
27 <% else %>
27 <div class="contextual">
28 <div class="contextual">
28 <% if @query.editable_by?(User.current) %>
29 <% if @query.editable_by?(User.current) %>
29 <%= link_to l(:button_edit), {:controller => 'queries', :action => 'edit', :id => @query}, :class => 'icon icon-edit' %>
30 <%= link_to l(:button_edit), {:controller => 'queries', :action => 'edit', :id => @query}, :class => 'icon icon-edit' %>
30 <%= link_to l(:button_delete), {:controller => 'queries', :action => 'destroy', :id => @query}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
31 <%= link_to l(:button_delete), {:controller => 'queries', :action => 'destroy', :id => @query}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %>
31 <% end %>
32 <% end %>
32 </div>
33 </div>
33
34
34 <h2><%= @query.name %></h2>
35 <h2><%= @query.name %></h2>
35 <div id="query_form"></div>
36 <div id="query_form"></div>
36 <% set_html_title @query.name %>
37 <% set_html_title @query.name %>
37 <% end %>
38 <% end %>
38 <%= error_messages_for 'query' %>
39 <%= error_messages_for 'query' %>
39 <% if @query.valid? %>
40 <% if @query.valid? %>
40 <% if @issues.empty? %>
41 <% if @issues.empty? %>
41 <p class="nodata"><%= l(:label_no_data) %></p>
42 <p class="nodata"><%= l(:label_no_data) %></p>
42 <% else %>
43 <% else %>
43 <% form_tag({:controller => 'projects', :action => 'bulk_edit_issues', :id => @project}, :id => 'issues_form', :onsubmit => "if (!checkBulkEdit(this)) {alert('#{l(:notice_no_issue_selected)}'); return false;}" ) do %>
44 <% form_tag({:controller => 'projects', :action => 'bulk_edit_issues', :id => @project}, :id => 'issues_form', :onsubmit => "if (!checkBulkEdit(this)) {alert('#{l(:notice_no_issue_selected)}'); return false;}" ) do %>
44 <%= render :partial => 'issues/list', :locals => {:issues => @issues, :query => @query} %>
45 <%= render :partial => 'issues/list', :locals => {:issues => @issues, :query => @query} %>
45 <div class="contextual">
46 <div class="contextual">
46 <%= l(:label_export_to) %>
47 <%= l(:label_export_to) %>
47 <%= link_to 'CSV', {:format => 'csv'}, :class => 'icon icon-csv' %>,
48 <%= link_to 'CSV', {:format => 'csv'}, :class => 'icon icon-csv' %>,
48 <%= link_to 'PDF', {:format => 'pdf'}, :class => 'icon icon-pdf' %>
49 <%= link_to 'PDF', {:format => 'pdf'}, :class => 'icon icon-pdf' %>
49 </div>
50 </div>
50 <p><%= pagination_links_full @issue_pages %>
51 <p><%= pagination_links_full @issue_pages %>
51 [ <%= @issue_pages.current.first_item %> - <%= @issue_pages.current.last_item %> / <%= @issue_count %> ]</p>
52 [ <%= @issue_pages.current.first_item %> - <%= @issue_pages.current.last_item %> / <%= @issue_count %> ]</p>
52 <% end %>
53 <% end %>
53 <% end %>
54 <% end %>
54 <% end %>
55 <% end %>
55
56
56 <% content_for :sidebar do %>
57 <% content_for :sidebar do %>
57 <%= render :partial => 'issues/sidebar' %>
58 <%= render :partial => 'issues/sidebar' %>
58 <% end if @project%>
59 <% end if @project%>
59
60
60 <% content_for :header_tags do %>
61 <% content_for :header_tags do %>
61 <%= auto_discovery_link_tag(:atom, {:query_id => @query, :format => 'atom', :page => nil, :key => User.current.rss_key}, :title => l(:label_issue_plural)) %>
62 <%= auto_discovery_link_tag(:atom, {:query_id => @query, :format => 'atom', :page => nil, :key => User.current.rss_key}, :title => l(:label_issue_plural)) %>
62 <%= auto_discovery_link_tag(:atom, {:action => 'changes', :query_id => @query, :format => 'atom', :page => nil, :key => User.current.rss_key}, :title => l(:label_changes_details)) %>
63 <%= auto_discovery_link_tag(:atom, {:action => 'changes', :query_id => @query, :format => 'atom', :page => nil, :key => User.current.rss_key}, :title => l(:label_changes_details)) %>
63 <%= javascript_include_tag 'calendar/calendar' %>
64 <%= javascript_include_tag 'calendar/calendar' %>
64 <%= javascript_include_tag "calendar/lang/calendar-#{current_language}.js" %>
65 <%= javascript_include_tag "calendar/lang/calendar-#{current_language}.js" %>
65 <%= javascript_include_tag 'calendar/calendar-setup' %>
66 <%= javascript_include_tag 'calendar/calendar-setup' %>
66 <%= stylesheet_link_tag 'calendar' %>
67 <%= stylesheet_link_tag 'calendar' %>
67 <%= javascript_include_tag 'context_menu' %>
68 <%= javascript_include_tag 'context_menu' %>
68 <%= stylesheet_link_tag 'context_menu' %>
69 <%= stylesheet_link_tag 'context_menu' %>
69 <% end %>
70 <% end %>
70
71
71 <div id="context-menu" style="display: none;"></div>
72 <div id="context-menu" style="display: none;"></div>
72 <%= javascript_tag 'new ContextMenu({})' %>
73 <%= javascript_tag 'new ContextMenu({})' %>
@@ -1,452 +1,462
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
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 = {'new' => DEFAULT_STATUS,
33 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
34 'reopened' => feedback_status,
34 'reopened' => feedback_status,
35 'assigned' => assigned_status,
35 'assigned' => assigned_status,
36 'closed' => closed_status
36 'closed' => closed_status
37 }
37 }
38
38
39 priorities = Enumeration.get_values('IPRI')
39 priorities = Enumeration.get_values('IPRI')
40 DEFAULT_PRIORITY = priorities[0]
40 DEFAULT_PRIORITY = priorities[0]
41 PRIORITY_MAPPING = {'lowest' => priorities[0],
41 PRIORITY_MAPPING = {'lowest' => priorities[0],
42 'low' => priorities[0],
42 'low' => priorities[0],
43 'normal' => priorities[1],
43 'normal' => priorities[1],
44 'high' => priorities[2],
44 'high' => priorities[2],
45 'highest' => priorities[3]
45 'highest' => priorities[3]
46 }
46 }
47
47
48 TRACKER_BUG = Tracker.find_by_position(1)
48 TRACKER_BUG = Tracker.find_by_position(1)
49 TRACKER_FEATURE = Tracker.find_by_position(2)
49 TRACKER_FEATURE = Tracker.find_by_position(2)
50 DEFAULT_TRACKER = TRACKER_BUG
50 DEFAULT_TRACKER = TRACKER_BUG
51 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
51 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
52 'enhancement' => TRACKER_FEATURE,
52 'enhancement' => TRACKER_FEATURE,
53 'task' => TRACKER_FEATURE,
53 'task' => TRACKER_FEATURE,
54 'patch' =>TRACKER_FEATURE
54 'patch' =>TRACKER_FEATURE
55 }
55 }
56
56
57 DEFAULT_ROLE = Role.find_by_position(3)
57 DEFAULT_ROLE = Role.find_by_position(3)
58 manager_role = Role.find_by_position(1)
58 manager_role = Role.find_by_position(1)
59 developer_role = Role.find_by_position(2)
59 developer_role = Role.find_by_position(2)
60 ROLE_MAPPING = {'admin' => manager_role,
60 ROLE_MAPPING = {'admin' => manager_role,
61 'developer' => developer_role
61 'developer' => developer_role
62 }
62 }
63
63
64 class TracComponent < ActiveRecord::Base
64 class TracComponent < ActiveRecord::Base
65 set_table_name :component
65 set_table_name :component
66 end
66 end
67
67
68 class TracMilestone < ActiveRecord::Base
68 class TracMilestone < ActiveRecord::Base
69 set_table_name :milestone
69 set_table_name :milestone
70
70
71 def due
71 def due
72 if read_attribute(:due) > 0
72 if read_attribute(:due) > 0
73 Time.at(read_attribute(:due)).to_date
73 Time.at(read_attribute(:due)).to_date
74 else
74 else
75 nil
75 nil
76 end
76 end
77 end
77 end
78 end
78 end
79
79
80 class TracTicketCustom < ActiveRecord::Base
80 class TracTicketCustom < ActiveRecord::Base
81 set_table_name :ticket_custom
81 set_table_name :ticket_custom
82 end
82 end
83
83
84 class TracAttachment < ActiveRecord::Base
84 class TracAttachment < ActiveRecord::Base
85 set_table_name :attachment
85 set_table_name :attachment
86 set_inheritance_column :none
86 set_inheritance_column :none
87
87
88 def time; Time.at(read_attribute(:time)) end
88 def time; Time.at(read_attribute(:time)) end
89
89
90 def original_filename
90 def original_filename
91 filename
91 filename
92 end
92 end
93
93
94 def content_type
94 def content_type
95 Redmine::MimeType.of(filename) || ''
95 Redmine::MimeType.of(filename) || ''
96 end
96 end
97
97
98 def exist?
98 def exist?
99 File.file? trac_fullpath
99 File.file? trac_fullpath
100 end
100 end
101
101
102 def read
102 def read
103 File.open("#{trac_fullpath}", 'rb').read
103 File.open("#{trac_fullpath}", 'rb').read
104 end
104 end
105
105
106 private
106 private
107 def trac_fullpath
107 def trac_fullpath
108 attachment_type = read_attribute(:type)
108 attachment_type = read_attribute(:type)
109 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
109 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
110 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
110 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
111 end
111 end
112 end
112 end
113
113
114 class TracTicket < ActiveRecord::Base
114 class TracTicket < ActiveRecord::Base
115 set_table_name :ticket
115 set_table_name :ticket
116 set_inheritance_column :none
116 set_inheritance_column :none
117
117
118 # ticket changes: only migrate status changes and comments
118 # ticket changes: only migrate status changes and comments
119 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
119 has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket
120 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
120 has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'ticket'"
121 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
121 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
122
122
123 def ticket_type
123 def ticket_type
124 read_attribute(:type)
124 read_attribute(:type)
125 end
125 end
126
126
127 def summary
127 def summary
128 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
128 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
129 end
129 end
130
130
131 def description
131 def description
132 read_attribute(:description).blank? ? summary : read_attribute(:description)
132 read_attribute(:description).blank? ? summary : read_attribute(:description)
133 end
133 end
134
134
135 def time; Time.at(read_attribute(:time)) end
135 def time; Time.at(read_attribute(:time)) end
136 end
136 end
137
137
138 class TracTicketChange < ActiveRecord::Base
138 class TracTicketChange < ActiveRecord::Base
139 set_table_name :ticket_change
139 set_table_name :ticket_change
140
140
141 def time; Time.at(read_attribute(:time)) end
141 def time; Time.at(read_attribute(:time)) end
142 end
142 end
143
143
144 class TracWikiPage < ActiveRecord::Base
144 class TracWikiPage < ActiveRecord::Base
145 set_table_name :wiki
145 set_table_name :wiki
146 end
146 end
147
147
148 class TracPermission < ActiveRecord::Base
148 class TracPermission < ActiveRecord::Base
149 set_table_name :permission
149 set_table_name :permission
150 end
150 end
151
151
152 def self.find_or_create_user(username, project_member = false)
152 def self.find_or_create_user(username, project_member = false)
153 u = User.find_by_login(username)
153 u = User.find_by_login(username)
154 if !u
154 if !u
155 # Create a new user if not found
155 # Create a new user if not found
156 mail = username[0,limit_for(User, 'mail')]
156 mail = username[0,limit_for(User, 'mail')]
157 mail = "#{mail}@foo.bar" unless mail.include?("@")
157 mail = "#{mail}@foo.bar" unless mail.include?("@")
158 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
158 u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'),
159 :lastname => '-',
159 :lastname => '-',
160 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
160 :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-')
161 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
161 u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-')
162 u.password = 'trac'
162 u.password = 'trac'
163 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
163 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
164 # finally, a default user is used if the new user is not valid
164 # finally, a default user is used if the new user is not valid
165 u = User.find(:first) unless u.save
165 u = User.find(:first) unless u.save
166 end
166 end
167 # Make sure he is a member of the project
167 # Make sure he is a member of the project
168 if project_member && !u.member_of?(@target_project)
168 if project_member && !u.member_of?(@target_project)
169 role = DEFAULT_ROLE
169 role = DEFAULT_ROLE
170 if u.admin
170 if u.admin
171 role = ROLE_MAPPING['admin']
171 role = ROLE_MAPPING['admin']
172 elsif TracPermission.find_by_username_and_action(username, 'developer')
172 elsif TracPermission.find_by_username_and_action(username, 'developer')
173 role = ROLE_MAPPING['developer']
173 role = ROLE_MAPPING['developer']
174 end
174 end
175 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
175 Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE)
176 u.reload
176 u.reload
177 end
177 end
178 u
178 u
179 end
179 end
180
180
181 # Basic wiki syntax conversion
181 # Basic wiki syntax conversion
182 def self.convert_wiki_text(text)
182 def self.convert_wiki_text(text)
183 # Titles
183 # Titles
184 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"}
184 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"}
185 # Links
185 # Links
186 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
186 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
187 # Revisions links
187 # Revisions links
188 text = text.gsub(/\[(\d+)\]/, 'r\1')
188 text = text.gsub(/\[(\d+)\]/, 'r\1')
189 text
189 text
190 end
190 end
191
191
192 def self.migrate
192 def self.migrate
193 establish_connection({:adapter => trac_adapter,
194 :database => trac_db_path})
195
193 # Quick database test before clearing Redmine data
196 # Quick database test before clearing Redmine data
194 TracComponent.count
197 TracComponent.count
195
198
196 puts "Deleting data"
199 puts "Deleting data"
197 CustomField.destroy_all
200 CustomField.destroy_all
198 Issue.destroy_all
201 Issue.destroy_all
199 IssueCategory.destroy_all
202 IssueCategory.destroy_all
200 Version.destroy_all
203 Version.destroy_all
201 User.destroy_all "login <> 'admin'"
204 User.destroy_all "login <> 'admin'"
202
205
203 migrated_components = 0
206 migrated_components = 0
204 migrated_milestones = 0
207 migrated_milestones = 0
205 migrated_tickets = 0
208 migrated_tickets = 0
206 migrated_custom_values = 0
209 migrated_custom_values = 0
207 migrated_ticket_attachments = 0
210 migrated_ticket_attachments = 0
208 migrated_wiki_edits = 0
211 migrated_wiki_edits = 0
209
212
210 # Components
213 # Components
211 print "Migrating components"
214 print "Migrating components"
212 issues_category_map = {}
215 issues_category_map = {}
213 TracComponent.find(:all).each do |component|
216 TracComponent.find(:all).each do |component|
214 print '.'
217 print '.'
215 c = IssueCategory.new :project => @target_project,
218 c = IssueCategory.new :project => @target_project,
216 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
219 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
217 next unless c.save
220 next unless c.save
218 issues_category_map[component.name] = c
221 issues_category_map[component.name] = c
219 migrated_components += 1
222 migrated_components += 1
220 end
223 end
221 puts
224 puts
222
225
223 # Milestones
226 # Milestones
224 print "Migrating milestones"
227 print "Migrating milestones"
225 version_map = {}
228 version_map = {}
226 TracMilestone.find(:all).each do |milestone|
229 TracMilestone.find(:all).each do |milestone|
227 print '.'
230 print '.'
228 v = Version.new :project => @target_project,
231 v = Version.new :project => @target_project,
229 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
232 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
230 :description => encode(milestone.description[0, limit_for(Version, 'description')]),
233 :description => encode(milestone.description[0, limit_for(Version, 'description')]),
231 :effective_date => milestone.due
234 :effective_date => milestone.due
232 next unless v.save
235 next unless v.save
233 version_map[milestone.name] = v
236 version_map[milestone.name] = v
234 migrated_milestones += 1
237 migrated_milestones += 1
235 end
238 end
236 puts
239 puts
237
240
238 # Custom fields
241 # Custom fields
239 # TODO: read trac.ini instead
242 # TODO: read trac.ini instead
240 print "Migrating custom fields"
243 print "Migrating custom fields"
241 custom_field_map = {}
244 custom_field_map = {}
242 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
245 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
243 print '.'
246 print '.'
244 f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
247 f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
245 :field_format => 'string'
248 :field_format => 'string'
246 next unless f.save
249 next unless f.save
247 f.trackers = Tracker.find(:all)
250 f.trackers = Tracker.find(:all)
248 f.projects << @target_project
251 f.projects << @target_project
249 custom_field_map[field.name] = f
252 custom_field_map[field.name] = f
250 end
253 end
251 puts
254 puts
252
255
253 # Trac 'resolution' field as a Redmine custom field
256 # Trac 'resolution' field as a Redmine custom field
254 r = IssueCustomField.new :name => 'Resolution',
257 r = IssueCustomField.new :name => 'Resolution',
255 :field_format => 'list',
258 :field_format => 'list',
256 :is_filter => true
259 :is_filter => true
257 r.trackers = Tracker.find(:all)
260 r.trackers = Tracker.find(:all)
258 r.projects << @target_project
261 r.projects << @target_project
259 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
262 r.possible_values = %w(fixed invalid wontfix duplicate worksforme)
260 custom_field_map['resolution'] = r if r.save
263 custom_field_map['resolution'] = r if r.save
261
264
262 # Tickets
265 # Tickets
263 print "Migrating tickets"
266 print "Migrating tickets"
264 TracTicket.find(:all).each do |ticket|
267 TracTicket.find(:all).each do |ticket|
265 print '.'
268 print '.'
266 i = Issue.new :project => @target_project,
269 i = Issue.new :project => @target_project,
267 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
270 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
268 :description => convert_wiki_text(encode(ticket.description)),
271 :description => convert_wiki_text(encode(ticket.description)),
269 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
272 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
270 :created_on => ticket.time
273 :created_on => ticket.time
271 i.author = find_or_create_user(ticket.reporter)
274 i.author = find_or_create_user(ticket.reporter)
272 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
275 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
273 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
276 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
274 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
277 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
275 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
278 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
276 i.id = ticket.id
279 i.id = ticket.id
277 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
280 i.custom_values << CustomValue.new(:custom_field => custom_field_map['resolution'], :value => ticket.resolution) unless ticket.resolution.blank?
278 next unless i.save
281 next unless i.save
279 migrated_tickets += 1
282 migrated_tickets += 1
280
283
281 # Owner
284 # Owner
282 unless ticket.owner.blank?
285 unless ticket.owner.blank?
283 i.assigned_to = find_or_create_user(ticket.owner, true)
286 i.assigned_to = find_or_create_user(ticket.owner, true)
284 i.save
287 i.save
285 end
288 end
286
289
287 # Comments and status/resolution changes
290 # Comments and status/resolution changes
288 ticket.changes.group_by(&:time).each do |time, changeset|
291 ticket.changes.group_by(&:time).each do |time, changeset|
289 status_change = changeset.select {|change| change.field == 'status'}.first
292 status_change = changeset.select {|change| change.field == 'status'}.first
290 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
293 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
291 comment_change = changeset.select {|change| change.field == 'comment'}.first
294 comment_change = changeset.select {|change| change.field == 'comment'}.first
292
295
293 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
296 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
294 :created_on => time
297 :created_on => time
295 n.user = find_or_create_user(changeset.first.author)
298 n.user = find_or_create_user(changeset.first.author)
296 n.journalized = i
299 n.journalized = i
297 if status_change &&
300 if status_change &&
298 STATUS_MAPPING[status_change.oldvalue] &&
301 STATUS_MAPPING[status_change.oldvalue] &&
299 STATUS_MAPPING[status_change.newvalue] &&
302 STATUS_MAPPING[status_change.newvalue] &&
300 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
303 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
301 n.details << JournalDetail.new(:property => 'attr',
304 n.details << JournalDetail.new(:property => 'attr',
302 :prop_key => 'status_id',
305 :prop_key => 'status_id',
303 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
306 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
304 :value => STATUS_MAPPING[status_change.newvalue].id)
307 :value => STATUS_MAPPING[status_change.newvalue].id)
305 end
308 end
306 if resolution_change
309 if resolution_change
307 n.details << JournalDetail.new(:property => 'cf',
310 n.details << JournalDetail.new(:property => 'cf',
308 :prop_key => custom_field_map['resolution'].id,
311 :prop_key => custom_field_map['resolution'].id,
309 :old_value => resolution_change.oldvalue,
312 :old_value => resolution_change.oldvalue,
310 :value => resolution_change.newvalue)
313 :value => resolution_change.newvalue)
311 end
314 end
312 n.save unless n.details.empty? && n.notes.blank?
315 n.save unless n.details.empty? && n.notes.blank?
313 end
316 end
314
317
315 # Attachments
318 # Attachments
316 ticket.attachments.each do |attachment|
319 ticket.attachments.each do |attachment|
317 next unless attachment.exist?
320 next unless attachment.exist?
318 a = Attachment.new :created_on => attachment.time
321 a = Attachment.new :created_on => attachment.time
319 a.file = attachment
322 a.file = attachment
320 a.author = find_or_create_user(attachment.author)
323 a.author = find_or_create_user(attachment.author)
321 a.container = i
324 a.container = i
322 migrated_ticket_attachments += 1 if a.save
325 migrated_ticket_attachments += 1 if a.save
323 end
326 end
324
327
325 # Custom fields
328 # Custom fields
326 ticket.customs.each do |custom|
329 ticket.customs.each do |custom|
327 v = CustomValue.new :custom_field => custom_field_map[custom.name],
330 v = CustomValue.new :custom_field => custom_field_map[custom.name],
328 :value => custom.value
331 :value => custom.value
329 v.customized = i
332 v.customized = i
330 next unless v.save
333 next unless v.save
331 migrated_custom_values += 1
334 migrated_custom_values += 1
332 end
335 end
333 end
336 end
334 puts
337 puts
335
338
336 # Wiki
339 # Wiki
337 print "Migrating wiki"
340 print "Migrating wiki"
338 @target_project.wiki.destroy if @target_project.wiki
341 @target_project.wiki.destroy if @target_project.wiki
339 @target_project.reload
342 @target_project.reload
340 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
343 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
341 if wiki.save
344 if wiki.save
342 TracWikiPage.find(:all, :order => 'name, version').each do |page|
345 TracWikiPage.find(:all, :order => 'name, version').each do |page|
343 print '.'
346 print '.'
344 p = wiki.find_or_new_page(page.name)
347 p = wiki.find_or_new_page(page.name)
345 p.content = WikiContent.new(:page => p) if p.new_record?
348 p.content = WikiContent.new(:page => p) if p.new_record?
346 p.content.text = page.text
349 p.content.text = page.text
347 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
350 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
348 p.content.comments = page.comment
351 p.content.comments = page.comment
349 p.new_record? ? p.save : p.content.save
352 p.new_record? ? p.save : p.content.save
350 migrated_wiki_edits += 1 unless p.content.new_record?
353 migrated_wiki_edits += 1 unless p.content.new_record?
351 end
354 end
352
355
353 wiki.reload
356 wiki.reload
354 wiki.pages.each do |page|
357 wiki.pages.each do |page|
355 page.content.text = convert_wiki_text(page.content.text)
358 page.content.text = convert_wiki_text(page.content.text)
356 page.content.save
359 page.content.save
357 end
360 end
358 end
361 end
359 puts
362 puts
360
363
361 puts
364 puts
362 puts "Components: #{migrated_components}/#{TracComponent.count}"
365 puts "Components: #{migrated_components}/#{TracComponent.count}"
363 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
366 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
364 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
367 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
365 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
368 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
366 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
369 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
367 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
370 puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
368 end
371 end
369
372
370 def self.limit_for(klass, attribute)
373 def self.limit_for(klass, attribute)
371 klass.columns_hash[attribute.to_s].limit
374 klass.columns_hash[attribute.to_s].limit
372 end
375 end
373
376
374 def self.encoding(charset)
377 def self.encoding(charset)
375 @ic = Iconv.new('UTF-8', charset)
378 @ic = Iconv.new('UTF-8', charset)
376 rescue Iconv::InvalidEncoding
379 rescue Iconv::InvalidEncoding
377 puts "Invalid encoding!"
380 puts "Invalid encoding!"
378 return false
381 return false
379 end
382 end
380
383
381 def self.set_trac_directory(path)
384 def self.set_trac_directory(path)
382 @trac_directory = path
385 @trac_directory = path
383 raise "This directory doesn't exist!" unless File.directory?(path)
386 raise "This directory doesn't exist!" unless File.directory?(path)
384 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
387 raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path)
385 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
388 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
386 @trac_directory
389 @trac_directory
387 rescue Exception => e
390 rescue Exception => e
388 puts e
391 puts e
389 return false
392 return false
390 end
393 end
391
394
392 def self.trac_directory
395 def self.trac_directory
393 @trac_directory
396 @trac_directory
394 end
397 end
395
398
399 def self.set_trac_adapter(adapter)
400 return false unless %w(sqlite sqlite3).include?(adapter)
401 @trac_adapter = adapter
402 end
403
404 def self.trac_adapter; @trac_adapter end
396 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
405 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
397 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
406 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
398
407
399 def self.target_project_identifier(identifier)
408 def self.target_project_identifier(identifier)
400 project = Project.find_by_identifier(identifier)
409 project = Project.find_by_identifier(identifier)
401 if !project
410 if !project
402 # create the target project
411 # create the target project
403 project = Project.new :name => identifier.humanize,
412 project = Project.new :name => identifier.humanize,
404 :description => identifier.humanize
413 :description => identifier.humanize
405 project.identifier = identifier
414 project.identifier = identifier
406 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
415 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
416 # enable issues and wiki for the created project
417 project.enabled_module_names = ['issue_tracking', 'wiki']
407 end
418 end
408 @target_project = project.new_record? ? nil : project
419 @target_project = project.new_record? ? nil : project
409 end
420 end
410
421
411 def self.establish_connection(params)
422 def self.establish_connection(params)
412 constants.each do |const|
423 constants.each do |const|
413 klass = const_get(const)
424 klass = const_get(const)
414 next unless klass.respond_to? 'establish_connection'
425 next unless klass.respond_to? 'establish_connection'
415 klass.establish_connection params
426 klass.establish_connection params
416 end
427 end
417 end
428 end
418
429
419 private
430 private
420 def self.encode(text)
431 def self.encode(text)
421 @ic.iconv text
432 @ic.iconv text
422 rescue
433 rescue
423 text
434 text
424 end
435 end
425 end
436 end
426
437
427 puts
438 puts
428 puts "WARNING: Your Redmine data will be deleted during this process."
439 puts "WARNING: Your Redmine data will be deleted during this process."
429 print "Are you sure you want to continue ? [y/N] "
440 print "Are you sure you want to continue ? [y/N] "
430 break unless STDIN.gets.match(/^y$/i)
441 break unless STDIN.gets.match(/^y$/i)
431 puts
442 puts
432
443
433 def prompt(text, options = {}, &block)
444 def prompt(text, options = {}, &block)
434 default = options[:default] || ''
445 default = options[:default] || ''
435 while true
446 while true
436 print "#{text} [#{default}]: "
447 print "#{text} [#{default}]: "
437 value = STDIN.gets.chomp!
448 value = STDIN.gets.chomp!
438 value = default if value.blank?
449 value = default if value.blank?
439 break if yield value
450 break if yield value
440 end
451 end
441 end
452 end
442
453
443 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
454 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory}
455 prompt('Trac database adapter (sqlite, sqlite3)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
444 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
456 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
445 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
457 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
446 puts
458 puts
447
459
448 TracMigrate.establish_connection({:adapter => 'sqlite',
449 :database => "#{TracMigrate.trac_db_path}"})
450 TracMigrate.migrate
460 TracMigrate.migrate
451 end
461 end
452 end
462 end
General Comments 0
You need to be logged in to leave comments. Login now