-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyoutrack.pm
328 lines (278 loc) · 8.22 KB
/
youtrack.pm
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
package youtrack;
use strict;
use Data::Dumper;
require LWP::UserAgent;
use JSON qw( decode_json );
use File::Temp qw ( tempdir );
use List::Util qw( first );
use File::Basename;
use Encode;
use utf8;
my $ua;
our $currentField;
our $currentIssue;
our $data;
sub new {
my $class = shift;
my %arg = @_;
return unless $arg{Url};
my $self;
$ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->default_headers->header(
'Content-Type'=>'application/json',
'Accept'=>'application/json',
'Authorization'=>'Bearer '.$arg{Token});
my $response = $ua->get($arg{Url}.'/api/users/me');
if ($response->is_success) {
print "Logged to YT successfully\n" if ($arg{Verbose});
print Dumper($response) if ($arg{Verbose});
$self = { url => $arg{Url}, project => $arg{Project}, verbose => $arg{Verbose} };
}
else {
die $response->status_line;
}
bless $self, $class;
}
# Downloads each attachment in the issue and stores it in tmp folder
sub downloadAttachments {
my $self = shift;
my %arg = @_;
my $attachments = $self->sendRequestToYouTrack(
Request => '/api/issues/'.$arg{IssueKey}.'/attachments?'.
'fields='.
'url,'.
'name',
ErrorMessage => "Got error while getting attachments\n");
unless (defined $attachments) {
return undef;
}
my @downloadedFilesPaths;
# Safe old file name to fix attachments links in issues
my %oldFileNamesDirectory;
my $tempdir = tempdir();
my $counter = 1;
foreach my $attachment (@{$attachments}) {
# Attachment URL includes 'youtrack/' word, remove it
$attachment->{url} =~ s/youtrack\///;
my $file = $ua->get($self->{url}.$attachment->{url});
# Extract file extension if any
my ($filename, $dirs, $suffix) = fileparse($attachment->{name}, qr/\.[^.]*$/);
my $localizedFileName = decode_utf8($attachment->{name});
$oldFileNamesDirectory{$localizedFileName} = "attachment$counter$suffix";
# Rename the file to avoid problems with exotic file names
open my $fh, ">", "$tempdir/".$oldFileNamesDirectory{$localizedFileName};
binmode $fh;
print $fh $file->content;
close $fh;
push @downloadedFilesPaths, "$tempdir/".$oldFileNamesDirectory{$localizedFileName};
$counter++;
}
return (\@downloadedFilesPaths, \%oldFileNamesDirectory);
}
# Returns the list of tag names for specific issue id
sub getWorkLog {
my $self = shift;
my %arg = @_;
return $self->sendRequestToYouTrack(
Request => '/api/issues/'.$arg{IssueKey}.'/timeTracking?'.
'fields='.
'workItems('.
'text,'.
'created,'.
'duration('.
'minutes'.
'),'.
'author('.
'login'.
')'.
')',
ErrorMessage => 'Got error while getting work log');
}
# Returns the list of tag names for specific issue id
sub getTags {
my $self = shift;
my %arg = @_;
my $tagsRaw = $self->sendRequestToYouTrack(
Request => '/api/issues/'.$arg{IssueKey}.'/tags?'.
'fields='.
'name',
ErrorMessage => 'Got error while getting tags\n',
CharacterSupport => 'true');
unless (defined $tagsRaw) {
return undef;
}
my @tags;
foreach (@{$tagsRaw}) {
push(@tags, $_->{name});
}
return @tags;
}
sub getIssueLinks {
my $self = shift;
my %arg = @_;
return $self->sendRequestToYouTrack(
Request => '/api/issues/'.$arg{IssueKey}.'/links?'.
'fields='.
'direction,'.
'linkType(name),'.
'issues(id)',
ErrorMessage => "Got error while getting links\n",
CharacterSupport => 'true');
}
sub getAllLinkTypes {
my $self = shift;
my %arg = @_;
return $self->sendRequestToYouTrack(
Request => '/api/issueLinkTypes?'.
'fields='.
'name',
ErrorMessage => "Got error while getting link types\n",
CharacterSupport => 'true');
}
sub getAllCustomFields {
my $self = shift;
my %arg = @_;
my $customFields = $self->sendRequestToYouTrack(
Request => '/api/admin/projects/'.$self->{project}.'/customFields?'.
'fields='.
'field('.
'instances('.
'field('.
'name'.
'),'.
'project('.
'shortName'.
'),'.
'bundle('.
'id,'.
'values('.
'name'.
')'.
')'.
')'.
')',
ErrorMessage => "Cannot retrieve custom fields information from YouTrack.",
CharacterSupport => 'true');
my %fieldsWithValues;
foreach my $fieldRef (@{$customFields}) {
foreach my $customField (@{$fieldRef->{field}->{instances}}) {
if($customField->{project}->{shortName} eq $self->{project}) {
my @bundleNames = map ($_->{name}, @{$customField->{bundle}->{values}});
$fieldsWithValues{$customField->{field}->{name}} = \@bundleNames;
}
}
}
return %fieldsWithValues;
}
sub exportIssues {
my $self = shift;
my %arg = @_;
my $max = $arg{Max} || 100000;
my $issues = $self->sendRequestToYouTrack(
Request => '/api/issues?'.
'query=project:%20'.$arg{Project}.'%20&'.
'$top='.$max.'&'.
'fields='.
'id,'.
'idReadable,'.
'created,'.
'numberInProject,'.
'summary,'.
'description,'.
'comments('.
'author('.
'login'.
'),'.
'text,'.
'created'.
'),'.
'reporter('.
'login'.
'),'.
'customFields('.
'name,'.
'value('.
'name,'.
# TextIssueCustomField
'text,'.
# PeriodIssueCustomField
'presentation,'.
'login'.
')'.
')',
ErrorMessage => "Got error while exporting issues\n",
CharacterSupport => 'true');
foreach my $issue (@{$issues}) {
foreach my $field (@{$issue->{customFields}}) {
$issue->{$field->{name}} = undef;
next if (not($field->{value}));
$issue->{$field->{name}} = collectValuesFromCustomField(\%{$field});
}
}
return $issues;
}
sub sendRequestToYouTrack {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}."".$arg{Request});
if ($response->is_success) {
# All languages support
my $content = $response->decoded_content;
if ($arg{CharacterSupport} eq "true") {
$content = decode_utf8($content);
}
my $json = JSON->new;
return $json->decode($content);
} else {
print $arg{ErrorMessage}."\n";
print $response->decoded_content."\n" if ($self->{verbose});
print $response->status_line."\n" if ($self->{verbose});
}
return undef;
}
my %periodType = ( 'PeriodIssueCustomField' => 1);
my %simpleType = ( 'SimpleIssueCustomField' => 1, 'DateIssueCustomField' => 1);
my %singleType = ( 'SingleValueIssueCustomField'=> 1, 'StateIssueCustomField'=> 1,
'SingleBuildIssueCustomField'=> 1, 'SingleUserIssueCustomField'=> 1,
'SingleGroupIssueCustomField'=> 1, 'SingleVersionIssueCustomField'=> 1,
'SingleOwnedIssueCustomField'=> 1, 'SingleEnumIssueCustomField'=> 1,
'StateMachineIssueCustomField'=> 1);
my %multiType = ( 'MultiValueIssueCustomField'=> 1, 'MultiBuildIssueCustomField'=> 1,
'MultiGroupIssueCustomField'=> 1, 'MultiVersionIssueCustomField'=> 1,
'MultiOwnedIssueCustomField'=> 1, 'MultiEnumIssueCustomField'=> 1,
'MultiUserIssueCustomField'=> 1);
my %textType = ( 'TextIssueCustomField'=> 1);
sub collectValuesFromCustomField {
my $customField = shift;
my $fieldType = $customField->{'$type'};
if ($periodType{$fieldType}) {
return $customField->{value}->{presentation};
} elsif ($textType{$fieldType}) {
return $customField->{value}->{text};
} elsif ($simpleType{$fieldType}) {
return $customField->{value};
} elsif ($singleType{$fieldType}) {
if (defined $customField->{value}->{login}) {
return $customField->{value}->{login};
} else {
return $customField->{value}->{name};
}
}
elsif ($multiType{$fieldType}) {
my @multiValuesList;
foreach my $value (@{$customField->{value}}) {
if (defined $value->{login}) {
push @multiValuesList, $value->{login};
} else {
push @multiValuesList, $value->{name};
}
}
return \@multiValuesList;
}
else {
print "\nCannot process ".$fieldType." type of custom field, will be skipped.\n";
}
return undef;
}
1;