Project

General

Profile

Patch #18001 ยป migrate_from_mantis_inheritable_categories.patch

Konstantin Tkachenko, 2014-09-30 12:12

View differences:

lib/tasks/migrate_from_mantis.rake (Arbeitskopie)
}
priorities = IssuePriority.all
DEFAULT_PRIORITY = priorities[2]
PRIORITY_MAPPING = {10 => priorities[1], # none
20 => priorities[1], # low
30 => priorities[2], # normal
40 => priorities[3], # high
50 => priorities[4], # urgent
60 => priorities[5] # immediate
DEFAULT_PRIORITY = priorities[1]
PRIORITY_MAPPING = {10 => priorities[0], # none
20 => priorities[0], # low
30 => priorities[1], # normal
40 => priorities[2], # high
50 => priorities[3], # urgent
60 => priorities[4] # immediate
}
TRACKER_BUG = Tracker.find_by_position(1)
......
has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
def identifier
read_attribute(:name).downcase.gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
read_attribute(:name).slice(0, Project::IDENTIFIER_MAX_LENGTH).downcase.gsub(/[^a-z0-9\-]+/, '-')
end
end
class MantisProjectHierarchy < ActiveRecord::Base
self.table_name = :mantis_project_hierarchy_table
end
class MantisVersion < ActiveRecord::Base
self.table_name = :mantis_project_version_table
......
end
class MantisCategory < ActiveRecord::Base
self.table_name = :mantis_project_category_table
self.table_name = :mantis_category_table
def category
read_attribute(:name).slice(0,30)
end
end
class MantisProjectUser < ActiveRecord::Base
......
class MantisBug < ActiveRecord::Base
self.table_name = :mantis_bug_table
belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
belongs_to :category, :class_name => "MantisCategory", :foreign_key => :category_id
has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
......
class MantisCustomField < ActiveRecord::Base
self.table_name = :mantis_custom_field_table
set_inheritance_column :none
self.inheritance_column = :none
has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
......
u = User.new :firstname => encode(user.firstname),
:lastname => encode(user.lastname),
:mail => user.email,
:last_login_on => user.last_visit
:last_login_on => (user.last_visit ? Time.at(user.last_visit).to_date : nil)
u.login = user.username
u.password = 'mantis'
u.status = User::STATUS_LOCKED if user.enabled != 1
......
Project.destroy_all
projects_map = {}
versions_map = {}
versions_locked = []
categories_map = {}
inheritable_categories = false
MantisProject.all.each do |project|
p = Project.new :name => encode(project.name),
:description => encode(project.description)
inheritable_categories = p.has_attribute?("inherit_categs") unless inheritable_categories
p.identifier = project.identifier
p.is_public = (project.view_state == 10)
next unless p.save
projects_map[project.id] = p.id
p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
p.enabled_module_names = ['issue_tracking', 'news', 'wiki', 'calendar', 'gantt', 'time_tracking']
p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG)
p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE)
print '.'
......
project.versions.each do |version|
v = Version.new :name => encode(version.version),
:description => encode(version.description),
:effective_date => (version.date_order ? version.date_order.to_date : nil)
:effective_date => (version.date_order ? Time.at(version.date_order).to_date : nil)
v.project = p
# we cannot directly lock versions, cause otherwise some bugs will not be migrated, that why we remember the versions to be locked after issues migration
if version.obsolete == 1
versions_locked << v
end
v.save
versions_map[version.id] = v.id
end
# Project categories
project.categories.each do |category|
g = IssueCategory.new :name => category.category[0,30]
g = IssueCategory.new :name => category.category
g.project = p
g.save
categories_map[category.category] = g.id
end
end
puts
# Project Hierarchy
print "Making Project Hierarchy"
MantisProjectHierarchy.find(:all).each do |link|
next unless p = Project.find_by_id(projects_map[link.child_id])
p.set_parent!(projects_map[link.parent_id])
if link.inherit_parent == 1
if inheritable_categories # p.has_attribute?("inherit_categs")
p.inherit_categs = 1
end
# Turn on the users inheritance
p.inherit_members = 1
p.save
# Turn on the versions inheritance
parent_project = Project.find_by_id(projects_map[link.parent_id])
parent_project.versions.each do |version|
version.sharing = 'descendants'
version.save
end
end
print '.'
end
puts
# Bugs
print "Migrating bugs"
ActiveRecord::Base.record_timestamps = false
Issue.destroy_all
issues_map = {}
keep_bug_ids = (Issue.count == 0)
......
:subject => encode(bug.summary),
:description => encode(bug.bug_text.full_description),
:priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
:created_on => bug.date_submitted,
:updated_on => bug.last_updated
:created_on => (bug.date_submitted ? Time.at(bug.date_submitted).to_date : nil),
:updated_on => (bug.last_updated ? Time.at(bug.last_updated).to_date : nil)
i.author = User.find_by_id(users_map[bug.reporter_id])
i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category.category) unless bug.category.blank?
if (i.category.nil?) && inheritable_categories
p = Project.find_by_id(projects_map[bug.project_id])
i.category = p.inherited_categories.find{|c| c.name == bug.category.category}
end
if !(bug.fixed_in_version.blank? && bug.target_version.blank?)
p = Project.find_by_id(projects_map[bug.project_id])
if !bug.fixed_in_version.blank?
vv = bug.fixed_in_version
else
vv = bug.target_version
end
i.fixed_version = p.shared_versions.find{|v| v.name == vv}
end
i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
i.start_date = Time.at(bug.date_submitted).to_date
if bug.due_date > bug.date_submitted
i.due_date = Time.at(bug.due_date).to_date
elsif bug.status >= 80 && i.updated_on > i.start_date
i.due_date = i.updated_on
end
i.id = bug.id if keep_bug_ids
next unless i.save
issues_map[bug.id] = i.id
......
bug.bug_notes.each do |note|
next unless users_map[note.reporter_id]
n = Journal.new :notes => encode(note.bug_note_text.note),
:created_on => note.date_submitted
:created_on => Time.at(note.date_submitted).to_date
n.user = User.find_by_id(users_map[note.reporter_id])
n.journalized = i
n.save
......
# Bug files
bug.bug_files.each do |file|
a = Attachment.new :created_on => file.date_added
a = Attachment.new :created_on => (file.date_added ? Time.at(file.date_added).to_date : nil)
a.file = file
a.author = User.first
a.container = i
......
end
end
# Locking versions after all bugs where migrated, otherwise no bugs with the locked version as target will be migrated
versions_locked.each do |version|
version.status = 'locked'
version.save
end
# update issue id sequence if needed (postgresql)
Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
puts
# Bug relationships
#debugger
ActiveRecord::Base.record_timestamps = true
print "Migrating bug relations"
MantisBugRelationship.all.each do |relation|
next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
......
n = News.new :project_id => projects_map[news.project_id],
:title => encode(news.headline[0..59]),
:description => encode(news.body),
:created_on => news.date_posted
:created_on => Time.at(news.date_posted).to_date
n.author = User.find_by_id(users_map[news.poster_id])
n.save
print '.'
    (1-1/1)