1
|
# redMine - project management software
|
2
|
# Copyright (C) 2006-2007 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
|
# Bugzilla migration by Arjen Roodselaar, Lindix bv edited by Oliver Sigge
|
19
|
#
|
20
|
# Successfully tested with Bugzilla 2.20, Redmine devBuild, Rails 2.1
|
21
|
#
|
22
|
# Please note that the users in the Bugzilla database must have a forename and surename
|
23
|
|
24
|
desc 'Bugzilla migration script'
|
25
|
|
26
|
require 'active_record'
|
27
|
require 'iconv'
|
28
|
require 'pp'
|
29
|
|
30
|
namespace :redmine do
|
31
|
task :migrate_from_bugzilla => :environment do
|
32
|
|
33
|
module BugzillaMigrate
|
34
|
|
35
|
DEFAULT_STATUS = IssueStatus.default
|
36
|
CLOSED_STATUS = IssueStatus.find :first, :conditions => { :is_closed => true }
|
37
|
assigned_status = IssueStatus.find_by_position(2)
|
38
|
resolved_status = IssueStatus.find_by_position(3)
|
39
|
feedback_status = IssueStatus.find_by_position(4)
|
40
|
|
41
|
STATUS_MAPPING = {
|
42
|
"UNCONFIRMED" => DEFAULT_STATUS,
|
43
|
"NEW" => DEFAULT_STATUS,
|
44
|
"VERIFIED" => DEFAULT_STATUS,
|
45
|
"ASSIGNED" => assigned_status,
|
46
|
"REOPENED" => assigned_status,
|
47
|
"RESOLVED" => resolved_status,
|
48
|
"CLOSED" => CLOSED_STATUS
|
49
|
}
|
50
|
# actually close resolved issues
|
51
|
resolved_status.is_closed = true
|
52
|
resolved_status.save
|
53
|
|
54
|
priorities = Enumeration.get_values('IPRI')
|
55
|
PRIORITY_MAPPING = {
|
56
|
"P1" => priorities[1], # low
|
57
|
"P2" => priorities[2], # normal
|
58
|
"P3" => priorities[3], # high
|
59
|
"P4" => priorities[4], # urgent
|
60
|
"P5" => priorities[5] # immediate
|
61
|
}
|
62
|
DEFAULT_PRIORITY = PRIORITY_MAPPING["P2"]
|
63
|
|
64
|
TRACKER_BUG = Tracker.find_by_position(1)
|
65
|
TRACKER_FEATURE = Tracker.find_by_position(2)
|
66
|
|
67
|
reporter_role = Role.find_by_position(5)
|
68
|
developer_role = Role.find_by_position(4)
|
69
|
manager_role = Role.find_by_position(3)
|
70
|
DEFAULT_ROLE = reporter_role
|
71
|
|
72
|
CUSTOM_FIELD_TYPE_MAPPING = {
|
73
|
0 => 'string', # String
|
74
|
1 => 'int', # Numeric
|
75
|
2 => 'int', # Float
|
76
|
3 => 'list', # Enumeration
|
77
|
4 => 'string', # Email
|
78
|
5 => 'bool', # Checkbox
|
79
|
6 => 'list', # List
|
80
|
7 => 'list', # Multiselection list
|
81
|
8 => 'date', # Date
|
82
|
}
|
83
|
|
84
|
RELATION_TYPE_MAPPING = {
|
85
|
0 => IssueRelation::TYPE_DUPLICATES, # duplicate of
|
86
|
1 => IssueRelation::TYPE_RELATES, # related to
|
87
|
2 => IssueRelation::TYPE_RELATES, # parent of
|
88
|
3 => IssueRelation::TYPE_RELATES, # child of
|
89
|
4 => IssueRelation::TYPE_DUPLICATES # has duplicate
|
90
|
}
|
91
|
|
92
|
class BugzillaProfile < ActiveRecord::Base
|
93
|
set_table_name :profiles
|
94
|
set_primary_key :userid
|
95
|
|
96
|
has_and_belongs_to_many :groups,
|
97
|
:class_name => "BugzillaGroup",
|
98
|
:join_table => :user_group_map,
|
99
|
:foreign_key => :user_id,
|
100
|
:association_foreign_key => :group_id
|
101
|
|
102
|
def login
|
103
|
login_name[0..29].gsub(/[^a-zA-Z0-9_\-@\.]/, '')
|
104
|
end
|
105
|
|
106
|
def email
|
107
|
if login_name.match(/^.*@.*$/i)
|
108
|
login_name
|
109
|
else
|
110
|
"#{login_name}@foo.bar"
|
111
|
end
|
112
|
end
|
113
|
|
114
|
def firstname
|
115
|
read_attribute(:realname).blank? ? login_name : read_attribute(:realname).split.first[0..29]
|
116
|
end
|
117
|
|
118
|
def lastname
|
119
|
read_attribute(:realname).blank? ? login_name : read_attribute(:realname).split[1..-1].join(' ')[0..29]
|
120
|
end
|
121
|
end
|
122
|
|
123
|
class BugzillaGroup < ActiveRecord::Base
|
124
|
set_table_name :groups
|
125
|
|
126
|
has_and_belongs_to_many :profiles,
|
127
|
:class_name => "BugzillaProfile",
|
128
|
:join_table => :user_group_map,
|
129
|
:foreign_key => :group_id,
|
130
|
:association_foreign_key => :user_id
|
131
|
end
|
132
|
|
133
|
class BugzillaProduct < ActiveRecord::Base
|
134
|
set_table_name :products
|
135
|
|
136
|
has_many :components, :class_name => "BugzillaComponent", :foreign_key => :product_id
|
137
|
has_many :versions, :class_name => "BugzillaVersion", :foreign_key => :product_id
|
138
|
has_many :bugs, :class_name => "BugzillaBug", :foreign_key => :product_id
|
139
|
end
|
140
|
|
141
|
class BugzillaComponent < ActiveRecord::Base
|
142
|
set_table_name :components
|
143
|
end
|
144
|
|
145
|
class BugzillaVersion < ActiveRecord::Base
|
146
|
set_table_name :versions
|
147
|
end
|
148
|
|
149
|
class BugzillaBug < ActiveRecord::Base
|
150
|
set_table_name :bugs
|
151
|
set_primary_key :bug_id
|
152
|
|
153
|
belongs_to :product, :class_name => "BugzillaProduct", :foreign_key => :product_id
|
154
|
has_many :descriptions, :class_name => "BugzillaDescription", :foreign_key => :bug_id
|
155
|
end
|
156
|
|
157
|
class BugzillaDescription < ActiveRecord::Base
|
158
|
set_table_name :longdescs
|
159
|
|
160
|
belongs_to :bug, :class_name => "BugzillaBug", :foreign_key => :bug_id
|
161
|
|
162
|
def eql(desc)
|
163
|
self.bug_when == desc.bug_when
|
164
|
end
|
165
|
|
166
|
def === desc
|
167
|
self.eql(desc)
|
168
|
end
|
169
|
|
170
|
def text
|
171
|
if self.thetext.blank?
|
172
|
return nil
|
173
|
else
|
174
|
self.thetext
|
175
|
end
|
176
|
end
|
177
|
end
|
178
|
|
179
|
def self.establish_connection(params)
|
180
|
constants.each do |const|
|
181
|
klass = const_get(const)
|
182
|
next unless klass.respond_to? 'establish_connection'
|
183
|
klass.establish_connection params
|
184
|
end
|
185
|
end
|
186
|
|
187
|
def self.migrate
|
188
|
|
189
|
# Profiles
|
190
|
print "Migrating profiles\n"
|
191
|
$stdout.flush
|
192
|
User.delete_all "login <> 'admin'"
|
193
|
users_map = {}
|
194
|
users_migrated = 0
|
195
|
BugzillaProfile.find(:all).each do |profile|
|
196
|
user = User.new
|
197
|
user.login = profile.login
|
198
|
user.password = "bugzilla"
|
199
|
user.firstname = profile.firstname
|
200
|
user.lastname = profile.lastname
|
201
|
user.mail = profile.email
|
202
|
user.status = User::STATUS_LOCKED if !profile.disabledtext.empty?
|
203
|
user.admin = true if profile.groups.include?(BugzillaGroup.find_by_name("admin"))
|
204
|
|
205
|
next unless user.save
|
206
|
users_migrated += 1
|
207
|
users_map[profile.userid] = user
|
208
|
print '.'
|
209
|
$stdout.flush
|
210
|
end
|
211
|
|
212
|
|
213
|
# Products
|
214
|
puts
|
215
|
print "Migrating products"
|
216
|
$stdout.flush
|
217
|
|
218
|
Project.destroy_all
|
219
|
projects_map = {}
|
220
|
versions_map = {}
|
221
|
categories_map = {}
|
222
|
BugzillaProduct.find(:all).each do |product|
|
223
|
project = Project.new
|
224
|
project.name = product.name
|
225
|
project.description = product.description
|
226
|
project.identifier = product.name.downcase.gsub(/[0-9_\s]*/, '')[0..15]
|
227
|
project.trackers[0] = TRACKER_BUG
|
228
|
#puts "Name: #{product.name}; Identifier: #{project.identifier}\n"
|
229
|
next unless project.save
|
230
|
projects_map[product.id] = project
|
231
|
print "."
|
232
|
$stdout.flush
|
233
|
|
234
|
# Enable issue tracking
|
235
|
enabled_module = EnabledModule.new(
|
236
|
:project => project,
|
237
|
:name => 'issue_tracking'
|
238
|
)
|
239
|
enabled_module.save
|
240
|
# Components
|
241
|
product.components.each do |component|
|
242
|
category = IssueCategory.new(:name => component.name[0,30])
|
243
|
category.project = project
|
244
|
category.assigned_to = users_map[component.initialowner]
|
245
|
category.save
|
246
|
categories_map[component.id] = category
|
247
|
end
|
248
|
# Add default user roles
|
249
|
1.upto(users_map.length) do |i|
|
250
|
membership = Member.new(
|
251
|
:user => users_map[i],
|
252
|
:project => project,
|
253
|
:role => DEFAULT_ROLE
|
254
|
)
|
255
|
membership.save
|
256
|
end
|
257
|
end
|
258
|
|
259
|
# Bugs
|
260
|
puts "\n"
|
261
|
print "Migrating bugs"
|
262
|
Issue.destroy_all
|
263
|
issues_map = {}
|
264
|
skipped_bugs = []
|
265
|
BugzillaBug.find(:all).each do |bug|
|
266
|
issue = Issue.new(
|
267
|
:project => projects_map[bug.product_id],
|
268
|
:tracker => TRACKER_BUG,
|
269
|
:subject => bug.short_desc,
|
270
|
:description => bug.descriptions.first.text || bug.short_desc,
|
271
|
:author => users_map[bug.reporter],
|
272
|
:priority => PRIORITY_MAPPING[bug.priority] || DEFAULT_PRIORITY,
|
273
|
:status => STATUS_MAPPING[bug.bug_status] || DEFAULT_STATUS,
|
274
|
:start_date => bug.creation_ts,
|
275
|
:created_on => bug.creation_ts,
|
276
|
:updated_on => bug.delta_ts
|
277
|
)
|
278
|
|
279
|
issue.category = categories_map[bug.component_id] unless bug.component_id.blank?
|
280
|
issue.assigned_to = users_map[bug.assigned_to] unless bug.assigned_to.blank?
|
281
|
if issue.save
|
282
|
print '.'
|
283
|
else
|
284
|
issue.id = bug.bug_id
|
285
|
skipped_bugs << issue
|
286
|
print "!"
|
287
|
next
|
288
|
end
|
289
|
$stdout.flush
|
290
|
|
291
|
# notes
|
292
|
bug.descriptions.each do |description|
|
293
|
# the first comment is already added to the description field of the bug
|
294
|
next if description === bug.descriptions.first
|
295
|
journal = Journal.new(
|
296
|
:journalized => issue,
|
297
|
:user => users_map[description.who],
|
298
|
:notes => description.text,
|
299
|
:created_on => description.bug_when
|
300
|
)
|
301
|
if (journal.user.nil?)
|
302
|
journal.user = User.find(:first)
|
303
|
end
|
304
|
next unless journal.save
|
305
|
end
|
306
|
end
|
307
|
puts
|
308
|
|
309
|
puts
|
310
|
puts "Profiles: #{users_migrated}/#{BugzillaProfile.count}"
|
311
|
puts "Products: #{Project.count}/#{BugzillaProduct.count}"
|
312
|
puts "Components: #{IssueCategory.count}/#{BugzillaComponent.count}"
|
313
|
puts "Bugs #{Issue.count}/#{BugzillaBug.count}"
|
314
|
puts
|
315
|
|
316
|
if !skipped_bugs.empty?
|
317
|
puts "The following bugs failed to import: "
|
318
|
skipped_bugs.each do |issue|
|
319
|
print "#{issue.id}, reason: "
|
320
|
issue.errors.each{|error| print "#{error}"}
|
321
|
puts
|
322
|
end
|
323
|
end
|
324
|
end
|
325
|
|
326
|
puts
|
327
|
puts "WARNING: Your Redmine data will be deleted during this process."
|
328
|
print "Are you sure you want to continue ? [y/N] "
|
329
|
break unless STDIN.gets.match(/^y$/i)
|
330
|
|
331
|
# Default Bugzilla database settings
|
332
|
db_params = {:adapter => 'mysql',
|
333
|
:database => 'bugzilla',
|
334
|
:host => 'localhost',
|
335
|
#:port => 3308,
|
336
|
#:socket => '/home/mysql2/mysql2.sock',
|
337
|
:username => 'bugzilla',
|
338
|
:password => 'bugzilla',
|
339
|
:encoding => 'utf8' }
|
340
|
|
341
|
puts
|
342
|
puts "Please enter settings for your Bugzilla database"
|
343
|
[:adapter, :host, :database, :username, :password].each do |param|
|
344
|
print "#{param} [#{db_params[param]}]: "
|
345
|
value = STDIN.gets.chomp!
|
346
|
db_params[param] = value unless value.blank?
|
347
|
end
|
348
|
|
349
|
BugzillaMigrate.establish_connection db_params
|
350
|
BugzillaMigrate.migrate
|
351
|
end
|
352
|
|
353
|
end
|
354
|
end
|