forked from trustmaster/trac2github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trac2github.php
323 lines (282 loc) · 10.7 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
<?php
/**
* @package trac2github
* @version 1.1
* @author Vladimir Sibirov
* @author Lukas Eder
* @copyright (c) Vladimir Sibirov 2011
* @license BSD
*/
if (!file_exists("./config.php")) {
echo "ERROR! Missing configuration file config.php. You can copy it from config.php.sample and edit with your data\n";
exit;
}
require "./config.php";
// DO NOT EDIT BELOW
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
set_time_limit(0);
$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;
$resp = github_add_milestone(array(
'title' => $row['name'],
'state' => $row['completed'] == 0 ? 'open' : 'closed',
'description' => empty($row['description']) ? 'None' : $row['description'],
'due_on' => date('Y-m-d\TH:i:s\Z', (int) $row['due'])
));
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";
}
}
// 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, '') <> ''");
foreach ($res->fetchAll() as $row) {
$resp = github_add_label(array(
'name' => $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";
}
}
// 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['/\[' . $svnRev . '\]/'] = $gitRev;
}
}
if (!$skip_tickets) {
// Export tickets
$limit = $ticket_limit > 0 ? "LIMIT $ticket_offset, $ticket_limit" : '';
$res = $trac_db->query("SELECT * FROM `ticket` ORDER BY `id` $limit");
foreach ($res->fetchAll() 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']]) && $github_users[$users_list[$row['reporter']]]) {
$username = $users_list[$row['reporter']];
$password = $github_users[$username];
} else {
$username = $default_username;
$password = $default_password;
}
// replace svn revision with git revision
if (!empty($convert_revision_regexp)) {
$row['description'] = preg_replace(array_keys($convert_revision_regexp), $convert_revision_regexp, $row['description']);
}
// There is a strange issue with summaries containing percent signs...
$issueData = array(
'title' => utf8_encode(preg_replace("/%/", '[pct]', $row['summary'])),
'body' => empty($row['description']) ? 'None' : 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";
if ($row['status'] == 'closed') {
// Close the issue
$issueData['state'] = 'closed';
$resp = github_update_issue($resp['number'], $issueData);
if (isset($resp['number'])) {
echo "Closed issue #{$resp['number']}\n";
}
}
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert a ticket #{$row['id']}: $error\n";
}
}
// 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'];
// replace svn revision with git revision
if (!empty($convert_revision_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']]) && $github_users[$users_list[$row['author']]]) {
$username = $users_list[$row['author']];
$password = $github_users[$username];
} else {
$username = $default_username;
$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 "Done whatever possible, sorry if not.\n";
function github_post($url, $json, $patch = false) {
global $username, $password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
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 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);
// 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);
// There is a strange issue with data containing percent signs...
$data = preg_replace("/%/", '[prc]', $data);
// Possibly translate other markup as well?
return $data;
}
?>