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

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