Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'active_record' Chris@909: require 'iconv' Chris@909: require 'pp' Chris@909: Chris@909: namespace :redmine do Chris@909: desc 'Trac migration script' Chris@909: task :migrate_from_trac => :environment do Chris@909: Chris@909: module TracMigrate Chris@909: TICKET_MAP = [] Chris@909: Chris@909: DEFAULT_STATUS = IssueStatus.default Chris@909: assigned_status = IssueStatus.find_by_position(2) Chris@909: resolved_status = IssueStatus.find_by_position(3) Chris@909: feedback_status = IssueStatus.find_by_position(4) Chris@909: closed_status = IssueStatus.find :first, :conditions => { :is_closed => true } Chris@909: STATUS_MAPPING = {'new' => DEFAULT_STATUS, Chris@909: 'reopened' => feedback_status, Chris@909: 'assigned' => assigned_status, Chris@909: 'closed' => closed_status Chris@909: } Chris@909: Chris@909: priorities = IssuePriority.all Chris@909: DEFAULT_PRIORITY = priorities[0] Chris@909: PRIORITY_MAPPING = {'lowest' => priorities[0], Chris@909: 'low' => priorities[0], Chris@909: 'normal' => priorities[1], Chris@909: 'high' => priorities[2], Chris@909: 'highest' => priorities[3], Chris@909: # --- Chris@909: 'trivial' => priorities[0], Chris@909: 'minor' => priorities[1], Chris@909: 'major' => priorities[2], Chris@909: 'critical' => priorities[3], Chris@909: 'blocker' => priorities[4] Chris@909: } Chris@909: Chris@909: TRACKER_BUG = Tracker.find_by_position(1) Chris@909: TRACKER_FEATURE = Tracker.find_by_position(2) Chris@909: DEFAULT_TRACKER = TRACKER_BUG Chris@909: TRACKER_MAPPING = {'defect' => TRACKER_BUG, Chris@909: 'enhancement' => TRACKER_FEATURE, Chris@909: 'task' => TRACKER_FEATURE, Chris@909: 'patch' =>TRACKER_FEATURE Chris@909: } Chris@909: Chris@909: roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC') Chris@909: manager_role = roles[0] Chris@909: developer_role = roles[1] Chris@909: DEFAULT_ROLE = roles.last Chris@909: ROLE_MAPPING = {'admin' => manager_role, Chris@909: 'developer' => developer_role Chris@909: } Chris@909: Chris@909: class ::Time Chris@909: class << self Chris@909: alias :real_now :now Chris@909: def now Chris@909: real_now - @fake_diff.to_i Chris@909: end Chris@909: def fake(time) Chris@909: @fake_diff = real_now - time Chris@909: res = yield Chris@909: @fake_diff = 0 Chris@909: res Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: class TracComponent < ActiveRecord::Base Chris@909: set_table_name :component Chris@909: end Chris@909: Chris@909: class TracMilestone < ActiveRecord::Base Chris@909: set_table_name :milestone Chris@909: # If this attribute is set a milestone has a defined target timepoint Chris@909: def due Chris@909: if read_attribute(:due) && read_attribute(:due) > 0 Chris@909: Time.at(read_attribute(:due)).to_date Chris@909: else Chris@909: nil Chris@909: end Chris@909: end Chris@909: # This is the real timepoint at which the milestone has finished. Chris@909: def completed Chris@909: if read_attribute(:completed) && read_attribute(:completed) > 0 Chris@909: Time.at(read_attribute(:completed)).to_date Chris@909: else Chris@909: nil Chris@909: end Chris@909: end Chris@909: Chris@909: def description Chris@909: # Attribute is named descr in Trac v0.8.x Chris@909: has_attribute?(:descr) ? read_attribute(:descr) : read_attribute(:description) Chris@909: end Chris@909: end Chris@909: Chris@909: class TracTicketCustom < ActiveRecord::Base Chris@909: set_table_name :ticket_custom Chris@909: end Chris@909: Chris@909: class TracAttachment < ActiveRecord::Base Chris@909: set_table_name :attachment Chris@909: set_inheritance_column :none Chris@909: Chris@909: def time; Time.at(read_attribute(:time)) end Chris@909: Chris@909: def original_filename Chris@909: filename Chris@909: end Chris@909: Chris@909: def content_type Chris@909: '' Chris@909: end Chris@909: Chris@909: def exist? Chris@909: File.file? trac_fullpath Chris@909: end Chris@909: Chris@909: def open Chris@909: File.open("#{trac_fullpath}", 'rb') {|f| Chris@909: @file = f Chris@909: yield self Chris@909: } Chris@909: end Chris@909: Chris@909: def read(*args) Chris@909: @file.read(*args) Chris@909: end Chris@909: Chris@909: def description Chris@909: read_attribute(:description).to_s.slice(0,255) Chris@909: end Chris@909: Chris@909: private Chris@909: def trac_fullpath Chris@909: attachment_type = read_attribute(:type) Chris@909: trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } Chris@909: "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}" Chris@909: end Chris@909: end Chris@909: Chris@909: class TracTicket < ActiveRecord::Base Chris@909: set_table_name :ticket Chris@909: set_inheritance_column :none Chris@909: Chris@909: # ticket changes: only migrate status changes and comments Chris@909: has_many :changes, :class_name => "TracTicketChange", :foreign_key => :ticket Chris@909: has_many :attachments, :class_name => "TracAttachment", Chris@909: :finder_sql => "SELECT DISTINCT attachment.* FROM #{TracMigrate::TracAttachment.table_name}" + Chris@909: " WHERE #{TracMigrate::TracAttachment.table_name}.type = 'ticket'" + Chris@909: ' AND #{TracMigrate::TracAttachment.table_name}.id = \'#{TracMigrate::TracAttachment.connection.quote_string(id.to_s)}\'' Chris@909: has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket Chris@909: Chris@909: def ticket_type Chris@909: read_attribute(:type) Chris@909: end Chris@909: Chris@909: def summary Chris@909: read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) Chris@909: end Chris@909: Chris@909: def description Chris@909: read_attribute(:description).blank? ? summary : read_attribute(:description) Chris@909: end Chris@909: Chris@909: def time; Time.at(read_attribute(:time)) end Chris@909: def changetime; Time.at(read_attribute(:changetime)) end Chris@909: end Chris@909: Chris@909: class TracTicketChange < ActiveRecord::Base Chris@909: set_table_name :ticket_change Chris@909: Chris@909: def time; Time.at(read_attribute(:time)) end Chris@909: end Chris@909: Chris@909: TRAC_WIKI_PAGES = %w(InterMapTxt InterTrac InterWiki RecentChanges SandBox TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \ Chris@909: TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization \ Chris@909: TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery \ Chris@909: TracReports TracRevisionLog TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets \ Chris@909: TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting \ Chris@909: WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks \ Chris@909: CamelCase TitleIndex) Chris@909: Chris@909: class TracWikiPage < ActiveRecord::Base Chris@909: set_table_name :wiki Chris@909: set_primary_key :name Chris@909: Chris@909: has_many :attachments, :class_name => "TracAttachment", Chris@909: :finder_sql => "SELECT DISTINCT attachment.* FROM #{TracMigrate::TracAttachment.table_name}" + Chris@909: " WHERE #{TracMigrate::TracAttachment.table_name}.type = 'wiki'" + Chris@909: ' AND #{TracMigrate::TracAttachment.table_name}.id = \'#{TracMigrate::TracAttachment.connection.quote_string(id.to_s)}\'' Chris@909: Chris@909: def self.columns Chris@909: # Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0) Chris@909: super.select {|column| column.name.to_s != 'readonly'} Chris@909: end Chris@909: Chris@909: def time; Time.at(read_attribute(:time)) end Chris@909: end Chris@909: Chris@909: class TracPermission < ActiveRecord::Base Chris@909: set_table_name :permission Chris@909: end Chris@909: Chris@909: class TracSessionAttribute < ActiveRecord::Base Chris@909: set_table_name :session_attribute Chris@909: end Chris@909: Chris@909: def self.find_or_create_user(username, project_member = false) Chris@909: return User.anonymous if username.blank? Chris@909: Chris@909: u = User.find_by_login(username) Chris@909: if !u Chris@909: # Create a new user if not found Chris@909: mail = username[0,limit_for(User, 'mail')] Chris@909: if mail_attr = TracSessionAttribute.find_by_sid_and_name(username, 'email') Chris@909: mail = mail_attr.value Chris@909: end Chris@909: mail = "#{mail}@foo.bar" unless mail.include?("@") Chris@909: Chris@909: name = username Chris@909: if name_attr = TracSessionAttribute.find_by_sid_and_name(username, 'name') Chris@909: name = name_attr.value Chris@909: end Chris@909: name =~ (/(.*)(\s+\w+)?/) Chris@909: fn = $1.strip Chris@909: ln = ($2 || '-').strip Chris@909: Chris@909: u = User.new :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-'), Chris@909: :firstname => fn[0, limit_for(User, 'firstname')], Chris@909: :lastname => ln[0, limit_for(User, 'lastname')] Chris@909: Chris@909: u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') Chris@909: u.password = 'trac' Chris@909: u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') Chris@909: # finally, a default user is used if the new user is not valid Chris@909: u = User.find(:first) unless u.save Chris@909: end Chris@909: # Make sure he is a member of the project Chris@909: if project_member && !u.member_of?(@target_project) Chris@909: role = DEFAULT_ROLE Chris@909: if u.admin Chris@909: role = ROLE_MAPPING['admin'] Chris@909: elsif TracPermission.find_by_username_and_action(username, 'developer') Chris@909: role = ROLE_MAPPING['developer'] Chris@909: end Chris@909: Member.create(:user => u, :project => @target_project, :roles => [role]) Chris@909: u.reload Chris@909: end Chris@909: u Chris@909: end Chris@909: Chris@909: # Basic wiki syntax conversion Chris@909: def self.convert_wiki_text(text) Chris@909: # Titles Chris@909: text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"} Chris@909: # External Links Chris@909: text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"} Chris@909: # Ticket links: Chris@909: # [ticket:234 Text],[ticket:234 This is a test] Chris@909: text = text.gsub(/\[ticket\:([^\ ]+)\ (.+?)\]/, '"\2":/issues/show/\1') Chris@909: # ticket:1234 Chris@909: # #1 is working cause Redmine uses the same syntax. Chris@909: text = text.gsub(/ticket\:([^\ ]+)/, '#\1') Chris@909: # Milestone links: Chris@909: # [milestone:"0.1.0 Mercury" Milestone 0.1.0 (Mercury)] Chris@909: # The text "Milestone 0.1.0 (Mercury)" is not converted, Chris@909: # cause Redmine's wiki does not support this. Chris@909: text = text.gsub(/\[milestone\:\"([^\"]+)\"\ (.+?)\]/, 'version:"\1"') Chris@909: # [milestone:"0.1.0 Mercury"] Chris@909: text = text.gsub(/\[milestone\:\"([^\"]+)\"\]/, 'version:"\1"') Chris@909: text = text.gsub(/milestone\:\"([^\"]+)\"/, 'version:"\1"') Chris@909: # milestone:0.1.0 Chris@909: text = text.gsub(/\[milestone\:([^\ ]+)\]/, 'version:\1') Chris@909: text = text.gsub(/milestone\:([^\ ]+)/, 'version:\1') Chris@909: # Internal Links Chris@909: text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below Chris@909: text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} Chris@909: text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} Chris@909: text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} Chris@909: text = text.gsub(/\[wiki:([^\s\]]+)\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"} Chris@909: text = text.gsub(/\[wiki:([^\s\]]+)\s(.*)\]/) {|s| "[[#{$1.delete(',./?;|:')}|#{$2.delete(',./?;|:')}]]"} Chris@909: Chris@909: # Links to pages UsingJustWikiCaps Chris@909: text = text.gsub(/([^!]|^)(^| )([A-Z][a-z]+[A-Z][a-zA-Z]+)/, '\\1\\2[[\3]]') Chris@909: # Normalize things that were supposed to not be links Chris@909: # like !NotALink Chris@909: text = text.gsub(/(^| )!([A-Z][A-Za-z]+)/, '\1\2') Chris@909: # Revisions links Chris@909: text = text.gsub(/\[(\d+)\]/, 'r\1') Chris@909: # Ticket number re-writing Chris@909: text = text.gsub(/#(\d+)/) do |s| Chris@909: if $1.length < 10 Chris@909: # TICKET_MAP[$1.to_i] ||= $1 Chris@909: "\##{TICKET_MAP[$1.to_i] || $1}" Chris@909: else Chris@909: s Chris@909: end Chris@909: end Chris@909: # We would like to convert the Code highlighting too Chris@909: # This will go into the next line. Chris@909: shebang_line = false Chris@909: # Reguar expression for start of code Chris@909: pre_re = /\{\{\{/ Chris@909: # Code hightlighing... Chris@909: shebang_re = /^\#\!([a-z]+)/ Chris@909: # Regular expression for end of code Chris@909: pre_end_re = /\}\}\}/ Chris@909: Chris@909: # Go through the whole text..extract it line by line Chris@909: text = text.gsub(/^(.*)$/) do |line| Chris@909: m_pre = pre_re.match(line) Chris@909: if m_pre Chris@909: line = '
'
Chris@909: else
Chris@909: m_sl = shebang_re.match(line)
Chris@909: if m_sl
Chris@909: shebang_line = true
Chris@909: line = ''
Chris@909: end
Chris@909: m_pre_end = pre_end_re.match(line)
Chris@909: if m_pre_end
Chris@909: line = '
'
Chris@909: if shebang_line
Chris@909: line = '' + line
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: line
Chris@909: end
Chris@909:
Chris@909: # Highlighting
Chris@909: text = text.gsub(/'''''([^\s])/, '_*\1')
Chris@909: text = text.gsub(/([^\s])'''''/, '\1*_')
Chris@909: text = text.gsub(/'''/, '*')
Chris@909: text = text.gsub(/''/, '_')
Chris@909: text = text.gsub(/__/, '+')
Chris@909: text = text.gsub(/~~/, '-')
Chris@909: text = text.gsub(/`/, '@')
Chris@909: text = text.gsub(/,,/, '~')
Chris@909: # Lists
Chris@909: text = text.gsub(/^([ ]+)\* /) {|s| '*' * $1.length + " "}
Chris@909:
Chris@909: text
Chris@909: end
Chris@909:
Chris@909: def self.migrate
Chris@909: establish_connection
Chris@909:
Chris@909: # Quick database test
Chris@909: TracComponent.count
Chris@909:
Chris@909: migrated_components = 0
Chris@909: migrated_milestones = 0
Chris@909: migrated_tickets = 0
Chris@909: migrated_custom_values = 0
Chris@909: migrated_ticket_attachments = 0
Chris@909: migrated_wiki_edits = 0
Chris@909: migrated_wiki_attachments = 0
Chris@909:
Chris@909: #Wiki system initializing...
Chris@909: @target_project.wiki.destroy if @target_project.wiki
Chris@909: @target_project.reload
Chris@909: wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart')
Chris@909: wiki_edit_count = 0
Chris@909:
Chris@909: # Components
Chris@909: print "Migrating components"
Chris@909: issues_category_map = {}
Chris@909: TracComponent.find(:all).each do |component|
Chris@909: print '.'
Chris@909: STDOUT.flush
Chris@909: c = IssueCategory.new :project => @target_project,
Chris@909: :name => encode(component.name[0, limit_for(IssueCategory, 'name')])
Chris@909: next unless c.save
Chris@909: issues_category_map[component.name] = c
Chris@909: migrated_components += 1
Chris@909: end
Chris@909: puts
Chris@909:
Chris@909: # Milestones
Chris@909: print "Migrating milestones"
Chris@909: version_map = {}
Chris@909: TracMilestone.find(:all).each do |milestone|
Chris@909: print '.'
Chris@909: STDOUT.flush
Chris@909: # First we try to find the wiki page...
Chris@909: p = wiki.find_or_new_page(milestone.name.to_s)
Chris@909: p.content = WikiContent.new(:page => p) if p.new_record?
Chris@909: p.content.text = milestone.description.to_s
Chris@909: p.content.author = find_or_create_user('trac')
Chris@909: p.content.comments = 'Milestone'
Chris@909: p.save
Chris@909:
Chris@909: v = Version.new :project => @target_project,
Chris@909: :name => encode(milestone.name[0, limit_for(Version, 'name')]),
Chris@909: :description => nil,
Chris@909: :wiki_page_title => milestone.name.to_s,
Chris@909: :effective_date => milestone.completed
Chris@909:
Chris@909: next unless v.save
Chris@909: version_map[milestone.name] = v
Chris@909: migrated_milestones += 1
Chris@909: end
Chris@909: puts
Chris@909:
Chris@909: # Custom fields
Chris@909: # TODO: read trac.ini instead
Chris@909: print "Migrating custom fields"
Chris@909: custom_field_map = {}
Chris@909: TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field|
Chris@909: print '.'
Chris@909: STDOUT.flush
Chris@909: # Redmine custom field name
Chris@909: field_name = encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize
Chris@909: # Find if the custom already exists in Redmine
Chris@909: f = IssueCustomField.find_by_name(field_name)
Chris@909: # Or create a new one
Chris@909: f ||= IssueCustomField.create(:name => encode(field.name[0, limit_for(IssueCustomField, 'name')]).humanize,
Chris@909: :field_format => 'string')
Chris@909:
Chris@909: next if f.new_record?
Chris@909: f.trackers = Tracker.find(:all)
Chris@909: f.projects << @target_project
Chris@909: custom_field_map[field.name] = f
Chris@909: end
Chris@909: puts
Chris@909:
Chris@909: # Trac 'resolution' field as a Redmine custom field
Chris@909: r = IssueCustomField.find(:first, :conditions => { :name => "Resolution" })
Chris@909: r = IssueCustomField.new(:name => 'Resolution',
Chris@909: :field_format => 'list',
Chris@909: :is_filter => true) if r.nil?
Chris@909: r.trackers = Tracker.find(:all)
Chris@909: r.projects << @target_project
Chris@909: r.possible_values = (r.possible_values + %w(fixed invalid wontfix duplicate worksforme)).flatten.compact.uniq
Chris@909: r.save!
Chris@909: custom_field_map['resolution'] = r
Chris@909:
Chris@909: # Tickets
Chris@909: print "Migrating tickets"
Chris@909: TracTicket.find_each(:batch_size => 200) do |ticket|
Chris@909: print '.'
Chris@909: STDOUT.flush
Chris@909: i = Issue.new :project => @target_project,
Chris@909: :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]),
Chris@909: :description => convert_wiki_text(encode(ticket.description)),
Chris@909: :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY,
Chris@909: :created_on => ticket.time
Chris@909: i.author = find_or_create_user(ticket.reporter)
Chris@909: i.category = issues_category_map[ticket.component] unless ticket.component.blank?
Chris@909: i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank?
Chris@909: i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS
Chris@909: i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER
Chris@909: i.id = ticket.id unless Issue.exists?(ticket.id)
Chris@909: next unless Time.fake(ticket.changetime) { i.save }
Chris@909: TICKET_MAP[ticket.id] = i.id
Chris@909: migrated_tickets += 1
Chris@909:
Chris@909: # Owner
Chris@909: unless ticket.owner.blank?
Chris@909: i.assigned_to = find_or_create_user(ticket.owner, true)
Chris@909: Time.fake(ticket.changetime) { i.save }
Chris@909: end
Chris@909:
Chris@909: # Comments and status/resolution changes
Chris@909: ticket.changes.group_by(&:time).each do |time, changeset|
Chris@909: status_change = changeset.select {|change| change.field == 'status'}.first
Chris@909: resolution_change = changeset.select {|change| change.field == 'resolution'}.first
Chris@909: comment_change = changeset.select {|change| change.field == 'comment'}.first
Chris@909:
Chris@909: n = Journal.new :notes => (comment_change ? convert_wiki_text(encode(comment_change.newvalue)) : ''),
Chris@909: :created_on => time
Chris@909: n.user = find_or_create_user(changeset.first.author)
Chris@909: n.journalized = i
Chris@909: if status_change &&
Chris@909: STATUS_MAPPING[status_change.oldvalue] &&
Chris@909: STATUS_MAPPING[status_change.newvalue] &&
Chris@909: (STATUS_MAPPING[status_change.oldvalue] != STATUS_MAPPING[status_change.newvalue])
Chris@909: n.details << JournalDetail.new(:property => 'attr',
Chris@909: :prop_key => 'status_id',
Chris@909: :old_value => STATUS_MAPPING[status_change.oldvalue].id,
Chris@909: :value => STATUS_MAPPING[status_change.newvalue].id)
Chris@909: end
Chris@909: if resolution_change
Chris@909: n.details << JournalDetail.new(:property => 'cf',
Chris@909: :prop_key => custom_field_map['resolution'].id,
Chris@909: :old_value => resolution_change.oldvalue,
Chris@909: :value => resolution_change.newvalue)
Chris@909: end
Chris@909: n.save unless n.details.empty? && n.notes.blank?
Chris@909: end
Chris@909:
Chris@909: # Attachments
Chris@909: ticket.attachments.each do |attachment|
Chris@909: next unless attachment.exist?
Chris@909: attachment.open {
Chris@909: a = Attachment.new :created_on => attachment.time
Chris@909: a.file = attachment
Chris@909: a.author = find_or_create_user(attachment.author)
Chris@909: a.container = i
Chris@909: a.description = attachment.description
Chris@909: migrated_ticket_attachments += 1 if a.save
Chris@909: }
Chris@909: end
Chris@909:
Chris@909: # Custom fields
Chris@909: custom_values = ticket.customs.inject({}) do |h, custom|
Chris@909: if custom_field = custom_field_map[custom.name]
Chris@909: h[custom_field.id] = custom.value
Chris@909: migrated_custom_values += 1
Chris@909: end
Chris@909: h
Chris@909: end
Chris@909: if custom_field_map['resolution'] && !ticket.resolution.blank?
Chris@909: custom_values[custom_field_map['resolution'].id] = ticket.resolution
Chris@909: end
Chris@909: i.custom_field_values = custom_values
Chris@909: i.save_custom_field_values
Chris@909: end
Chris@909:
Chris@909: # update issue id sequence if needed (postgresql)
Chris@909: Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
Chris@909: puts
Chris@909:
Chris@909: # Wiki
Chris@909: print "Migrating wiki"
Chris@909: if wiki.save
Chris@909: TracWikiPage.find(:all, :order => 'name, version').each do |page|
Chris@909: # Do not migrate Trac manual wiki pages
Chris@909: next if TRAC_WIKI_PAGES.include?(page.name)
Chris@909: wiki_edit_count += 1
Chris@909: print '.'
Chris@909: STDOUT.flush
Chris@909: p = wiki.find_or_new_page(page.name)
Chris@909: p.content = WikiContent.new(:page => p) if p.new_record?
Chris@909: p.content.text = page.text
Chris@909: p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac'
Chris@909: p.content.comments = page.comment
Chris@909: Time.fake(page.time) { p.new_record? ? p.save : p.content.save }
Chris@909:
Chris@909: next if p.content.new_record?
Chris@909: migrated_wiki_edits += 1
Chris@909:
Chris@909: # Attachments
Chris@909: page.attachments.each do |attachment|
Chris@909: next unless attachment.exist?
Chris@909: next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
Chris@909: attachment.open {
Chris@909: a = Attachment.new :created_on => attachment.time
Chris@909: a.file = attachment
Chris@909: a.author = find_or_create_user(attachment.author)
Chris@909: a.description = attachment.description
Chris@909: a.container = p
Chris@909: migrated_wiki_attachments += 1 if a.save
Chris@909: }
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: wiki.reload
Chris@909: wiki.pages.each do |page|
Chris@909: page.content.text = convert_wiki_text(page.content.text)
Chris@909: Time.fake(page.content.updated_on) { page.content.save }
Chris@909: end
Chris@909: end
Chris@909: puts
Chris@909:
Chris@909: puts
Chris@909: puts "Components: #{migrated_components}/#{TracComponent.count}"
Chris@909: puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
Chris@909: puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
Chris@909: puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => {:type => 'ticket'}).to_s
Chris@909: puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
Chris@909: puts "Wiki edits: #{migrated_wiki_edits}/#{wiki_edit_count}"
Chris@909: puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => {:type => 'wiki'}).to_s
Chris@909: end
Chris@909:
Chris@909: def self.limit_for(klass, attribute)
Chris@909: klass.columns_hash[attribute.to_s].limit
Chris@909: end
Chris@909:
Chris@909: def self.encoding(charset)
Chris@909: @ic = Iconv.new('UTF-8', charset)
Chris@909: rescue Iconv::InvalidEncoding
Chris@909: puts "Invalid encoding!"
Chris@909: return false
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_directory(path)
Chris@909: @@trac_directory = path
Chris@909: raise "This directory doesn't exist!" unless File.directory?(path)
Chris@909: raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory)
Chris@909: @@trac_directory
Chris@909: rescue Exception => e
Chris@909: puts e
Chris@909: return false
Chris@909: end
Chris@909:
Chris@909: def self.trac_directory
Chris@909: @@trac_directory
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_adapter(adapter)
Chris@909: return false if adapter.blank?
Chris@909: raise "Unknown adapter: #{adapter}!" unless %w(sqlite sqlite3 mysql postgresql).include?(adapter)
Chris@909: # If adapter is sqlite or sqlite3, make sure that trac.db exists
Chris@909: raise "#{trac_db_path} doesn't exist!" if %w(sqlite sqlite3).include?(adapter) && !File.exist?(trac_db_path)
Chris@909: @@trac_adapter = adapter
Chris@909: rescue Exception => e
Chris@909: puts e
Chris@909: return false
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_host(host)
Chris@909: return nil if host.blank?
Chris@909: @@trac_db_host = host
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_port(port)
Chris@909: return nil if port.to_i == 0
Chris@909: @@trac_db_port = port.to_i
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_name(name)
Chris@909: return nil if name.blank?
Chris@909: @@trac_db_name = name
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_username(username)
Chris@909: @@trac_db_username = username
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_password(password)
Chris@909: @@trac_db_password = password
Chris@909: end
Chris@909:
Chris@909: def self.set_trac_db_schema(schema)
Chris@909: @@trac_db_schema = schema
Chris@909: end
Chris@909:
Chris@909: 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@909:
Chris@909: def self.trac_db_path; "#{trac_directory}/db/trac.db" end
Chris@909: def self.trac_attachments_directory; "#{trac_directory}/attachments" end
Chris@909:
Chris@909: def self.target_project_identifier(identifier)
Chris@909: project = Project.find_by_identifier(identifier)
Chris@909: if !project
Chris@909: # create the target project
Chris@909: project = Project.new :name => identifier.humanize,
Chris@909: :description => ''
Chris@909: project.identifier = identifier
Chris@909: puts "Unable to create a project with identifier '#{identifier}'!" unless project.save
Chris@909: # enable issues and wiki for the created project
Chris@909: project.enabled_module_names = ['issue_tracking', 'wiki']
Chris@909: else
Chris@909: puts
Chris@909: puts "This project already exists in your Redmine database."
Chris@909: print "Are you sure you want to append data to this project ? [Y/n] "
Chris@909: STDOUT.flush
Chris@909: exit if STDIN.gets.match(/^n$/i)
Chris@909: end
Chris@909: project.trackers << TRACKER_BUG unless project.trackers.include?(TRACKER_BUG)
Chris@909: project.trackers << TRACKER_FEATURE unless project.trackers.include?(TRACKER_FEATURE)
Chris@909: @target_project = project.new_record? ? nil : project
Chris@909: @target_project.reload
Chris@909: end
Chris@909:
Chris@909: def self.connection_params
Chris@909: if %w(sqlite sqlite3).include?(trac_adapter)
Chris@909: {:adapter => trac_adapter,
Chris@909: :database => trac_db_path}
Chris@909: else
Chris@909: {:adapter => trac_adapter,
Chris@909: :database => trac_db_name,
Chris@909: :host => trac_db_host,
Chris@909: :port => trac_db_port,
Chris@909: :username => trac_db_username,
Chris@909: :password => trac_db_password,
Chris@909: :schema_search_path => trac_db_schema
Chris@909: }
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: def self.establish_connection
Chris@909: constants.each do |const|
Chris@909: klass = const_get(const)
Chris@909: next unless klass.respond_to? 'establish_connection'
Chris@909: klass.establish_connection connection_params
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: private
Chris@909: def self.encode(text)
Chris@909: @ic.iconv text
Chris@909: rescue
Chris@909: text
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: puts
Chris@909: if Redmine::DefaultData::Loader.no_data?
Chris@909: puts "Redmine configuration need to be loaded before importing data."
Chris@909: puts "Please, run this first:"
Chris@909: puts
Chris@909: puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
Chris@909: exit
Chris@909: end
Chris@909:
Chris@909: puts "WARNING: a new project will be added to Redmine during this process."
Chris@909: print "Are you sure you want to continue ? [y/N] "
Chris@909: STDOUT.flush
Chris@909: break unless STDIN.gets.match(/^y$/i)
Chris@909: puts
Chris@909:
Chris@909: def prompt(text, options = {}, &block)
Chris@909: default = options[:default] || ''
Chris@909: while true
Chris@909: print "#{text} [#{default}]: "
Chris@909: STDOUT.flush
Chris@909: value = STDIN.gets.chomp!
Chris@909: value = default if value.blank?
Chris@909: break if yield value
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
Chris@909:
Chris@909: prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory.strip}
Chris@909: prompt('Trac database adapter (sqlite, sqlite3, mysql, postgresql)', :default => 'sqlite') {|adapter| TracMigrate.set_trac_adapter adapter}
Chris@909: unless %w(sqlite sqlite3).include?(TracMigrate.trac_adapter)
Chris@909: prompt('Trac database host', :default => 'localhost') {|host| TracMigrate.set_trac_db_host host}
Chris@909: prompt('Trac database port', :default => DEFAULT_PORTS[TracMigrate.trac_adapter]) {|port| TracMigrate.set_trac_db_port port}
Chris@909: prompt('Trac database name') {|name| TracMigrate.set_trac_db_name name}
Chris@909: prompt('Trac database schema', :default => 'public') {|schema| TracMigrate.set_trac_db_schema schema}
Chris@909: prompt('Trac database username') {|username| TracMigrate.set_trac_db_username username}
Chris@909: prompt('Trac database password') {|password| TracMigrate.set_trac_db_password password}
Chris@909: end
Chris@909: prompt('Trac database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding}
Chris@909: prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier}
Chris@909: puts
Chris@909:
Chris@909: # Turn off email notifications
Chris@909: Setting.notified_events = []
Chris@909:
Chris@909: TracMigrate.migrate
Chris@909: end
Chris@909: end
Chris@909: