Project

General

Profile

Defect #14969 » migrate_from_mantis.rake

migrate_from_mantis.rake edited script - Mac Chang, 2013-09-23 05:28

 
1
# Redmine - project management software
2
# Copyright (C) 2006-2013  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

    
18
desc 'Mantis migration script'
19

    
20
require 'active_record'
21
require 'iconv' if RUBY_VERSION < '1.9'
22
require 'pp'
23

    
24
namespace :redmine do
25
task :migrate_from_mantis => :environment do
26

    
27
  module MantisMigrate
28

    
29
      DEFAULT_STATUS = IssueStatus.default
30
      assigned_status = IssueStatus.find_by_position(2)
31
      resolved_status = IssueStatus.find_by_position(3)
32
      feedback_status = IssueStatus.find_by_position(4)
33
      closed_status = IssueStatus.where(:is_closed => true).first
34
      STATUS_MAPPING = {10 => DEFAULT_STATUS,  # new
35
                        20 => feedback_status, # feedback
36
                        30 => DEFAULT_STATUS,  # acknowledged
37
                        40 => DEFAULT_STATUS,  # confirmed
38
                        50 => assigned_status, # assigned
39
                        80 => resolved_status, # resolved
40
                        90 => closed_status    # closed
41
                        }
42

    
43
      priorities = IssuePriority.all
44
      DEFAULT_PRIORITY = priorities[1]
45
      PRIORITY_MAPPING = {10 => priorities[0], # none
46
                          20 => priorities[0], # low
47
                          30 => priorities[1], # normal
48
                          40 => priorities[2], # high
49
                          50 => priorities[3], # urgent
50
                          60 => priorities[4]  # immediate
51
                          }
52

    
53
      TRACKER_BUG = Tracker.find_by_position(1)
54
      TRACKER_FEATURE = Tracker.find_by_position(2)
55

    
56
      roles = Role.where(:builtin => 0).order('position ASC').all
57
      manager_role = roles[0]
58
      developer_role = roles[1]
59
      DEFAULT_ROLE = roles.last
60
      ROLE_MAPPING = {10 => DEFAULT_ROLE,   # viewer
61
                      25 => DEFAULT_ROLE,   # reporter
62
                      40 => DEFAULT_ROLE,   # updater
63
                      55 => developer_role, # developer
64
                      70 => manager_role,   # manager
65
                      90 => manager_role    # administrator
66
                      }
67

    
68
      CUSTOM_FIELD_TYPE_MAPPING = {0 => 'string', # String
69
                                   1 => 'int',    # Numeric
70
                                   2 => 'int',    # Float
71
                                   3 => 'list',   # Enumeration
72
                                   4 => 'string', # Email
73
                                   5 => 'bool',   # Checkbox
74
                                   6 => 'list',   # List
75
                                   7 => 'list',   # Multiselection list
76
                                   8 => 'date',   # Date
77
                                   }
78

    
79
      RELATION_TYPE_MAPPING = {1 => IssueRelation::TYPE_RELATES,    # related to
80
                               2 => IssueRelation::TYPE_RELATES,    # parent of
81
                               3 => IssueRelation::TYPE_RELATES,    # child of
82
                               0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
83
                               4 => IssueRelation::TYPE_DUPLICATES  # has duplicate
84
                               }
85

    
86
    class MantisUser < ActiveRecord::Base
87
      self.table_name = :mantis_user_table
88

    
89
      def firstname
90
        @firstname = realname.blank? ? username : realname.split.first[0..29]
91
        @firstname
92
      end
93

    
94
      def lastname
95
        @lastname = realname.blank? ? '-' : realname.split[1..-1].join(' ')[0..29]
96
        @lastname = '-' if @lastname.blank?
97
        @lastname
98
      end
99

    
100
      def email
101
        if read_attribute(:email).match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) &&
102
             !User.find_by_mail(read_attribute(:email))
103
          @email = read_attribute(:email)
104
        else
