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