migrate_from_mantis_inheritable_categories.patch

Konstantin Tkachenko, 2014-09-30 12:12

Download (10.4 KB)

View differences:

lib/tasks/migrate_from_mantis.rake (Arbeitskopie)
41 41
                        }
42 42

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

  
53 53
      TRACKER_BUG = Tracker.find_by_position(1)
......
119 119
      has_many :members, :class_name => "MantisProjectUser", :foreign_key => :project_id
120 120

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

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

  
......
136 140
    end
137 141

  
138 142
    class MantisCategory < ActiveRecord::Base
139
      self.table_name = :mantis_project_category_table
143
      self.table_name = :mantis_category_table
144
      def category
145
        read_attribute(:name).slice(0,30)
146
      end
140 147
    end
141 148

  
142 149
    class MantisProjectUser < ActiveRecord::Base
......
146 153
    class MantisBug < ActiveRecord::Base
147 154
      self.table_name = :mantis_bug_table
148 155
      belongs_to :bug_text, :class_name => "MantisBugText", :foreign_key => :bug_text_id
156
      belongs_to :category, :class_name => "MantisCategory", :foreign_key => :category_id
149 157
      has_many :bug_notes, :class_name => "MantisBugNote", :foreign_key => :bug_id
150 158
      has_many :bug_files, :class_name => "MantisBugFile", :foreign_key => :bug_id
151 159
      has_many :bug_monitors, :class_name => "MantisBugMonitor", :foreign_key => :bug_id
......
213 221

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

  
......
245 253
        u = User.new :firstname => encode(user.firstname),
246 254
                     :lastname => encode(user.lastname),
247 255
                     :mail => user.email,
248
                     :last_login_on => user.last_visit
256
                     :last_login_on => (user.last_visit ? Time.at(user.last_visit).to_date : nil)
249 257
        u.login = user.username
250 258
        u.password = 'mantis'
251 259
        u.status = User::STATUS_LOCKED if user.enabled != 1
......
262 270
      Project.destroy_all
263 271
      projects_map = {}
264 272
      versions_map = {}
273
      versions_locked = []
265 274
      categories_map = {}
275
      inheritable_categories = false
266 276
      MantisProject.all.each do |project|
267 277
        p = Project.new :name => encode(project.name),
268 278
                        :description => encode(project.description)
279
        inheritable_categories = p.has_attribute?("inherit_categs") unless inheritable_categories
269 280
        p.identifier = project.identifier
281
        p.is_public = (project.view_state == 10)
270 282
        next unless p.save
271 283
        projects_map[project.id] = p.id
272
        p.enabled_module_names = ['issue_tracking', 'news', 'wiki']
284
        p.enabled_module_names = ['issue_tracking', 'news', 'wiki', 'calendar', 'gantt', 'time_tracking']
273 285
        p.trackers << TRACKER_BUG unless p.trackers.include?(TRACKER_BUG)
274 286
        p.trackers << TRACKER_FEATURE unless p.trackers.include?(TRACKER_FEATURE)
275 287
        print '.'
......
286 298
        project.versions.each do |version|
287 299
          v = Version.new :name => encode(version.version),
288 300
                          :description => encode(version.description),
289
                          :effective_date => (version.date_order ? version.date_order.to_date : nil)
301
                          :effective_date => (version.date_order ? Time.at(version.date_order).to_date : nil)
290 302
          v.project = p
303
          # 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
304
          if version.obsolete == 1
305
            versions_locked << v
306
          end
291 307
          v.save
292 308
          versions_map[version.id] = v.id
293 309
        end
294 310

  
295 311
        # Project categories
296 312
        project.categories.each do |category|
297
          g = IssueCategory.new :name => category.category[0,30]
313
          g = IssueCategory.new :name => category.category
298 314
          g.project = p
299 315
          g.save
300 316
          categories_map[category.category] = g.id
301 317
        end
302 318
      end
303 319
      puts
320
      
321
      # Project Hierarchy
322
      print "Making Project Hierarchy"
323
      MantisProjectHierarchy.find(:all).each do |link|
324
        next unless p = Project.find_by_id(projects_map[link.child_id])
325
        p.set_parent!(projects_map[link.parent_id])
326
        if link.inherit_parent == 1
327
          if inheritable_categories # p.has_attribute?("inherit_categs")
328
            p.inherit_categs = 1
329
          end
330
          # Turn on the users inheritance
331
          p.inherit_members = 1
332
          p.save
333
          # Turn on the versions inheritance
334
          parent_project = Project.find_by_id(projects_map[link.parent_id])
