annotate .svn/pristine/bc/bcefb9a2bbe2d9c2d605eef1915f4d25e465456c.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 require 'active_record'
Chris@1296 19 require 'iconv'
Chris@1296 20 require 'pp'
Chris@1296 21
Chris@1296 22 namespace :redmine do
Chris@1296 23 desc 'Trac migration script'
Chris@1296 24 task :migrate_from_trac => :environment do
Chris@1296 25
Chris@1296 26 module TracMigrate
Chris@1296 27 TICKET_MAP = []
Chris@1296 28
Chris@1296 29 DEFAULT_STATUS = IssueStatus.default
Chris@1296 30 assigned_status = IssueStatus.find_by_position(2)
Chris@1296 31 resolved_status = IssueStatus.find_by_position(3)
Chris@1296 32 feedback_status = IssueStatus.find_by_position(4)
Chris@1296 33 closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
Chris@1296 34 STATUS_MAPPING = {'new' => DEFAULT_STATUS,
Chris@1296 35 'reopened' => feedback_status,
Chris@1296 36 'assigned' => assigned_status,
Chris@1296 37 'closed' => closed_status
Chris@1296 38 }
Chris@1296 39
Chris@1296 40 priorities = IssuePriority.all
Chris@1296 41 DEFAULT_PRIORITY = priorities[0]
Chris@1296 42 PRIORITY_MAPPING = {'lowest' => priorities[0],
Chris@1296 43 'low' => priorities[0],
Chris@1296 44 'normal' => priorities[1],
Chris@1296 45 'high' => priorities[2],
Chris@1296 46 'highest' => priorities[3],
Chris@1296 47 # ---
Chris@1296 48 'trivial' => priorities[0],
Chris@1296 49 'minor' => priorities[1],
Chris@1296 50 'major' => priorities[2],
Chris@1296 51 'critical' => priorities[3],
Chris@1296 52 'blocker' => priorities[4]
Chris@1296 53 }
Chris@1296 54
Chris@1296 55 TRACKER_BUG = Tracker.find_by_position(1)
Chris@1296 56 TRACKER_FEATURE = Tracker.find_by_position(2)
Chris@1296 57 DEFAULT_TRACKER = TRACKER_BUG
Chris@1296 58 TRACKER_MAPPING = {'defect' => TRACKER_BUG,
Chris@1296 59 'enhancement' => TRACKER_FEATURE,
Chris@1296 60 'task' => TRACKER_FEATURE,
Chris@1296 61 'patch' =>TRACKER_FEATURE
Chris@1296 62 }
Chris@1296 63
Chris@1296 64 roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
Chris@1296 65 manager_role = roles[0]
Chris@1296 66 developer_role = roles[1]
Chris@1296 67 DEFAULT_ROLE = roles.last
Chris@1296 68 ROLE_MAPPING = {'admin' => manager_role,
Chris@1296 69 'developer' => developer_role
Chris@1296 70 }
Chris@1296 71
Chris@1296 72 class ::Time
Chris@1296 73 class << self
Chris@1296 74 alias :real_now :now
Chris@1296 75 def now
Chris@1296 76 real_now - @fake_diff.to_i
Chris@1296 77 end
Chris@1296 78 def fake(time)
Chris@1296 79 @fake_diff = real_now - time
Chris@1296 80 res = yield
Chris@1296 81 @fake_diff = 0
Chris@1296 82 res
Chris@1296 83 end
Chris@1296 84 end
Chris@1296 85 end
Chris@1296 86
Chris@1296 87 class TracComponent < ActiveRecord::Base
Chris@1296 88 self.table_name = :component
Chris@1296 89 end
Chris@1296 90
Chris@1296 91 class TracMilestone < ActiveRecord::Base
Chris@1296 92 self.table_name = :milestone
Chris@1296 93 # If this attribute is set a milestone has a defined target timepoint
Chris@1296 94 def due
Chris@1296 95 if read_attribute(:due) && read_attribute(:due) > 0
Chris@1296 96 Time.at(read_attribute(:due)).to_date
Chris@1296 97 else
Chris@1296 98 nil
Chris@1296 99 end
Chris@1296 100 end
Chris@1296 101 # This is the real timepoint at which the milestone has finished.
Chris@1296 102 def completed
Chris@1296 103 if read_attribute(:completed) && read_attribute(:completed) > 0
Chris@1296 104 Time.at(read_attribute(:completed)).to_date
Chris@1296 105 else
Chris@1296 106 nil
Chris@1296 107 end
Chris@1296 108 end
Chris@1296 109
Chris@1296 110 def description
Chris@1296 111 # Attribute is named descr in Trac v0.8.x
Chris@1296 112 has_attribute?(:descr) ? read_attribute(:descr) : read_attribute(:description)
Chris@1296 113 end
Chris@1296 114 end
Chris@1296 115
Chris@1296 116 class TracTicketCustom < ActiveRecord::Base
Chris@1296 117 self.table_name = :ticket_custom
Chris@1296 118 end
Chris@1296 119
Chris@1296 120 class TracAttachment < ActiveRecord::Base
Chris@1296 121 self.table_name = :attachment
Chris@1296 122 set_inheritance_column :none
Chris@1296 123
Chris@1296 124 def time; Time.at(read_attribute(:time)) end
Chris@1296 125
Chris@1296 126 def original_filename
Chris@1296 127 filename
Chris@1296 128 end
Chris@1296 129
Chris@1296 130 def content_type
Chris@1296 131 ''
Chris@1296 132 end
Chris@1296 133
Chris@1296 134 def exist?
Chris@1296 135 File.file? trac_fullpath
Chris@1296 136 end
Chris@1296 137
Chris@1296 138 def open
Chris@1296 139 File.open("#{trac_fullpath}", 'rb') {|f|
Chris@1296 140 @file = f
Chris@1296 141 yield self
Chris@1296 142 }
Chris@1296 143 end
Chris@1296 144
Chris@1296 145 def read(*args)
Chris@1296 146 @file.read(*args)
Chris@1296 147 end
Chris@1296 148
Chris@1296 149 def description
Chris@1296 150 read_attribute(:description).to_s.slice(0,255)
Chris@1296 151 end
Chris@1296 152
Chris@1296 153 private
Chris@1296 154 def trac_fullpath
Chris@1296 155 attachment_type = read_attribute(:type)
Chris@1296 156 trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) }
Chris@1296 157 "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
Chris@1296 158 end
Chris@1296 159 end
Chris@1296 160
Chris@1296 161 class TracTicket < ActiveRecord::Base
Chris@1296 162 self.table_name = :ticket
Chris@1296 163 set_inheritance_column :none
Chris@1296 164
Chris@1296 165 # ticket changes: only migrate status changes and comments
Chris@1296 166 has_many :ticket_changes, :class_name => "TracTicketChange", :foreign_key => :ticket
Chris@1296 167 has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket
Chris@1296 168
Chris@1296 169 def attachments
Chris@1296 170 TracMigrate::TracAttachment.all(:conditions => ["type = 'ticket' AND id = ?", self.id.to_s])
Chris@1296 171 end
Chris@1296 172
Chris@1296 173 def ticket_type
Chris@1296 174 read_attribute(:type)
Chris@1296 175 end
Chris@1296 176
Chris@1296 177 def summary
Chris@1296 178 read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary)
Chris@1296 179 end
Chris@1296 180
Chris@1296 181 def description
Chris@1296 182 read_attribute(:description).blank? ? summary : read_attribute(:description)
Chris@1296 183 end
Chris@1296 184
Chris@1296 185 def time; Time.at(read_attribute(:time)) end
Chris@1296 186 def changetime; Time.at(read_attribute(:changetime)) end
Chris@1296 187 end
Chris@1296 188
Chris@1296 189 class TracTicketChange < ActiveRecord::Base
Chris@1296 190 self.table_name = :ticket_change
Chris@1296 191
Chris@1296 192 def self.columns
Chris@1296 193 # Hides Trac field 'field' to prevent clash with AR field_changed? method (Rails 3.0)
Chris@1296 194 super.select {|column| column.name.to_s != 'field'}
Chris@1296 195 end
Chris@1296 196
Chris@1296 197 def time; Time.at(read_attribute(:time)) end
Chris@1296 198 end
Chris@1296 199
Chris@1296 200 TRAC_WIKI_PAGES = %w(InterMapTxt InterTrac InterWiki RecentChanges SandBox TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \
Chris@1296 201 TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization \
Chris@1296 202 TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery \
Chris@1296 203 TracReports TracRevisionLog TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets \
Chris@1296 204 TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting \
Chris@1296 205 WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks \
Chris@1296 206 CamelCase TitleIndex)
Chris@1296 207
Chris@1296 208 class TracWikiPage < ActiveRecord::Base
Chris@1296 209 self.table_name = :wiki
Chris@1296 210 set_primary_key :name
Chris@1296 211
Chris@1296 212 def self.columns
Chris@1296 213 # Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0)
Chris@1296 214 super.select {|column| column.name.to_s != 'readonly'}
Chris@1296 215 end
Chris@1296 216
Chris@1296 217 def attachments
Chris@1296 218 TracMigrate::TracAttachment.all(:conditions => ["type = 'wiki' AND id = ?", self.id.to_s])
Chris@1296 219 end
Chris@1296 220
Chris@1296 221 def time; Time.at(read_attribute(:time)) end
Chris@1296 222 end
Chris@1296 223
Chris@1296 224 class TracPermission < ActiveRecord::Base
Chris@1296 225 self.table_name = :permission
Chris@1296 226 end
Chris@1296 227
Chris@1296 228 class TracSessionAttribute < ActiveRecord::Base
Chris@1296 229 self.table_name = :session_attribute
Chris@1296 230 end
Chris@1296 231
Chris@1296 232 def self.find_or_create_user(username, project_member = false)
Chris@1296 233 return User.anonymous if username.blank?
Chris@1296 234
Chris@1296 235 u = User.find_by_login(username)
Chris@1296 236 if !u
Chris@1296 237 # Create a new user if not found
Chris@1296 238 mail = username[0, User::MAIL_LENGTH_LIMIT]
Chris@1296 239 if mail_attr = TracSessionAttribute.find_by_sid_and_name(username, 'email')
Chris@1296 240 mail = mail_attr.value
Chris@1296 241 end
Chris@1296 242 mail = "#{mail}@foo.bar" unless mail.include?("@")
Chris@1296 243
Chris@1296 244 name = username
Chris@1296 245 if name_attr = TracSessionAttribute.find_by_sid_and_name(username, 'name')
Chris@1296 246 name = name_attr.value
Chris@1296 247 end
Chris@1296 248 name =~ (/(.*)(\s+\w+)?/)
Chris@1296 249 fn = $1.strip
Chris@1296 250 ln = ($2 || '-').strip
Chris@1296 251
Chris@1296 252 u = User.new :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-'),
Chris@1296 253 :firstname => fn[0, limit_for(User, 'firstname')],
Chris@1296 254 :lastname => ln[0, limit_for(User, 'lastname')]
Chris@1296 255
Chris@1296 256 u.login = username[0, User::LOGIN_LENGTH_LIMIT].gsub(/[^a-z0-9_\-@\.]/i, '-')
Chris@1296 257 u.password = 'trac'
Chris@1296 258 u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
Chris@1296 259 # finally, a default user is used if the new user is not valid
Chris@1296 260 u = User.find(:first) unless u.save
Chris@1296 261 end
Chris@1296 262 # Make sure he is a member of the project
Chris@1296 263 if project_member && !u.member_of?(@target_project)
Chris@1296 264 role = DEFAULT_ROLE
Chris@1296 265 if u.admin
Chris@1296 266 role = ROLE_MAPPING['admin']
Chris@1296 267 elsif TracPermission.find_by_username_and_action(username, 'developer')
Chris@1296 268 role = ROLE_MAPPING['developer']
Chris@1296 269 end
Chris@1296 270 Member.create(:user => u, :project => @target_project, :roles => [role])
Chris@1296 271 u.reload
Chris@1296 272 end
Chris@1296 273 u
Chris@1296 274 end
Chris@1296 275
Chris@1296 276 # Basic wiki syntax conversion
Chris@1296 277 def self.convert_wiki_text(text)
Chris@1296 278 # Titles
Chris@1296 279 text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
Chris@1296 280 # External Links
Chris@1296 281 text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
Chris@1296 282 # Ticket links:
Chris@1296 283 # [ticket:234 Text],[ticket:234 This is a test]
Chris@1296 284 text = text.gsub(/\[ticket\:([^\ ]+)\ (.+?)\]/, '"\2":/issues/show/\1')
Chris@1296 285 # ticket:1234
Chris@1296 286 # #1 is working cause Redmine uses the same syntax.
Chris@1296 287 text = text.gsub(/ticket\:([^\ ]+)/, '#\1')
Chris@1296 288 # Milestone links:
Chris@1296 289 # [milestone:"0.1.0 Mercury" Milestone 0.1.0 (Mercury)]
Chris@1296 290 # The text "Milestone 0.1.0 (Mercury)" is not converted,
Chris@1296 291 # cause Redmine's wiki does not support this.
Chris@1296 292 text = text.gsub(/\[milestone\:\"([^\"]+)\"\ (.+?)\]/, 'version:"\1"')
Chris@1296 293 # [milestone:"0.1.0 Mercury"]
Chris@1296 294 text = text.gsub(/\[milestone\:\"([^\"]+)\"\]/, 'version:"\1"')
Chris@1296 295 text = text.gsub(/milestone\:\"([^\"]+)\"/, 'version:"\1"')
Chris@1296 296 # milestone:0.1.0
Chris@1296 297 text = text.gsub(/\[milestone\:([^\ ]+)\]/, 'version:\1')
Chris@1296 298 text = text.gsub(/milestone\:([^\ ]+)/, 'version:\1')
Chris@1296 299 # Internal Links
Chris@1296 300 text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
Chris@1296 301 text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
Chris@1296 302 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
Chris@1296 303 text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
Chris@1296 304 text = text.gsub(/\[wiki:([^\s\]]+)\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
Chris@1296 305 text = text.gsub(/\[wiki:([^\s\]]+)\s(.*)\]/) {|s| "[[#{$1.delete(',./?;|:')}|#{$2.delete(',./?;|:')}]]"}
Chris@1296 306
Chris@1296 307 # Links to pages UsingJustWikiCaps
Chris@1296 308 text = text.gsub(/([^!]|^)(^| )([A-Z][a-z]+[A-Z][a-zA-Z]+)/, '\\1\\2[[\3]]')
Chris@1296 309 # Normalize things that were supposed to not be links
Chris@1296 310 # like !NotALink
Chris@1296 311 text = text.gsub(/(^| )!([A-Z][A-Za-z]+)/, '\1\2')
Chris@1296 312 # Revisions links
Chris@1296 313 text = text.gsub(/\[(\d+)\]/, 'r\1')
Chris@1296 314 # Ticket number re-writing
Chris@1296 315 text = text.gsub(/#(\d+)/) do |s|
Chris@1296 316 if $1.length < 10
Chris@1296 317 # TICKET_MAP[$1.to_i] ||= $1
Chris@1296 318 "\##{TICKET_MAP[$1.to_i] || $1}"
Chris@1296 319 else
Chris@1296 320 s
Chris@1296 321 end
Chris@1296 322 end
Chris@1296 323 # We would like to convert the Code highlighting too
Chris@1296 324 # This will go into the next line.
Chris@1296 325 shebang_line = false
Chris@1296 326 # Reguar expression for start of code
Chris@1296 327 pre_re = /\{\{\{/
Chris@1296 328 # Code hightlighing...
Chris@1296 329 shebang_re = /^\#\!([a-z]+)/
Chris@1296 330 # Regular expression for end of code
Chris@1296 331 pre_end_re = /\}\}\}/
Chris@1296 332
Chris@1296 333 # Go through the whole text..extract it line by line
Chris@1296 334 text = text.gsub(/^(.*)$/) do |line|
Chris@1296 335 m_pre = pre_re.match(line)
Chris@1296 336 if m_pre
Chris@1296 337 line = '<pre>'
Chris@1296 338 else
Chris@1296 339 m_sl = shebang_re.match(line)
Chris@1296 340 if m_sl
Chris@1296 341 shebang_line = true
Chris@1296 342 line = '<code class="' + m_sl[1] + '">'
Chris@1296 343 end
Chris@1296 344 m_pre_end = pre_end_re.match(line)
Chris@1296 345 if m_pre_end
Chris@1296 346 line = '</pre>'
Chris@1296 347 if shebang_line
Chris@1296 348 line = '</code>' + line
Chris@1296 349 end
Chris@1296 350 end
Chris@1296 351 end
Chris@1296 352 line
Chris@1296 353 end
Chris@1296 354
Chris@1296 355 # Highlighting
Chris@1296 356 text = text.gsub(/'''''([^\s])/, '_*\1')
Chris@1296 357 text = text.gsub(/([^\s])'''''/, '\1*_')
Chris@1296 358 text = text.gsub(/'''/, '*')
Chris@1296 359 text = text.gsub(/''/, '_')
Chris@1296 360 text = text.gsub(/__/, '+')
Chris@1296 361 text = text.gsub(/~~/, '-')
Chris@1296 362 text = text.gsub(/`/, '@')
Chris@1296 363 text = text.gsub(/,,/, '~')
Chris@1296 364 # Lists
Chris@1296 365 text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
Chris@1296 366
Chris@1296 367 text
Chris@1296 368 end
Chris@1296 369
Chris@1296 370 def self.migrate
Chris@1296 371 establish_connection
Chris@1296 372
Chris@1296 373 # Quick database test
Chris@1296 374 TracComponent.count
Chris@1296 375
Chris@1296 376 migrated_components = 0
Chris@1296 377 migrated_milestones = 0
Chris@1296 378 migrated_tickets = 0
Chris@1296 379 migrated_custom_values = 0
Chris@1296 380 migrated_ticket_attachments = 0
Chris@1296 381 migrated_wiki_edits = 0
Chris@1296 382 migrated_wiki_attachments = 0
Chris@1296 383
Chris@1296 384 #Wiki system initializing...
Chris@1296 385 @target_project.wiki.destroy if @target_project.wiki
Chris@1296 386 @target_project.reload
Chris@1296 387 wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
Chris@1296 388 wiki_edit_count = 0
Chris@1296 389
Chris@1296 390 # Components
Chris@1296 391 print "Migrating components"
Chris@1296 392 issues_category_map = {}
Chris@1296 393 TracComponent.find(:all).each do |component|
Chris@1296 394 print '.'
Chris@1296 395 STDOUT.flush
Chris@1296 396 c = IssueCategory.new :project => @target_project,
Chris@1296 397 :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
Chris@1296 398 next unless c.save
Chris@1296 399 issues_category_map[component.name] = c
Chris@1296 400 migrated_components += 1
Chris@1296 401 end
Chris@1296 402 puts
Chris@1296 403
Chris@1296 404 # Milestones
Chris@1296 405 print "Migrating milestones"
Chris@1296 406 version_map = {}
Chris@1296 407 TracMilestone.find(:all).each do |milestone|
Chris@1296 408 print '.'
Chris@1296 409 STDOUT.flush
Chris@1296 410 # First we try to find the wiki page...
Chris@1296 411 p = wiki.find_or_new_page(milestone.name.to_s)
Chris@1296 412 p.content = WikiContent.new(:page => p) if p.new_record?
Chris@1296 413 p.content.text = milestone.description.to_s
Chris@1296 414 p.content.author = find_or_create_user('trac')
Chris@1296 415 p.content.comments = 'Milestone'
Chris@1296 416 p.save
Chris@1296 417
Chris@1296 418 v = Version.new :project => @target_project,
Chris@1296 419 :name => encode(milestone.name[0, limit_for(Version, 'name')]),
Chris@1296 420 :description => nil,
Chris@1296 421 :wiki_page_title => milestone.name.to_s,
Chris@1296 422 :effective_date => milestone.completed
Chris@1296 423
Chris@1296 424 next unless v.save
Chris@1296 425 version_map[milestone.name] = v
Chris@1296 426 migrated_milestones += 1
Chris@1296 427 end
Chris@1296 428 puts
Chris@1296 429
Chris@1296 430 # Custom fields
Chris@1296 431 # TODO: read trac.ini instead
Chris@1296 432 print "Migrating custom fields"
Chris@1296 433 custom_field_map = {}
Chris@1296 434 TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
Chris@1296 435 print '.'
Chris@1296 436 STDOUT.flush
Chris@1296 437 # Redmine custom field name
Chris@1296 438 field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
Chris@1296 439 # Find if the custom already exists in Redmine
Chris@1296 440 f = IssueCustomField.find_by_name(field_name)
Chris@1296 441 # Or create a new one
Chris@1296 442 f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
Chris@1296 443 :field_format => 'string')
Chris@1296 444
Chris@1296 445 next if f.new_record?
Chris@1296 446 f.trackers = Tracker.find(:all)
Chris@1296 447 f.projects << @target_project
Chris@1296 448 custom_field_map[field.name] = f
Chris@1296 449 end
Chris@1296 450 puts
Chris@1296 451
Chris@1296 452 # Trac 'resolution' field as a Redmine custom field
Chris@1296 453 r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
Chris@1296 454 r = IssueCustomField.new(:name => 'Resolution',
Chris@1296 455 :field_format => 'list',
Chris@1296 456 :is_filter => true) if r.nil?
Chris@1296 457 r.trackers = Tracker.find(:all)
Chris@1296 458 r.projects << @target_project
Chris@1296 459 r.possible_values = (r.possible_values + %w(fixed invalid wontfix duplicate worksforme)).flatten.compact.uniq
Chris@1296 460 r.save!
Chris@1296 461 custom_field_map['resolution'] = r
Chris@1296 462
Chris@1296 463 # Tickets
Chris@1296 464 print "Migrating tickets"
Chris@1296 465 TracTicket.find_each(:batch_size => 200) do |ticket|
Chris@1296 466 print '.'
Chris@1296 467 STDOUT.flush
Chris@1296 468 i = Issue.new :project => @target_project,
Chris@1296 469 :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
Chris@1296 470 :description => convert_wiki_text(encode(ticket.description)),
Chris@1296 471 :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
Chris@1296 472 :created_on => ticket.time
Chris@1296 473 i.author = find_or_create_user(ticket.reporter)
Chris@1296 474 i.category = issues_category_map[ticket.component] unless ticket.component.blank?
Chris@1296 475 i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
Chris@1296 476 i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
Chris@1296 477 i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
Chris@1296 478 i.id = ticket.id unless Issue.exists?(ticket.id)
Chris@1296 479 next unless Time.fake(ticket.changetime) { i.save }
Chris@1296 480 TICKET_MAP[ticket.id] = i.id
Chris@1296 481 migrated_tickets += 1
Chris@1296 482
Chris@1296 483 # Owner
Chris@1296 484 unless ticket.owner.blank?
Chris@1296 485 i.assigned_to = find_or_create_user(ticket.owner, true)
Chris@1296 486 Time.fake(ticket.changetime) { i.save }
Chris@1296 487 end
Chris@1296 488
Chris@1296 489 # Comments and status/resolution changes
Chris@1296 490 ticket.ticket_changes.group_by(&:time).each do |time, changeset|
Chris@1296 491 status_change = changeset.select {|change| change.field == 'status'}.first
Chris@1296 492 resolution_change = changeset.select {|change| change.field == 'resolution'}.first
Chris@1296 493 comment_change = changeset.select {|change| change.field == 'comment'}.first
Chris@1296 494
Chris@1296 495 n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
Chris@1296 496 :created_on => time
Chris@1296 497 n.user = find_or_create_user(changeset.first.author)
Chris@1296 498 n.journalized = i
Chris@1296 499 if status_change &&
Chris@1296 500 STATUS_MAPPING[status_change.oldvalue] &&
Chris@1296 501 STATUS_MAPPING[status_change.newvalue] &&
Chris@1296 502 (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
Chris@1296 503 n.details << JournalDetail.new(:property => 'attr',
Chris@1296 504 :prop_key => 'status_id',
Chris@1296 505 :old_value => STATUS_MAPPING[status_change.oldvalue].id,
Chris@1296 506 :value => STATUS_MAPPING[status_change.newvalue].id)
Chris@1296 507 end
Chris@1296 508 if resolution_change
Chris@1296 509 n.details << JournalDetail.new(:property => 'cf',
Chris@1296 510 :prop_key => custom_field_map['resolution'].id,
Chris@1296 511 :old_value => resolution_change.oldvalue,
Chris@1296 512 :value => resolution_change.newvalue)
Chris@1296 513 end
Chris@1296 514 n.save unless n.details.empty? && n.notes.blank?
Chris@1296 515 end
Chris@1296 516
Chris@1296 517 # Attachments
Chris@1296 518 ticket.attachments.each do |attachment|
Chris@1296 519 next unless attachment.exist?
Chris@1296 520 attachment.open {
Chris@1296 521 a = Attachment.new :created_on => attachment.time
Chris@1296 522 a.file = attachment
Chris@1296 523 a.author = find_or_create_user(attachment.author)
Chris@1296 524 a.container = i
Chris@1296 525 a.description = attachment.description
Chris@1296 526 migrated_ticket_attachments += 1 if a.save
Chris@1296 527 }
Chris@1296 528 end
Chris@1296 529
Chris@1296 530 # Custom fields
Chris@1296 531 custom_values = ticket.customs.inject({}) do |h, custom|
Chris@1296 532 if custom_field = custom_field_map[custom.name]
Chris@1296 533 h[custom_field.id] = custom.value
Chris@1296 534 migrated_custom_values += 1
Chris@1296 535 end
Chris@1296 536 h
Chris@1296 537 end
Chris@1296 538 if custom_field_map['resolution'] && !ticket.resolution.blank?
Chris@1296 539 custom_values[custom_field_map['resolution'].id] = ticket.resolution
Chris@1296 540 end
Chris@1296 541 i.custom_field_values = custom_values
Chris@1296 542 i.save_custom_field_values
Chris@1296 543 end
Chris@1296 544
Chris@1296 545 # update issue id sequence if needed (postgresql)
Chris@1296 546 Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
Chris@1296 547 puts
Chris@1296 548
Chris@1296 549 # Wiki
Chris@1296 550 print "Migrating wiki"
Chris@1296 551 if wiki.save
Chris@1296 552 TracWikiPage.find(:all, :order => 'name, version').each do |page|
Chris@1296 553 # Do not migrate Trac manual wiki pages
Chris@1296 554 next if TRAC_WIKI_PAGES.include?(page.name)
Chris@1296 555 wiki_edit_count += 1
Chris@1296 556 print '.'
Chris@1296 557 STDOUT.flush
Chris@1296 558 p = wiki.find_or_new_page(page.name)
Chris@1296 559 p.content = WikiContent.new(:page => p) if p.new_record?
Chris@1296 560 p.content.text = page.text
Chris@1296 561 p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
Chris@1296 562 p.content.comments = page.comment
Chris@1296 563 Time.fake(page.time) { p.new_record? ? p.save : p.content.save }
Chris@1296 564
Chris@1296 565 next if p.content.new_record?
Chris@1296 566 migrated_wiki_edits += 1
Chris@1296 567
Chris@1296 568 # Attachments
Chris@1296 569 page.attachments.each do |attachment|
Chris@1296 570 next unless attachment.exist?
Chris@1296 571 next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
Chris@1296 572 attachment.open {
Chris@1296 573 a = Attachment.new :created_on => attachment.time
Chris@1296 574 a.file = attachment
Chris@1296 575 a.author = find_or_create_user(attachment.author)
Chris@1296 576 a.description = attachment.description
Chris@1296 577 a.container = p
Chris@1296 578 migrated_wiki_attachments += 1 if a.save
Chris@1296 579 }
Chris@1296 580 end
Chris@1296 581 end
Chris@1296 582
Chris@1296 583 wiki.reload
Chris@1296 584 wiki.pages.each do |page|
Chris@1296 585 page.content.text = convert_wiki_text(page.content.text)
Chris@1296 586 Time.fake(page.content.updated_on) { page.content.save }
Chris@1296 587 end
Chris@1296 588 end
Chris@1296 589 puts
Chris@1296 590
Chris@1296 591 puts
Chris@1296 592 puts "Components: #{migrated_components}/#{TracComponent.count}"
Chris@1296 593 puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
Chris@1296 594 puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
Chris@1296 595 puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => {:type => 'ticket'}).to_s
Chris@1296 596 puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
Chris@1296 597 puts "Wiki edits: #{migrated_wiki_edits}/#{wiki_edit_count}"
Chris@1296 598 puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => {:type => 'wiki'}).to_s
Chris@1296 599 end
Chris@1296 600
Chris@1296 601 def self.limit_for(klass, attribute)
Chris@1296 602 klass.columns_hash[attribute.to_s].limit
Chris@1296 603 end
Chris@1296 604
Chris@1296 605 def self.encoding(charset)
Chris@1296 606 @ic = Iconv.new('UTF-8', charset)
Chris@1296 607 rescue Iconv::InvalidEncoding
Chris@1296 608 puts "Invalid encoding!"
Chris@1296 609 return false
Chris@1296 610 end
Chris@1296 611
Chris@1296 612 def self.set_trac_directory(path)
Chris@1296 613 @@trac_directory = path
Chris@1296 614 raise "This directory doesn't exist!" unless File.directory?(path)
Chris@1296 615 raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
Chris@1296 616 @@trac_directory
Chris@1296 617 rescue Exception => e
Chris@1296 618 puts e
Chris@1296 619 return false
Chris@1296 620 end
Chris@1296 621
Chris@1296 622 def self.trac_directory
Chris@1296 623 @@trac_directory
Chris@1296 624 end
Chris@1296 625
Chris@1296 626 def self.set_trac_adapter(adapter)
Chris@1296 627 return false if adapter.blank?
Chris@1296 628 raise "Unknown adapter: #{adapter}!" unless %w(sqlite3 mysql postgresql).include?(adapter)
Chris@1296 629 # If adapter is sqlite or sqlite3, make sure that trac.db exists
Chris@1296 630 raise "#{trac_db_path} doesn't exist!" if %w(sqlite3).include?(adapter) && !File.exist?(trac_db_path)
Chris@1296 631 @@trac_adapter = adapter
Chris@1296 632 rescue Exception => e
Chris@1296 633 puts e
Chris@1296 634 return false
Chris@1296 635 end
Chris@1296 636
Chris@1296 637 def self.set_trac_db_host(host)
Chris@1296 638 return nil if host.blank?
Chris@1296 639 @@trac_db_host = host
Chris@1296 640 end
Chris@1296 641
Chris@1296 642 def self.set_trac_db_port(port)
Chris@1296 643 return nil if port.to_i == 0
Chris@1296 644 @@trac_db_port = port.to_i
Chris@1296 645 end
Chris@1296 646
Chris@1296 647 def self.set_trac_db_name(name)
Chris@1296 648 return nil if name.blank?
Chris@1296 649 @@trac_db_name = name
Chris@1296 650 end
Chris@1296 651
Chris@1296 652 def self.set_trac_db_username(username)
Chris@1296 653 @@trac_db_username = username
Chris@1296 654 end
Chris@1296 655
Chris@1296 656 def self.set_trac_db_password(password)
Chris@1296 657 @@trac_db_password = password
Chris@1296 658 end
Chris@1296 659
Chris@1296 660 def self.set_trac_db_schema(schema)
Chris@1296 661 @@trac_db_schema = schema
Chris@1296 662 end
Chris@1296 663
Chris@1296 664 mattr_reader :trac_directory, :trac_adapter, :trac_db_host, :trac_db_port, :trac_db_name, :trac_db_schema, :trac_db_username, :trac_db_password
Chris@1296 665
Chris@1296 666 def self.trac_db_path; "#{trac_directory}/db/trac.db" end
Chris@1296 667 def self.trac_attachments_directory; "#{trac_directory}/attachments" end
Chris@1296 668
Chris@1296 669 def self.target_project_identifier(identifier)
Chris@1296 670 project = Project.find_by_identifier(identifier)
Chris@1296 671 if !project
Chris@1296 672 # create the target project
Chris@1296 673 project = Project.new :name => identifier.humanize,
Chris@1296 674 :description => ''
Chris@1296 675 project.identifier = identifier
Chris@1296 676 puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
Chris@1296 677 # enable issues and wiki for the created project
Chris@1296 678 project.enabled_module_names = ['issue_tracking', 'wiki']
Chris@1296 679 else
Chris@1296 680 puts
Chris@1296 681 puts "This project already exists in your Redmine database."
Chris@1296 682 print "Are you sure you want to append data to this project ? [Y/n] "
Chris@1296 683 STDOUT.flush
Chris@1296 684 exit if STDIN.gets.match(/^n$/i)
Chris@1296 685 end
Chris@1296 686 project.trackers << TRACKER_BUG unless project.trackers.include?(TRACKER_BUG)
Chris@1296 687 project.trackers << TRACKER_FEATURE unless project.trackers.include?(TRACKER_FEATURE)
Chris@1296 688 @target_project = project.new_record? ? nil : project
Chris@1296 689 @target_project.reload
Chris@1296 690 end
Chris@1296 691
Chris@1296 692 def self.connection_params
Chris@1296 693 if trac_adapter == 'sqlite3'
Chris@1296 694 {:adapter => 'sqlite3',
Chris@1296 695 :database => trac_db_path}
Chris@1296 696 else
Chris@1296 697 {:adapter => trac_adapter,
Chris@1296 698 :database => trac_db_name,
Chris@1296 699 :host => trac_db_host,
Chris@1296 700 :port => trac_db_port,
Chris@1296 701 :username => trac_db_username,
Chris@1296 702 :password => trac_db_password,
Chris@1296 703 :schema_search_path => trac_db_schema
Chris@1296 704 }
Chris@1296 705 end
Chris@1296 706 end
Chris@1296 707
Chris@1296 708 def self.establish_connection
Chris@1296 709 constants.each do |const|
Chris@1296 710 klass = const_get(const)
Chris@1296 711 next unless klass.respond_to? 'establish_connection'
Chris@1296 712 klass.establish_connection connection_params
Chris@1296 713 end
Chris@1296 714 end
Chris@1296 715
Chris@1296 716 private
Chris@1296 717 def self.encode(text)
Chris@1296 718 @ic.iconv text
Chris@1296 719 rescue
Chris@1296 720 text
Chris@1296 721 end
Chris@1296 722 end
Chris@1296 723
Chris@1296 724 puts
Chris@1296 725 if Redmine::DefaultData::Loader.no_data?
Chris@1296 726 puts "Redmine configuration need to be loaded before importing data."
Chris@1296 727 puts "Please, run this first:"
Chris@1296 728 puts
Chris@1296 729 puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
Chris@1296 730 exit
Chris@1296 731 end
Chris@1296 732
Chris@1296 733 puts "WARNING: a new project will be added to Redmine during this process."
Chris@1296 734 print "Are you sure you want to continue ? [y/N] "
Chris@1296 735 STDOUT.flush
Chris@1296 736 break unless STDIN.gets.match(/^y$/i)
Chris@1296 737 puts
Chris@1296 738
Chris@1296 739 def prompt(text, options = {}, &block)
Chris@1296 740 default = options[:default] || ''
Chris@1296 741 while true
Chris@1296 742 print "#{text} [#{default}]: "
Chris@1296 743 STDOUT.flush
Chris@1296 744 value = STDIN.gets.chomp!
Chris@1296 745 value = default if value.blank?
Chris@1296 746 break if yield value
Chris@1296 747 end
Chris@1296 748 end
Chris@1296 749
Chris@1296 750 DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
Chris@1296 751
Chris@1296 752 prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory.strip}
Chris@1296 753 prompt('Trac database adapter (sqlite3, mysql2, postgresql)', :default => 'sqlite3') {|adapter| TracMigrate.set_trac_adapter adapter}
Chris@1296 754 unless %w(sqlite3).include?(TracMigrate.trac_adapter)
Chris@1296 755 prompt('Trac database host', :default => 'localhost') {|host| TracMigrate.set_trac_db_host host}
Chris@1296 756 prompt('Trac database port', :default => DEFAULT_PORTS[TracMigrate.trac_adapter]) {|port| TracMigrate.set_trac_db_port port}
Chris@1296 757 prompt('Trac database name') {|name| TracMigrate.set_trac_db_name name}
Chris@1296 758 prompt('Trac database schema', :default => 'public') {|schema| TracMigrate.set_trac_db_schema schema}
Chris@1296 759 prompt('Trac database username') {|username| TracMigrate.set_trac_db_username username}
Chris@1296 760 prompt('Trac database password') {|password| TracMigrate.set_trac_db_password password}
Chris@1296 761 end
Chris@1296 762 prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
Chris@1296 763 prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
Chris@1296 764 puts
Chris@1296 765
Chris@1296 766 # Turn off email notifications
Chris@1296 767 Setting.notified_events = []
Chris@1296 768
Chris@1296 769 TracMigrate.migrate
Chris@1296 770 end
Chris@1296 771 end
Chris@1296 772