-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsyncLinks.pl
executable file
·197 lines (170 loc) · 6.06 KB
/
syncLinks.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
#!/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);
my $issuesCount = scalar @{$export};
print "Exported issues: $issuesCount\n";
# 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
);
# Just check the issueLinks
unless ($notest) {
$check->issueLinks();
}
$display->printTitle("Getting Jira Issues");
$allIssues = $jira->getAllIssues(Project => $JiraProject, Max => $issuesCount);
my $jiraCount = scalar @{$allIssues->{'issues'}};
my $jiraCount2 = $allIssues->{'total'};
print "Exported issues: $jiraCount out of $jiraCount2\n";
my $jiraIssues = {};
foreach my $jira (@{$allIssues->{'issues'}}) {
$jiraIssues->{$jira->{key}} = $jira;
}
# Check that the issue numbers line up
$display->printTitle("Checking issue names");
foreach my $issue (sort { $a->{numberInProject} <=> $b->{numberInProject} } @{$export}) {
#$display->printTitle($YTProject."-".$issue->{numberInProject});
my $jiraIssue = $jiraIssues->{$JiraProject."-".$issue->{numberInProject}};
my $ok = "???";
if ($jiraIssue) {
$ok = ($issue->{summary} eq $jiraIssue->{fields}->{summary}) ? "OK" : "BAD";
$issue->{jiraKey} = $jiraIssue->{key};
} else {
$issue->{jiraKey} = ($JiraProject."-".$issue->{numberInProject});
}
check::printRelation(
$YTProject."-".$issue->{numberInProject}." ".$issue->{summary},
$issue->{jiraKey}." ".$jiraIssue->{fields}->{summary},
$ok
);
}
print "If all issue numbers match, it's safe to continue";
&ifProceed;
# 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 {
print $link->{linkType}->{name} . " is not defined in map\n";
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 $issue->{jiraKey} . ": 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. ";
if (($issue->{id} =~ m/.*-/g) eq ($linkedIssue->{id} =~ m/.*-/g )) {
print "Most likely the second issue is not migrated yet\n";
} else {
print "Cross-project link not migrated\n";
}
}
} else {
print $issue->{idReadable}.": already linked ". ($issuesById{$linkedIssue->{id}}->{jiraKey}) ."\n";
}
} else {
print $issue->{jiraKey} .": " .$linkedIssue->{id} . " does not exist in map (cross-project link?)\n"
}
}
}
}
}
$display->printTitle("ENJOY :)");
sub ifProceed {
print "\nProceed? (y/N) ";
my $input = <>;
chomp $input;
exit unless (lc($input) eq 'y');
}