annotate .svn/pristine/aa/aa0beb1e634d59699f850105fb8491055e09d5c3.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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