Kanbanize.pm untested

filter: Statementstmt 0.0% 0 / 272 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 443 CC 35 SCAR 30.9
CC
35
Cov
0%
sync_bug
72.0
sync_card
72.0
fill_missing_bugs_info
12.0
CRAP 21.9
1 use strict;
2 use warnings;
3  
4 package Net::Bugzilla::Kanbanize;
5 $Net::Bugzilla::Kanbanize::VERSION = '0.007'; # TRIAL
6 our $VERSION;
7  
8 #ABSTRACT: Bugzilla and Kanbanize sync tool
9  
10 use Data::Dumper;
11  
12 use Net::Bugzilla::Kanbanize;
13  
14 use LWP::Simple;
15 use JSON;
16  
17 use LWP::UserAgent;
18 use File::HomeDir;
19  
20 use HTTP::Request;
21 use URI::Escape;
22 use List::MoreUtils qw(uniq);
23  
24 #XXX: https://bugzil.la/970457
25  
26 sub new {
27 my ( $class, $config ) = @_;
28  
29 my $self = bless { config => $config }, $class;
30  
31 return $self;
32 }
33  
34 sub version {
35 print STDERR "Version $VERSION\n";
36 }
37  
38 #XXX: Wrong, need to be instance variables
39  
40 my $APIKEY;
41 my $BOARD_ID;
42 my $BUGZILLA_TOKEN;
43 my $ua = LWP::UserAgent->new();
44  
45 my $total;
46 my $count;
47 my $config;
48  
49 sub run {
50 my $self = shift;
51  
52 $config = $self->{config};
53  
54 $APIKEY = $config->kanbanize_apikey or die "Please configure an apikey";
55 $BOARD_ID = $config->kanbanize_boardid
56 or die "Please configure a kanbanize_boardid";
57 $BUGZILLA_TOKEN = $config->bugzilla_token
58 or die "Please configure a bugzilla_token";
59  
60 $ua->timeout(15);
61 $ua->env_proxy;
62 $ua->default_header( 'apikey' => $APIKEY );
63  
64 my %bugs;
65  
66 if (@ARGV) {
67 fill_missing_bugs_info( \%bugs, @ARGV );
68 }
69 else {
70 %bugs = get_bugs();
71 }
72  
73 $count = scalar keys %bugs;
74  
75 print STDERR "Found a total of $count bugs\n";
76  
77 $total = 0;
78  
79 while ( my ( $bugid, $bug ) = each %bugs ) {
80 sync_bug($bug);
81 }
82  
83 return 1;
84 }
85  
86 sub get_bugs {
87 my $req =
88 HTTP::Request->new( GET =>
89 "https://bugzilla.mozilla.org/rest/bug?token=$BUGZILLA_TOKEN&include_fields=id,status,whiteboard,summary,assigned_to&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&component=WebOps%3A Bugzilla&component=WebOps%3A Community Platform&component=WebOps%3A Engagement&component=WebOps%3A IT-Managed Tools&component=WebOps%3A Labs&component=WebOps%3A Other&component=WebOps%3A Product Delivery&component=WebOps%3A SSL and Domain Names&product=Infrastructure %26 Operations"
90 );
91  
92 my $res = $ua->request($req);
93  
94 if ( !$res->is_success ) {
95 die Dumper($res); #$res->status_line;
96 }
97  
98 my $data = decode_json( $res->decoded_content );
99  
100 my %bugs;
101  
102 foreach my $bug ( @{ $data->{bugs} } ) {
103 $bugs{ $bug->{id} } = $bug;
104 }
105  
106 my @marked = get_marked_bugs();
107 foreach my $bug (@marked) {
108 $bugs{ $bug->{id} } = $bug;
109 }
110  
111 my @cards = get_bugs_from_all_cards();
112  
113 fill_missing_bugs_info( \%bugs, @cards );
114  
115 return %bugs;
116 }
117  
118 sub fill_missing_bugs_info {
119 my ( $bugs, @bugs ) = @_;
120  
121 my @missing_bugs;
122  
123 foreach my $bugid (@bugs) {
124 if ( not exists $bugs->{$bugid} ) {
125 push @missing_bugs, $bugid;
126 }
127 }
128  
129 my $missing_bugs_ids = join ",", sort @bugs;
130  
131 my $req =
132 HTTP::Request->new( GET =>
133 "https://bugzilla.mozilla.org/rest/bug?token=$BUGZILLA_TOKEN&include_fields=id,status,whiteboard,summary,assigned_to&id=$missing_bugs_ids"
134 );
135  
136 my $res = $ua->request($req);
137  
138 if ( !$res->is_success ) {
139 die Dumper($res); #$res->status_line;
140 }
141  
142 my $data = decode_json( $res->decoded_content );
143  
144 my @found_bugs = @{ $data->{bugs} };
145  
146 foreach my $bug ( sort @found_bugs ) {
147 $bugs->{ $bug->{id} } = $bug;
148 }
149  
150 return;
151 }
152  
153 sub get_marked_bugs {
154 my $req =
155 HTTP::Request->new( GET =>
156 "https://bugzilla.mozilla.org/rest/bug?token=$BUGZILLA_TOKEN&include_fields=id,status,whiteboard,summary,assigned_to&status_whiteboard_type=allwordssubstr&query_format=advanced&status_whiteboard=[kanban]"
157 );
158  
159 my $res = $ua->request($req);
160  
161 if ( !$res->is_success ) {
162 die Dumper($res); #$res->status_line;
163 }
164  
165 my $data = decode_json( $res->decoded_content );
166  
167 my @bugs = @{ $data->{bugs} };
168  
169 return @bugs;
170 }
171  
172 sub get_bugs_from_all_cards {
173  
174 my $req =
175 HTTP::Request->new( POST =>
176 "http://kanbanize.com/index.php/api/kanbanize/get_all_tasks/boardid/$BOARD_ID/format/json"
177 );
178  
179 my $res = $ua->request($req);
180  
181 if ( !$res->is_success ) {
182 die Dumper($res); #$res->status_line;
183 }
184  
185 my $cards = decode_json( $res->decoded_content );
186  
187 my @bugs;
188 foreach my $card (@$cards) {
189 my $extlink = $card->{extlink}; # XXX: Smarter parsing
190 if ( $extlink =~ /(\d+)$/ ) {
191 my $bugid = $1;
192 push @bugs, $bugid;
193 }
194 }
195  
196 return @bugs;
197 }
198  
199 sub sync_bug {
200 my $bug = shift;
201  
202 # print STDERR "Bugid: $bug->{id}\n" if $config->verbose;
203  
204 $total++;
205  
206 if ( not defined $bug ) {
207 print STDERR "[$total/$count] No info for bug $bug->{id}\n";
208 return;
209 }
210  
211 if ( $bug->{error} ) {
212 print STDERR
213 "[$total/$count] No info for bug $bug->{id} (Private bug?)\n";
214 return;
215 }
216  
217 my $summary = $bug->{summary};
218 my $whiteboard = $bug->{whiteboard};
219  
220 my $card = parse_whiteboard($whiteboard);
221  
222 my $status = "";
223 if ( not defined $card ) {
224 $card = create_card($bug);
225  
226 if ( not $card ) {
227 warn "Failed to create card for bug $bug->{id}";
228 return;
229 }
230  
231 update_whiteboard( $bug->{id}, $card->{taskid}, $whiteboard );
232  
233 $status .= "[card created]";
234 }
235  
236 $card = retrieve_card( $card->{taskid} );
237  
238 my $cardid = $card->{taskid};
239  
240 my @changes = sync_card( $card, $bug );
241 if (@changes) {
242 $status .= " [synced]";
243 }
244  
245 if ( $status ne "" or $config->verbose ) {
246 printf STDERR "[%4d/%4d] Card %4d - Bug %8d - $summary $status\n",
247 $total, $count, $cardid, $bug->{id};
248 }
249  
250 if (@changes) {
251 foreach my $change (@changes) {
252 printf STDERR "[%4d/%4d] Card %4d - Bug %8d - $summary ** %s **\n",
253 $total, $count, $cardid, $bug->{id}, $change;
254 }
255 }
256 }
257  
258 sub retrieve_card {
259 my $card_id = shift;
260  
261 my $req =
262 HTTP::Request->new( POST =>
263 "http://kanbanize.com/index.php/api/kanbanize/get_task_details/boardid/$BOARD_ID/taskid/$card_id/format/json"
264 );
265  
266 my $res = $ua->request($req);
267  
268 if ( !$res->is_success ) {
269 die Dumper($res); #$res->status_line;
270 }
271  
272 my $card = decode_json( $res->decoded_content );
273  
274 return $card;
275 }
276  
277 sub sync_bugzilla {
278  
279 }
280  
281 sub sync_card {
282 my ( $card, $bug ) = @_;
283  
284 my @updated;
285  
286 # Check Assignee
287 my $bug_assigned = $bug->{assigned_to};
288 my $card_assigned = $card->{assignee};
289  
290 if ( defined $card_assigned
291 && $card_assigned ne "None"
292 && $bug_assigned =~ m/\@.*\.bugs$/ )
293 {
294 push @updated, "Update bug $bug->{id} assigned to $card_assigned";
295 update_bug_assigned( $bug, $card_assigned );
296 }
297 elsif ($bug_assigned !~ m[^$card_assigned@]
298 && $bug_assigned !~ m/\@.*\.bugs$/ )
299 {
300 push @updated, "Update card assigned to $bug_assigned";
301  
302 #print STDERR
303 # "bug_asigned: $bug_assigned card_assigned: $card_assigned\n";
304 update_card_assigned( $card, $bug_assigned );
305 }
306  
307 #Check summary (XXX: Formatting assumption here)
308 my $bug_summary = "$bug->{id} - $bug->{summary}";
309 my $card_summary = $card->{title};
310  
311 if ( $bug_summary ne $card_summary ) {
312 update_card_summary( $card, $bug_summary );
313 push @updated, "Updated card summary";
314 }
315  
316 # Check status
317 my $bug_status = $bug->{status};
318 my $card_status = $card->{columnname};
319  
320 # Close card on bug completion
321  
322 #warn "[$bug->{id}] bug: $bug_status card: $card_status" if $config->verbose;
323  
324 if ( ( $bug_status eq "RESOLVED" or $bug_status eq "VERIFIED" )
325 and $card_status ne "Done" )
326 {
327 complete_card($card);
328 push @updated, "Card completed";
329 }
330  
331 # XXX: Should we close bug on card completion?
332 if ( ( $bug_status ne "RESOLVED" and $bug_status ne "VERIFIED" )
333 and $card_status eq "Done" )
334 {
335 if ( $bug_status eq "REOPENED" ) {
336 reopen_card($card);
337  
338 #$updated++;
339 }
340 else {
341 warn
342 "Bug $bug->{id} is not RESOLVED ($bug_status) but card $card->{taskid} says $card_status";
343 }
344 }
345  
346 # Check extlink
347 my $bug_link = "https://bugzilla.mozilla.org/show_bug.cgi?id=$bug->{id}";
348  
349 if ( $card->{extlink} ne $bug_link ) {
350 update_card_extlink( $card, $bug_link );
351 push @updated, "Updated external link to bugzilla";
352 }
353  
354 return @updated;
355 }
356  
357 sub reopen_card {
358 my $card = shift;
359  
360 warn
361 "[notimplemented] Should be reopening card $card->{taskid} and moving back to ready";
362  
363 return;
364 }
365  
366 sub complete_card {
367 my $card = shift;
368  
369 my $taskid = $card->{taskid};
370  
371 my $data = {
372 boardid => $BOARD_ID,
373 taskid => $taskid,
374 column => 'Done',
375 };
376  
377 my $req =
378 HTTP::Request->new( POST =>
379 "http://kanbanize.com/index.php/api/kanbanize/move_task/format/json"
380 );
381  
382 $req->content( encode_json($data) );
383  
384 my $res = $ua->request($req);
385  
386 if ( !$res->is_success ) {
387 die Dumper($res); #$res->status_line;
388 }
389 }
390  
391 sub update_card_extlink {
392 my ( $card, $extlink ) = @_;
393  
394 my $taskid = $card->{taskid};
395  
396 my $data = {
397 boardid => $BOARD_ID,
398 taskid => $taskid,
399 extlink => $extlink,
400 };
401  
402 my $req =
403 HTTP::Request->new( POST =>
404 "http://kanbanize.com/index.php/api/kanbanize/edit_task/format/json"
405 );
406  
407 $req->content( encode_json($data) );
408  
409 my $res = $ua->request($req);
410  
411 if ( !$res->is_success ) {
412 die Dumper($res); #$res->status_line;
413 }
414 }
415  
416 sub update_bug_assigned {
417 my ( $bug, $assigned ) = @_;
418  
419 $assigned .= '@mozilla.com';
420  
421 my $bugid = $bug->{id};
422  
423 my $req =
424 HTTP::Request->new(
425 PUT => "https://bugzilla.mozilla.org/rest/bug/$bugid" );
426  
427 $req->content("assigned_to=$assigned&token=$BUGZILLA_TOKEN");
428  
429 my $res = $ua->request($req);
430  
431 if ( !$res->is_success ) {
432 die Dumper($res); #$res->status_line;
433 }
434 }
435  
436 sub update_card_summary {
437 my ( $card, $bug_summary ) = @_;
438  
439 my $taskid = $card->{taskid};
440  
441 my $data = {
442 boardid => $BOARD_ID,
443 taskid => $taskid,
444 title => $bug_summary,
445 };
446  
447 my $req =
448 HTTP::Request->new( POST =>
449 "http://kanbanize.com/index.php/api/kanbanize/edit_task/format/json"
450 );
451  
452 $req->content( encode_json($data) );
453  
454 my $res = $ua->request($req);
455  
456 if ( !$res->is_success ) {
457 die Dumper($res); #$res->status_line;
458 }
459 }
460  
461 sub update_card_assigned {
462 my ( $card, $bug_assigned ) = @_;
463  
464 my $taskid = $card->{taskid};
465 ( my $assignee = $bug_assigned ) =~ s/\@.*//;
466  
467 my $req =
468 HTTP::Request->new( POST =>
469 "http://kanbanize.com/index.php/api/kanbanize/edit_task/format/json/boardid/$BOARD_ID/taskid/$taskid/assignee/$assignee"
470 );
471  
472 $req->content("[]");
473  
474 my $res = $ua->request($req);
475  
476 if ( !$res->is_success ) {
477 die $res->status_line;
478 }
479  
480 }
481  
482 sub update_whiteboard {
483 my ( $bugid, $cardid, $whiteboard ) = @_;
484  
485 my $req =
486 HTTP::Request->new(
487 PUT => "https://bugzilla.mozilla.org/rest/bug/$bugid" );
488  
489 if ( $whiteboard =~ m/\[kanban\]/ ) {
490 $whiteboard =~ s/\[kanban\]//;
491 }
492  
493 $whiteboard =
494 "[kanban:https://kanbanize.com/ctrl_board/$BOARD_ID/$cardid] $whiteboard";
495  
496 $req->content("whiteboard=$whiteboard&token=$BUGZILLA_TOKEN");
497  
498 my $res = $ua->request($req);
499  
500 if ( !$res->is_success ) {
501 die $res->status_line;
502 }
503  
504 }
505  
506 #XXX: https://bugzil.la/970457
507 sub create_card {
508 my $bug = shift;
509  
510 my $data = {
511 'title' => "$bug->{id} - $bug->{summary}",
512 'extlink' => "https://bugzilla.mozilla.org/show_bug.cgi?id=$bug->{id}",
513 'boardid' => $BOARD_ID,
514 };
515  
516 my $req =
517 HTTP::Request->new( POST =>
518 "http://kanbanize.com/index.php/api/kanbanize/create_new_task/format/json"
519 );
520  
521 $req->content( encode_json($data) );
522  
523 my $res = $ua->request($req);
524  
525 if ( !$res->is_success ) {
526 warn "can't create card:" . $res->status_line;
527 die Dumper($res);
528 return;
529 }
530  
531 my $card = decode_json( $res->decoded_content );
532  
533 $card->{taskid} = $card->{id};
534  
535 move_card( $card, 'Pending Triage' );
536  
537 return $card;
538 }
539  
540 sub move_card {
541 my ( $card, $lane ) = @_;
542  
543 my $data = {
544 boardid => $BOARD_ID,
545 taskid => $card->{taskid},
546 column => 'Backlog',
547 lane => $lane,
548 };
549  
550 my $req =
551 HTTP::Request->new( POST =>
552 "http://kanbanize.com/index.php/api/kanbanize/move_task/format/json"
553 );
554  
555 $req->content( encode_json($data) );
556  
557 my $res = $ua->request($req);
558  
559 if ( !$res->is_success ) {
560 die Dumper($res);
561 }
562  
563 }
564  
565 sub get_bug_info {
566 my $bugid = shift;
567 my $data =
568 get("https://bugzilla.mozilla.org/rest/bug/$bugid?token=$BUGZILLA_TOKEN");
569  
570 if ( not $data ) {
571 warn "Failed getting Bug info for Bug $bugid from bugzilla\n";
572 return { id => $bugid, error => "No Data" };
573 }
574  
575 print STDERR "Retrieving info for Bug $bugid from bugzilla\n"
576 if $config->verbose;
577  
578 $data = decode_json($data);
579  
580 return $data->{bugs}[0];
581 }
582  
583 sub parse_whiteboard {
584 my $whiteboard = shift;
585  
586 my $card;
587  
588 if ( $whiteboard =~
589 m{\[kanban:https://kanbanize.com/ctrl_board/(\d+)/(\d+)\]} )
590 {
591 my $boardid = $1;
592 my $cardid = $2;
593  
594 $card = { taskid => $cardid };
595 }
596  
597 return $card;
598 }
599  
600 1;
601  
602 __END__