forked from delatbabel/redmine-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme.php
executable file
·500 lines (432 loc) · 16.2 KB
/
runme.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
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
#!/usr/bin/env php
<?php
/**
* Define the servers and store into the configuration.
*
* @param array $config
* @return array
*/
function defineServers(array $config)
{
if (! isset($config['source']) || ! is_array($config['source'])) {
echo "\n\nSource Server Configuration\n===========================\n\n";
$config['source']['URL'] = readline('Source server URL (e.g. https://some.redmine.server.com): ');
$config['source']['key'] = readline('Source server Key (e.g. 1234567890abcdef1234567890abcdef12345678): ');
}
if (! isset($config['dest']) || ! is_array($config['dest'])) {
echo "\n\nDestination Server Configuration\n================================\n\n";
$config['dest']['URL'] = readline('Destination server URL (e.g. https://another.redmine.server.com): ');
$config['dest']['key'] = readline('Destination server Key (e.g. 1234567890abcdef1234567890abcdef12345678): ');
}
return $config;
}
/**
* Show trackers
*
* @param Redmine\Client $client
*/
function showTrackers($client)
{
$trackers = $client->tracker->all();
print "+-------+----------------------------------------------------+\n";
printf("| %5s | %-50s |\n", 'id', 'Tracker Name');
print "+-------+----------------------------------------------------+\n";
foreach ($trackers['trackers'] as $tracker) {
printf("| %5d | %-50s |\n", $tracker['id'], $tracker['name']);
}
print "+-------+----------------------------------------------------+\n\n";
}
/**
* Define the tracker mapping from source trackers to destination trackers
*
* @param array $config
* @param Redmine\Client $source
* @param Redmine\Client $dest
* @return array
*/
function defineTrackerMapping(array $config, $source, $dest)
{
if (isset($config['tracker_map']) && is_array($config['tracker_map'])) {
return $config;
}
//
// Show Trackers
//
print "\nSource Issue Types\n\n";
showTrackers($source);
$trackers = $source->tracker->all();
foreach ($trackers['trackers'] as $tracker) {
print "Source Issue Type ID " . $tracker['id'] . ", name: " . $tracker['name'] . "\n";
print "\nDestination Issue Types\n\n";
showTrackers($dest);
$config['tracker_map'][$tracker['id']] = readline('Enter the destination issue type ID for source issue type ' .
$tracker['name'] . ': ');
}
return $config;
}
/**
* Show statuses
*
* @param Redmine\Client $client
*/
function showStatuses($client)
{
$statuses = $client->issue_status->all();
print "+-------+----------------------------------------------------+\n";
printf("| %5s | %-50s |\n", 'id', 'Issue Status Name');
print "+-------+----------------------------------------------------+\n";
foreach ($statuses['issue_statuses'] as $status) {
printf("| %5d | %-50s |\n", $status['id'], $status['name']);
}
print "+-------+----------------------------------------------------+\n\n";
}
/**
* Define the status mapping from source issue statuses to destination issue statuses
*
* @param array $config
* @param Redmine\Client $source
* @param Redmine\Client $dest
* @return array
*/
function defineStatusMapping(array $config, $source, $dest)
{
if (isset($config['status_map']) && is_array($config['status_map'])) {
return $config;
}
//
// Show Statuses
//
print "\nSource Issue Statuses\n\n";
showStatuses($source);
$statuses = $source->issue_status->all();
foreach ($statuses['issue_statuses'] as $status) {
print "Source Issue Status ID " . $status['id'] . ", name: " . $status['name'] . "\n";
print "\nDestination Issue Statuses\n\n";
showStatuses($dest);
$config['status_map'][$status['id']] = readline('Enter the destination issue status ID for source issue status ' .
$status['name'] . ': ');
}
return $config;
}
/**
* Show priorities
*
* @param Redmine\Client $client
*/
function showPriorities($client)
{
$priorities = $client->issue_priority->all();
print "+-------+----------------------------------------------------+\n";
printf("| %5s | %-50s |\n", 'id', 'Issue Priority Name');
print "+-------+----------------------------------------------------+\n";
foreach ($priorities['issue_priorities'] as $priority) {
printf("| %5d | %-50s |\n", $priority['id'], $priority['name']);
}
print "+-------+----------------------------------------------------+\n\n";
}
/**
* Define the priority mapping from source issue priorities to destination issue priorities
*
* @param array $config
* @param Redmine\Client $source
* @param Redmine\Client $dest
* @return array
*/
function definePriorityMapping(array $config, $source, $dest)
{
if (isset($config['priority_map']) && is_array($config['priority_map'])) {
return $config;
}
//
// Show Priorities
//
print "\nSource Issue Priorities\n\n";
showPriorities($source);
$priorities = $source->issue_priority->all();
foreach ($priorities['issue_priorities'] as $priority) {
print "Source Issue Priority ID " . $priority['id'] . ", name: " . $priority['name'] . "\n";
print "\nDestination Issue Priorities\n\n";
showPriorities($dest);
$config['priority_map'][$priority['id']] = readline('Enter the destination issue priority ID for source issue priority ' .
$priority['name'] . ': ');
}
return $config;
}
/**
* Show users
*
* @param Redmine\Client $client
*/
function showUsers($client)
{
$users = $client->user->all();
print "+-------+----------------------------------------------------+\n";
printf("| %5s | %-50s |\n", 'id', 'User Name');
print "+-------+----------------------------------------------------+\n";
foreach ($users['users'] as $user) {
printf("| %5d | %-50s |\n", $user['id'], $user['firstname'] . ' ' . $user['lastname']);
}
print "+-------+----------------------------------------------------+\n\n";
}
/**
* Define the user mapping from source users to destination users
*
* @param array $config
* @param Redmine\Client $source
* @param Redmine\Client $dest
* @return array
*/
function defineUserMapping(array $config, $source, $dest)
{
if (isset($config['user_map']) && is_array($config['user_map'])) {
return $config;
}
//
// Show Statuses
//
print "\nSource Users\n\n";
showUsers($source);
$users = $source->user->all();
foreach ($users['users'] as $user) {
print "Source User ID " . $user['id'] . ", name: " . $user['firstname'] . ' ' . $user['lastname'] . "\n";
print "\nDestination Users\n\n";
showUsers($dest);
$config['user_map'][$user['id']] = readline('Enter the destination user ID for source user ' .
$user['firstname'] . ' ' . $user['lastname'] . ': ');
}
return $config;
}
/**
* Show projects
*
* @param Redmine\Client $client
*/
function showProjects($client)
{
$projects = $client->project->all();
print "+-------+----------------------------------------------------+\n";
printf("| %5s | %-50s |\n", 'id', 'Project Name');
print "+-------+----------------------------------------------------+\n";
foreach ($projects['projects'] as $project) {
printf("| %5d | %-50s |\n", $project['id'], $project['name']);
}
print "+-------+----------------------------------------------------+\n\n";
}
/**
* Define the source and destination projects
*
* @param array $config
* @param Redmine\Client $source
* @param Redmine\Client $dest
* @return array
*/
function defineProjects(array $config, $source, $dest)
{
if (isset($config['project_map']) && is_array($config['project_map'])) {
return $config;
}
print "\nSource Projects\n\n";
showProjects($source);
$source_project_id = readline('Select a source project ID: ');
$config['project_map']['source_project_id'] = $source_project_id;
print "\nDestination Projects\n\n";
showProjects($dest);
$config['project_map'][$source_project_id] = readline('Select a destination project ID: ');
return $config;
}
//
// Variables
//
$config_file = 'config.json';
// For Composer users (this file is generated by Composer)
require_once 'vendor/autoload.php';
// Grab the config so far, as an associative array
if (file_exists($config_file)) {
$config = json_decode(file_get_contents($config_file), true);
} else {
$config = [];
}
//
// Have the source and destination servers been defined?
//
$config = defineServers($config);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
$source = new Redmine\Client($config['source']['URL'], $config['source']['key']);
$dest = new Redmine\Client($config['dest']['URL'], $config['dest']['key']);
//
// Have the tracker mappings been defined?
//
$config = defineTrackerMapping($config, $source, $dest);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
//
// Have the status mappings been defined?
//
$config = defineStatusMapping($config, $source, $dest);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
//
// Have the priority mappings been defined?
//
$config = definePriorityMapping($config, $source, $dest);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
//
// Have the user mappings been defined?
//
$config = defineUserMapping($config, $source, $dest);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
//
// Define project mapping
//
$config = defineProjects($config, $source, $dest);
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
$source_project_id = $config['project_map']['source_project_id'];
$dest_project_id = $config['project_map'][$source_project_id];
//
// Get destination server user ID to username mapping, required for setImpersonateUser()
//
$users = $dest->user->all();
$userNameMap = [];
foreach ($users['users'] as $user) {
$userNameMap[$user['id']] = $user['login'];
}
/////////////////////////////////////////////////////////////////////////////////////
//
// Issues
//
/////////////////////////////////////////////////////////////////////////////////////
//
// Get the list of issues
// FIXME: Only does 100 issues, does not page through the entire project.
//
$issues = $source->issue->all([
'limit' => 100,
'sort' => 'id',
'project_id' => $source_project_id,
]);
# print_r($issues);
foreach ($issues['issues'] as $issue) {
print "Processing issue ID " . $issue['id'] . ', ' . $issue['subject'] . ', Status: ' . $issue['status']['name'] . "\n";
// Don't process the same issue twice.
if (! empty($config['issue_map'][$issue['id']])) {
print "Already processed -- continuing.\n";
continue;
}
// If you just wanted to try one issue you could find the issue ID and put it in here.
// if ($issue['id'] != 174) {
// continue;
// }
// Get the issue data
$issue_data = $source->issue->show($issue['id'], ['include' => 'attachments,journals']);
$issue_data = $issue_data['issue'];
// echo "\nSource Issue Data\n=================\n\n";
// print_r($issue_data);
// Create a new issue on the destination server
$create_attributes = [
'project_id' => $dest_project_id,
'tracker_id' => $config['tracker_map'][$issue_data['tracker']['id']],
'status_id' => $config['status_map'][$issue_data['status']['id']],
'priority_id' => $config['priority_map'][$issue_data['priority']['id']],
'subject' => $issue_data['subject'],
'description' => $issue_data['description'],
'assigned_to_id' => $config['user_map'][$issue_data['assigned_to']['id']],
'author_id' => $config['user_map'][$issue_data['author']['id']],
];
if (! empty($issue_data['estimated_hours'])) {
$create_attributes['estimated_hours'] = $issue_data['estimated_hours'];
}
// echo "\nDestination Issue Data\n======================\n\n";
// print_r($create_attributes);
// Switch user and create the destination issue
$dest->setImpersonateUser($userNameMap[$create_attributes['author_id']]);
$postResult = $dest->issue->create($create_attributes);
$dest->setImpersonateUser();
// echo "\nPOST Results\n============\n\n";
// print_r($postResult);
/** @var int $new_issue_id */
$new_issue_id = $postResult->id;
echo "New issue $new_issue_id created.\n";
// Add this to the issue map
$config['issue_map'][$issue['id']] = $new_issue_id;
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
// Go through all of the journals and add the notes. At this stage I'm not really interested
// in journals that don't have notes (e.g. status changes).
if (! empty($issue_data['journals'])) {
foreach ($issue_data['journals'] as $journal) {
if (empty($journal['notes'])) {
continue;
}
$private_note = false;
$dest->issue->addNoteToIssue($new_issue_id, $journal['notes'], $private_note);
echo "Note added to issue.\n";
}
}
// Go through all of the attachments and add them ot the destination server
if (! empty($issue_data['attachments'])) {
foreach ($issue_data['attachments'] as $attachment) {
$file_content = $source->attachment->download($attachment['id']);
// To upload a file + attach it to an existing issue with $issueId
$upload = json_decode($dest->attachment->upload($file_content));
$dest->issue->attach($new_issue_id, [
'token' => $upload->upload->token,
'filename' => $attachment['filename'],
'description' => $attachment['description'],
]);
echo "Attachment added to issue.\n";
}
}
}
/////////////////////////////////////////////////////////////////////////////////////
//
// Wikis
//
/////////////////////////////////////////////////////////////////////////////////////
$wikis = $source->wiki->all($source_project_id);
$wikis = $wikis['wiki_pages'];
// print_r($wikis);
foreach ($wikis as $wiki) {
print 'Processing wiki page ' . $wiki['title'] . "\n";
// Don't process the same page twice.
if (! empty($config['wiki_page_map'][$wiki['title']])) {
print "Already processed -- continuing.\n";
continue;
}
// To limit migration to one page only, uncomment this.
// if ($wiki['title'] != 'Wiki') {
// continue;
// }
// Get the page contents
$page = $source->wiki->show($source_project_id, $wiki['title']);
$page = $page['wiki_page'];
// Create the new page on the destination server
$dest->wiki->create($dest_project_id, $wiki['title'], [
'text' => $page['text'],
]);
echo "New page title " . $wiki['title'] . " created.\n";
// Save this to the wiki page map
$config['wiki_page_map'][$wiki['title']] = $wiki['title'];
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
// Go through all of the attachments and add them ot the destination server
if (! empty($page['attachments'])) {
foreach ($page['attachments'] as $attachment) {
$file_content = $source->attachment->download($attachment['id']);
// Currently the php-redmine-api doesn't support attaching files to a wiki page
// so we just store the file locally and ask the user to attach.
file_put_contents($attachment['filename'], $file_content);
echo "Please upload file " . $attachment['filename'] . " to page " . $wiki['title'] . "\n";
/*
// To upload a file + attach it to an existing issue with $issueId
$upload = json_decode($dest->attachment->upload($file_content));
$result = $dest->wiki->update($dest_project_id, $wiki['title'], [
'text' => $page['text'],
'uploads' => [0 => [
'token' => $upload->upload->token,
'filename' => $attachment['filename'],
]]
]);
print "=====\n";
print_r($result);
print "=====\n";
echo "Attachment " . $attachment['filename'] . " added to wiki page.\n";
*/
}
}
}