105
          @email = "#{username}@foo.bar"
106
        end
107
      end
108

    
109
      def username
110
        read_attribute(:username)[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '-')
111
      end
112
    end
113

    
114
    class MantisProject < ActiveRecord::Base
115
      self.table_name = :mantis_project_table
116
      has_many :versions, :class_name => "MantisVersion", :foreign_key => :project_id
117
      has_many :categories, :class_name => "MantisCategory", :foreign_key => :project_id
118
      has_many :news, :class_name => "MantisNews", :foreign_key => :project_id
119
      has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
120

    
121
      def identifier
122
        read_attribute(:name).slice(0, Project::IDENTIFIER_MAX_LENGTH).downcase.gsub(/[^a-z0-9\-]+/, '-')
123
      end
124
    end
125

    
126
    class MantisProjectHierarchy < ActiveRecord::Base
127
        set_table_name :mantis_project_hierarchy_table
128
    end
129
   
130
    class MantisVersion < ActiveRecord::Base
131
      self.table_name = :mantis_project_version_table
132

    
133
      def version
134
        read_attribute(:version)[0..29]
135
      end
136

    
137
      def description
138
        read_attribute(:description)[0..254]
139
      end
140
    end
141

    
142
    class MantisCategory < ActiveRecord::Base
143
      set_table_name :mantis_category_table
144
      def category
145
        read_attribute(:name).slice(0,30)
146
      end
147
    end
148

    
149
    class MantisProjectUser < ActiveRecord::Base
150
      self.table_name = :mantis_project_user_list_table
151
    end
152

    
153
    class MantisBug < ActiveRecord::Base
154
      self.table_name = :mantis_bug_table
155
      belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
156
      has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
157
      has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
158
      has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
159
      belongs_to :category, :class_name => "MantisCategory", :foreign_key => :category_id
160
    end
161

    
162
    class MantisBugText < ActiveRecord::Base
163
      self.table_name = :mantis_bug_text_table
164

    
165
      # Adds Mantis steps_to_reproduce and additional_information fields
166
      # to description if any
167
      def full_description
168
        full_description = description
169
        full_description += "\n\n*Steps to reproduce:*\n\n#{steps_to_reproduce}" unless steps_to_reproduce.blank?
170
        full_description += "\n\n*Additional information:*\n\n#{additional_information}" unless additional_information.blank?
171
        full_description
172
      end
173
    end
174

    
175
    class MantisBugNote < ActiveRecord::Base
176
      self.table_name = :mantis_bugnote_table
177
      belongs_to :bug, :class_name => "MantisBug", :foreign_key => :bug_id
178
      belongs_to :bug_note_text, :class_name => "MantisBugNoteText", :foreign_key => :bugnote_text_id
179
    end
180

    
181
    class MantisBugNoteText < ActiveRecord::Base
182
      self.table_name = :mantis_bugnote_text_table
183
    end
184

    
185
    class MantisBugFile < ActiveRecord::Base
186
      self.table_name = :mantis_bug_file_table
187

    
188
      def size
189
        filesize
190
      end
191

    
192
      def original_filename
193
        MantisMigrate.encode(filename)
194
      end
195

    
196
      def content_type
197
        file_type
198
      end
199

    
200
      def read(*args)
201
          if @read_finished
202
              nil
203
          else
204
              @read_finished = true
205
              content
206
          end
207
      end
208
    end
209

    
210
    class MantisBugRelationship < ActiveRecord::Base
211
      self.table_name = :mantis_bug_relationship_table
212
    end
213

    
214
    class MantisBugMonitor < ActiveRecord::Base
215
      self.table_name = :mantis_bug_monitor_table
216
    end
217

    
218
    class MantisNews < ActiveRecord::Base
219
      self.table_name = :mantis_news_table
220
    end
221

    
222
    class MantisCustomField < ActiveRecord::Base
223
      self.table_name = :mantis_custom_field_table
224
      set_inheritance_column :none
