You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migrate_from_mantis.rake 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. desc 'Mantis migration script'
  18. require 'active_record'
  19. require 'iconv'
  20. require 'pp'
  21. namespace :redmine do
  22. task :migrate_from_mantis => :environment do
  23. module MantisMigrate
  24. DEFAULT_STATUS = IssueStatus.default
  25. assigned_status = IssueStatus.find_by_position(2)
  26. resolved_status = IssueStatus.find_by_position(3)
  27. feedback_status = IssueStatus.find_by_position(4)
  28. closed_status = IssueStatus.find :first, :conditions => { :is_closed => true }
  29. STATUS_MAPPING = {10 => DEFAULT_STATUS, # new
  30. 20 => feedback_status, # feedback
  31. 30 => DEFAULT_STATUS, # acknowledged
  32. 40 => DEFAULT_STATUS, # confirmed
  33. 50 => assigned_status, # assigned
  34. 80 => resolved_status, # resolved
  35. 90 => closed_status # closed
  36. }
  37. priorities = IssuePriority.all
  38. DEFAULT_PRIORITY = priorities[2]
  39. PRIORITY_MAPPING = {10 => priorities[1], # none
  40. 20 => priorities[1], # low
  41. 30 => priorities[2], # normal
  42. 40 => priorities[3], # high
  43. 50 => priorities[4], # urgent
  44. 60 => priorities[5] # immediate
  45. }
  46. TRACKER_BUG = Tracker.find_by_position(1)
  47. TRACKER_FEATURE = Tracker.find_by_position(2)
  48. roles = Role.find(:all, :conditions => {:builtin => 0}, :order => 'position ASC')
  49. manager_role = roles[0]
  50. developer_role = roles[1]
  51. DEFAULT_ROLE = roles.last
  52. ROLE_MAPPING = {10 => DEFAULT_ROLE, # viewer
  53. 25 => DEFAULT_ROLE, # reporter
  54. 40 => DEFAULT_ROLE, # updater
  55. 55 => developer_role, # developer
  56. 70 => manager_role, # manager
  57. 90 => manager_role # administrator
  58. }
  59. CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
  60. 1 => 'int', # Numeric
  61. 2 => 'int', # Float
  62. 3 => 'list', # Enumeration
  63. 4 => 'string', # Email
  64. 5 => 'bool', # Checkbox
  65. 6 => 'list', # List
  66. 7 => 'list', # Multiselection list
  67. 8 => 'date', # Date
  68. }
  69. RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES, # related to
  70. 2 => IssueRelation::TYPE_RELATES, # parent of
  71. 3 => IssueRelation::TYPE_RELATES, # child of
  72. 0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
  73. 4 => IssueRelation::TYPE_DUPLICATES # has duplicate
  74. }
  75. class MantisUser < ActiveRecord::Base
  76. self.table_name = :mantis_user_table
  77. def firstname
  78. @firstname = realname.blank? ? username : realname.split.first[0..29]
  79. @firstname
  80. end
  81. def lastname
  82. @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
  83. @lastname = '-' if @lastname.blank?
  84. @lastname
  85. end
  86. def email
  87. if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
  88. !User.find_by_mail(read_attribute(:email))
  89. @email = read_attribute(:email)
  90. else
  91. @email = "#{username}@foo.bar"
  92. end
  93. end
  94. def username
  95. read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
  96. end
  97. end
  98. class MantisProject < ActiveRecord::Base
  99. self.table_name = :mantis_project_table
  100. has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
  101. has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
  102. has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
  103. has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
  104. def identifier
  105. read_attribute(:name).gsub(/[^a-z0-9\-]+/, '-').slice(0, Project::IDENTIFIER_MAX_LENGTH)
  106. end
  107. end
  108. class MantisVersion < ActiveRecord::Base
  109. self.table_name = :mantis_project_version_table
  110. def version
  111. read_attribute(:version)[0..29]
  112. end
  113. def description
  114. read_attribute(:description)[0..254]
  115. end
  116. end
  117. class MantisCategory < ActiveRecord::Base
  118. self.table_name = :mantis_project_category_table
  119. end
  120. class MantisProjectUser < ActiveRecord::Base
  121. self.table_name = :mantis_project_user_list_table
  122. end
  123. class MantisBug < ActiveRecord::Base
  124. self.table_name = :mantis_bug_table
  125. belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
  126. has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
  127. has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
  128. has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
  129. end
  130. class MantisBugText < ActiveRecord::Base
  131. self.table_name = :mantis_bug_text_table
  132. # Adds Mantis steps_to_reproduce and additional_information fields
  133. # to description if any
  134. def full_description
  135. full_description = description
  136. full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
  137. full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
  138. full_description
  139. end
  140. end
  141. class MantisBugNote < ActiveRecord::Base
  142. self.table_name = :mantis_bugnote_table
  143. belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
  144. belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
  145. end
  146. class MantisBugNoteText < ActiveRecord::Base
  147. self.table_name = :mantis_bugnote_text_table
  148. end
  149. class MantisBugFile < ActiveRecord::Base
  150. self.table_name = :mantis_bug_file_table
  151. def size
  152. filesize
  153. end
  154. def original_filename
  155. MantisMigrate.encode(filename)
  156. end
  157. def content_type
  158. file_type
  159. end
  160. def read(*args)
  161. if @read_finished
  162. nil
  163. else
  164. @read_finished = true
  165. content
  166. end
  167. end
  168. end
  169. class MantisBugRelationship < ActiveRecord::Base
  170. self.table_name = :mantis_bug_relationship_table
  171. end
  172. class MantisBugMonitor < ActiveRecord::Base
  173. self.table_name = :mantis_bug_monitor_table
  174. end
  175. class MantisNews < ActiveRecord::Base
  176. self.table_name = :mantis_news_table
  177. end
  178. class MantisCustomField < ActiveRecord::Base
  179. self.table_name = :mantis_custom_field_table
  180. set_inheritance_column :none
  181. has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
  182. has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
  183. def format
  184. read_attribute :type
  185. end
  186. def name
  187. read_attribute(:name)[0..29]
  188. end
  189. end
  190. class MantisCustomFieldProject < ActiveRecord::Base
  191. self.table_name = :mantis_custom_field_project_table
  192. end
  193. class MantisCustomFieldString < ActiveRecord::Base
  194. self.table_name = :mantis_custom_field_string_table
  195. end
  196. def self.migrate
  197. # Users
  198. print "Migrating users"
  199. User.delete_all "login <> 'admin'"
  200. users_map = {}
  201. users_migrated = 0
  202. MantisUser.find(:all).each do |user|
  203. u = User.new :firstname => encode(user.firstname),
  204. :lastname => encode(user.lastname),
  205. :mail => user.email,
  206. :last_login_on => user.last_visit
  207. u.login = user.username
  208. u.password = 'mantis'
  209. u.status = User::STATUS_LOCKED if user.enabled != 1
  210. u.admin = true if user.access_level == 90
  211. next unless u.save!
  212. users_migrated += 1
  213. users_map[user.id] = u.id
  214. print '.'
  215. end
  216. puts
  217. # Projects
  218. print "Migrating projects"
  219. Project.destroy_all
  220. projects_map = {}
  221. versions_map = {}
  222. categories_map = {}
  223. MantisProject.find(:all).each do |project|
  224. p = Project.new :name => encode(project.name),
  225. :description => encode(project.description)
  226. p.identifier = project.identifier
  227. next unless p.save
  228. projects_map[project.id] = p.id
  229. p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
  230. p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG)
  231. p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE)
  232. print '.'
  233. # Project members
  234. project.members.each do |member|
  235. m = Member.new :user => User.find_by_id(users_map[member.user_id]),
  236. :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
  237. m.project = p
  238. m.save
  239. end
  240. # Project versions
  241. project.versions.each do |version|
  242. v = Version.new :name => encode(version.version),
  243. :description => encode(version.description),
  244. :effective_date => (version.date_order ? version.date_order.to_date : nil)
  245. v.project = p
  246. v.save
  247. versions_map[version.id] = v.id
  248. end
  249. # Project categories
  250. project.categories.each do |category|
  251. g = IssueCategory.new :name => category.category[0,30]
  252. g.project = p
  253. g.save
  254. categories_map[category.category] = g.id
  255. end
  256. end
  257. puts
  258. # Bugs
  259. print "Migrating bugs"
  260. Issue.destroy_all
  261. issues_map = {}
  262. keep_bug_ids = (Issue.count == 0)
  263. MantisBug.find_each(:batch_size => 200) do |bug|
  264. next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
  265. i = Issue.new :project_id => projects_map[bug.project_id],
  266. :subject => encode(bug.summary),
  267. :description => encode(bug.bug_text.full_description),
  268. :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
  269. :created_on => bug.date_submitted,
  270. :updated_on => bug.last_updated
  271. i.author = User.find_by_id(users_map[bug.reporter_id])
  272. i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
  273. i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
  274. i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
  275. i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
  276. i.id = bug.id if keep_bug_ids
  277. next unless i.save
  278. issues_map[bug.id] = i.id
  279. print '.'
  280. STDOUT.flush
  281. # Assignee
  282. # Redmine checks that the assignee is a project member
  283. if (bug.handler_id && users_map[bug.handler_id])
  284. i.assigned_to = User.find_by_id(users_map[bug.handler_id])
  285. i.save(:validate => false)
  286. end
  287. # Bug notes
  288. bug.bug_notes.each do |note|
  289. next unless users_map[note.reporter_id]
  290. n = Journal.new :notes => encode(note.bug_note_text.note),
  291. :created_on => note.date_submitted
  292. n.user = User.find_by_id(users_map[note.reporter_id])
  293. n.journalized = i
  294. n.save
  295. end
  296. # Bug files
  297. bug.bug_files.each do |file|
  298. a = Attachment.new :created_on => file.date_added
  299. a.file = file
  300. a.author = User.find :first
  301. a.container = i
  302. a.save
  303. end
  304. # Bug monitors
  305. bug.bug_monitors.each do |monitor|
  306. next unless users_map[monitor.user_id]
  307. i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
  308. end
  309. end
  310. # update issue id sequence if needed (postgresql)
  311. Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
  312. puts
  313. # Bug relationships
  314. print "Migrating bug relations"
  315. MantisBugRelationship.find(:all).each do |relation|
  316. next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
  317. r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
  318. r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
  319. r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
  320. pp r unless r.save
  321. print '.'
  322. STDOUT.flush
  323. end
  324. puts
  325. # News
  326. print "Migrating news"
  327. News.destroy_all
  328. MantisNews.find(:all, :conditions => 'project_id > 0').each do |news|
  329. next unless projects_map[news.project_id]
  330. n = News.new :project_id => projects_map[news.project_id],
  331. :title => encode(news.headline[0..59]),
  332. :description => encode(news.body),
  333. :created_on => news.date_posted
  334. n.author = User.find_by_id(users_map[news.poster_id])
  335. n.save
  336. print '.'
  337. STDOUT.flush
  338. end
  339. puts
  340. # Custom fields
  341. print "Migrating custom fields"
  342. IssueCustomField.destroy_all
  343. MantisCustomField.find(:all).each do |field|
  344. f = IssueCustomField.new :name => field.name[0..29],
  345. :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
  346. :min_length => field.length_min,
  347. :max_length => field.length_max,
  348. :regexp => field.valid_regexp,
  349. :possible_values => field.possible_values.split('|'),
  350. :is_required => field.require_report?
  351. next unless f.save
  352. print '.'
  353. STDOUT.flush
  354. # Trackers association
  355. f.trackers = Tracker.find :all
  356. # Projects association
  357. field.projects.each do |project|
  358. f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
  359. end
  360. # Values
  361. field.values.each do |value|
  362. v = CustomValue.new :custom_field_id => f.id,
  363. :value => value.value
  364. v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
  365. v.save
  366. end unless f.new_record?
  367. end
  368. puts
  369. puts
  370. puts "Users: #{users_migrated}/#{MantisUser.count}"
  371. puts "Projects: #{Project.count}/#{MantisProject.count}"
  372. puts "Memberships: #{Member.count}/#{MantisProjectUser.count}"
  373. puts "Versions: #{Version.count}/#{MantisVersion.count}"
  374. puts "Categories: #{IssueCategory.count}/#{MantisCategory.count}"
  375. puts "Bugs: #{Issue.count}/#{MantisBug.count}"
  376. puts "Bug notes: #{Journal.count}/#{MantisBugNote.count}"
  377. puts "Bug files: #{Attachment.count}/#{MantisBugFile.count}"
  378. puts "Bug relations: #{IssueRelation.count}/#{MantisBugRelationship.count}"
  379. puts "Bug monitors: #{Watcher.count}/#{MantisBugMonitor.count}"
  380. puts "News: #{News.count}/#{MantisNews.count}"
  381. puts "Custom fields: #{IssueCustomField.count}/#{MantisCustomField.count}"
  382. end
  383. def self.encoding(charset)
  384. @ic = Iconv.new('UTF-8', charset)
  385. rescue Iconv::InvalidEncoding
  386. return false
  387. end
  388. def self.establish_connection(params)
  389. constants.each do |const|
  390. klass = const_get(const)
  391. next unless klass.respond_to? 'establish_connection'
  392. klass.establish_connection params
  393. end
  394. end
  395. def self.encode(text)
  396. @ic.iconv text
  397. rescue
  398. text
  399. end
  400. end
  401. puts
  402. if Redmine::DefaultData::Loader.no_data?
  403. puts "Redmine configuration need to be loaded before importing data."
  404. puts "Please, run this first:"
  405. puts
  406. puts " rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
  407. exit
  408. end
  409. puts "WARNING: Your Redmine data will be deleted during this process."
  410. print "Are you sure you want to continue ? [y/N] "
  411. STDOUT.flush
  412. break unless STDIN.gets.match(/^y$/i)
  413. # Default Mantis database settings
  414. db_params = {:adapter => 'mysql2',
  415. :database => 'bugtracker',
  416. :host => 'localhost',
  417. :username => 'root',
  418. :password => '' }
  419. puts
  420. puts "Please enter settings for your Mantis database"
  421. [:adapter, :host, :database, :username, :password].each do |param|
  422. print "#{param} [#{db_params[param]}]: "
  423. value = STDIN.gets.chomp!
  424. db_params[param] = value unless value.blank?
  425. end
  426. while true
  427. print "encoding [UTF-8]: "
  428. STDOUT.flush
  429. encoding = STDIN.gets.chomp!
  430. encoding = 'UTF-8' if encoding.blank?
  431. break if MantisMigrate.encoding encoding
  432. puts "Invalid encoding!"
  433. end
  434. puts
  435. # Make sure bugs can refer bugs in other projects
  436. Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
  437. # Turn off email notifications
  438. Setting.notified_events = []
  439. MantisMigrate.establish_connection db_params
  440. MantisMigrate.migrate
  441. end
  442. end