forked from batopa/trac2github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trac2github.php
390 lines (342 loc) · 13.4 KB
/
trac2github.php
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
<?php
/**
* @package trac2github
* @version 1.1
* @author Vladimir Sibirov
* @author Lukas Eder
* @copyright (c) Vladimir Sibirov 2011
* @license BSD
*/
$scriptDirectory = dirname(__FILE__) . '/';
$configFile = $scriptDirectory . 'config.php';
if (!file_exists($configFile)) {
echo "ERROR! Missing configuration file config.php. You can copy it from config.php.sample and edit with your data\n";
exit;
}
require $configFile;
// DO NOT EDIT BELOW
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
set_time_limit(0);
// Connect to the trac database, either using SQLite or MySQL
if ($sqlitePath) {
$trac_db = new PDO('sqlite:' . $sqlitePath);
} else {
$trac_db = new PDO('mysql:host='.$mysqlhost_trac.';dbname='.$mysqldb_trac, $mysqluser_trac, $mysqlpassword_trac);
}
echo "Connected to Trac\n";
$milestones = array();
if (file_exists($save_milestones)) {
$milestones = unserialize(file_get_contents($save_milestones));
}
if (!$skip_milestones) {
// Export all milestones
$res = $trac_db->query("SELECT * FROM `milestone` ORDER BY `due`");
$mnum = 1;
foreach ($res->fetchAll() as $row) {
//$milestones[$row['name']] = ++$mnum;
$milestoneData = array(
'title' => $row['name'],
'state' => $row['completed'] == 0 ? 'open' : 'closed',
'description' => empty($row['description']) ? 'None' : $row['description'],
);
if ($row['due'] != '0') {
$milestoneData['due_on'] = date('Y-m-d\TH:i:s\Z', substr ($row['due'], 0, -6));
}
$resp = github_add_milestone($milestoneData);
if (isset($resp['number'])) {
// OK
$milestones[crc32($row['name'])] = (int) $resp['number'];
echo "Milestone {$row['name']} converted to {$resp['number']}\n";
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert milestone {$row['name']}: $error\n";
echo "Terminating conversion; please address the problem and retry\n";
die;
}
}
// Serialize to restore in future
file_put_contents($save_milestones, serialize($milestones));
}
$labels = array();
$labels['T'] = array();
$labels['C'] = array();
$labels['P'] = array();
$labels['R'] = array();
if (file_exists($save_labels)) {
$labels = unserialize(file_get_contents($save_labels));
}
if (!$skip_labels) {
// Export all "labels"
$res = $trac_db->query("SELECT DISTINCT 'T' label_type, type name, 'cccccc' color
FROM ticket WHERE IFNULL(type, '') <> ''
UNION
SELECT DISTINCT 'C' label_type, component name, '0000aa' color
FROM ticket WHERE IFNULL(component, '') <> ''
UNION
SELECT DISTINCT 'P' label_type, priority name, case when lower(priority) = 'urgent' then 'ff0000'
when lower(priority) = 'high' then 'ff6666'
when lower(priority) = 'medium' then 'ffaaaa'
when lower(priority) = 'low' then 'ffdddd'
else 'aa8888' end color
FROM ticket WHERE IFNULL(priority, '') <> ''
UNION
SELECT DISTINCT 'R' label_type, resolution name, '55ff55' color
FROM ticket WHERE IFNULL(resolution, '') <> ''");
// Define label name expansions
$labelTypeNames = array (
'P' => 'Priority',
'R' => 'Resolution',
'T' => 'Type',
);
foreach ($res->fetchAll() as $row) {
$resp = github_add_label(array(
'name' => $labelTypeNames[$row['label_type']] . ': ' . $row['name'],
'color' => $row['color']
));
if (isset($resp['url'])) {
// OK
$labels[$row['label_type']][crc32($row['name'])] = $resp['name'];
echo "Label {$row['name']} converted to {$resp['name']}\n";
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert label {$row['name']}: $error\n";
echo "Terminating conversion; please address the problem and retry\n";
die;
}
}
// Serialize to restore in future
file_put_contents($save_labels, serialize($labels));
}
// Try get previously fetched tickets
$tickets = array();
if (file_exists($save_tickets)) {
$tickets = unserialize(file_get_contents($save_tickets));
}
// get convert_revision array to replace svn revision with git revision in ticket and ticket comments
if (empty($convert_revision) && !empty($convert_revision_file)) {
include $convert_revision_file;
$convert_revision_regexp = array();
foreach ($convert_revision as $svnRev => $gitRev) {
$convert_revision_regexp['/\br' . $svnRev . '\b/'] = $gitRev;
$convert_revision_regexp['/\s\[' . $svnRev . '\][\s.,;]/'] = ' ' . $gitRev . ' ';
}
}
if (!$skip_tickets) {
// Export tickets
$limit = $ticket_limit > 0 ? "LIMIT $ticket_offset, $ticket_limit" : '';
$resTickets = $trac_db->query("SELECT * FROM `ticket` ORDER BY `id` $limit");
$resTicketsAll = $resTickets->fetchAll();
$responsesCache = array ();
foreach ($resTicketsAll as $row) {
// do not esclude ticket without milestone
// if (empty($row['milestone'])) {
// continue;
// }
$ticketLabels = array();
if (!empty($labels['T'][crc32($row['type'])])) {
$ticketLabels[] = $labels['T'][crc32($row['type'])];
}
if (!empty($labels['C'][crc32($row['component'])])) {
$ticketLabels[] = $labels['C'][crc32($row['component'])];
}
if (!empty($labels['P'][crc32($row['priority'])])) {
$ticketLabels[] = $labels['P'][crc32($row['priority'])];
}
if (!empty($labels['R'][crc32($row['resolution'])])) {
$ticketLabels[] = $labels['R'][crc32($row['resolution'])];
}
// if more trac ticket is assigned at more users get the first one (github api v3 dosen't support multi assignee)
if (empty($row['owner'])) {
$row['owner'] = $default_username;
}
$owners = str_replace(' ', ',', $row['owner']);
$owners = explode(',', $owners);
$owner = $owners[0];
if (isset($users_list[$owner])) {
$assignee = $users_list[$owner];
} else {
$assignee = $default_username;
}
// set github username and password to post ticket
if (isset($users_list[$row['reporter']])) {
$username = $users_list[$row['reporter']];
$oauth_token = (isSet ($github_users_oauth_tokens[$username]) ? $github_users_oauth_tokens[$username] : false);
$password = (isSet ($github_users_passwords[$username]) ? $github_users_passwords[$username] : false);
} else {
$username = $default_username;
$oauth_token = $default_oauth_token;
$password = $default_password;
}
// There is a strange issue with summaries containing percent signs...
$date = date ('g.ia, l, jS F Y', substr ($row['time'], 0, -6));
$issueData = array(
'title' => utf8_encode($row['summary']),
'body' => "**[Submitted to the original trac issue database at {$date}]**\n\n" . (empty($row['description']) ? '[No description]' : translate_markup(utf8_encode($row['description']))),
'assignee' => $assignee,
'labels' => $ticketLabels,
);
// set milestone only if ticket is assigned to it
if (!empty($row['milestone'])) {
$issueData['milestone'] = $milestones[crc32($row['milestone'])];
}
$resp = github_add_issue($issueData);
if (isset($resp['number'])) {
// OK
$tickets[$row['id']] = (int) $resp['number'];
echo "Ticket #{$row['id']} converted to issue #{$resp['number']}\n";
//
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert a ticket #{$row['id']}: $error\n";
echo "Terminating conversion; please address the problem and retry\n";
die;
}
}
// Serialize to restore in future
file_put_contents($save_tickets, serialize($tickets));
}
if (!$skip_comments) {
// Export all comments
$limit = $comments_limit > 0 ? "LIMIT $comments_offset, $comments_limit" : '';
$res = $trac_db->query("SELECT * FROM `ticket_change` where `field` = 'comment' AND `newvalue` != '' ORDER BY `ticket`, `time` $limit");
foreach ($res->fetchAll() as $row) {
$text = $row['newvalue'];
// Prepend the date, since the Github API doesn't permit date-setting
$date = date ('g.ia, l, jS F Y', substr ($row['time'], 0, -6));
$text = "**[Added to the original trac issue at {$date}]**\n\n" . $text;
// replace svn revision with git revision
if (!empty($convert_revision_regexp)) {
if (preg_match ('/\br([0-9]+)\b/', $text) || preg_match ('/\s\[([0-9]+)\][\s.,;]\b/', $text)) { // Only bother running this conversion if a r... marker is actually there, as it could be quite a large regexp
$text = preg_replace(array_keys($convert_revision_regexp), $convert_revision_regexp, $text);
}
}
// set github username and password to post comment to ticket
if (isset($users_list[$row['author']])) {
$username = $users_list[$row['author']];
$oauth_token = (isSet ($github_users_oauth_tokens[$username]) ? $github_users_oauth_tokens[$username] : false);
$password = (isSet ($github_users_passwords[$username]) ? $github_users_passwords[$username] : false);
} else {
$username = $default_username;
$oauth_token = $default_oauth_token;
$password = $default_password;
if ( (isset($users_list[$row['author']]) && strtolower($users_list[$row['author']]) != strtolower($username)) || (!isset($users_list[$row['author']]) && $username != $row['author']) ) {
$text = '**Author: ' . $row['author'] . "**\n" . $text;
}
}
$resp = github_add_comment($tickets[$row['ticket']], translate_markup(utf8_encode($text)));
if (isset($resp['url'])) {
// OK
echo "Added comment {$resp['url']}\n";
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to add a comment: $error\n";
echo "Terminating conversion; please address the problem and retry\n";
die;
}
}
}
// Close issues that are closed
if (!$skip_tickets) {
$username = $default_username; // #!# Ideally we need to work out who closed the ticket, instead of this
foreach ($resTicketsAll as $row) {
if ($row['status'] == 'closed') {
$issueData = array ('state' => 'closed');
$resp = github_update_issue($tickets[$row['id']], $issueData);
if (isset($resp['number'])) {
echo "Closed issue #{$resp['number']}\n";
}
}
}
}
echo "Done whatever possible, sorry if not.\n";
function github_post($url, $json, $patch = false) {
global $username, $password, $oauth_token, $rateLimit;
if ($rateLimit) {
usleep (ceil (1000000 * (60 * 60) / $rateLimit));
}
$ch = curl_init();
if ($oauth_token) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("Authorization: token {$oauth_token}"));
} else { // Fall back to password auth
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
}
curl_setopt($ch, CURLOPT_USERAGENT, 'trac2github ticket-to-issue conversion script'); // Define user-agent string as required by http://developer.github.com/v3/#user-agent-required
curl_setopt($ch, CURLOPT_URL, "https://api.github.com$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
if ($patch) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
$ret = curl_exec($ch);
if(!$ret) {
trigger_error(curl_error($ch));
}
curl_close($ch);
return $ret;
}
function github_add_milestone($data) {
global $project, $repo, $verbose;
if ($verbose) print_r($data);
return json_decode(github_post("/repos/$project/$repo/milestones", json_encode($data)), true);
}
function github_add_label($data) {
global $project, $repo, $verbose;
if ($verbose) print_r($data);
return json_decode(github_post("/repos/$project/$repo/labels", json_encode($data)), true);
}
function github_add_issue($data) {
global $project, $repo, $verbose;
if ($verbose) print_r($data);
return json_decode(github_post("/repos/$project/$repo/issues", json_encode($data)), true);
}
function github_add_comment($issue, $body) {
global $project, $repo, $verbose;
if ($verbose) print_r($body);
return json_decode(github_post("/repos/$project/$repo/issues/$issue/comments", json_encode(array('body' => $body))), true);
}
function github_update_issue($issue, $data) {
global $project, $repo, $verbose;
if ($verbose) print_r($body);
return json_decode(github_post("/repos/$project/$repo/issues/$issue", json_encode($data), true), true);
}
function translate_markup($data) {
// Replace SVN revision with Git revision
global $convert_revision_regexp;
if (!empty($convert_revision_regexp)) {
$data = preg_replace(array_keys($convert_revision_regexp), array_values ($convert_revision_regexp), $data);
}
// Replace code blocks with an associated language
$data = preg_replace('/\{\{\{(\s*#!(\w+))?/m', '```$2', $data);
$data = preg_replace('/\}\}\}/', '```', $data);
// Replace title blocks
$data = preg_replace("/^=(\s.+\s)=$/", '#$1#', $data);
$data = preg_replace("/^==(\s.+\s)==$/", '##$1##', $data);
$data = preg_replace("/^===(\s.+\s)===$/", '###$1###', $data);
$data = preg_replace("/^====(\s.+\s)====$/", '####$1####', $data);
// Do string replacement if required
global $textReplacements;
if ($textReplacements) {
$data = strtr ($data, $textReplacements);
}
// Remove e-mail addresses out of courtesy
global $removeEmailAddresses;
if ($removeEmailAddresses) {
$data = preg_replace('|([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6})|', '[e-mail address removed]', $data);
}
// Replace bold and italic
$data = preg_replace("/'''(\S.+\S)'''/", '**$1**', $data);
$data = preg_replace("/''(\S.+\S)''/", '*$1*', $data);
// Avoid non-ASCII characters, as that will cause trouble with json_encode()
$data = preg_replace('/[^(\x00-\x7F)]*/','', $data);
// Possibly translate other markup as well?
return $data;
}
?>