225
      has_many :values, :class_name => "MantisCustomFieldString", :foreign_key => :field_id
226
      has_many :projects, :class_name => "MantisCustomFieldProject", :foreign_key => :field_id
227

    
228
      def format
229
        read_attribute :type
230
      end
231

    
232
      def name
233
        read_attribute(:name)[0..29]
234
      end
235
    end
236

    
237
    class MantisCustomFieldProject < ActiveRecord::Base
238
      self.table_name = :mantis_custom_field_project_table
239
    end
240

    
241
    class MantisCustomFieldString < ActiveRecord::Base
242
      self.table_name = :mantis_custom_field_string_table
243
    end
244

    
245
    def self.migrate
246

    
247
      # Users
248
      print "Migrating users"
249
      User.delete_all "login <> 'admin'"
250
      users_map = {}
251
      users_migrated = 0
252
      MantisUser.all.each do |user|
253
        u = User.new :firstname => encode(user.firstname), 
254
             :lastname => encode(user.lastname),
255
             :mail => user.email,
256
             :last_login_on => user.last_visit
257
       u.login = user.username
258
       u.password = 'mantis'
259
       u.status = User::STATUS_LOCKED if user.enabled != 1
260
       u.admin = true if user.access_level == 90
261
       next unless u.save!
262
       users_migrated += 1
263
       users_map[user.id] = u.id
264
       print '.'
265
      end
266
      puts
267

    
268
      # Projects
269
      print "Migrating projects"
270
      Project.destroy_all
271
      projects_map = {}
272
      versions_map = {}
273
      categories_map = {}
274
      MantisProject.all.each do |project|
275
        p = Project.new :name => encode(project.name), 
276
                        :description => encode(project.description),
277
                        :trackers => [TRACKER_BUG,TRACKER_FEATURE]
278
        p.identifier = project.identifier
279
        p.is_public = (project.view_state == 10)
280
        next unless p.save
281
        projects_map[project.id] = p.id
282
        p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
283
        print '.'
284

    
285
        # Project members
286
        project.members.each do |member|
287
          m = Member.new :user => User.find_by_id(users_map[member.user_id]),
288
                         :roles => [ROLE_MAPPING[member.access_level] || DEFAULT_ROLE]
289
          m.project = p
290
          m.save
291
        end	
292

    
293
       # Project versions
294
        project.versions.each do |version|
295
          v = Version.new :name => encode(version.version),
296
                          :description => encode(version.description),
297
                          :effective_date => (version.date_order ? Time.at(version.date_order).to_date : nil)
298
          v.project = p
299
          v.save
300
          versions_map[version.id] = v.id
301
        end
302
 
303
        # Project categories
304
        project.categories.each do |category|
305
          g = IssueCategory.new :name => category.category
306
          g.project = p
307
          g.save
308
          categories_map[category.category] = g.id
309
    	  end
310
      end
311
      puts
312
      
313
      # Project Hierarchy
314
      print "Making Project Hierarchy"
315
      MantisProjectHierarchy.find(:all).each do |link|
316
        next unless p = Project.find_by_id(projects_map[link.child_id])
317
        p.set_parent!(projects_map[link.parent_id])
318
        print '.'
319
      end
320
      puts
321

    
322
      # Bugs
323
      print "Migrating bugs"
324
      Issue.destroy_all
325
      issues_map = {}
326
      keep_bug_ids = (Issue.count == 0)
327
      MantisBug.find_each(:batch_size => 200) do |bug|
328
        next unless projects_map[bug.project_id] && users_map[bug.reporter_id]
329
        i = Issue.new :project_id => projects_map[bug.project_id], 
330
                      :subject => encode(bug.summary),
331
                      :description => encode(bug.bug_text.full_description),
332
                      :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
333
                      :created_on => Time.at(bug.date_submitted_redmine)
334
        i.author = User.find_by_id(users_map[bug.reporter_id])
335
        i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category) unless bug.category.blank?
336
        i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
