forked from ralli/bz2redmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bz2redmine.rb
executable file
·660 lines (596 loc) · 24.1 KB
/
bz2redmine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/ruby
#
# Copyright (c) 2009, Ralph Juhnke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
require "rubygems"
require "mysql"
require File.expand_path(File.join(File.dirname(__FILE__), "settings"))
require "digest/sha1"
class ConnectionInfo
attr_accessor :host
attr_accessor :user
attr_accessor :password
attr_accessor :dbname
def initialize(host, user, password, dbname)
@host = host
@user = user
@password = password
@dbname = dbname
end
end
class BugzillaToRedmine
def initialize
@bugzillainfo = ConnectionInfo.new(BUGZILLA_HOST, BUGZILLA_USER, BUGZILLA_PASSWORD, BUGZILLA_DB)
@redmineinfo = ConnectionInfo.new(REDMINE_HOST, REDMINE_USER, REDMINE_PASSWORD, REDMINE_DB)
# Bugzilla priority to Redmine priority map
@issuePriorities = ISSUE_PRIORITIES
# Bugzilla severity to Redmine tracker map
@issueTrackers = ISSUE_TRACKERS
# Bugzilla status to Redmine status map
@issueStatus = ISSUE_STATUS
# create hashcode for default password
@passwordSalt = ""
@defaultPassword = Digest::SHA1::hexdigest(@passwordSalt+Digest::SHA1::hexdigest(REDMINE_DEFAULT_USER_PASSWORD))
end
def migrate
self.open_connections
self.perform_sanity_checks
self.clear_redmine_tables
self.migrate_projects
self.migrate_versions
self.migrate_users
self.migrate_groups
self.migrate_members
self.migrate_member_roles
self.migrate_groups_users
self.migrate_categories
self.migrate_issues
self.migrate_time_entries
self.migrate_watchers
self.migrate_issue_relations
self.migrate_attachments
self.close_connections
end
def open_connections
@bugzilladb = self.open_connection(@bugzillainfo)
@redminedb = self.open_connection(@redmineinfo)
end
def close_connections
self.log "closing database connections"
@bugzilladb.close
@redminedb.close
end
def open_connection(info)
self.log "opening #{info.inspect}"
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME, 'utf8')
db.real_connect(info.host, info.user, info.password, info.dbname)
db.query("SET NAMES utf8")
return db
end
def clear_redmine_tables
sqls = [
"DELETE FROM projects",
"DELETE FROM projects_trackers",
"DELETE FROM enabled_modules",
"DELETE FROM boards",
"DELETE FROM custom_fields_projects",
"DELETE FROM documents",
"DELETE FROM news",
"DELETE FROM queries",
"DELETE FROM repositories",
"DELETE FROM time_entries",
"DELETE FROM wiki_content_versions",
"DELETE FROM wiki_contents",
"DELETE FROM wiki_pages",
"DELETE FROM wiki_redirects",
"DELETE FROM wikis",
]
sqls.each do |sql|
self.red_exec_sql(sql)
end
end
def log(s)
puts s
end
def migrate_projects
tree_idx = 1
self.bz_select_sql("SELECT products.id, products.name, products.description, products.classification_id, classifications.name as classification_name FROM products, classifications WHERE products.classification_id = classifications.id order by products.name") do |row|
identifier = row[1].downcase
status = row[3] == 1 ? 9 : 1
created_at = self.find_min_created_at_for_product(row[0])
updated_at = self.find_max_bug_when_for_product(row[0])
self.red_exec_sql("INSERT INTO projects (id, name, description, is_public, identifier, created_on, updated_on, status, lft, rgt) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", row[0], row[1], row[2], 1, identifier,
created_at, updated_at, status, tree_idx, tree_idx+1)
tree_idx = tree_idx + 2
tree_idx = tree_idx + 2
self.insert_project_trackers(row[0])
self.insert_project_modules(row[0])
end
end
def find_min_created_at_for_product(product_id)
bug_when = '1970-01-01 10:22:25'
sql = "select min(b.creation_ts) from products p join bugs b on b.product_id = p.id where product_id=?"
self.bz_select_sql(sql, product_id) do |row|
bug_when = row[0]
end
return bug_when
end
def find_max_bug_when_for_product(product_id)
bug_when = '1970-01-01 10:22:25'
sql = "select max(l.bug_when) from products p join bugs b on b.product_id = p.id join longdescs l on l.bug_id = b.bug_id where b.product_id=?"
self.bz_select_sql(sql, product_id) do |row|
bug_when = row[0]
end
return bug_when
end
def migrate_versions
self.red_exec_sql("delete from versions")
self.bz_select_sql("SELECT id, product_id AS project_id, value AS name FROM versions") do |row|
self.red_exec_sql("INSERT INTO versions (id, project_id, name) VALUES (?, ?, ?)", row[0], row[1], row[2])
end
end
def migrate_users
["DELETE FROM users",
"DELETE FROM user_preferences",
"DELETE FROM members",
"DELETE FROM member_roles",
"DELETE FROM groups_users",
"DELETE FROM messages",
"DELETE FROM tokens",
"DELETE FROM watchers"].each do |sql|
self.red_exec_sql(sql)
end
if not REDMINE_DEFAULT_AUTH_SOURCE_ID.nil?
ldap = Net::LDAP.new :host => REDMINE_LDAP['host'], # your LDAP host name or IP goes here,
:port => REDMINE_LDAP['port'], # your LDAP host port goes here,
:base => REDMINE_LDAP['base'], # the base of your AD tree goes here,
:auth => {
:method => :simple,
:username => REDMINE_LDAP['bind_user'], # a user w/sufficient privileges to read from AD goes here,
:password => REDMINE_LDAP['bind_pass'] # the user's password goes here
}
raise "LDAP bind failed." unless ldap.bind
end
self.bz_select_sql("SELECT userid, login_name, realname, disabledtext, extern_id FROM profiles") do |row|
user_id = row[0]
login_name = row[1]
real_name = row[2]
disabled_text = row[3]
if real_name.nil?
(last_name, first_name) = ['empty', 'empty']
else
(last_name, first_name) = real_name.split(/[ ,]+/)
if first_name.to_s.strip.empty?
first_name = 'empty'
end
if last_name.to_s.strip.empty?
last_name = 'empty'
end
end
status = disabled_text.to_s.strip.empty? ? 1 : 3
if not extern_id.nil? and not REDMINE_DEFAULT_AUTH_SOURCE_ID.nil?
search_filter = Net::LDAP::Filter.eq(REDMINE_LDAP['email_attr'], login_name)
self.log("Searching LDAP for %s" % login_name)
result = ldap.search(:filter => search_filter, :attributes => [REDMINE_LDAP['login_attr']], :return_result => true) do |user|
self.log("User found in LDAP")
self.red_exec_sql("INSERT INTO users (id, login, mail, firstname, lastname, language, mail_notification, status, type, auth_source_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
user_id, user[REDMINE_LDAP['login_attr']].first, login_name, last_name, first_name, 'en', 'only_my_events', status, 'User', REDMINE_DEFAULT_AUTH_SOURCE_ID)
end
end
if result.nil? or extern_id.nil?
self.red_exec_sql("INSERT INTO users (id, login, mail, firstname, lastname, language, mail_notification, status, hashed_password, type, salt) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
user_id, login_name, login_name, last_name, first_name, 'en', 'only_my_events', status, @defaultPassword, 'User', @passwordSalt)
end
other = """---
:comments_sorting: asc
:no_self_notified: true
"""
self.red_exec_sql("INSERT INTO user_preferences (user_id,others) values (?, ?)", user_id, other)
end
end
def migrate_groups
self.bz_select_sql("select name from groups") do |row|
name = row[0]
self.red_exec_sql("insert into users (lastname, mail_notification, admin, status, type, language) values (?, ?, ?, ?, ?, ?)",
name, 'only_my_events', (name == 'admin' ? 1 : 0) , 1, 'Group', 'en')
end
end
def find_version_id(project_id, version)
result = -1
self.red_select_sql("select id from versions where project_id=? and name=?", project_id, version) do |row|
result = row[0]
end
return result
end
def find_max_bug_when(bug_id)
bug_when = '1970-01-01 10:22:25'
self.bz_select_sql("select max(bug_when) from longdescs where bug_id=?", bug_id) do |row|
bug_when = row[0]
end
return bug_when
end
def migrate_categories
self.red_exec_sql("delete from issue_categories")
self.bz_select_sql("SELECT id, name, product_id AS project_id, initialowner AS assigned_to_id FROM components") do |row|
self.red_exec_sql("INSERT INTO issue_categories (id, name, project_id, assigned_to_id) VALUES (?, ?, ?, ?)", row[0], row[1], row[2], row[3])
end
end
def migrate_watchers
self.red_exec_sql("delete from watchers")
self.bz_select_sql("select bug_id, who FROM cc") do |row|
self.red_exec_sql("insert into watchers (watchable_type, watchable_id, user_id) values (?, ?, ?)", 'Issue', row[0], row[1])
end
end
def insert_custom_fields
self.red_exec_sql("delete from custom_fields")
self.red_exec_sql("delete from custom_fields_trackers")
self.red_exec_sql("delete from custom_values")
self.red_exec_sql("INSERT INTO custom_fields (id, type, name, field_format, possible_values, max_length, is_for_all, is_filter, searchable, default_value) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 1, 'IssueCustomField', 'URL', 'string', '--- []/n/n', 255, 1, 1, 1, '')
[1,2,3].each do |tracker_id|
self.red_exec_sql("INSERT INTO custom_fields_trackers (custom_field_id, tracker_id) VALUES (?, ?)", 1, tracker_id)
end
end
def migrate_time_entries
self.red_exec_sql("delete from time_entries")
self.bz_select_sql("select
b.product_id as project_id,
a.who as user_id,
a.bug_id as issue_id,
a.work_time as hours,
substr(a.thetext,1,255) as comments,
9 as activity_id,
date(bug_when) as spent_on,
year(bug_when) as tyear,
month(bug_when) as tmonth,
week(bug_when) as tweek,
bug_when as created_on,
bug_when as updated_on
from
longdescs a inner join bugs b on b.bug_id = a.bug_id
where work_time <> 0") do |row|
project_id = row[0]
user_id = row[1]
issue_id = row[2]
hours = row[3]
comments = row[4]
activity_id = row[5]
spent_on = row[6]
tyear = row[7]
tmonth = row[8]
tweek = row[9]
created_on = row[10]
updated_on = row[11]
self.red_exec_sql("insert into time_entries (project_id, user_id, issue_id, hours, comments, activity_id, spent_on, tyear, tmonth, tweek, created_on, updated_on) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
project_id, user_id, issue_id, hours, comments, activity_id, spent_on, tyear, tmonth, tweek, created_on, updated_on)
end
end
def migrate_issues
self.red_exec_sql("delete from issues")
self.red_exec_sql("delete from journals")
self.insert_custom_fields
sql = "SELECT bugs.bug_id,
bugs.assigned_to,
bugs.bug_status,
bugs.creation_ts,
bugs.short_desc,
bugs.product_id,
bugs.reporter,
bugs.version,
bugs.resolution,
bugs.estimated_time,
bugs.remaining_time,
bugs.deadline,
bugs.target_milestone,
bugs.bug_severity,
bugs.priority,
bugs.component_id,
bugs.status_whiteboard AS whiteboard,
bugs.bug_file_loc AS url,
longdescs.comment_id,
longdescs.thetext,
longdescs.bug_when,
longdescs.who,
longdescs.isprivate
FROM bugs, longdescs
WHERE bugs.bug_id = longdescs.bug_id
ORDER BY bugs.bug_id, longdescs.bug_when"
current_bug_id = -1
self.bz_select_sql(sql) do |row|
( bug_id,
assigned_to,
bug_status,
creation_ts,
short_desc,
product_id,
reporter,
version,
resolution,
estimated_time,
remaining_time,
deadline,
target_milestone,
bug_severity,
priority,
component_id,
whiteboard,
url,
comment_id,
thetext,
bug_when,
who,
isprivate) = row
if(current_bug_id != bug_id)
sql = "INSERT INTO issues (id, project_id, subject, description, assigned_to_id, author_id, created_on, updated_on, start_date, due_date, done_ratio, estimated_hours, priority_id, fixed_version_id, category_id, tracker_id, status_id, root_id, lft, rgt) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
target_milestone_id = self.find_version_id(product_id, version)
updated_at = self.find_max_bug_when(bug_id)
priority_id = map_priority(bug_id, priority)
tracker_id = map_tracker(bug_id, bug_severity)
status_id = map_status(bug_id, bug_status)
done_ratio = estimated_time.to_f.abs < 1e-3 ? 0.00 : ((estimated_time.to_f - remaining_time.to_f)/estimated_time.to_f)*100
self.red_exec_sql(sql, bug_id, product_id, short_desc, thetext, assigned_to, reporter, creation_ts, updated_at, creation_ts, deadline, done_ratio, estimated_time, priority_id, target_milestone_id, component_id, tracker_id, status_id, bug_id, 1, 2)
current_bug_id = bug_id
sql = "INSERT INTO custom_values (customized_type, customized_id, custom_field_id, value) VALUES (?, ?, ?, ?)"
self.red_exec_sql(sql, 'Issue', bug_id, 1, url)
else
sql = "INSERT INTO journals (id, journalized_id, journalized_type, user_id, notes, created_on) VALUES (?, ?, ?, ?, ?, ?)"
self.red_exec_sql(sql, comment_id, bug_id, "Issue", who, thetext, bug_when)
end
end
end
def map_priority(bug_id, priority)
priority_id = @issuePriorities[priority]
throw "bugzilla bug #{bug_id}: cannot map the issue priority #{priority}." if priority_id.nil?
return priority_id
end
def map_tracker(bug_id, bug_severity)
return @issueTrackers[bug_severity] || 1 # use the "bug" tracker, if the bug severity does not match
end
def map_status(bug_id, bug_status)
status_id = @issueStatus[bug_status]
throw "bugzilla bug #{bug_id}: cannot map the issue priority #{bug_status}." if status_id.nil?
return status_id
end
def migrate_issue_relations
self.red_exec_sql("delete from issue_relations")
sql = "SELECT dependson, blocked FROM dependencies"
self.bz_select_sql(sql) do |row|
self.red_exec_sql("INSERT INTO issue_relations (issue_from_id, issue_to_id, relation_type) values (?, ?, ?)", row[0], row[1], "blocks")
end
sql = "SELECT dupe, dupe_of FROM duplicates"
self.bz_select_sql(sql) do |row|
self.red_exec_sql("INSERT INTO issue_relations (issue_from_id, issue_to_id, relation_type) values (?, ?, ?)", row[0], row[1], "duplicates")
end
end
def migrate_attachments
self.red_exec_sql("DELETE FROM attachments")
sql = "SELECT attachments.attach_id, attachments.bug_id, attachments.filename, attachments.mimetype, attachments.submitter_id, attachments.creation_ts, attachments.description, attach_data.thedata FROM attachments, attach_data WHERE attachments.attach_id = attach_data.id"
self.bz_select_sql(sql) do |row|
(attach_id, bug_id, filename, mimetype, submitter_id, creation_ts, description, thedata ) = row
disk_filename = self.get_disk_filename(attach_id, filename)
filesize = thedata.size()
sql = "INSERT INTO attachments (id, container_id, container_type, filename, filesize, disk_filename, content_type, digest, downloads, author_id, created_on, description) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
self.red_exec_sql(sql, attach_id, bug_id, 'Issue', filename, filesize, disk_filename, mimetype, '', 0, submitter_id, creation_ts, description)
File.open("#{ATTACHMENT_PATH}/#{disk_filename}", "wb") do |f|
f.write(thedata)
end
end
end
def get_disk_filename(attach_id, filename)
return "a#{attach_id}.#{self.get_file_extension(filename)}".downcase
end
def get_file_extension(s)
m = /\.(\w+)$/.match(s)
if(m)
return m[1]
else
return 'dat'
end
end
def migrate_members
self.log("*** migrate members ***")
self.bz_select_sql("SELECT DISTINCT user_group_map.user_id, group_control_map.product_id AS project_id FROM group_control_map, user_group_map WHERE group_control_map.group_id = user_group_map.group_id") do |row|
user_id = row[0]
product_id = row[1]
role_id = DEFAULT_ROLE_ID
created_on = "2007-01-01 12:00:00"
mail_notification = 0
self.red_exec_sql("INSERT INTO members (user_id, project_id, created_on, mail_notification) VALUES (?,?,?,?)", user_id, product_id, created_on, mail_notification)
end
end
def select_group_id(group_name)
result = nil
red_select_sql("select id from users where lastname=? and type=?", group_name, 'Group') do |row|
result = row[0]
end
result
end
def select_last_insert_id
result = 0
red_select_sql("select last_insert_id()") do |row|
result = row[0]
end
result
end
def migrate_member_roles
self.log("*** migrate member roles ***")
self.bz_select_sql("SELECT DISTINCT groups.name, group_control_map.product_id AS project_id FROM group_control_map, groups WHERE groups.id = group_control_map.group_id") do |row|
group_name = row[0]
product_id = row[1]
role_id = DEFAULT_ROLE_ID
created_on = "2007-01-01 12:00:00"
mail_notification = 0
group_id = select_group_id(group_name)
self.red_exec_sql("INSERT INTO members (user_id, project_id, created_on, mail_notification) values (?,?,?,?)", group_id, product_id, created_on, mail_notification)
member_id_of_group = select_last_insert_id()
self.red_exec_sql("INSERT INTO member_roles (member_id, role_id, inherited_from) values (?,?,?)", member_id_of_group, role_id, 0)
self.red_exec_sql("INSERT INTO member_roles (member_id, role_id, inherited_from) select members.id, ?, ? FROM members,users where members.project_id = ? and members.user_id = users.id and users.type = ?", role_id, member_id_of_group, product_id, 'User')
end
end
def migrate_groups_users
self.log("*** migrate groups users ***")
self.red_select_sql("select distinct (select members.user_id from members where members.id = mr.inherited_from) as group_id, m.user_id FROM member_roles as mr, members as m where mr.inherited_from is not null and mr.inherited_from <> 0 and mr.member_id = m.id") do |row|
group_id = row[0]
user_id = row[1]
self.red_exec_sql("INSERT INTO groups_users (group_id, user_id) values (?, ?)", group_id, user_id)
end
end
def insert_project_trackers(project_id)
[1,2,3].each do |tracker_id|
self.red_exec_sql("INSERT INTO projects_trackers (project_id, tracker_id) VALUES (?, ?)", project_id, tracker_id)
end
end
def insert_project_modules(project_id)
['issue_tracking',
'time_tracking',
'news',
'documents',
'files',
'wiki',
'repository',
'boards',].each do |m|
self.red_exec_sql("INSERT INTO enabled_modules (project_id, name) VALUES (?, ?)", project_id, m)
end
end
def bz_select_sql(sql, *args, &block)
self.log("bugzilla: #{sql} args=#{args.join(',')}")
statement = @bugzilladb.prepare(sql)
statement.execute(*args)
while row = statement.fetch do
yield row
end
statement.close()
end
def red_exec_sql(sql, *args)
self.log("redmine: #{sql} args=#{args.join(',')}")
statement = @redminedb.prepare(sql)
statement.execute(*args)
statement.close()
end
def red_select_sql(sql, *args, &block)
self.log("redmine: #{sql} args=#{args.join(',')}")
statement = @redminedb.prepare(sql)
statement.execute(*args)
while row = statement.fetch do
yield row
end
statement.close()
end
def perform_sanity_checks
verify_bug_priorities()
verify_bug_status()
verify_bug_severities()
verify_trackers()
verify_issue_statuses()
verify_issue_priorities()
end
def make_query_string(strings)
quoted_strings = strings.collect {|s| s.to_s. sub(/^(.*)$/, '\'\1\'') }
return "(#{quoted_strings.join(", ")})"
end
def verify_bug_priorities
self.log("checking bug priorities...")
sql = "select bug_id, priority from bugs where priority not in #{make_query_string(ISSUE_PRIORITIES.keys)}"
count = 0
self.bz_select_sql(sql) do |row|
(bug_id, priority) = row
self.log "bug #{bug_id}: unknown bug priority #{priority}."
count += 1
end
if count > 0
throw "there are bug priorities, which cannot be mapped. please modify the ISSUE_PRIORITIES in settings.rb accordingly."
end
end
def verify_bug_status
self.log("checking bug status...")
sql = "select bug_id, bug_status from bugs where bug_status not in #{make_query_string(ISSUE_STATUS.keys)}"
count = 0
self.bz_select_sql(sql) do |row|
(bug_id, bug_status) = row
self.log "bug #{bug_id}: unknown bug status #{bug_status}."
count += 1
end
if count > 0
throw "there are bug priorities, which cannot be mapped. please modify the ISSUE_STATUS in settings.rb accordingly."
end
end
def verify_bug_status
self.log("checking bug status...")
sql = "select bug_id, bug_status from bugs where bug_status not in #{make_query_string(ISSUE_STATUS.keys)}"
count = 0
self.bz_select_sql(sql) do |row|
(bug_id, bug_status) = row
self.log "bug #{bug_id}: unknown bug status #{bug_status}."
count += 1
end
if count > 0
throw "there are bug priorities, which cannot be mapped. please modify the ISSUE_STATUS in settings.rb accordingly."
end
end
def verify_bug_severities
self.log("checking bug status...")
sql = "select bug_id, bug_severity from bugs where bug_severity not in #{make_query_string(ISSUE_TRACKERS.keys)}"
count = 0
self.bz_select_sql(sql) do |row|
(bug_id, bug_severity) = row
self.log "bug #{bug_id}: unknown bug severity #{bug_severity}."
count += 1
end
if count > 0
throw "there are bug priorities, which cannot be mapped. please modify the ISSUE_TRACKERS in settings.rb accordingly."
end
end
def red_check_exists(sql, *args)
self.red_select_sql(sql, *args) { |i| return true }
return false
end
def verify_trackers
ISSUE_TRACKERS.values.uniq.each do |id|
unless red_check_exists("select id from trackers where id=?", id)
throw "cannot find tracker in trackers table with id #{id}"
end
end
end
def verify_issue_statuses
ISSUE_STATUS.values.uniq.each do |id|
unless red_check_exists("select id from issue_statuses where id=?", id)
throw "cannot find status in issue_statuses table with id #{id}"
end
end
end
def verify_issue_priorities
ISSUE_PRIORITIES.values.uniq.each do |id|
unless red_check_exists("select id from enumerations where id=?", id)
throw "cannot find issue priority in enumerations table with id=#{id}"
end
end
end
end
begin
bzred = BugzillaToRedmine.new
bzred.migrate
rescue => e
puts e.inspect
puts e.backtrace
end