-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmigrate.pl
executable file
·438 lines (367 loc) · 14 KB
/
migrate.pl
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
#!/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";
use Data::Dumper;
use Getopt::Long;
use IPC::Run qw( run );
use Date::Format;
use Encode;
# Used to display column-like output
my $display = display->new();
$display->printTitle("Initialization");
my ($skip, $notest, $maxissues, $cookieFile, $verbose);
Getopt::Long::Configure('bundling');
GetOptions(
"skip|s=i" => \$skip,
"no-test|t" => \$notest,
"max-issues|m=i" => \$maxissues,
"cookie-file|c=s" => \$cookieFile,
"verbose|v" => \$verbose
);
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,
);
unless ($jira) {
die "Could not login to $JiraUrl\n";
}
print "Success\n";
$display->printTitle("Getting YouTrack Issues");
my $export = $yt->exportIssues(Project => $YTProject, Max => $maxissues);
print "Exported issues: ".scalar @{$export}."\n";
# Find active users from issues, commetns 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 %User) {
$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 => \%User,
TypeFieldName => $typeCustomFieldName,
Types => \%Type,
Links => \%IssueLinks,
ExportCreationTime => $exportCreationTime,
CreationTimeFieldName => $creationTimeCustomFieldName,
Fields => \%CustomFields,
PriorityFieldName => $priorityCustomFieldName,
Priorities => \%Priority,
StatusFieldName => $stateCustomFieldName,
Statuses => \%Status,
StatusToResolutions => \%StatusToResolution
);
%User = %{$check->users()};
unless ($notest) {
$check->passwords();
$check->issueTypes();
$check->issueLinks();
$check->fields();
$check->priorities();
$check->statuses();
$check->resolutions();
&ifProceed;
}
my $issuesCount = 0;
$display->printTitle("Export To Jira");
foreach my $issue (sort { $a->{numberInProject} <=> $b->{numberInProject} } @{$export}) {
$display->printTitle($YTProject."-".$issue->{numberInProject});
if ($skip && $issue->{numberInProject} <= $skip) {
print "Skipping issue $YTProject-".$issue->{numberInProject}."\n";
next;
}
$issuesCount++;
last if ($maxissues && $issuesCount>$maxissues);
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);
}
print "Will import issue $YTProject-".$issue->{numberInProject}."\n";
# 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 ($User{$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);
}
my %import = ( project => { key => $JiraProject },
issuetype => { name => $Type{$issue->{$typeCustomFieldName}} || $issue->{$typeCustomFieldName} },
assignee => { id => $JiraUserIds{$User{$issue->{Assignee}} || $issue->{Assignee}} },
reporter => { id => $JiraUserIds{$User{$issue->{reporter}->{login}} || $issue->{reporter}->{login}} },
summary => $issue->{summary},
description => $header.$description,
priority => { name => $Priority{$issue->{Priority}} || $issue->{Priority} || 'Medium' }
);
# Let's check through custom fields
my %custom;
foreach my $field (keys %CustomFields) {
if (defined $issue->{$field}) {
if (defined $User{$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 {
$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);
}
# Let's check for labels
if ($exportTags eq 'true') {
my @tags = $yt->getTags(IssueKey => $issue->{id});
if (@tags) {
$import{labels} = [@tags];
print "Found tags: ".Dumper(@tags) if ($verbose);
}
}
my $key = $jira->createIssue(Issue => \%import, CustomFields => \%custom) || warn "Error while creating issue";
print "Jira issue key generated $key\n";
# Checking issue number in key (eg in FOO-20 the issue number is 20)
if ($key =~ /^[A-Z]+-(\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";
}
$key = $jira->createIssue(Issue => \%import, CustomFields => \%custom) || warn "Error while creating issue";
print "\nNew Jira issue key generated $key\n";
$key =~ /^[A-Z]+-(\d+)$/;
}
} else {
die "Wrong issue key $key";
}
# Save Jira issue key for forther linking
$issue->{jiraKey} = $key;
# Transition
if ($Status{$issue->{State}}) {
print "\nChanging status to ".$Status{$issue->{State}}."\n";
unless ($jira->doTransition(Key => $key, Status => $Status{$issue->{State}})) {
warn "Failed doing transition";
}
}
# Resolution
if ($StatusToResolution{$issue->{State}}) {
print "\nChanging resolution to ".$StatusToResolution{$issue->{State}}."\n";
unless ($jira->changeFields(Key => $key, Fields => { 'Resolution' => $StatusToResolution{$issue->{State}} } )) {
warn "Failed updating fields"
}
}
# Create comments
print "\nCreating comments\n";
foreach my $comment (@{$issue->{comments}}) {
my $author = $User{$comment->{author}->{login}} || $comment->{author}->{login};
my $date = scalar localtime ($comment->{created}/1000);
my $text = $comment->{text};
# Convert Markdown to Jira-specific rich text formatting
$text = convertUserMentions($text);
$text = convertAttachmentsLinks($text, $attachmentFileNamesMapping);
if($convertTextFormatting eq 'true') {
$text = convertCodeSnippets($text);
$text = convertQuotations($text);
$text = convertMarkdownToJira($text);
}
my $header;
if ( $JiraPasswords{$author} && not $JiraPasswords{$author} eq $JiraPassword ) {
$header = "[ $date ]\n";
$text = $header.$text;
my $jiraComment = $jira->createComment(IssueKey => $key, Body => $text, Login => $author, Password => $JiraPasswords{$author}) || warn "Error creating comment";
} else {
$header = convertUserMentions("[ \@".$comment->{author}->{login}." $date ]\n");
$text = $header.$text;
my $jiraComment = $jira->createComment(IssueKey => $key, Body => $text) || warn "Error creating comment";
}
}
# Export work log
if ($exportWorkLog eq 'true') {
print "\nExporting 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{$User{ $workLog->{author}->{login} }} and not $JiraPasswords{$User{ $workLog->{author}->{login} }} eq $JiraPassword ) {
$jira->addWorkLog(Key => $key,
WorkLog => \%jiraWorkLog,
Login => $User{ $workLog->{author}->{login} },
Password => $JiraPasswords{$User{ $workLog->{author}->{login} }})
|| warn "\nError creating work log";
} else {
my $originalAuthor = convertUserMentions("[ Original Author: \@".$workLog->{author}->{login}." ]\n");
$jiraWorkLog{comment} = $originalAuthor."".$jiraWorkLog{comment};
$jira->addWorkLog(Key => $key, WorkLog => \%jiraWorkLog)
|| warn "\nError creating work log";
}
}
}
# If descriptions exceeds Jira limitations - save it as an attachment
if (length $header.$description >= 32766) {
print "\nDescription 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}." files\n";
unless ($jira->addAttachments(IssueKey => $key, Files => $attachments)) {
warn "Cannot upload attachment to $key";
}
}
}
# Create Issue Links
if ($exportLinks eq 'true') {
$display->printTitle("Creating Issue Links");
# 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}) {
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}}) {
if ($link->{direction} eq 'INWARD' || $link->{direction} eq 'BOTH') {
$jiraLink->{inwardIssue}->{key} = $issue->{jiraKey};
$jiraLink->{outwardIssue}->{key} = $issuesById{$linkedIssue->{id}}->{jiraKey};
} elsif ($link->{direction} eq 'OUTWARD') {
$jiraLink->{inwardIssue}->{key} = $issuesById{$linkedIssue->{id}}->{jiraKey};
$jiraLink->{outwardIssue}->{key} = $issue->{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{$User{$1}}\]/g;
return $textToConvert;
}
# Converts links to attachments
sub convertAttachmentsLinks {
my $textToConvert = shift;
my $attachmentFileNamesMapping = shift;
# Convert attachment ![](image.png) 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;
}