Repository navigation
Expand file tree
/
Copy pathmigrate.pl
More file actions
executable file
·596 lines (515 loc) · 18.9 KB
/
Copy pathmigrate.pl
File metadata and controls
executable file
·596 lines (515 loc) · 18.9 KB
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
#!/usr/bin/perl
use File::Basename qw/dirname/;
use File::Temp qw ( tempdir );
use lib dirname(__FILE__);
use display;
use youtrack;
use check;
use jira;
require "config.pl";
require "credentials.pl";
use Data::Dumper;
use Getopt::Long;
use IPC::Run qw( run );
use Date::Format;
use Encode;
use IO::Handle;
STDOUT->autoflush(1);
STDERR->autoflush(1); # if you print to stderr too
# Used to display column-like output
my $display = display->new();
$display->printTitle("Initialization");
my ($skip, $notest, $first, $checkKeys, $cookieFile, $verbose);
Getopt::Long::Configure('bundling');
GetOptions(
"first|f=i" => \$first,
"skip|s=i" => \$skip,
"check-keys!" => \$checkKeys,
"no-test|t" => \$notest,
"cookie-file|c=s" => \$cookieFile,
"verbose|v" => \$verbose
);
if (not defined $checkKeys) {
$checkKeys = 0;
}
my $yt = youtrack->new( Url => $YTUrl,
Token => $YTtoken,
Verbose => $verbose,
Project => $YTProject );
unless ($yt) {
die "Could not login to $YTUrl";
}
my $jira = jira->new(Url => $JiraUrl,
Login => $JiraLogin,
Password => $JiraPassword,
Verbose => $verbose,
Project => $JiraProject,
CookieFile => $cookieFile,
ZtnaBoard => $ztnaBoardName);
unless ($jira) {
die "Could not login to $JiraUrl\n";
}
print "Success\n";
$display->printTitle("Getting YouTrack Issues");
my $export = $yt->exportIssues(Project => $YTProject, Max => 0);
print "Exported issues: ".scalar @{$export}."\n";
# Find active users from issues, comments and other YT activity
my %users;
foreach my $issue (@{$export}) {
$users{$issue->{Assignee}} = 1;
$users{$issue->{reporter}->{login}} = 1;
foreach (@{$issue->{comments}}) {
$users{$_->{author}->{login}} = 1;
}
}
# Join those active with those that are listed in config
# in case if config ones are not listed in the %users
foreach my $configUser (keys %Users) {
$users{$configUser} = 1;
}
print Dumper(%users) if ($verbose);
my $check = check->new(
Jira => $jira,
YouTrack => $yt,
Url => $JiraUrl,
JiraLogin => $JiraLogin,
Passwords => \%JiraPasswords,
JiraUserIds => \%JiraUserIds,
RealUsers => \%users,
Users => \%Users,
TypeFieldName => $typeCustomFieldName,
Types => \%Type,
Links => \%IssueLinks,
ExportCreationTime => $exportCreationTime,
CreationTimeFieldName => $creationTimeCustomFieldName,
Fields => \%CustomFields,
PriorityFieldName => $priorityCustomFieldName,
Priorities => \%Priority,
StatusFieldName => $stateCustomFieldName,
Statuses => \%Status,
StatusToResolutions => \%StatusToResolution
);
%Users = %{$check->users()};
unless ($notest) {
$check->passwords();
$check->issueTypes();
$check->issueLinks();
$check->fields();
$check->priorities();
$check->statuses();
$check->resolutions();
}
my $issuesCount = 0;
$display->printTitle("Export To Jira");
print "Ready to import into Jira using options:\n";
print "\tfirst=$first\n";
print "\tskip=$skip\n";
print "\tcheck-keys=$checkKeys\n";
print "\tYouTrack Project=$YTProject\n";
print "\tJira Project=$JiraProject\n";
&ifProceed;
my @sortedIssues = sort { $a->{numberInProject} <=> $b->{numberInProject} } @{$export};
my $length = scalar @{$export};
if ($verbose){
print "count items export: $length\n";
%length = scalar keys @sortedIssues;
print "count items sortedIssues: $length\n";
}
$first = $length if (not defined $first);
print "Will process first $first issues\n";
my @firstNIssues = grep { $_->{numberInProject} <= $first } @sortedIssues;
print "Print this list of issues to help with troubleshooting";
if ($verbose){
foreach my $issue (@firstNIssues) {
print "Issue ID: " . $issue->{idReadable} . "\tNumber in Project: " . $issue->{numberInProject} . "\n";
}
#Issue ID: SA-2 Number in Project: 2
#Issue ID: SA-3 Number in Project: 3
#Issue ID: SA-4 Number in Project: 4
#Issue ID: SA-5 Number in Project: 5
#Issue ID: SA-6 Number in Project: 6
#Issue ID: SA-7 Number in Project: 7
#Issue ID: SA-8 Number in Project: 8
#Issue ID: SA-9 Number in Project: 9
#Issue ID: SA-11 Number in Project: 11
#Issue ID: SA-12 Number in Project: 12
&ifProceed;
}
foreach my $issue (@firstNIssues) {
$display->printTitle($issue->{idReadable});
if ($skip && $issue->{numberInProject} <= $skip) {
print "Skipping issue ".$issue->{idReadable}."\n";
next;
}
$issuesCount++;
last if ($first && $issuesCount > $first);
print "Will import issue ".$issue->{idReadable}."\n";
print "Processing issue number $issuesCount\n";
my $attachmentFileNamesMapping;
my $attachments;
# Download attachments
if ($exportAttachments eq 'true') {
print "Check for attachments\n";
($attachments, $attachmentFileNamesMapping) = $yt->downloadAttachments(IssueKey => $issue->{id});
print Dumper(@{$attachments}) if ($verbose);
}
# Prepare creation time message if exportCreationTime setting is not set
my $creationTime = scalar localtime ($issue->{created}/1000);
my $header = "";
if (not($exportCreationTime)) {
$header .= "[Created ";
if ($Users{$issue->{reporter}->{login}} eq $JiraLogin) {
$header .= "by ".$issue->{reporter}->{login}." ";
}
$header .= $creationTime;
$header .= "]\n";
}
# Convert Markdown to Jira-specific rich text formatting
# my $description = convertUserMentions($issue->{description});
# $description = convertAttachmentsLinks($description, $attachmentFileNamesMapping);
# if($convertTextFormatting eq 'true') {
# $description = convertCodeSnippets($description);
# $description = convertQuotations($description);
# $description = convertMarkdownToJira($description);
# }
# $description = removeHtmlTags($description);
my $description = $issue->{description};
my %import = ( project => { key => $JiraProject },
issuetype => { name => $Type{$issue->{$typeCustomFieldName}} || $issue->{$typeCustomFieldName} },
assignee => { id => $JiraUserIds{$Users{$issue->{Assignee}} || $issue->{Assignee}} },
reporter => { id => $JiraUserIds{$Users{$issue->{reporter}->{login}} || $issue->{reporter}->{login}} },
summary => $issue->{summary},
description => $description,
priority => { name => $Priority{$issue->{Priority}} || $issue->{Priority} || '200' } #TODO: change this to empty string
);
# if there is no assignee in YouTrack, then remove the assignee field from the import hash so it's unassigned in Jira
if(!defined $issue->{Assignee} || $issue->{Assignee} eq '') {
delete $import{assignee};
}
# Let's check through custom fields
my %custom;
foreach my $field (keys %CustomFields) {
if (defined $issue->{$field}) {
if (defined $Users{$issue->{$field}}) {
# If the value of the field happens to be a username, assume this is a user field.
$custom{$CustomFields{$field}} = $JiraUserIds{$User{$issue->{$field}}};
} else {
if ($field eq "Fix for") {
#regex to remove the blank space and anything after. example "0.8 (P8)" -> "0.8"
$issue->{$field} =~ s/ .*$// if defined $issue->{$field};
} elsif ($field eq "Impact") {
# if Impact in YouTrack is empty, then set it to "Not reviewed" in JIRA
$issue->{$field}->[0] = "Not reviewed" if defined $issue->{$field} and $issue->{$field}->[0] eq '';
}
$custom{$CustomFields{$field}} = $issue->{$field};
}
}
}
# Epic Name special field is required for Jira Epic issues type
if ($import{issuetype}->{name} == 'Epic') {
$custom{"Epic Name"} = $issue->{summary};
}
# Add YouTrack original creation date field
my %dateTimeFormats = (
RFC822 => "%a, %d %b %Y %H:%M:%S %z",
RFC3389 => "%Y-%m-%dT%H:%M:%S%z",
ISO8601 => "%Y-%m-%dT%T%z",
GOST7.0.64 => "%Y%m%dT%H%M%S%z",
JIRA8601 => "%Y-%m-%dT%T.00%z"
);
if ($exportCreationTime eq 'true') {
my @parsedTime = localtime ($issue->{created}/1000);
$custom{$creationTimeCustomFieldName} = strftime($dateTimeFormats{"$creationDateTimeFormat"}, @parsedTime);
print "Setting original creation time to ".$custom{$creationTimeCustomFieldName}."\n";
}
# Let's check for labels/tags
if ($exportTags eq 'true') {
$msg = "Checking for tags";
my @tags = $yt->getTags(IssueKey => $issue->{id});
if (@tags) {
$msg .= " - setting tags and their values";
$import{labels} = [@tags];
print "Found tags: ".Dumper(@tags) if ($verbose);
}
else {
$msg .= " - no tags found";
}
print $msg."\n";
}
my $key = $jira->createIssue(Issue => \%import, CustomFields => \%custom) || warn "Error while creating issue\n";
print "Jira issue key generated $key\n";
# Checking issue number in key (eg in FOO-20 the issue number is 20)
if ($checkKeys) {
if ($key =~ /^[A-Z0-9]+-(\d+)$/) {
while ( $1 < $issue->{numberInProject} && ($issue->{numberInProject} - $1) <= $maximumKeyGap ) {
print "We're having a gap and will delete the issue\n";
unless ($jira->deleteIssue(Key => $key)) {
warn "Error while deleting the issue $key\n";
}
$key = $jira->createIssue(Issue => \%import, CustomFields => \%custom) || warn "Error while creating issue\n";
print "New Jira issue key generated $key\n";
$key =~ /^[A-Z0-9]+-(\d+)$/;
}
} else {
die "Wrong issue key $key\n";
}
}
# Save Jira issue key for further linking
$issue->{jiraKey} = $key;
# Transition
if ($Status{$issue->{State}}) {
print "Changing status to ".$Status{$issue->{State}}."\n";
unless ($jira->doTransition(Key => $key, Status => $Status{$issue->{State}})) {
warn "Failed doing transition\n";
}
}
# Resolution
if ($StatusToResolution{$issue->{State}}) {
print "Changing resolution to ".$StatusToResolution{$issue->{State}}."\n";
unless ($jira->changeFields(Key => $key, Fields => { 'Resolution' => $StatusToResolution{$issue->{State}} } )) {
warn "Failed updating fields\n"
}
}
# Create comments
print "Creating comments\n";
my @sorted_comments = sort { $b->{created} <=> $a->{created} } @{$issue->{comments}};
foreach my $comment (@sorted_comments) {
my $login = $comment->{author}->{login};
my $author = $Users{$login} || $login;
my $date = scalar localtime ($comment->{created}/1000);
my $text = $comment->{text};
# Check if text is ADF format (hash reference) or plain text
my $isADF = ref($text) eq 'HASH';
if ( $JiraPasswords{$author} && not $JiraPasswords{$author} eq $JiraPassword ) {
if (!$isADF) {
my $header = "[ $date ]\n";
$text = $header.$text;
}
# For ADF, skip header - original timestamp preserved in Jira comment metadata
my $jiraComment = $jira->createComment(IssueKey => $key, Body => $text, Login => $author, Password => $JiraPasswords{$author}) || warn "Error creating comment\n";
} else {
if (!$isADF) {
my $header = convertUserMentions("[ \@$login $date ]\n");
$text = $header.$text;
}else{
# prepend a header to the ADF document's content array.
unshift @{$text->{content}},
{
type => "paragraph",
content => [
{ type => "text", text => "[ " },
{
type => "text",
text => "\@".$login,
marks => [
{
type => "link",
attrs => {
href => "/jira/people/".$JiraUserIds{$author}
}
}
]
},
{ type => "text", text => " $date ]" }
]
};
}
my $jiraComment = $jira->createComment(IssueKey => $key, Body => $text) || warn "Error creating comment\n";
}
}
# Export work log
if ($exportWorkLog eq 'true') {
print "Exporting work log\n";
my $workLogs = $yt->getWorkLog( IssueKey => $issue->{idReadable} );
foreach my $workLog (@{$workLogs->{workItems}}) {
my @parsedTime = localtime ($workLog->{created}/1000);
my %jiraWorkLog = (
comment => $workLog->{text},
started => strftime($dateTimeFormats{"$creationDateTimeFormat"}, @parsedTime),
timeSpentSeconds => $workLog->{duration}->{minutes} * 60
);
if ( $JiraPasswords{$Users{ $workLog->{author}->{login} }} and not $JiraPasswords{$Users{ $workLog->{author}->{login} }} eq $JiraPassword ) {
$jira->addWorkLog(Key => $key,
WorkLog => \%jiraWorkLog,
Login => $Users{ $workLog->{author}->{login} },
Password => $JiraPasswords{$Users{ $workLog->{author}->{login} }})
|| warn "Error creating work log\n";
} else {
my $originalAuthor = convertUserMentions("[ Original Author: \@".$workLog->{author}->{login}." ]\n");
$jiraWorkLog{comment} = $originalAuthor."".$jiraWorkLog{comment};
$jira->addWorkLog(Key => $key, WorkLog => \%jiraWorkLog)
|| warn "Error creating work log\n";
}
}
}
# If descriptions exceeds Jira limitations - save it as an attachment
if (length $header.$description >= 32766) {
print "Description exceeds Jira max symbol limitation and will be saved as attachment.\n";
my $tempdir = tempdir();
open my $fh, ">", "$tempdir/description.md";
binmode $fh, "encoding(UTF-8)";
print $fh $issue->{description};
close $fh;
push @{$attachments}, "$tempdir/description.md";
}
# Upload attachments to Jira
if (@{$attachments}) {
print "Uploading ".scalar @{$attachments}." attachments\n";
unless ($jira->addAttachments(IssueKey => $key, Files => $attachments)) {
warn "Cannot upload attachment to $key\n";
}
}
}
# Create Issue Links
if ($exportLinks eq 'true') {
$display->printTitle("Creating Issue Links");
&ifProceed;
# Turn YT issues to a hash to be able to search for issue ID
my %issuesById = map { $_->{id} => $_ } @{$export};
# Keep linked issues in hash to avoid duplicates on BOTH type of links
my %alreadyEstablishedLinksWith = map { $_ => () } keys %IssueLinks;
foreach my $issue (sort { $a->{numberInProject} <=> $b->{numberInProject} } @{$export}) {
print "Attempting to create links on issue ".$issue->{idReadable}."\n";
my $links = $yt->getIssueLinks(IssueKey => $issue->{id});
foreach my $link (@{$links}) {
my $jiraLink;
# If this link does not have any issues attached - skip to the next one
if (!@{$link->{issues}}){
next;
}
# Check if config has this issue link type name
if (defined $IssueLinks{$link->{linkType}->{name}}) {
$jiraLink->{type}->{name} = $IssueLinks{$link->{linkType}->{name}};
} else {
next;
}
foreach my $linkedIssue (@{$link->{issues}}) {
if (exists $issuesById{$linkedIssue->{id}}) {
my $jiraKey = $issue->{jiraKey};
if (!$jiraKey) {
# replace "SA" or "ZTP" with Jira project key
my ($ytProject, $ytId) = split /-/, $issue->{idReadable};
if ($ytProject eq "SA" || $ytProject eq "ZTP") {
$ytProject = $JiraProject;
}
if ($ytProject eq "ZTP") {
#increment id
}
$jiraKey = $ytProject."-".$ytId;
}
my $linkedJiraKey = $issuesById{$linkedIssue->{id}}->{jiraKey};
if (!$linkedJiraKey) {
# replace "SA" or "ZTP" with Jira project key
my ($ytProject, $ytId) = split /-/, $issuesById{$linkedIssue->{id}}->{idReadable};
if ($ytProject eq "SA" || $ytProject eq "ZTP") {
$ytProject = $JiraProject;
}
if ($ytProject eq "ZTP") {
#increment id
}
$linkedJiraKey = $ytProject."-".$ytId;
}
if ($link->{direction} eq 'INWARD' || $link->{direction} eq 'BOTH') {
$jiraLink->{inwardIssue}->{key} = $jiraKey;
$jiraLink->{outwardIssue}->{key} = $linkedJiraKey;
} elsif ($link->{direction} eq 'OUTWARD') {
$jiraLink->{inwardIssue}->{key} = $linkedJiraKey;
$jiraLink->{outwardIssue}->{key} = $jiraKey;
}
if (not $alreadyEstablishedLinksWith{$link->{linkType}->{name}}{join(" ", sort($linkedIssue->{id}, $issue->{id}))}) {
print "Creating link between ".$jiraLink->{outwardIssue}->{key}." and ".$jiraLink->{inwardIssue}->{key}."\n";
if ($jira->createIssueLink( Link => $jiraLink )) {
# To avoid link duplications (for BOTH direction type of issue link)
$alreadyEstablishedLinksWith{$link->{linkType}->{name}}{join(" ", sort($linkedIssue->{id}, $issue->{id}))} = 1;
print " Done\n";
} else {
print " Failed. Most likely the second issue is not migrated yet\n";
}
}
}
}
}
}
}
$display->printTitle("ENJOY :)");
sub ifProceed {
print "\nProceed? (y/N) ";
my $input = <>;
chomp $input;
exit unless (lc($input) eq 'y');
}
# Converts Markdown text format to Jira format using J2M utility
sub convertMarkdownToJira {
my $textToConvert = shift;
my @j2mCommand = ('j2m', '--toJ', '--stdin');
run(\@j2mCommand, \$textToConvert, \my $j2mConvertedText)
or die "Something wrong with J2M tool, is it installed? ".
"Try install it using:\n\n\tnpm install j2m --save\n\n";
return decode_utf8($j2mConvertedText);
}
# Converts user mentions to correct usernames
sub convertUserMentions {
my $textToConvert = shift;
# Convert user @foo mentions to Jira [~accountid:$personId] links (doesn't seem to work)
# $textToConvert =~ s/\B\@(\S+)/\@$1 \[\~acccountid:$JiraUserIds{$User{$1}}\])/g;
# Convert user @foo mentions to Jira [@foo|/jira/people/$personId] links
$textToConvert =~ s/\B\@([^\s,]+)/\[\@$1\|\/jira\/people\/$JiraUserIds{$Users{$1}}\]/g;
return $textToConvert;
}
# Converts links to attachments
sub convertAttachmentsLinks {
my $textToConvert = shift;
my $attachmentFileNamesMapping = shift;
# Convert attachment  links to Jira links !image.png|thumbnail!
$textToConvert =~ s/!\[\]\((.+?)\)/"!".%{$attachmentFileNamesMapping}{$1}."|thumbnail!"/eg;
return $textToConvert;
}
sub convertCodeSnippets {
my $textToConvert = shift;
# Convert ``` to {code}
$textToConvert =~ s/```(\w*)\n/($1 ? "{code:$1}\n" : "{code}\n")/eg;
$textToConvert =~ s/```/\n{code}\n/g;
return $textToConvert;
}
sub convertQuotations {
my $textToConvert = shift;
# Convert > to {quote}
$textToConvert =~ s/^> *(.*)/{quote}\n$1\n{quote}/gm;
return $textToConvert;
}
sub removeHtmlTags {
my $textToConvert = shift;
my $div_begin = '[div class="wiki text prewrapped"]';
my $div_end = '[/div]';
$textToConvert =~ s/\Q$div_begin\E//;
$textToConvert =~ s/\Q$div_end\E//;
my $begin_pos = index($textToConvert, '[a');
my $a = '';
while($begin_pos != -1) {
my $end_pos = index($textToConvert, '[/a]');
$a = substr($textToConvert, $begin_pos, $end_pos-$begin_pos + length('[/a]'));
$textToConvert =~ s/\Q$a\E//;
$begin_pos = index($textToConvert, '[a');
}
$html_begin = '[/li]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[li]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[h3]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[/h3]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[ul class="wiki-list0"]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[/ul]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[strong]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[/strong]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '[div class="wiki quote"]';
$textToConvert =~ s/\Q$html_begin\E//g;
$html_begin = '\[br/\]';
$textToConvert =~ s/$html_begin/\n/g;
return $textToConvert;
}