Bugzilla.pm

filter: Statementstmt 15.4% 47 / 305 Branchbran 3.0% 5 / 166 Conditioncond 11.1% 3 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 40.0% 10 / 25 Podpod 92.8% 13 / 14 totaltotal 14.0% 78 / 554 CC 99 SCAR 51.7
CC
99
Cov
11%
commit
650.0
add_attachment
210.0
get_attachment
72.0
CRAP 175.5
↑ Summary Bugzilla/Search.pm »
1 package WWW::Bugzilla;
2  
3 2 $WWW::Bugzilla::VERSION = '1.5';
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Fatal qw(:void open opendir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use constant FIELDS => qw( bugzilla_version bugzilla_version_minor version component status resolution dup_id assigned_to summary bug_number description os platform severity priority cc url add_cc target_milestone status_whiteboard keywords depends_on blocks additional_comments );
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 2 my %new_field_map = ( product => 'product',
14 version => 'version',
15 component => 'component',
16 assigned_to => 'assigned_to',
17 summary => 'short_desc',
18 description => 'comment',
19 os => 'op_sys',
20 platform => 'rep_platform',
21 severity => 'bug_severity',
22 priority => 'priority',
23 cc => 'cc',
24 url => 'bug_file_loc' );
25  
26 2 my %other_field_map = (
27 resolution => 'resolution_knob_5',
28 );
29  
30 2 my %update_field_map = (product => 'product',
31 # bug_number => 'id', # this cannot be updated
32 platform => 'rep_platform',
33 os => 'op_sys',
34 add_cc => 'newcc',
35 component => 'component',
36 version => 'version',
37 # cc => 'cc', # this field should not be updated, use add_cc
38 status => 'knob',
39 priority => 'priority',
40 severity => 'bug_severity',
41 target_milestone => 'target_milestone',
42 url => 'bug_file_loc',
43 summary => 'short_desc',
44 status_whiteboard => 'status_whiteboard',
45 keywords => 'keywords',
46 depends_on => 'dependson',
47 blocks => 'blocked',
48 additional_comments => 'comment' );
49  
50 =head1 NAME
51  
52 WWW::Bugzilla - Handles submission/update of bugzilla bugs via WWW::Mechanize.
53  
54 =head1 SYNOPSIS
55  
56 use WWW::Bugzilla;
57  
58 # create new bug
59 my $bz = WWW::Bugzilla->new( server => 'www.mybugzilla.com',
60 email => 'buguser@bug.com',
61 password => 'mypassword' );
62  
63 # enter info into some fields and save new bug
64  
65 # get list of available version choices
66 my @versions = $bz->available('version');
67  
68 # set version
69 $bz->version( $versions[0] );
70  
71 # get list of available products
72 my @products = $bz->available('product');
73  
74 # set product
75 $bz->product( $products[0] );
76  
77 # get list of components available
78 my @components = $bz->available('component');
79  
80 # set component
81 $bz->component( $components[0] );
82  
83 # optionally do the same for platform, os, priority, severity.
84  
85 $bz->assigned_to( 'joeschmoe@whatever.com' );
86 $bz->summary( $some_text );
87 $bz->description( $some_more_text );
88  
89 # submit bug, returning new bug number
90 my $bug_number = $bz->commit;
91  
92 # all of the above could have been done in a much easier
93 # way, had we known what values to use. See below:
94  
95 my $bz = WWW::Bugzilla->new( server => 'www.mybugzilla.com',
96 email => 'buguser@bug.com',
97 password => 'mypassword'
98 version => 'Alpha',
99 product => 'MyProduct',
100 component => 'API',
101 assigned_to => 'joeschmoe@whatever.com',
102 summary => $some_text,
103 description => $some_more_text);
104  
105 my $bug_number = $bz->commit;
106  
107 # Below is an example of how one would update a bug.
108  
109 my $bz = WWW::Bugzilla->new( server => 'www.mybugzilla.com',
110 email => 'buguser@bug.com',
111 password => 'mypassword'
112 bug_number => 46 );
113  
114 # show me the chosen component
115 my $component = $bz->component;
116  
117 # change component
118 $bz->component( 'Test Failures' );
119  
120 $bz->add_cc( 'me@me.org' );
121  
122 $bz->add_attachment( filepath => '/home/me/file.txt',
123 description => 'description text',
124 is_patch => 0,
125 comment => 'comment text here' );
126  
127 $bz->additional_comments( "comments here");
128  
129 # below are examples of changing bug status
130 $bz->change_status("assigned");
131 $bz->change_status("fixed");
132 $bz->change_status("later");
133 $bz->mark_as_duplicate("12");
134 $bz->reassign("someone@else.com");
135  
136 $bz->commit;
137  
138 =head1 DESCRIPTION
139  
140 WWW::Bugzilla currently provides an API to posting new Bugzilla
141 bugs, as well as updating existing Bugzilla bugs.
142  
143 =head1 INTERFACE
144  
145 =head2 METHODS
146  
147 =over
148  
149 =item new()
150  
151 Initialize WWW::Bugzilla object. If bug_number is passed in,
152 will initialize as existing bug. Will croak() unless the
153 Bugzilla login page on server specified returns a 200 or 404.
154 new() supports the following name-value parameters.
155  
156 =over
157  
158 =item server (required)
159  
160 URL of the bugzilla server you wish to interface with. Do not
161 place http:// or https:// in front of the url (see 'use_ssl' option
162 below)
163  
164 =item email (required)
165  
166 Your email address used by bugzilla, in other words your
167 bugzilla login.
168  
169 =item password (required)
170  
171 Bugzilla password.
172  
173 =item use_ssl (optional)
174  
175 If set, will use https:// protocol, defaults to http://.
176  
177 NOTE: This option requires Crypt::SSLeay.
178  
179 =item product
180  
181 Bugzilla product name, required if entering new bug
182 (not updating).
183  
184 =item bug_number (optional)
185  
186 If you mean to update an existing bug (not create a new one)
187 include a valid bug number here.
188  
189 =item version component status assigned_to resolution dup_id assigned_to summary bug_number description os platform severity priority cc url add_cc target_milestone status_whiteboard keywords depends_on blocks additional_comments
190  
191 These are fields that can be initialized on new(), useful for new bugs.
192 Please note that some of these fields apply only to bugs being updated,
193 and if you set them here, they will be overridden if the value is already
194 set in the actual bug on the server. These fields also have thier own
195 get/set methods (see below).
196  
197 =back
198  
199 =cut
200  
201 use Class::MethodMaker
202 2 ▶ new_with_init => 'new',
Subroutine BEGIN: called CC=1 SCAR=0.0
203 new_hash_init => 'hash_init',
204 2 get_set => [ FIELDS ];
205  
206 1 ▶sub init {
Subroutine init: called CC=8 SCAR=29.8
Pod undocumented
207 1 my $self = shift;
208 1 my %args = @_;
209
210 1 ▶ croak("'server', 'email', and 'password' are all required arguments.") if ( (not $args{server}) or (not $args{email}) or (not $args{password}) );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"server"} and $args{"email"} and $args{"password"} 0 1 1
Condition33%2/6
$args{"server"} and $args{"email"} and $args{"password"}
!ll&&!rl&&r
001
$args{"server"} and $args{"email"}
!ll&&!rl&&r
001
MC/DC0%0/3
$args{"server"} and $args{"email"} and $args{"password"}
$args{"server"} $args{"email"} $args{"password"}
Truth table25%1/4
A and B and C
A= $args{"server"}B= $args{"email"}C= $args{"password"}
ABCresult
0XX0
10X0
1100
1111
211  
212 # croak("'product' required for new bug.") if ( (not $args{product}) and (not $args{bug_number}) );
213 1 ▶ $self->{'product'} = $args{'product'} if (defined($args{'product'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"product"} 0 1 1
214  
215 1 $self->{mech} = WWW::Mechanize->new();
216  
217 1 ▶ $self->{protocol} = delete($args{use_ssl}) ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'use_ssl'} ? : 1 0 1
218  
219 1 $self->{server} = $args{server};
220 1 $self->_login( delete $args{server}, delete $args{email}, delete $args{password});
221
222 # finish the object
223 0 $self->hash_init(%args);
224  
225 0 ▶ if ($self->{bug_number}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'bug_number'}) { } 0 0 0
elsif ($self->{'product'}) { } 0 0 0
226 0 $self->_get_update_page();
227 } elsif ($self->{product}) {
228 0 $self->_get_new_page();
229 }
230  
231 0 $self->check_error();
232 0 return $self;
233 }
234  
235 0 ▶sub _get_new_page {
Subroutine _get_new_page: not called CC=4 SCAR=30.0
236 0 my $self = shift;
237
238 0 my $mech = $self->{mech};
239 0 my $new_page = $self->{protocol}.'://'.$self->{server}.'/enter_bug.cgi?product='.$self->{product};
240 0 $mech->get($new_page);
241 0 $self->check_error();
242 0 ▶ $mech->form_name('Create') if ($self->bugzilla_version == 3);
Branch: 1 branch
BranchTrueFalseTotal
if $self->bugzilla_version == 3 0 0 0
243  
244 # bail unless OK or Redirect happens
245 0 ▶ croak("Cannot open page $new_page") unless ( ($mech->status == '200') or ($mech->status == '404') );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->status == 200 or $mech->status == 404 0 0 0
Condition0%0/3
$mech->status == 200 or $mech->status == 404
l!l&&r!l&&!r
000
MC/DC0%0/2
$mech->status == 200 or $mech->status == 404
$mech->status == 200 $mech->status == 404
Truth table0%0/3
A or B
A= $mech->status == 200B= $mech->status == 404
ABresult
1X1
011
000
246 }
247  
248 0 ▶sub _get_update_page {
Subroutine _get_update_page: not called CC=3 SCAR=24.8
249 0 my $self = shift;
250  
251 0 my $mech = $self->{mech};
252 0 $self->_get_form_by_field('quicksearch');
253 0 $mech->field('quicksearch', $self->{bug_number});
254 0 $mech->submit();
255 0 $self->check_error();
256
257 0 $mech->form_name("changeform");
258 # set fields to chosen values
259 0 foreach my $field ( keys %update_field_map ) {
260 0 ▶ if ($mech->current_form->find_input($update_field_map{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->current_form->find_input($update_field_map{$field})) { } 0 0 0
261 0 $self->{$field} = $mech->current_form->value( $update_field_map{$field} );
262 } else {
263 # warn "# Couldn't find $field";
264 }
265 }
266 }
267  
268 # based on the current page, set the current form to the first form with a specified field
269 1 ▶sub _get_form_by_field {
Subroutine _get_form_by_field: called CC=4 SCAR=17.9
270 1 my ($self, $field) = @_;
271 1 ▶ croak("invalid field") if !$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 1 1
272  
273 1 my $mech = $self->{mech};
274 1 my $i = 1;
275 1 foreach my $form ($mech->forms()) {
276 0 ▶ if ($form->find_input($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->find_input($field)) 0 0 0
277 0 $mech->form_number($i);
278 0 return;
279 }
280 0 $i++;
281 }
282 1 croak("No form with the field $field available");
283 }
284  
285 1 ▶sub _login {
Subroutine _login: called CC=6 SCAR=29.1
286 1 my $self = shift;
287 1 my ($server, $email, $password) = @_;
288  
289 1 my $mech = $self->{mech};
290  
291 1 my $login_page = $self->{protocol}.'://'.$server.'/query.cgi?GoAheadAndLogIn=1';
292
293 1 $mech->get( $login_page );
294  
295 # bail unless OK or Redirect happens
296 1 ▶ croak("Cannot open page $login_page") unless ( ($mech->status == '200') or ($mech->status == '404') );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->status == 200 or $mech->status == 404 0 1 1
Condition33%1/3
$mech->status == 200 or $mech->status == 404
l!l&&r!l&&!r
100
MC/DC0%0/2
$mech->status == 200 or $mech->status == 404
$mech->status == 200 $mech->status == 404
Truth table33%1/3
A or B
A= $mech->status == 200B= $mech->status == 404
ABresult
1X1
011
000
297  
298 1 $self->_get_form_by_field('Bugzilla_login');
299 0 $mech->field('Bugzilla_login', $email);
300 0 $mech->field('Bugzilla_password', $password);
301 0 $mech->submit_form();
302  
303
304 0 $mech->get($self->{protocol}.'://'.$server.'/');
305  
306 0 ▶ if ($mech->content() =~ /<span>Version (\d+)\.(\d+)(\.\d+)?\+?<\/span>/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mech->content =~ m[<span>Version (\d+)\.(\d+)(\.\d+)?\+?</span>]) { } 0 0 0
elsif ($mech->content =~ /<p class="header_addl_info">version (\d+)\./ism) { } 0 0 0
307 0 $self->bugzilla_version($1);
308 0 $self->bugzilla_version_minor($2);
309 } elsif ($mech->content() =~ /<p class="header_addl_info">version (\d+)\./smi) {
310 0 $self->bugzilla_version($1);
311 } else {
312 0 croak("Unable to verify bugzilla version.");
313 }
314  
315 0 ▶ if ($self->bugzilla_version > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bugzilla_version > 2) 0 0 0
316 0 $update_field_map{'status'} = 'bug_status';
317 0 $other_field_map{'resolution'} = 'resolution';
318 }
319 }
320  
321 =item product() version() component() status() assigned_to() resolution() dup_id() assigned_to() summary() bug_number() description() os() platform() severity() priority() cc() url() add_cc() target_milestone() status_whiteboard() keywords() depends_on() blocks() additional_comments()
322  
323 get/set the value of these bug fields. Some apply only to new bugs, some
324 only to bugs being updated. commit() must be called to save these
325 permanently.
326  
327 =item available()
328  
329 Returns list of available options for field requested. Below are known
330 valid fields:
331  
332 product
333 platform
334 os
335 version
336 priority
337 severity
338 component
339 target_milestone
340  
341 =cut
342  
343 0 ▶sub available {
Subroutine available: not called CC=4 SCAR=30.0
Pod documented
344 0 my $self = shift;
345 0 my $field_choice = shift;
346 0 my $mech = $self->{mech};
347  
348 # we handle product seperately because bugzilla requires it to be handled
349 # seperately on bug creation
350 0 ▶ if ('product' eq lc($field_choice)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("product" eq lc $field_choice) 0 0 0
351 0 return $self->get_products();
352 }
353  
354 # make sure that we've set a product before we do any of the other stuff
355 0 ▶ croak("available() needs a valid product to be specified") if not $self->{'product'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"product"} 0 0 0
356
357 # note that we are using %new_field map regardless if this is a new bug
358 # or not. this should work, as these fields should be the same for
359 # both new and old, but look here if problems occur!
360
361 0 ▶ if (my $item = $mech->current_form->find_input( $new_field_map{$field_choice} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $mech->current_form->find_input($new_field_map{$field_choice})) { } 0 0 0
362 0 return $item->possible_values();
363 } else {
364 0 return undef;
365 }
366 }
367  
368 =item product()
369  
370 Set the Product for the bug
371  
372 =cut
373  
374 0 ▶sub product {
Subroutine product: not called CC=4 SCAR=30.0
Pod documented
375 0 my ($self, $product) = @_;
376  
377 0 ▶ if ($product) {
Branch: 1 branch
BranchTrueFalseTotal
if ($product) 0 0 0
378 0 $self->{'product'} = $product;
379 0 ▶ if ($self->{bug_number}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'bug_number'}) { } 0 0 0
elsif ($self->{'product'}) { } 0 0 0
380 0 $self->_get_update_page();
381 } elsif ($self->{'product'}) {
382 0 $self->_get_new_page();
383 }
384 }
385 0 return ($self->{'product'});
386 }
387  
388  
389 =item reassign()
390  
391 Mark bug being updated as reassigned to another user. Takes email
392 address as parameter. Status/resolution will not be updated
393 until commit() is called.
394  
395 =cut
396  
397 0 ▶sub reassign {
Subroutine reassign: not called CC=3 SCAR=24.8
Pod documented
398 0 my $self = shift;
399 0 my $email = shift;
400
401 0 ▶ croak("reassign() needs a bug number passed in as a parameter") if not $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 0 0
402
403 0 ▶ croak("reassign() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
404  
405 0 $self->{status} = 'reassign';
406 0 $self->{assigned_to} = $email;
407 }
408  
409 =item mark_as_duplicate()
410  
411 Mark bug being updated as duplicate of another bug number.
412 Takes bug number as argument.
413 Status/resolution will not be updated until commit() is called.
414  
415 =cut
416  
417 0 ▶sub mark_as_duplicate {
Subroutine mark_as_duplicate: not called CC=3 SCAR=24.8
Pod documented
418 0 my $self = shift;
419 0 my $dup_id = shift;
420  
421 0 ▶ croak("mark_as_duplicate() needs a bug number passed in as a parameter") if not $dup_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $dup_id 0 0 0
422
423 0 ▶ croak("mark_as_duplicate() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
424  
425 0 $self->{status} = 'RESOLVED';
426 0 $self->{resolution} = 'DUPLICATE';
427 0 $self->{dup_id} = $dup_id;
428 }
429  
430 =item change_status()
431  
432 Change status of bug being updated. Status/resolution will not
433 be updated until commit() is called. The following are valid
434 options (case-insensitive):
435  
436 assigned
437 fixed
438 invalid
439 wontfix
440 later
441 remind
442 worksforme
443 reopen
444 verified
445 closed
446  
447 =cut
448  
449 0 ▶sub change_status {
Subroutine change_status: not called CC=5 SCAR=34.0
Pod documented
450 0 my ($self, $status) = @_;
451  
452 0 ▶ croak("change_status() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
453  
454 0 $status = uc($status);
455  
456 0 my %status = (
457 'ASSIGNED' => 'accept',
458 'REOPENED' => 'reopen',
459 'VERIFIED' => 'verify',
460 'CLOSED' => 'close'
461 );
462  
463 0 my %resolution = (
464 'FIXED' => 1,
465 'INVALID' => 1,
466 'WONTFIX' => 1,
467 'LATER' => 1,
468 'REMIND' => 1,
469 'DUPLICATE' => 1,
470 'WORKSFORME' => 1
471 );
472  
473 0 ▶ croak ("$status is not a valid status.") if not ($resolution{$status} or $status{$status});
Branch: 1 branch
BranchTrueFalseTotal
unless $resolution{$status} or $status{$status} 0 0 0
Condition0%0/3
$resolution{$status} or $status{$status}
l!l&&r!l&&!r
000
MC/DC0%0/2
$resolution{$status} or $status{$status}
$resolution{$status} $status{$status}
Truth table0%0/3
A or B
A= $resolution{$status}B= $status{$status}
ABresult
1X1
011
000
474  
475 0 ▶ if ($status{$status}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status{$status}) { } 0 0 0
476 0 $self->{status} = $status;
477 0 $self->{resolution} = '';
478 # $status{$status};
479 } else {
480 0 $self->{status} = "RESOLVED";
481 0 $self->{resolution} = $status;
482 }
483  
484 0 return 1;
485 }
486  
487 =item add_attachment()
488  
489 Adds attachment to existing bug - will not work for new
490 bugs. Below are available params:
491  
492 =over
493  
494 =item *
495  
496 filepath (required)
497  
498 =item *
499  
500 description (required)
501  
502 =item *
503  
504 is_patch (optional boolean)
505  
506 =item *
507  
508 content_type - Autodetected if not defined.
509  
510 =item *
511  
512 comment (optional)
513  
514 =item *
515  
516 finished - will not return object to update form if set (optional boolean)
517  
518 =back
519  
520 =cut
521  
522 0 ▶sub add_attachment {
Subroutine add_attachment: not called CC=14 SCAR=53.5
Pod documented
523 0 my $self = shift;
524 0 my %args = @_;
525 0 my $mech = $self->{mech};
526
527 0 ▶ croak("add_attachment() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
528  
529 0 ▶ croak("You must include a filepath and description.") unless ($args{filepath} and $args{description});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"filepath"} and $args{"description"} 0 0 0
Condition0%0/3
$args{"filepath"} and $args{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"filepath"} and $args{"description"}
$args{"filepath"} $args{"description"}
Truth table0%0/3
A and B
A= $args{"filepath"}B= $args{"description"}
ABresult
0X0
100
111
530
531 0 my $attach_page = $self->{protocol}.'://'.$self->{server}.'/attachment.cgi?bugid='.$self->{bug_number}.'&action=enter';
532
533 0 $mech->get( $attach_page );
534 0 $self->check_error();
535 0 $mech->form_name('entryform');
536 0 $mech->field( 'data', $args{'filepath'} );
537 0 $mech->field( 'description', $args{description} );
538 0 ▶ $mech->field( 'comment', $args{comment} ) if $args{comment};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"comment"} 0 0 0
539 0 ▶ $mech->field( 'ispatch', 1 ) if $args{'is_patch'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"is_patch"} 0 0 0
540  
541 0 ▶ if ($args{'bigfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bigfile"}) 0 0 0
542 0 ▶ if ($mech->current_form->find_input('bigfile', 'checkbox', 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->current_form->find_input('bigfile', 'checkbox', 0)) { } 0 0 0
543 0 $mech->tick('bigfile', 'bigfile');
544 } else {
545 0 croak('Bigfile support is not available');
546 }
547 }
548 0 ▶ if ( $args{content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'content_type'}) { } 0 0 0
549 0 $mech->field( 'contenttypemethod', 'manual' );
550 0 $mech->field( 'contenttypeentry', $args{content_type} );
551 } else {
552 0 $mech->field( 'contenttypemethod', 'autodetect' );
553 }
554  
555 0 $mech->submit_form();
556 0 $self->check_error();
557 0 my $id;
558 0 ▶ if ($mech->content =~ /created/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ /created/i) 0 0 0
559 0 my $link = $mech->find_link(text_regex => qr/^Attachment #\d+$/);
560 0 my $title = $link->attrs()->{'title'};
561
562 0 ▶ if ($title ne "'" . $args{'description'} . "'" && $title ne $args{'description'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title ne q['] . $args{"description"} . q['] and $title ne $args{"description"}) 0 0 0
Condition0%0/3
$title ne q['] . $args{"description"} . q['] and $title ne $args{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
$title ne q['] . $args{"description"} . q['] and $title ne $args{"description"}
$title ne q['] . $args{"description"} . q['] $title ne $args{"description"}
Truth table0%0/3
A and B
A= $title ne q['] . $args{"description"} . q[']B= $title ne $args{"description"}
ABresult
0X0
100
111
563 0 croak('attachment not created');
564 }
565 0 ▶ if ($link->text =~ /^Attachment #(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->text =~ /^Attachment #(\d+)$/) { } 0 0 0
566 0 $id = $1;
567 } else {
568 0 croak('attachment not created');
569 }
570 }
571  
572 0 ▶ $self->_get_update_page() unless ($args{finished});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"finished"} 0 0 0
573 0 return $id;
574 }
575  
576 =item list_attachments()
577  
578 Lists attachments that are attached to an existing bug - will not work for new bugs.
579  
580 =cut
581  
582 0 ▶sub list_attachments {
Subroutine list_attachments: not called CC=6 SCAR=37.4
Pod documented
583 0 my $self = shift;
584 0 my $mech = $self->{mech};
585
586 0 ▶ croak("list_attachments() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
587
588 0 my $bug_page = $self->{protocol}.'://'.$self->{server}.'/show_bug.cgi?id='.$self->{bug_number};
589 0 $mech->get($bug_page);
590 0 $self->check_error();
591
592 0 my (@attachments);
593 my %seen;
594 0 foreach my $link ($mech->find_all_links(url_regex => qr/attachment\.cgi\?id=\d+$/)) {
595 0 ▶ if ($link->url() =~ /^attachment.cgi\?id=(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->url =~ /^attachment.cgi\?id=(\d+)$/) { } 0 0 0
596 0 my $id = $1;
597 0 ▶ next if ($seen{$id});
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$id} 0 0 0
598 0 $seen{$id}++;
599 0 my $i = $link->url();
600 0 $i =~ s/\?/\\?/g;
601 0 my $re = '<a(?: name="a\d+")? href="' . $i . '"(?:\s*title="View the content of the attachment">\s*<b>|>)?<span class="(bz_obsolete)">';
602 0 ▶ my $obsolete = ($mech->content() =~ /$re/smi) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mech->content =~ /$re/ism ? : 0 0 0
603 0 push (@attachments, { id => $id, name => $link->text(), obsolete => $obsolete });
604 } else {
605 0 croak("WWW::Mechanize find_all_links gave us a bogus URL");
606 }
607 }
608 0 return (@attachments);
609 }
610  
611 =item get_attachment()
612  
613 Get the specified attachment from an existing bug - will not work for new bugs.
614  
615 =cut
616  
617 0 ▶sub get_attachment {
Subroutine get_attachment: not called CC=8 SCAR=42.8
Pod documented
618 0 my $self = shift;
619 0 my %args = @_;
620 0 my $mech = $self->{mech};
621
622 0 ▶ croak("get_attachment() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
623
624 0 ▶ croak("You must provide either the 'id' or 'name' of the attachment you wish to retreive") unless ($args{id} || $args{name});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} or $args{"name"} 0 0 0
Condition0%0/3
$args{"id"} or $args{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"id"} or $args{"name"}
$args{"id"} $args{"name"}
Truth table0%0/3
A or B
A= $args{"id"}B= $args{"name"}
ABresult
1X1
011
000
625
626 0 my $bug_page = $self->{protocol}.'://'.$self->{server}.'/show_bug.cgi?id='.$self->{bug_number};
627 0 $mech->get($bug_page);
628 0 $self->check_error();
629
630 0 my @links;
631 0 ▶ if ($args{'id'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'id'}) { } 0 0 0
elsif ($args{'name'}) { } 0 0 0
632 0 @links = $mech->find_all_links( url => 'attachment.cgi?id=' . $args{'id'} );
633 } elsif ($args{'name'}) {
634 0 @links = $mech->find_all_links( text => $args{'name'} );
635 0 ▶ if (scalar(@links) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @links > 1) 0 0 0
636 0 carp('multiple attachments have the same name, returning the first one');
637 }
638 }
639  
640 0 ▶ croak('No such attachment') if (!@links);
Branch: 1 branch
BranchTrueFalseTotal
unless @links 0 0 0
641 0 $mech->get($links[0]);
642 0 return $mech->content();
643 }
644  
645 =item obsolete_attachment()
646  
647 Mark the specified attachment obsolete. - will not work for new bugs.
648  
649 =cut
650  
651 0 ▶sub obsolete_attachment {
Subroutine obsolete_attachment: not called CC=8 SCAR=42.8
Pod documented
652 0 my $self = shift;
653 0 my %args = @_;
654 0 my $mech = $self->{mech};
655
656 0 ▶ croak("obsolete_attachment() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
657
658 0 ▶ croak("You must provide either the 'id' or 'name' of the attachment you wish to obsolete") unless ($args{id} || $args{name});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} or $args{"name"} 0 0 0
Condition0%0/3
$args{"id"} or $args{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"id"} or $args{"name"}
$args{"id"} $args{"name"}
Truth table0%0/3
A or B
A= $args{"id"}B= $args{"name"}
ABresult
1X1
011
000
659
660 0 my $bug_page = $self->{protocol}.'://'.$self->{server}.'/show_bug.cgi?id='.$self->{bug_number};
661 0 $mech->get($bug_page);
662 0 $self->check_error();
663
664 0 my @links;
665 0 ▶ if ($args{'id'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'id'}) { } 0 0 0
elsif ($args{'name'}) { } 0 0 0
666 0 @links = $mech->find_all_links( url => 'attachment.cgi?id=' . $args{'id'} );
667 } elsif ($args{'name'}) {
668 0 @links = $mech->find_all_links( text => $args{'name'} );
669 0 ▶ if (scalar(@links) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @links > 1) 0 0 0
670 0 carp('multiple attachments have the same name, returning the first one');
671 }
672 }
673 0 ▶ croak('No such attachment') if (!@links);
Branch: 1 branch
BranchTrueFalseTotal
unless @links 0 0 0
674 0 $links[0]->[0] = $links[0]->[0] . '&action=edit';
675 0 $links[0]->[5]->{'href'} = $links[0]->[5]->{'href'} . '&action=edit';
676 0 $mech->get($links[0]);
677 0 $mech->form_with_fields('id', 'action', 'contenttypemethod');
678 0 $mech->tick("isobsolete", 1);
679 0 $mech->submit();
680 0 return $mech->content();
681 }
682  
683  
684 =item commit()
685  
686 Submits bugzilla new or update form. Returns bug_number. Optionally
687 takes parameter finished- if set will you are done updating the bug,
688 and wil not return you to the update page.
689  
690 =cut
691  
692 0 ▶sub commit {
Subroutine commit: not called CC=25 SCAR=64.8
Pod documented
693 0 my $self = shift;
694 0 my %args = @_;
695 0 my $mech = $self->{mech};
696
697 # print $mech->uri() . "\n";
698 0 ▶ if ($mech->content() !~ /a href="index\.cgi\?logout=1">/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->content =~ /a href="index\.cgi\?logout=1">/) 0 0 0
699 0 croak("must be logged in to commit bugs");
700 }
701  
702 0 ▶ if ($self->{bug_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bug_number'}) { } 0 0 0
703 # bugzilla > 3.0
704 0 ▶ if ($self->bugzilla_version() > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bugzilla_version > 2) { } 0 0 0
705 0 ▶ if ($self->{resolution}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'resolution'}) { } 0 0 0
elsif ($self->{'status'}) { } 0 0 0
706 0 $mech->field($update_field_map{'status'}, $self->{'status'});
707 0 $mech->field($other_field_map{'resolution'}, $self->{resolution});
708 0 $self->{resolution} = undef;
709 0 $self->{status} = undef;
710 } elsif ($self->{status}) {
711 0 $mech->field('bug_status', $self->{'status'});
712 0 $self->{resolution} = undef;
713 0 $self->{status} = undef;
714 }
715 } else {
716 0 ▶ if ($self->{resolution}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'resolution'}) { } 0 0 0
elsif ($self->{'status'}) { } 0 0 0
717 0 $mech->field($update_field_map{'status'}, 'resolve');
718 0 $mech->field($other_field_map{'resolution'}, $self->{resolution});
719 0 $self->{resolution} = undef;
720 0 $self->{status} = undef;
721 } elsif ($self->{status}) {
722 0 $mech->field($update_field_map{'status'}, $self->{status});
723 0 $self->{status} = undef;
724 }
725 }
726
727 0 ▶ if ($self->{dup_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dup_id"}) 0 0 0
728 0 $mech->field('dup_id', $self->{dup_id});
729 0 $self->{dup_id} = undef;
730 }
731 0 ▶ if ($self->{assigned_to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"assigned_to"}) 0 0 0
732 0 $mech->field('assigned_to', $self->{assigned_to});
733 0 $self->{assigned_to} = undef;
734 }
735 0 foreach my $field ( keys %update_field_map ) {
736 # field is missing
737 0 ▶ if (!$mech->current_form->find_input($update_field_map{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->current_form->find_input($update_field_map{$field})) 0 0 0
738 # warn "# $field is missing";
739 0 next;
740 }
741
742 # field is hidden
743 0 ▶ next if $mech->current_form->find_input($update_field_map{$field})->type eq 'hidden';
Branch: 1 branch
BranchTrueFalseTotal
if $mech->current_form->find_input($update_field_map{$field})->type eq "hidden" 0 0 0
744 0 ▶ $mech->field( $update_field_map{$field}, $self->{$field} ) if defined($self->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$field} 0 0 0
745 }
746 } else {
747 0 foreach my $field ( keys %new_field_map ) {
748 0 ▶ if ($mech->current_form->find_input($new_field_map{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->current_form->find_input($new_field_map{$field})) 0 0 0
749 # if field is defined and it has changed
750 0 ▶ if ( defined($self->{$field}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$field}) 0 0 0
751 0 ▶ $mech->field( $new_field_map{$field}, $self->{$field} ) if ($mech->current_form->value($new_field_map{$field}) ne $self->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $mech->current_form->value($new_field_map{$field}) ne $self->{$field} 0 0 0
752 }
753 }
754 }
755 }
756  
757 # delete the comment such that we don't reuse the same comment again accidentally.
758 0 delete($self->{'comment'});
759  
760 0 $mech->submit_form();
761
762 # 3.3+ token checking
763 0 ▶ if ($mech->content() =~ /You submitted changes to process_bug\.cgi with an invalid/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->content =~ /You submitted changes to process_bug\.cgi with an invalid/) 0 0 0
764 0 $mech->form_name('check');
765 0 $mech->submit_form();
766 }
767  
768  
769 0 $self->check_error();
770 0 ▶ if (!$self->{bug_number}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bug_number"}) 0 0 0
771 0 ▶ if ($mech->content() =~ /<h2>Bug (\d+) has been added to the database/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($mech->content =~ /<h2>Bug (\d+) has been added to the database/) { } 0 0 0
elsif ($mech->content =~ m[>Bug (\d+)</a></i> has been added to the database</dt>]) { } 0 0 0
elsif ($mech->content =~ /Bug (\d+) Submitted</) { } 0 0 0
elsif ($mech->content =~ /Bug&nbsp;(\d+) Submitted</) { } 0 0 0
772 0 $self->{bug_number} = $1;
773 } elsif ($mech->content() =~ />Bug (\d+)<\/a><\/i> has been added to the database<\/dt>/) {
774 0 $self->{bug_number} = $1;
775 } elsif ($mech->content() =~ /Bug (\d+) Submitted</) {
776 0 $self->{bug_number} = $1;
777 } elsif ($mech->content() =~ /Bug&nbsp;(\d+) Submitted</) {
778 0 $self->{bug_number} = $1;
779 } else {
780 # warn $mech->content();
781 0 croak("bug was not saved");
782 }
783 }
784 0 ▶ $self->_get_update_page() unless ($args{finished});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"finished"} 0 0 0
785  
786 0 return $self->{bug_number};
787 }
788  
789 =item check_error ()
790  
791 Checks if an error was given, croaking if it did.
792  
793 =cut
794  
795 0 ▶sub check_error {
Subroutine check_error: not called CC=4 SCAR=30.0
Pod documented
796 0 my ($self) = @_;
797 0 my $mech = $self->{mech};
798
799 0 ▶ if ($mech->content() =~ /<td bgcolor="#ff0000">[\s\r\n]*<font size="\+2">[\s\r\n]*(.*?)[\s\r\n]*<\/font>[\s\r\n]*<\/td>/smi) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mech->content =~ m[<td bgcolor="#ff0000">[\s\r\n]*<font size="\+2">[\s\r\n]*(.*?)[\s\r\n]*</font>[\s\r\n]*</td>]ism) { } 0 0 0
elsif ($mech->content =~ m[<td id="error_msg" class="throw_error">\s*(.*?)\s*</td>]ism) { } 0 0 0
elsif ($mech->content =~ m[<div class="throw_error">\s*(.*?)</div>]ism) { } 0 0 0
800 0 croak("error : $1");
801 } elsif ($mech->content() =~ /<td id="error_msg" class="throw_error">\s*(.*?)\s*<\/td>/smi) {
802 0 croak("error : $1");
803 } elsif ($mech->content() =~ /<div class="throw_error">\s*(.*?)<\/div>/smi) {
804 0 croak("error : $1");
805 }
806 }
807  
808 =item get_products ()
809  
810 Gets a list of products
811  
812 =cut
813  
814 0 ▶sub get_products {
Subroutine get_products: not called CC=3 SCAR=24.8
Pod documented
815 0 my ($self) = @_;
816 0 my $mech = $self->{mech};
817
818 0 my $url = $self->{protocol}.'://'.$self->{server}.'/enter_bug.cgi';
819 # version >= 3.0
820 0 ▶ if ($self->bugzilla_version == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bugzilla_version == 3) 0 0 0
821 0 $url .= '?classification=__all';
822 }
823 0 $mech->get($url);
824 0 $self->check_error();
825  
826 0 my @products;
827 0 foreach my $product ($mech->find_all_links( url_regex => qr/enter_bug.cgi\?product=/)) {
828 0 push (@products, $product->text());
829 }
830  
831 0 return (@products);
832 }
833  
834  
835 =item get_comments()
836  
837 Lists comments made on an existing bug - will not work for new bugs.
838  
839 =cut
840  
841 0 ▶sub get_comments {
Subroutine get_comments: not called CC=4 SCAR=30.0
Pod documented
842 0 my ($self) = @_;
843 0 ▶ croak("get_comments() may not be called until the bug is committed for the first time") if not $self->{bug_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bug_number"} 0 0 0
844
845 0 my $mech = $self->{mech};
846 0 my $bug_page = $self->{protocol}.'://'.$self->{server}.'/show_bug.cgi?id='.$self->{bug_number};
847 0 $mech->get($bug_page);
848 0 $self->check_error();
849  
850 0 my @comments;
851 0 my $content = $mech->content();
852 0 ▶ while ($content =~ m/<pre id="comment_text_\d+">(.*?)<\/pre>/smg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<pre id="comment_text_\d+">(.*?)</pre>]gms) 0 0 0
853 0 my $comment = $1;
854 0 chomp($comment);
855 0 push (@comments, $comment);
856 }
857  
858 # 3.3+
859 0 ▶ while ($content =~ m/<pre class="bz_comment_text" id="comment_text_\d+">\s*(.*?)<\/pre>/smg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<pre class="bz_comment_text" id="comment_text_\d+">\s*(.*?)</pre>]gms) 0 0 0
860 0 my $comment = $1;
861 0 chomp($comment);
862 0 push (@comments, $comment);
863 }
864  
865 0 return (@comments);
866 }
867  
868 =back
869  
870 =head1 BUGS, IMPROVEMENTS
871  
872 There may well be bugs in this module. Using it as I have, I just have not run
873 into any. In addition, this module does not support ALL of Bugzilla's
874 features. I will consider any patches or improvements, just send me an email
875 at the address listed below.
876
877 =head1 AUTHOR
878  
879 Maintained by:
880 Brian Caswell, bmc@shmoo.com
881  
882 Originally written by:
883 Matthew C. Vella, the_mcv@yahoo.com
884  
885 =head1 LICENSE
886
887 WWW::Bugzilla - Module providing API to create or update Bugzilla bugs.
888 Copyright (C) 2003 Matthew C. Vella (the_mcv@yahoo.com)
889  
890 Portions Copyright (C) 2006 Brian Caswell (bmc@shmoo.com)
891
892 This module is free software; you can redistribute it and/or modify it
893 under the terms of either:
894
895 a) the GNU General Public License as published by the Free Software
896 Foundation; either version 1, or (at your option) any later version,
897 or
898
899 b) the "Artistic License" which comes with this module.
900
901 This program is distributed in the hope that it will be useful,
902 but WITHOUT ANY WARRANTY; without even the implied warranty of
903 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
904 the GNU General Public License or the Artistic License for more details.
905
906 You should have received a copy of the Artistic License with this
907 module, in the file ARTISTIC. If not, I'll be glad to provide one.
908
909 You should have received a copy of the GNU General Public License
910 along with this program; if not, write to the Free Software
911 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
912 USA
913  
914 =cut
915  
916 2 1;
↑ Summary Bugzilla/Search.pm »