-
Notifications
You must be signed in to change notification settings - Fork 0
/
trac2tracker.rb
executable file
·167 lines (143 loc) · 4.33 KB
/
trac2tracker.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
#!/usr/bin/env ruby
require 'io/console'
require 'optparse'
require 'ruby-progressbar'
require 'sqlite3'
require 'pivotal-tracker'
trac_db = 'trac.db'
default_user = 'ezhou'
# pt_project_id = '784261' # CPF spt
pt_project_id = '820749' # CPF Test
#pt_project_id = '820865' # matt's CPF Test
pt_email = '[email protected]'
unless ENV['PIVOTAL_TOKEN']
unless pt_email
print "Pivotal email: "
pt_email = gets.chomp
end
print 'Pivotal Password: '
pt_password = STDIN.noecho(&:gets).chomp
puts
unless pt_project_id
puts "\nPivotal Project ID: "
pt_project_id = gets.chomp
end
PivotalTracker::Client.token(pt_email, pt_password)
puts "Authenticated as #{pt_email}"
else
PivotalTracker::Client.token = ENV['PIVOTAL_TOKEN']
end
project = PivotalTracker::Project.find(pt_project_id)
if project
puts "Found project '#{project.name}'"
else
puts 'You do not appear to have permission to manage this project'
end
db = SQLite3::Database.new(trac_db)
puts 'Trac db loaded'
ticket_count = db.get_first_value('select count(*) from ticket')
memberships = (project.memberships.all).collect(&:name).map(&:downcase)
# TODO: verify memberships
story = nil
errors = 0
ticket_progress = ProgressBar.create(:title => 'Tickets: ',
:format => '%t %c/%C (%p%) |%b>>%i|', :total => ticket_count.to_i)
columns = nil
db.execute2('select * from ticket order by id desc') do |row_array|
if columns.nil?
columns = row_array
next
end
row = {}
columns.each_with_index do |name, index|
row[name.to_sym] = row_array[index]
end
if row[:severity].nil?
row[:severity] = '1'
end
if row[:status].nil?
row[:status] = 'unscheduled'
end
if row[:owner].nil?
row[:owner] = default_user
end
# translate statuses
if (row[:status] == 'closed' && (['fixed', 'duplicate', 'wontfix', 'invalid', 'worksforme'].include? row[:resolution].chomp))
row[:status] = 'accepted'
elsif (row[:status] == 'closed' && (['readytotest', 'reviewfix'].include? row[:resolution]))
row[:status] = 'delivered'
elsif row[:status] == 'assigned'
row[:status] = 'unstarted'
elsif row[:status] == 'new'
row[:status] = 'unscheduled'
elsif row[:status] == 'reopened'
row[:status] = 'rejected'
end
#translate types
row[:type] = case row[:type]
when 'defect'
'bug'
when 'enhancement'
'feature'
when 'roadmap'
'release'
when 'spec needed', 'task'
'chore'
else
row[:type]
end
if row[:type] == 'release' && row[:status] == 'delivered'
row[:status] = 'accepted'
end
if row[:type] == 'chore' && row[:status] == 'delivered'
row[:status] = 'accepted'
end
id = row[:id]
story = row[:summary]
labels = row[:milestone]
story_type = row[:type]
estimate = '1'
current_state = row[:status]
requested_by = row[:reporter]
owner = row[:owner]
description = row[:description]
unless memberships.include? requested_by.downcase
# project.memberships.create(name: requested_by)
memberships << requested_by.downcase
end
accepted_at = Time.at(row[:changetime]) if row[:status] == 'accepted'
# bugs and releases can't have estimate
estimate = nil if ['bug', 'release', 'chore'].include? story_type
begin
story = project.stories.create(
name: story,
labels: labels,
story_type: story_type,
estimate: estimate,
current_state: current_state,
created_at: Time.at(row[:time]),
accepted_at: accepted_at,
# requested_by: requested_by,
# owner: owner,
description: description.chomp + "\n[trac#{id}] Imported from trac, original id #{id}"
)
rescue
puts $!.backtrace
puts "Ticket: #{id}"
binding.pry
end
# # migrate comments
# db.execute(query = 'select newvalue from ticket_change where field=="comment" and newvalue != \'\' and ticket=' + id.to_s + ' and newvalue !=' + id.to_s) do |comment|
# story.notes.create(:text => comment[0]) unless comment[0].empty?
# end
if story.errors.count > 0
puts "Failed on ticket #{id}"
puts story.errors
binding.pry
errors = errors + 1
else
ticket_progress.title = "Ticket #{id}"
ticket_progress.increment
end
end
puts "Errors: #{errors}"