Patch #2928 ยป migrate_from_scarab_patch.diff
| lib/tasks/migrate_from_scarab.rake (revision 0) | ||
|---|---|---|
|
require 'active_record'
|
||
|
require 'iconv'
|
||
|
require 'pp'
|
||
|
$DBG = false
|
||
|
$stdout.sync = true
|
||
|
ActiveRecord::Base.record_timestamps = false
|
||
|
namespace :redmine do
|
||
|
desc 'Scarab migration script'
|
||
|
task :migrate_from_scarab => :environment do
|
||
|
|
||
|
module ScarabMigrate
|
||
|
TICKET_MAP = []
|
||
|
DEFAULT_STATUS = IssueStatus.default
|
||
|
assigned_status = IssueStatus.find_by_position(2)
|
||
|
resolved_status = IssueStatus.find_by_position(3)
|
||
|
feedback_status = IssueStatus.find_by_position(4)
|
||
|
closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
|
||
|
STATUS_MAPPING = {'New' => DEFAULT_STATUS,
|
||
|
'Reopened' => feedback_status,
|
||
|
'Assigned' => assigned_status,
|
||
|
'Resolved' => resolved_status,
|
||
|
'Unconfirmed' => feedback_status,
|
||
|
'Verified' => closed_status,
|
||
|
'Closed' => closed_status,
|
||
|
'Fixed' => closed_status,
|
||
|
'Invalid' => closed_status,
|
||
|
'Wontfix' => closed_status,
|
||
|
'Later' => feedback_status,
|
||
|
'Remind' => feedback_status,
|
||
|
'Duplicate' => closed_status,
|
||
|
'Worksforme' => closed_status,
|
||
|
'Moved' => closed_status,
|
||
|
'Init' => DEFAULT_STATUS,
|
||
|
'Assigned' => assigned_status,
|
||
|
'Assigend' => assigned_status,
|
||
|
'Missing Data' => feedback_status
|
||
|
}
|
||
|
|
||
|
priorities = Enumeration.get_values('IPRI')
|
||
|
DEFAULT_PRIORITY = priorities[0]
|
||
|
PRIORITY_MAPPING = {'lowest' => priorities[0],
|
||
|
'low' => priorities[0],
|
||
|
'normal' => priorities[1],
|
||
|
'high' => priorities[2],
|
||
|
'highest' => priorities[3],
|
||
|
# ---
|
||
|
'trivial' => priorities[0],
|
||
|
'minor' => priorities[1],
|
||
|
'major' => priorities[2],
|
||
|
'critical' => priorities[3],
|
||
|
'blocker' => priorities[4],
|
||
|
# --- 7
|
||
|
'Low' => priorities[0],
|
||
|
'Undecided' => priorities[0],
|
||
|
'Medium' => priorities[1],
|
||
|
'High' => priorities[2],
|
||
|
# --- 9
|
||
|
'Blocker' => priorities[4],
|
||
|
'Critical' => priorities[3],
|
||
|
'Major' => priorities[2],
|
||
|
'Normal' => priorities[1],
|
||
|
'Minor' => priorities[0],
|
||
|
'Trivial' => priorities[0],
|
||
|
'Enhancement' => priorities[0],
|
||
|
'Cosmetic' => priorities[0],
|
||
|
'Serious' => priorities[3],
|
||
|
'Undecided' => priorities[0]
|
||
|
}
|
||
|
|
||
|
|
||
|
TRACKER_BUG = Tracker.find_by_position(1)
|
||
|
TRACKER_FEATURE = Tracker.find_by_position(2)
|
||
|
DEFAULT_TRACKER = TRACKER_BUG
|
||
|
TRACKER_MAPPING = {'defect' => TRACKER_BUG,
|
||
|
'Enhancement' => TRACKER_FEATURE,
|
||
|
'task' => TRACKER_FEATURE,
|
||
|
'patch' =>TRACKER_FEATURE
|
||
|
}
|
||
|
|
||
|
roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
|
||
|
manager_role = roles[0]
|
||
|
developer_role = roles[1]
|
||
|
reporter_role = roles[2]
|
||
|
DEFAULT_ROLE = roles.last
|
||
|
ROLE_MAPPING = {'admin' => manager_role,
|
||
|
'developer' => developer_role,
|
||
|
'reporter' => reporter_role
|
||
|
}
|
||
|
ATTRIBUTE_NAMES = {
|
||
|
:NullAttribute => 0,
|
||
|
:Description => 1,
|
||
|
:AssignedTo => 2,
|
||
|
:Status => 3,
|
||
|
:Resolution => 4,
|
||
|
:Platform => 5,
|
||
|
:OperatingSystem => 6,
|
||
|
:Priority => 7,
|
||
|
:Vote => 8,
|
||
|
:Severity => 9,
|
||
|
:AssignedCC => 13,
|
||
|
:Summary => 11,
|
||
|
:FunctionalArea => 12,
|
||
|
:DueTime => 10022,
|
||
|
:Version => 10000,
|
||
|
:Projekt_type => 10020 # Internes / Externes Projekt
|
||
|
}
|
||
|
ATTACHMENT_TYPES = {
|
||
|
:ATTACHMENT => 1,
|
||
|
:COMMENT => 2,
|
||
|
:URL => 3,
|
||
|
:MODIFICATION => 4
|
||
|
}
|
||
|
class ScarabProject < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_MODULE
|
||
|
set_primary_key :MODULE_ID
|
||
|
belongs_to :owner,
|
||
|
:class_name => "ScarabUser",
|
||
|
:foreign_key => :OWNER_ID
|
||
|
belongs_to :parent,
|
||
|
:class_name => "ScarabProject",
|
||
|
:foreign_key => :PARENT_ID
|
||
|
def id
|
||
|
read_attribute(:MODULE_ID)
|
||
|
end
|
||
|
|
||
|
def name
|
||
|
read_attribute(:MODULE_NAME)[0..29]
|
||
|
end
|
||
|
|
||
|
def identifier
|
||
|
prefix = ""
|
||
|
if parent.name.split("-").size > 1
|
||
|
prefix = parent.name.split("-")[0].downcase + "-"
|
||
|
end
|
||
|
|
||
|
idt = prefix + read_attribute(:MODULE_CODE)
|
||
|
if idt.size > 2
|
||
|
idt.underscore[0..19].gsub(/[^a-z0-9\-]/, '-')
|
||
|
else
|
||
|
idt.underscore[0..19].gsub(/[^a-z0-9\-]/, '-') + "-module"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def description
|
||
|
read_attribute(:MODULE_DESCRIPTION)
|
||
|
end
|
||
|
|
||
|
def parent_id
|
||
|
read_attribute(:PARENT_ID)
|
||
|
end
|
||
|
|
||
|
def owner_id
|
||
|
read_attribute(:OWNER_ID)
|
||
|
end
|
||
|
|
||
|
def deleted?
|
||
|
read_attribute(:DELETED).to_i == 1 ? true : false
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabIssue < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_ISSUE
|
||
|
set_primary_key :ISSUE_ID
|
||
|
|
||
|
belongs_to :project,
|
||
|
:class_name => "ScarabProject",
|
||
|
:foreign_key => :MODULE_ID
|
||
|
has_many :attribute_values,
|
||
|
:class_name => "ScarabIssueAttributeValue",
|
||
|
:foreign_key => :ISSUE_ID
|
||
|
has_many :activities,
|
||
|
:class_name => "ScarabActivity",
|
||
|
:foreign_key => :ISSUE_ID
|
||
|
has_many :attachments,
|
||
|
:class_name => "ScarabAttachment",
|
||
|
:foreign_key => :ISSUE_ID
|
||
|
def id
|
||
|
read_attribute(:ISSUE_ID)
|
||
|
end
|
||
|
|
||
|
def project_id
|
||
|
read_attribute(:MODULE_ID)
|
||
|
end
|
||
|
|
||
|
def scarab_number
|
||
|
"(_ScarabId_ *#" + read_attribute(:ID_PREFIX).to_s + read_attribute(:ID_COUNT).to_s + "*) "
|
||
|
end
|
||
|
|
||
|
def id_prefix
|
||
|
read_attribute(:ID_PREFIX)
|
||
|
end
|
||
|
|
||
|
def id_count
|
||
|
read_attribute(:ID_COUNT)
|
||
|
end
|
||
|
def created_transaction_id
|
||
|
read_attribute(:CREATED_TRANS_ID)
|
||
|
end
|
||
|
|
||
|
def updated_transaction_id
|
||
|
read_attribute(:LAST_TRANS_ID)
|
||
|
end
|
||
|
|
||
|
def deleted?
|
||
|
read_attribute(:DELETED).to_i == 1 ? true : false
|
||
|
end
|
||
|
|
||
|
def moved?
|
||
|
read_attribute(:MOVED).to_i == 1 ? true : false
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabIssueAttributeValue < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_ISSUE_ATTRIBUTE_VALUE
|
||
|
belongs_to :issue,
|
||
|
:class_name => "ScarabIssue",
|
||
|
:foreign_key => :ISSUE_ID
|
||
|
def value
|
||
|
read_attribute(:VALUE)
|
||
|
end
|
||
|
|
||
|
def user_id
|
||
|
read_attribute(:USER_ID)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabAttribute < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_ATTRIBUTE
|
||
|
set_primary_key :ATTRIBUTE_ID
|
||
|
|
||
|
def name
|
||
|
read_attribute(:ATTRIBUTE_NAME)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabActivity < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_ACTIVITY
|
||
|
set_primary_key :ACTIVITY_ID
|
||
|
belongs_to :issue,
|
||
|
:class_name => "ScarabIssue",
|
||
|
:foreign_key => :ISSUE_ID
|
||
|
belongs_to :transaction,
|
||
|
:class_name => "ScarabTransaction",
|
||
|
:foreign_key => :TRANSACTION_ID
|
||
|
belongs_to :attribute,
|
||
|
:class_name => "ScarabAttribute",
|
||
|
:foreign_key => :ATTRIBUTE_ID
|
||
|
def old_value
|
||
|
read_attribute(:OLD_VALUE)
|
||
|
end
|
||
|
def new_value
|
||
|
read_attribute(:NEW_VALUE)
|
||
|
end
|
||
|
def type
|
||
|
read_attribute(:ACTIVITY_TYPE)
|
||
|
end
|
||
|
|
||
|
def attribute_id
|
||
|
read_attribute(:ATTRIBUTE_ID)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabAttachment < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_ATTACHMENT
|
||
|
set_primary_key :ATTACHMENT_ID
|
||
|
belongs_to :issue, :class_name => "ScarabIssue", :foreign_key => :ISSUE_ID
|
||
|
belongs_to :creator, :class_name => "ScarabUser", :foreign_key => :CREATED_BY
|
||
|
belongs_to :updator, :class_name => "ScarabUser", :foreign_key => :MODIFIED_BY
|
||
|
def name
|
||
|
read_attribute(:ATTACHMENT_NAME)
|
||
|
end
|
||
|
def data
|
||
|
read_attribute(:ATTACHMENT_DATA)
|
||
|
end
|
||
|
def attachment_type_id
|
||
|
read_attribute(:ATTACHMENT_TYPE_ID)
|
||
|
end
|
||
|
def original_filename
|
||
|
orig_file_name = ScarabMigrate.encode(read_attribute(:ATTACHMENT_FILE_PATH))
|
||
|
scarab_number = issue.id_prefix + issue.id_count.to_s
|
||
|
scarab_number + "_" + id.to_s + "_" + orig_file_name
|
||
|
end
|
||
|
def content_type
|
||
|
read_attribute(:ATTACHMENT_MIME_TYPE)
|
||
|
end
|
||
|
|
||
|
def exist?
|
||
|
File.file? scarab_fullpath
|
||
|
end
|
||
|
def read
|
||
|
File.open("#{scarab_fullpath}").read
|
||
|
end
|
||
|
|
||
|
def size
|
||
|
File.new("#{scarab_fullpath}").stat.blksize
|
||
|
end
|
||
|
def description
|
||
|
read_attribute(:ATTACHMENT_NAME)
|
||
|
end
|
||
|
def updated_date
|
||
|
read_attribute(:MODIFIED_DATE)
|
||
|
end
|
||
|
def created_date
|
||
|
read_attribute(:CREATED_DATE)
|
||
|
end
|
||
|
def deleted?
|
||
|
read_attribute(:DELETED) == 1 ? true : false
|
||
|
end
|
||
|
private
|
||
|
def scarab_fullpath
|
||
|
full_path = []
|
||
|
full_path << "#{ScarabMigrate.scarab_attachments_directory}"
|
||
|
full_path << "mod" + issue.project_id.to_s
|
||
|
full_path << "0" # TODO investigate what's "0" in scarab dir path
|
||
|
full_path << "#{original_filename}"
|
||
|
full_path.join(File::SEPARATOR)
|
||
|
end
|
||
|
end
|
||
|
class ScarabUser < ActiveRecord::Base
|
||
|
set_table_name :TURBINE_USER
|
||
|
has_many :projects,
|
||
|
:class_name => "ScarabProject",
|
||
|
:foreign_key => :OWNER_ID
|
||
|
has_many :transactions,
|
||
|
:class_name => "ScarabTransaction",
|
||
|
:foreign_key => :CREATED_BY
|
||
|
|
||
|
set_primary_key :USER_ID
|
||
|
|
||
|
def id
|
||
|
read_attribute(:USER_ID)
|
||
|
end
|
||
|
|
||
|
def username
|
||
|
read_attribute(:LOGIN_NAME)
|
||
|
end
|
||
|
|
||
|
def firstname
|
||
|
read_attribute(:FIRST_NAME)
|
||
|
end
|
||
|
|
||
|
def lastname
|
||
|
read_attribute(:LAST_NAME)
|
||
|
end
|
||
|
|
||
|
def email
|
||
|
read_attribute(:EMAIL)
|
||
|
end
|
||
|
|
||
|
def last_visit
|
||
|
read_attribute(:LAST_LOGIN)
|
||
|
end
|
||
|
|
||
|
def deleted?
|
||
|
read_attribute(:CONFIRM_VALUE) == 'DELETED'
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class ScarabTransaction < ActiveRecord::Base
|
||
|
set_table_name :SCARAB_TRANSACTION
|
||
|
belongs_to :creator,
|
||
|
:class_name => "ScarabUser",
|
||
|
:foreign_key => :CREATED_BY
|
||
|
set_primary_key :TRANSACTION_ID
|
||
|
def id
|
||
|
read_attribute(:TRANSACTION_ID)
|
||
|
end
|
||
|
def date
|
||
|
read_attribute(:CREATED_DATE)
|
||
|
end
|
||
|
end
|
||
|
def self.connection_params
|
||
|
if %w(sqlite sqlite3).include?(scarab_adapter)
|
||
|
{:adapter => scarab_adapter,
|
||
|
:database => scarab_db_path}
|
||
|
else
|
||
|
{:adapter => scarab_adapter,
|
||
|
:database => scarab_db_name,
|
||
|
:host => scarab_db_host,
|
||
|
:port => scarab_db_port,
|
||
|
:username => scarab_db_username,
|
||
|
:password => scarab_db_password
|
||
|
}
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def self.set_scarab_directory(path)
|
||
|
@@scarab_directory = path
|
||
|
raise "This directory doesn't exist!" unless File.directory?(path)
|
||
|
raise "#{scarab_attachments_directory} doesn't exist!" unless File.directory?(scarab_attachments_directory)
|
||
|
@@scarab_directory
|
||
|
rescue Exception => e
|
||
|
puts e
|
||
|
return false
|
||
|
end
|
||
|
def self.scarab_directory
|
||
|
@@scarab_directory
|
||
|
end
|
||
|
|
||
|
def self.set_scarab_adapter(adapter)
|
||
|
return false if adapter.blank?
|
||
|
raise "Unknown adapter: #{adapter}!" unless %w(sqlite sqlite3 mysql postgresql).include?(adapter)
|
||
|
# If adapter is sqlite or sqlite3, make sure that scarab.db exists
|
||
|
raise "#{scarab_db_path} doesn't exist!" if %w(sqlite sqlite3).include?(adapter) && !File.exist?(scarab_db_path)
|
||
|
@@scarab_adapter = adapter
|
||
|
rescue Exception => e
|
||
|
puts e
|
||
|
return false
|
||
|
end
|
||
|
def self.set_scarab_db_host(host)
|
||
|
return nil if host.blank?
|
||
|
@@scarab_db_host = host
|
||
|
end
|
||
|
def self.set_scarab_db_port(port)
|
||
|
return nil if port.to_i == 0
|
||
|
@@scarab_db_port = port.to_i
|
||
|
end
|
||
|
def self.set_scarab_db_name(name)
|
||
|
return nil if name.blank?
|
||
|
@@scarab_db_name = name
|
||
|
end
|
||
|
def self.set_scarab_db_username(username)
|
||
|
@@scarab_db_username = username
|
||
|
end
|
||
|
def self.set_scarab_db_password(password)
|
||
|
@@scarab_db_password = password
|
||
|
end
|
||
|
def self.encoding(charset)
|
||
|
@ic = Iconv.new('UTF-8', charset)
|
||
|
rescue Iconv::InvalidEncoding
|
||
|
puts "Invalid encoding!"
|
||
|
return false
|
||
|
end
|
||
|
mattr_reader :scarab_directory, :scarab_adapter, :scarab_db_host, :scarab_db_port, :scarab_db_name, :scarab_db_username, :scarab_db_password
|
||
|
def self.scarab_db_path; "#{scarab_directory}/db/scarab.db" end
|
||
|
def self.scarab_attachments_directory
|
||
|
"#{scarab_directory}/" # TODO set correctly
|
||
|
end
|
||
|
def self.establish_connection
|
||
|
constants.each do |const|
|
||
|
klass = const_get(const)
|
||
|
next unless klass.respond_to? 'establish_connection'
|
||
|
klass.establish_connection connection_params
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def self.migrate
|
||
|
establish_connection
|
||
|
|
||
|
# Users
|
||
|
print "Migrating users"
|
||
|
#User.delete_all "login <> 'admin'"
|
||
|
users_map = {}
|
||
|
users_migrated = 0
|
||
|
ScarabUser.find(:all).each do |user|
|
||
|
next if user.deleted?
|
||
|
u = User.new :firstname => encode(user.firstname),
|
||
|
:lastname => encode(user.lastname),
|
||
|
:mail => user.email,
|
||
|
:last_login_on => user.last_visit
|
||
|
u.login = user.username
|
||
|
u.password = 'scarab'
|
||
|
#u.admin = true if user.access_level == 90
|
||
|
next unless u.save
|
||
|
users_migrated += 1
|
||
|
users_map[user.id] = u.id
|
||
|
print '.'
|
||
|
end
|
||
|
puts
|
||
|
|
||
|
|
||
|
# Projects
|
||
|
print "Migrating projects"
|
||
|
#Project.destroy_all
|
||
|
projects_map = {}
|
||
|
versions_map = {}
|
||
|
categories_map = {}
|
||
|
ScarabProject.find(:all, :order => :MODULE_ID).each do |project|
|
||
|
next if project.deleted?
|
||
|
next if project.id == 0 # Global Scarab parent project, do not migrate
|
||
|
p = Project.new :name => encode(project.name),
|
||
|
:description => encode(project.description),
|
||
|
:created_on => Time.now,
|
||
|
:updated_on => Time.now
|
||
|
# Project parent relationship
|
||
|
parent = ScarabProject.find(:first, :conditions => {:MODULE_ID => project.parent_id})
|
||
|
unless project.parent_id == 0
|
||
|
p.parent_id = Project.find(:first, :conditions => {:identifier => parent.identifier}).id
|
||
|
end
|
||
|
p.identifier = project.identifier
|
||
|
print "\nCreate project: " + project.name + " [yN] "
|
||
|
answer = STDIN.gets.chomp!
|
||
|
next unless answer == "y"
|
||
|
|
||
|
unless p.save
|
||
|
p p.errors if $DBG
|
||
|
next
|
||
|
end
|
||
|
projects_map[project.id] = p.id
|
||
|
p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
|
||
|
p.trackers << TRACKER_BUG
|
||
|
p.trackers << TRACKER_FEATURE
|
||
|
puts "created with identifier: " + p.identifier
|
||
|
|
||
|
# Project Owner as manager member
|
||
|
scarab_owner = ScarabUser.find(:first, :conditions => {:USER_ID => project.owner_id})
|
||
|
owner = Member.new
|
||
|
owner.role = ROLE_MAPPING['admin']
|
||
|
owner.user = User.find(:first, :conditions => {:login => scarab_owner.username})
|
||
|
owner.project = p
|
||
|
owner.save
|
||
|
end
|
||
|
puts
|
||
|
|
||
|
|
||
|
# Issues
|
||
|
print "Migrating issues"
|
||
|
issues_map = {}
|
||
|
moved = 0
|
||
|
deleted = 0
|
||
|
no_scarab_project = 0
|
||
|
no_project = 0
|
||
|
not_validated = 0
|
||
|
issue_counter = 0
|
||
|
ScarabIssue.find(:all, :order => 'ISSUE_ID ASC').each do |issue|
|
||
|
issue_counter += 1
|
||
|
if issue.deleted?
|
||
|
deleted += 1
|
||
|
next
|
||
|
end
|
||
|
if issue.moved?
|
||
|
moved += 1
|
||
|
next
|
||
|
end
|
||
|
scarab_project = ScarabProject.find(:first, :conditions => {:MODULE_ID => issue.project_id})
|
||
|
unless scarab_project
|
||
|
no_scarab_project += 1
|
||
|
next
|
||
|
end
|
||
|
project = Project.find(:first, :conditions => {:identifier => scarab_project.identifier})
|
||
|
unless project
|
||
|
no_project += 1
|
||
|
next
|
||
|
end
|
||
|
i = Issue.new :project_id => project.id
|
||
|
description = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Description]
|
||
|
})
|
||
|
summary = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Summary]
|
||
|
})
|
||
|
if summary && summary.value && summary.value.size > 1
|
||
|
i.subject = encode(summary.value)
|
||
|
else
|
||
|
i.subject = encode(description.value[0..40])
|
||
|
end
|
||
|
if description
|
||
|
i.description = issue.scarab_number.to_s + encode(description.value)
|
||
|
else
|
||
|
i.description = issue.scarab_number.to_s + encode(summary.value)
|
||
|
end
|
||
|
|
||
|
status = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Status]
|
||
|
})
|
||
|
i.status = status ? STATUS_MAPPING[status.value] : DEFAULT_STATUS
|
||
|
resolution = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Resolution]
|
||
|
})
|
||
|
i.status = STATUS_MAPPING[resolution.value] if resolution && resolution.value
|
||
|
severity = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Severity]
|
||
|
})
|
||
|
if severity
|
||
|
i.priority = PRIORITY_MAPPING[severity.value]
|
||
|
i.tracker = TRACKER_MAPPING[severity.value] || TRACKER_BUG
|
||
|
else
|
||
|
priority = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:Priority]
|
||
|
})
|
||
|
i.priority = PRIORITY_MAPPING[priority.value] if priority
|
||
|
i.tracker = TRACKER_BUG
|
||
|
end
|
||
|
created_transaction = ScarabTransaction.find(:first, :conditions => {
|
||
|
:TRANSACTION_ID => issue.created_transaction_id
|
||
|
})
|
||
|
scarab_author = ScarabUser.find(:first, :conditions => {:USER_ID => created_transaction.creator.id})
|
||
|
i.author = User.find(:first, :conditions => {:login => scarab_author.username})
|
||
|
i.author ||= User.find(:first, :conditions => {:login => 'Admin'})
|
||
|
i.author ||= User.find(:first)
|
||
|
updated_transaction = ScarabTransaction.find(:first, :conditions => {
|
||
|
:TRANSACTION_ID => issue.updated_transaction_id
|
||
|
})
|
||
|
i.created_on = created_transaction ? created_transaction.date : Time.now
|
||
|
i.updated_on = updated_transaction ? updated_transaction.date : Time.now
|
||
|
|
||
|
assigned_tos = ScarabIssueAttributeValue.find(:all, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:AssignedTo]
|
||
|
})
|
||
|
assigned_ccs = ScarabIssueAttributeValue.find(:all, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:AssignedCC]
|
||
|
})
|
||
|
scarab_assignee = ScarabUser.find(:first, :conditions => {:USER_ID => assigned_tos[0].user_id}) if assigned_tos[0]
|
||
|
i.assigned_to = User.find(:first, :conditions => {:login => scarab_assignee.username}) if scarab_assignee
|
||
|
|
||
|
# Assigned to as 'developer' Member of current Project
|
||
|
assigned_tos.each do |assigned_to|
|
||
|
s_assignee = ScarabUser.find(:first, :conditions => {:USER_ID => assigned_to.user_id})
|
||
|
developer = Member.new
|
||
|
developer.role = ROLE_MAPPING['developer']
|
||
|
r_user = User.find(:first, :conditions => {:login => s_assignee.username})
|
||
|
developer.user = r_user
|
||
|
developer.project = project
|
||
|
#i.add_watcher(r_user)
|
||
|
next unless developer.save
|
||
|
end
|
||
|
|
||
|
# Assigned to as 'reporter' Member of current Project
|
||
|
assigned_ccs.each do |assigned_cc|
|
||
|
s_assignee = ScarabUser.find(:first, :conditions => {:USER_ID => assigned_cc.user_id})
|
||
|
reporter = Member.new
|
||
|
reporter.role = ROLE_MAPPING['reporter']
|
||
|
r_user = User.find(:first, :conditions => {:login => s_assignee.username})
|
||
|
reporter.user = r_user
|
||
|
reporter.project = project
|
||
|
#i.add_watcher(r_user)
|
||
|
next unless reporter.save
|
||
|
end
|
||
|
|
||
|
due_date = ScarabIssueAttributeValue.find(:first, :conditions => {
|
||
|
:ISSUE_ID => issue.id,
|
||
|
:ATTRIBUTE_ID => ATTRIBUTE_NAMES[:DueTime]
|
||
|
})
|
||
|
i.due_date = due_date if due_date
|
||
|
unless i.save
|
||
|
p i.errors if $DBG
|
||
|
not_validated += 1
|
||
|
next
|
||
|
end
|
||
|
# Issue Watchers (to)
|
||
|
assigned_tos.each do |assigned_to|
|
||
|
s_assignee = ScarabUser.find(:first, :conditions => {:USER_ID => assigned_to.user_id})
|
||
|
r_user = User.find(:first, :conditions => {:login => s_assignee.username})
|
||
|
i.add_watcher(r_user)
|
||
|
end
|
||
|
# Issue Watchers (cc)
|
||
|
assigned_ccs.each do |assigned_cc|
|
||
|
s_assignee = ScarabUser.find(:first, :conditions => {:USER_ID => assigned_cc.user_id})
|
||
|
r_user = User.find(:first, :conditions => {:login => s_assignee.username})
|
||
|
i.add_watcher(r_user)
|
||
|
end
|
||
|
|
||
|
# Issue Attachments (Comments and Attachments)
|
||
|
issue.attachments.each do |a|
|
||
|
next if a.deleted?
|
||
|
if a.attachment_type_id == ATTACHMENT_TYPES[:COMMENT] ||
|
||
|
a.attachment_type_id == ATTACHMENT_TYPES[:MODIFICATION]
|
||
|
j = Journal.new
|
||
|
next unless encode(a.data).size > 0
|
||
|
j.notes = encode(a.data)
|
||
|
if r_user = User.find(:first, :conditions => {:login => a.creator.username})
|
||
|
j.user = r_user
|
||
|
elsif r_adminuser = User.find(:first, :conditions => {:login => 'Admin'})
|
||
|
j.user = r_adminuser
|
||
|
else
|
||
|
j.user = User.find(:first)
|
||
|
end
|
||
|
j.created_on = a.created_date
|
||
|
j.journalized = i
|
||
|
unless j.save
|
||
|
p j.errors if $DBG
|
||
|
next
|
||
|
end
|
||
|
elsif a.attachment_type_id == ATTACHMENT_TYPES[:ATTACHMENT]
|
||
|
r_attachement = Attachment.new :created_on => a.created_date
|
||
|
if r_user = User.find(:first, :conditions => {:login => a.creator.username})
|
||
|
r_attachement.author = r_user
|
||
|
elsif r_adminuser = User.find(:first, :conditions => {:login => 'Admin'})
|
||
|
r_attachement.author = r_adminuser
|
||
|
else
|
||
|
r_attachement.author = User.find(:first)
|
||
|
end
|
||
|
r_attachement.container = i
|
||
|
r_attachement.description = a.name
|
||
|
r_attachement.file = a
|
||
|
r_attachement.save
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# Issue Activities
|
||
|
notes_transaction_map = {}
|
||
|
issue.activities.each do |a|
|
||
|
if a.type == "user_attribute_changed" || a.type == "attribute_changed"
|
||
|
if !a.new_value || a.new_value.size == 0
|
||
|
next
|
||
|
end
|
||
|
notes_transaction_map[a.transaction] = [] unless notes_transaction_map[a.transaction]
|
||
|
attribute_name = "* *" + a.attribute.name + "*: "
|
||
|
from = "_changed from_ " + encode(a.old_value) if a.old_value
|
||
|
to = " _to_ " + encode(a.new_value) if a.new_value
|
||
|
notes_transaction_map[a.transaction] << attribute_name.to_s + from.to_s + to.to_s
|
||
|
end
|
||
|
end
|
||
|
notes_transaction_map.each do |t, notes|
|
||
|
j = Journal.new
|
||
|
j.notes = notes.join("\n")
|
||
|
if r_user = User.find(:first, :conditions => {:login => t.creator.username})
|
||
|
j.user = r_user
|
||
|
elsif r_adminuser = User.find(:first, :conditions => {:login => 'Admin'})
|
||
|
j.user = r_adminuser
|
||
|
else
|
||
|
j.user = User.find(:first)
|
||
|
end
|
||
|
j.created_on = t.date
|
||
|
j.journalized = i
|
||
|
unless j.save
|
||
|
p j.errors if $DBG
|
||
|
next
|
||
|
end
|
||
|
end
|
||
|
print "."
|
||
|
end
|
||
|
puts
|
||
|
|
||
|
# Summary
|
||
|
puts
|
||
|
puts "i moved: #{moved}" if $DBG
|
||
|
puts "i deleted: #{deleted}" if $DBG
|
||
|
puts "i no_project: #{no_project}" if $DBG
|
||
|
puts "i no_scarab_project: #{no_scarab_project}" if $DBG
|
||
|
puts "i not_validated: #{not_validated}" if $DBG
|
||
|
puts "i issue_counter: #{issue_counter}" if $DBG
|
||
|
puts "Users: #{users_migrated}/#{ScarabUser.count}"
|
||
|
puts "Projects: #{Project.count}/#{ScarabProject.count}"
|
||
|
puts "Issues: #{Issue.count}/#{ScarabIssue.count}"
|
||
|
puts "Comments and Attachments: #{Journal.count + Attachment.count}/#{ScarabAttachment.count + ScarabActivity.count}"
|
||
|
end
|
||
|
|
||
|
def self.encoding(charset)
|
||
|
@ic = Iconv.new('UTF-8', charset)
|
||
|
#rescue Iconv::InvalidEncoding
|
||
|
# return false
|
||
|
end
|
||
|
|
||
|
private
|
||
|
def self.encode(text)
|
||
|
@ic.iconv text
|
||
|
rescue
|
||
|
text
|
||
|
end
|
||
|
end
|
||
|
puts
|
||
|
if Redmine::DefaultData::Loader.no_data?
|
||
|
puts "Redmine configuration need to be loaded before importing data."
|
||
|
puts "Please, run this first:"
|
||
|
puts
|
||
|
puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
|
||
|
exit
|
||
|
end
|
||
|
puts "WARNING: new projects will be added to Redmine during this process."
|
||
|
print "Are you sure you want to continue ? [y/N] "
|
||
|
break unless STDIN.gets.match(/^y$/i)
|
||
|
puts
|
||
|
def prompt(text, options = {}, &block)
|
||
|
default = options[:default] || ''
|
||
|
while true
|
||
|
print "#{text} [#{default}]: "
|
||
|
value = STDIN.gets.chomp!
|
||
|
value = default if value.blank?
|
||
|
break if yield value
|
||
|
end
|
||
|
end
|
||
|
DEFAULT_PORTS = {'mysql' => 3306, 'postgresql' => 5432}
|
||
|
prompt('Scarab attachments directory') {|directory| ScarabMigrate.set_scarab_directory directory.strip}
|
||
|
prompt('Scarab database adapter (sqlite, sqlite3, mysql, postgresql)', :default => 'mysql') {|adapter| ScarabMigrate.set_scarab_adapter adapter}
|
||
|
unless %w(sqlite sqlite3).include?(ScarabMigrate.scarab_adapter)
|
||
|
prompt('Scarab database host', :default => 'localhost') {|host| ScarabMigrate.set_scarab_db_host host}
|
||
|
prompt('Scarab database port', :default => DEFAULT_PORTS[ScarabMigrate.scarab_adapter]) {|port| ScarabMigrate.set_scarab_db_port port}
|
||
|
prompt('Scarab database name', :default => 'scarab') {|name| ScarabMigrate.set_scarab_db_name name}
|
||
|
prompt('Scarab database username', :default => 'root') {|username| ScarabMigrate.set_scarab_db_username username}
|
||
|
prompt('Scarab database password') {|password| ScarabMigrate.set_scarab_db_password password}
|
||
|
end
|
||
|
prompt('Scarab database encoding', :default => 'Latin1') {|encoding| ScarabMigrate.encoding encoding}
|
||
|
puts
|
||
|
ScarabMigrate.migrate
|
||
|
end
|
||
|
end
|
||