-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjira.pm
407 lines (364 loc) · 13.4 KB
/
jira.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
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
package jira;
use strict;
use warnings;
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Cookies::Netscape;
use JSON;
use MIME::Base64;
my $ua;
my $meta;
sub new {
my $class = shift;
my %arg = @_;
return "No URL defined" unless $arg{Url};
my $basic = encode_base64($arg{Login}.":".$arg{Password});
my $cookie_jar;
my $self;
if ($arg{CookieFile}) {
print "Asked to use Cookie_file at $arg{CookieFile}\n" if ($self->{verbose});
$cookie_jar = HTTP::Cookies::Netscape->new(
file => $arg{CookieFile},
);
}
$ua = LWP::UserAgent->new;
if ($arg{CookieFile}) {
$ua->cookie_jar( $cookie_jar );
}
$ua->timeout(30);
my $response = $ua->get($arg{Url}.'/rest/auth/latest/session', Authorization => 'Basic '.$basic);
if ($response->is_success) {
print "Logged to Jira successfully\n" if ($self->{verbose});
$self = { basic => $basic, url => $arg{Url}, verbose => $arg{Verbose} };
} else {
print "Login to Jira was unsuccessfull\n" if ($self->{verbose});;
print $response->status_line if ($self->{verbose});
return;
}
if ($arg{Project}) {
print "Project is defined in constructor. So I am going to get the metadata for project $arg{Project}\n" if $arg{Verbose};
my $response = $ua->get($arg{Url}.'/rest/api/latest/issue/createmeta?projectKeys='.$arg{Project}.'&expand=projects.issuetypes.fields', Authorization => 'Basic '.$basic);
if ($response->is_success) {
my $rawMeta = decode_json $response->decoded_content;
foreach my $project (@{$rawMeta->{projects}}) {
if ($project->{key} eq $arg{Project}) {
foreach my $issuetype (@{$project->{issuetypes}}) {
foreach my $field (keys %{$issuetype->{fields}}) {
$meta->{fields}->{$issuetype->{name}}->{$issuetype->{fields}->{$field}->{name}} = $field;
$meta->{fieldtypes}->{$issuetype->{fields}->{$field}->{name}} = $issuetype->{fields}->{$field}->{schema}->{type};
}
}
}
}
print Dumper($meta) if $arg{Verbose};
$self->{meta} = $meta;
$self->{project} = $arg{Project};
} else {
print
die "Cannot get meta for project ".$arg{Project}." (".$response->status_line.")\n";
}
}
bless $self, $class;
}
sub getMeta {
return $meta;
}
sub getUser {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/latest/user?accountId='.$arg{Id}, Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return decode_json $response->decoded_content;
}
return;
}
sub getAllIssues {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/latest/search?maxResults='.$arg{Max}.'&jql=project="'.$arg{Project}.'"', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return decode_json $response->decoded_content;
} else {
print "Got error while getting issues\n";
print $response->status_line;
}
}
sub getAllLinkTypes {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/2/issueLinkType', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return \@{decode_json($response->decoded_content)->{issueLinkTypes}};
} else {
print "Got error while getting issues\n";
print $response->status_line;
}
}
sub getAllPriorities {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/2/priority', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return \@{decode_json($response->decoded_content)};
} else {
print "Got error while getting priorities\n";
print $response->status_line;
}
}
sub getAllStatuses {
my $self = shift;
my %arg = @_;
my ($issues) = @{getAllIssues($self, Project => $self->{project}, Max => 1)->{issues}};
unless (defined $issues->{key}) {
return [];
}
my $response = $ua->get($self->{url}.'/rest/api/latest/issue/'.$issues->{key}.'/transitions?expand=transitions.fields', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return \@{decode_json($response->decoded_content)->{transitions}};
} else {
print "Got error while getting statuses\n";
print $response->status_line;
}
}
sub getAllResolutions {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/2/resolution', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
return \@{decode_json($response->decoded_content)};
} else {
print "Got error while getting resolutions\n";
print $response->status_line;
}
}
sub addWorkLog {
my $self = shift;
my %arg = @_;
my %workLog = %{$arg{WorkLog}};
my $content = encode_json \%workLog;
my $basic = ($arg{Login} && $arg{Password}) ? encode_base64($arg{Login}.":".$arg{Password}) : $self->{basic};
my $response = $ua->post($self->{url}.'/rest/api/latest/issue/'.$arg{Key}.'/worklog',
Authorization => 'Basic '.$basic,
'Content-Type' => 'application/json',
'Content' => $content);
}
sub getIssue {
my $self = shift;
my %arg = @_;
my $response = $ua->get($self->{url}.'/rest/api/latest/issue/'.$arg{Key}, Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
print $response->decoded_content if ($self->{verbose});
} else {
print "Got error while getting issue\n";
print $response->status_line;
}
}
sub deleteIssue {
my $self = shift;
my %arg = @_;
my $response = $ua->delete($self->{url}.'/rest/api/latest/issue/'.$arg{Key}, Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
print "Issue ".$arg{Key}." deleted.\n";
print $response->status_line."\n" if ($self->{verbose});
print $response->decoded_content."\n" if ($self->{verbose});
return 1;
} else {
print "Got error while deleting issue\n";
print $response->status_line;
return;
}
}
sub createIssue {
my $self = shift;
my %arg = @_;
my %data;
$data{fields} = $arg{Issue};
# Jira doesn't allow to post description longer than 32k
$data{fields}->{description} = substr ($data{fields}->{description}, 0, 32766) if ($data{fields}->{description});
# Jira doesn't allow to post summary longer than 255 bytes
$data{fields}->{summary} = substr ($data{fields}->{summary}, 0, 254);
# # Jira doesn't allow spaces in labels
foreach (@{$data{fields}->{labels}}) {
$_ =~ s/\s/_/g;
}
foreach my $customField (keys %{$arg{CustomFields}}) {
my $fieldId = $self->{meta}->{fields}->{$arg{Issue}->{issuetype}->{name}}->{$customField};
my $fieldType = $self->{meta}->{fieldtypes}->{$customField};
if (defined $fieldId && defined $fieldType) {
if ($fieldType eq 'string') {
$data{fields}->{$fieldId} = $arg{CustomFields}->{$customField};
} elsif ($fieldType eq 'option') {
$data{fields}->{$fieldId}->{value} = $arg{CustomFields}->{$customField};
} elsif ($fieldType eq 'array') {
$data{fields}->{$fieldId}->[0]->{name} = $arg{CustomFields}->{$customField};
} elsif ($fieldType eq 'resolution') {
$data{fields}->{$fieldId}->{name} = $arg{CustomFields}->{$customField};
} elsif ($fieldType eq 'datetime') {
$data{fields}->{$fieldId} = $arg{CustomFields}->{$customField};
} elsif ($fieldType eq 'user') {
$data{fields}->{$fieldId} = {id => $arg{CustomFields}->{$customField} };
}
}
}
my $content = encode_json \%data;
print $content."\n" if ($self->{verbose});
my $basic = ($arg{Login} && $arg{Password}) ? encode_base64($arg{Login}.":".$arg{Password}) : $self->{basic};
my $response = $ua->post($self->{url}.'/rest/api/latest/issue', Authorization => 'Basic '.$basic, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
print $response->status_line."\n" if ($self->{verbose});
print $response->decoded_content."\n" if ($self->{verbose});
my $answer = decode_json $response->decoded_content;
print $answer->{key}."\n" if ($self->{verbose});
return $answer->{key};
} else {
print "Got error while creating issues\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
return;
}
sub changeFields {
my $self = shift;
my %arg = @_;
my %data;
foreach my $customField (keys %{$arg{Fields}}) {
# Ugly but quick
my $fieldId = $self->{meta}->{fields}->{Task}->{$customField} || $self->{meta}->{fields}->{Bug}->{$customField};
my $fieldType = $self->{meta}->{fieldtypes}->{$customField};
if (defined $fieldId && defined $fieldType) {
if ($fieldType eq 'string') {
$data{fields}->{$fieldId} = $arg{Fields}->{$customField};
} elsif ($fieldType eq 'option') {
$data{fields}->{$fieldId}->{value} = $arg{Fields}->{$customField};
} elsif ($fieldType eq 'array') {
$data{fields}->{$fieldId}->[0]->{name} = $arg{Fields}->{$customField};
} elsif ($fieldType eq 'resolution' || $fieldType eq 'issuetype' || $fieldType eq 'priority' || $fieldType eq 'assignee') {
$data{fields}->{$fieldId}->{name} = $arg{Fields}->{$customField};
} elsif ($fieldType eq 'datetime') {
$data{fields}->{$fieldId} = $arg{CustomFields}->{$customField};
}
}
}
#print Dumper (\%data);
my $content = encode_json \%data;
print $content."\n" if ($self->{verbose});
my $basic = ($arg{Login} && $arg{Password}) ? encode_base64($arg{Login}.":".$arg{Password}) : $self->{basic};
my $response = $ua->put($self->{url}.'/rest/api/latest/issue/'.$arg{Key}, Authorization => 'Basic '.$basic, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
print $response->status_line."\n" if ($self->{verbose});
print $response->decoded_content."\n" if ($self->{verbose});
return 1;
} else {
print "Got error while changing fields\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
return;
}
sub doTransition {
my $self = shift;
my %arg = @_;
my %data;
my $transitionId;
my $response = $ua->get($self->{url}.'/rest/api/latest/issue/'.$arg{Key}.'/transitions?expand=transitions.fields', Authorization => 'Basic '.$self->{basic});
if ($response->is_success) {
my $answer = decode_json $response->decoded_content;
foreach (@{$answer->{transitions}}) {
if ($_->{name} eq $arg{Status}) {
$transitionId = $_->{id};
print "Transition Id for Status ".$arg{Status}." is ".$transitionId."\n";
last;
}
}
} else {
print "Got error while getting transitions\n";
print $response->status_line;
}
return unless ($transitionId);
$data{transition} = { "id" => $transitionId };
my $content = encode_json \%data;
print $content."\n" if ($self->{verbose});
$response = $ua->post($self->{url}.'/rest/api/latest/issue/'.$arg{Key}.'/transitions', Authorization => 'Basic '.$self->{basic}, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
print $response->status_line."\n" if ($self->{verbose});
return 1;
} else {
print "Got error while doing transition\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
}
sub createIssues {
my $self = shift;
my %arg = @_;
my %data;
foreach (@{$arg{Issues}}) {
push @{$data{issueUpdates}}, { fields => $_ };
}
my $content = encode_json \%data;
print $content."\n" if ($self->{verbose});
my $response = $ua->post($self->{url}.'/rest/api/latest/issue/bulk', Authorization => 'Basic '.$self->{basic}, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
print $response->status_line."\n" if ($self->{verbose});
print $response->decoded_content if ($self->{verbose});
my $answer = decode_json $response->decoded_content;
} else {
print "Got error while creating issue\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
}
sub createComment {
my $self = shift;
my %arg = @_;
my %data;
$data{body} = substr ($arg{Body}, 0, 32766); # Jira doesn't allow to post comments longer than 32k
my $content = encode_json \%data;
print $content."\n" if ($self->{verbose});
my $basic = ($arg{Login} && $arg{Password}) ? encode_base64($arg{Login}.":".$arg{Password}) : $self->{basic};
my $response = $ua->post($self->{url}.'/rest/api/latest/issue/'.$arg{IssueKey}.'/comment', Authorization => 'Basic '.$basic, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
return decode_json $response->decoded_content;
} else {
print "Got error while creating comment\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
}
return;
}
sub createIssueLink {
my $self = shift;
my %arg = @_;
my $content = encode_json $arg{Link};
print $content."\n" if ($self->{verbose});
my $response = $ua->post($self->{url}.'/rest/api/latest/issueLink', Authorization => 'Basic '.$self->{basic}, 'Content-Type' => 'application/json', 'Content' => $content);
if ($response->is_success) {
print $response->decoded_content."\n" if ($self->{verbose});
return 1;
}
print $response->status_line."\n" if ($self->{verbose});
return;
}
sub addAttachments {
my $self = shift;
my %arg = @_;
foreach my $file (@{$arg{Files}}) {
my $filesize = -s $file;
if ( $filesize > 10485760 ) {
print "The file size exceeds the maximum permitted size of 10485760 bytes";
return 1;
}
my $response = $ua->post($self->{url}.'/rest/api/latest/issue/'.$arg{IssueKey}.'/attachments', Authorization => 'Basic '.$self->{basic}, 'Content_Type' => 'multipart/form-data', Content => [file => [$file]], 'X-Atlassian-Token' => 'no-check');
if ($response->is_success) {
print $response->status_line."\n" if ($self->{verbose});
print $response->decoded_content if ($self->{verbose});
} else {
print "Got error while uploading files\n";
print $response->status_line."\n";
print $response->decoded_content."\n";
return;
}
}
return 1;
}
1;