337
        i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
338
        i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
339
        i.start_date = Time.at(bug.date_submitted_redmine).to_date
340
        i.due_date = (bug.due_date <= bug.date_submitted_redmine)? nil:Time.at(bug.due_date).to_date
341
        i.updated_on = Time.at(bug.last_updated_redmine)
342
        i.id = bug.id if keep_bug_ids
343
        next unless i.save
344
        issues_map[bug.id] = i.id
345
        print '.'
346
        STDOUT.flush
347

    
348
        # Assignee
349
        # Redmine checks that the assignee is a project member
350
        if (bug.handler_id && users_map[bug.handler_id])
351
          i.assigned_to = User.find_by_id(users_map[bug.handler_id])
352
          i.save(:validate => false)
353
        end
354
      
355
        # Bug notes
356
        bug.bug_notes.each do |note|
357
          next unless users_map[note.reporter_id]
358
          n = Journal.new :notes => encode(note.bug_note_text.note),
359
                          :created_on => Time.at(note.date_submitted_redmine)
360
          n.user = User.find_by_id(users_map[note.reporter_id])
361
          n.journalized = i
362
          n.save
363
        end
364
      
365
        # Bug files
366
        bug.bug_files.each do |file|
367
          a = Attachment.new :created_on => file.date_added_redmine
368
          a.file = file
369
          a.author = User.first
370
          a.container = i
371
          a.save
372
        end
373

    
374
        # Bug monitors
375
        bug.bug_monitors.each do |monitor|
376
          next unless users_map[monitor.user_id]
377
          i.add_watcher(User.find_by_id(users_map[monitor.user_id]))
378
        end
379
      end
380

    
381
      # update issue id sequence if needed (postgresql)
382
      Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
383
      puts
384

    
385
      # Bug relationships
386
      print "Migrating bug relations"
387
      MantisBugRelationship.all.each do |relation|
388
        next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
389
        r = IssueRelation.new :relation_type => RELATION_TYPE_MAPPING[relation.relationship_type]
390
        r.issue_from = Issue.find_by_id(issues_map[relation.source_bug_id])
391
        r.issue_to = Issue.find_by_id(issues_map[relation.destination_bug_id])
392
        pp r unless r.save
393
        print '.'
394
        STDOUT.flush
395
      end
396
      puts
397
      # News
398
      print "Migrating news"
399
      News.destroy_all
400
      MantisNews.where('project_id > 0').all.each do |news|
401
        next unless projects_map[news.project_id]
402
        n = News.new :project_id => projects_map[news.project_id],
403
                     :title => encode(news.headline[0..59]),
404
                     :description => encode(news.body),
405
                     :created_on => Time.at(news.date_posted_redmine)
406
        n.author = User.find_by_id(users_map[news.poster_id])
407
        n.save
408
        print '.'
409
        STDOUT.flush
410
      end
411
      puts
412

    
413
      # Custom fields
414
      print "Migrating custom fields"
415
      IssueCustomField.destroy_all
416
      MantisCustomField.all.each do |field|
417
        f = IssueCustomField.new :name => field.name[0..29],
418
                                 :field_format => CUSTOM_FIELD_TYPE_MAPPING[field.format],
419
                                 :min_length => field.length_min,
420
                                 :max_length => field.length_max,
421
                                 :regexp => field.valid_regexp,
422
                                 :possible_values => field.possible_values.split('|'),
423
                                 :is_required => field.require_report?
424
        next unless f.save
425
        print '.'
426
        STDOUT.flush
427
        # Trackers association
428
        f.trackers = Tracker.all
429

    
430
        # Projects association
431
        field.projects.each do |project|
432
          f.projects << Project.find_by_id(projects_map[project.project_id]) if projects_map[project.project_id]
433
        end
434

    
435
        # Values
436
        field.values.each do |value|
437
          v = CustomValue.new :custom_field_id => f.id,
438
                              :value => value.value
