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