335
          parent_project.versions.each do |version|
336
            version.sharing = 'descendants'
337
            version.save
338
          end
339
        end
340
        print '.'
341
      end
342
      puts
304 343

  
305 344
      # Bugs
306 345
      print "Migrating bugs"
346
      ActiveRecord::Base.record_timestamps = false
307 347
      Issue.destroy_all
308 348
      issues_map = {}
309 349
      keep_bug_ids = (Issue.count == 0)
......
313 353
                      :subject => encode(bug.summary),
314 354
                      :description => encode(bug.bug_text.full_description),
315 355
                      :priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
316
                      :created_on => bug.date_submitted,
317
                      :updated_on => bug.last_updated
356
                      :created_on => (bug.date_submitted ? Time.at(bug.date_submitted).to_date : nil),
357
                      :updated_on => (bug.last_updated ? Time.at(bug.last_updated).to_date : nil)
318 358
        i.author = User.find_by_id(users_map[bug.reporter_id])
319
        i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category[0,30]) unless bug.category.blank?
320
        i.fixed_version = Version.find_by_project_id_and_name(i.project_id, bug.fixed_in_version) unless bug.fixed_in_version.blank?
359
        i.category = IssueCategory.find_by_project_id_and_name(i.project_id, bug.category.category) unless bug.category.blank?
360
        if (i.category.nil?) && inheritable_categories
361
          p = Project.find_by_id(projects_map[bug.project_id])
362
          i.category = p.inherited_categories.find{|c| c.name == bug.category.category}
363
        end
364
        if !(bug.fixed_in_version.blank? && bug.target_version.blank?)
365
          p = Project.find_by_id(projects_map[bug.project_id])
366
          if !bug.fixed_in_version.blank?
367
            vv = bug.fixed_in_version
368
          else
369
            vv = bug.target_version
370
          end
371
          i.fixed_version = p.shared_versions.find{|v| v.name == vv}
372
        end
321 373
        i.status = STATUS_MAPPING[bug.status] || DEFAULT_STATUS
322 374
        i.tracker = (bug.severity == 10 ? TRACKER_FEATURE : TRACKER_BUG)
375
        i.start_date = Time.at(bug.date_submitted).to_date
376
        if bug.due_date > bug.date_submitted
377
          i.due_date = Time.at(bug.due_date).to_date
378
        elsif bug.status >= 80 && i.updated_on > i.start_date
379
          i.due_date = i.updated_on
380
        end
323 381
        i.id = bug.id if keep_bug_ids
324 382
        next unless i.save
325 383
        issues_map[bug.id] = i.id
......
337 395
        bug.bug_notes.each do |note|
338 396
          next unless users_map[note.reporter_id]
339 397
          n = Journal.new :notes => encode(note.bug_note_text.note),
340
                          :created_on => note.date_submitted
398
                          :created_on => Time.at(note.date_submitted).to_date
341 399
          n.user = User.find_by_id(users_map[note.reporter_id])
342 400
          n.journalized = i
343 401
          n.save
......
345 403

  
346 404
        # Bug files
347 405
        bug.bug_files.each do |file|
348
          a = Attachment.new :created_on => file.date_added
406
          a = Attachment.new :created_on => (file.date_added ? Time.at(file.date_added).to_date : nil)
349 407
          a.file = file
350 408
          a.author = User.first
351 409
          a.container = i
......
359 417
        end
360 418
      end
361 419

  
420
      # Locking versions after all bugs where migrated, otherwise no bugs with the locked version as target will be migrated
421
      versions_locked.each do |version|
422
        version.status = 'locked'
423
        version.save
424
      end
425

  
362 426
      # update issue id sequence if needed (postgresql)
363 427
      Issue.connection.reset_pk_sequence!(Issue.table_name) if Issue.connection.respond_to?('reset_pk_sequence!')
364 428
      puts
365 429

  
366 430
      # Bug relationships
431
      #debugger
432
      ActiveRecord::Base.record_timestamps = true
367 433
      print "Migrating bug relations"
368 434
      MantisBugRelationship.all.each do |relation|
369 435
        next unless issues_map[relation.source_bug_id] && issues_map[relation.destination_bug_id]
......
384 450
        n = News.new :project_id => projects_map[news.project_id],
385 451
                     :title => encode(news.headline[0..59]),
386 452
                     :description => encode(news.body),
387
                     :created_on => news.date_posted
453
                     :created_on => Time.at(news.date_posted).to_date
388 454
        n.author = User.find_by_id(users_map[news.poster_id])
389 455
        n.save
390 456
        print '.'