439
          v.customized = Issue.find_by_id(issues_map[value.bug_id]) if issues_map[value.bug_id]
440
          v.save
441
        end unless f.new_record?
442
      end
443
      puts
444

    
445
      puts
446
      puts "Users:           #{users_migrated}/#{MantisUser.count}"
447
      puts "Projects:        #{Project.count}/#{MantisProject.count}"
448
      puts "Memberships:     #{Member.count}/#{MantisProjectUser.count}"
449
      puts "Versions:        #{Version.count}/#{MantisVersion.count}"
450
      puts "Categories:      #{IssueCategory.count}/#{MantisCategory.count}"
451
      puts "Bugs:            #{Issue.count}/#{MantisBug.count}"
452
      puts "Bug notes:       #{Journal.count}/#{MantisBugNote.count}"
453
      puts "Bug files:       #{Attachment.count}/#{MantisBugFile.count}"
454
      puts "Bug relations:   #{IssueRelation.count}/#{MantisBugRelationship.count}"
455
      puts "Bug monitors:    #{Watcher.count}/#{MantisBugMonitor.count}"
456
      puts "News:            #{News.count}/#{MantisNews.count}"
457
      puts "Custom fields:   #{IssueCustomField.count}/#{MantisCustomField.count}"
458
    end
459

    
460
    def self.encoding(charset)
461
      @charset = charset
462
    end
463

    
464
    def self.establish_connection(params)
465
      constants.each do |const|
466
        klass = const_get(const)
467
        next unless klass.respond_to? 'establish_connection'
468
        klass.establish_connection params
469
      end
470
    end
471

    
472
    def self.encode(text)
473
      if RUBY_VERSION < '1.9'
474
        @ic ||= Iconv.new('UTF-8', @charset)
475
        @ic.iconv text
476
      else
477
        text.to_s.force_encoding(@charset).encode('UTF-8')
478
      end
479
    end
480
  end
481

    
482
  puts
483
  if Redmine::DefaultData::Loader.no_data?
484
    puts "Redmine configuration need to be loaded before importing data."
485
    puts "Please, run this first:"
486
    puts
487
    puts "  rake redmine:load_default_data RAILS_ENV=\"#{ENV['RAILS_ENV']}\""
488
    exit
489
  end
490

    
491
  puts "WARNING: Your Redmine data will be deleted during this process."
492
  print "Are you sure you want to continue ? [y/N] "
493
  STDOUT.flush
494
  break unless STDIN.gets.match(/^y$/i)
495

    
496
  # Default Mantis database settings
497
  db_params = {:adapter => 'mysql2',
498
               :database => 'bugtracker',
499
               :host => '10.7.218.214',
500
               :username => 'root',
501
               :password => 'root' }
502

    
503
  puts
504
  puts "Please enter settings for your Mantis database"
505
  [:adapter, :host, :database, :username, :password].each do |param|
506
    print "#{param} [#{db_params[param]}]: "
507
    value = STDIN.gets.chomp!
508
    db_params[param] = value unless value.blank?
509
  end
510

    
511
  while true
512
    print "encoding [UTF-8]: "
513
    STDOUT.flush
514
    encoding = STDIN.gets.chomp!
515
    encoding = 'UTF-8' if encoding.blank?
516
    break if MantisMigrate.encoding encoding
517
    puts "Invalid encoding!"
518
  end
519
  puts
520

    
521
  # Make sure bugs can refer bugs in other projects
522
  Setting.cross_project_issue_relations = 1 if Setting.respond_to? 'cross_project_issue_relations'
523

    
524
  old_notified_events = Setting.notified_events
525
  begin
526
    # Turn off email notifications temporarily
527
    Setting.notified_events = []
528
    # Run the migration
529
    MantisMigrate.establish_connection db_params
530
    MantisMigrate.migrate
531
  ensure
532
    # Restore previous notification settings even if the migration fails
533
    Setting.notified_events = old_notified_events
534
  end
535

    
536
end
537
end
(2-2/2)