Bugzilla/Search.pm

filter: Statementstmt 48.1% 65 / 135 Branchbran 19.2% 10 / 52 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 70.5% 12 / 17 Podpod 66.6% 2 / 3 totaltotal 40.0% 91 / 227 CC 40 SCAR 46.2
CC
40
Cov
39%
search
306.0
_field_values
42.0
new
12.2
CRAP 101.2
« Bugzilla.pm ↑ Summary
1 package WWW::Bugzilla::Search;
2  
3 1 $WWW::Bugzilla::Search::VERSION = '0.2';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WWW::Bugzilla;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 WWW::Bugzilla::Search - Handles searching bugzilla bugs via WWW::Mechanize.
16  
17 =head1 SYNOPSIS
18  
19 use WWW::Bugzilla::Search;
20  
21 # Login
22 my $search = WWW::Bugzilla::Search->new(
23 server => 'bugs.example.com',
24 email => 'user@example.com',
25 password => 'my_passwd',
26 );
27  
28 $search->summary('This is my summary');
29 my @bugs = $search->search();
30  
31 =head1 DESCRIPTION
32  
33 WWW::Bugzilla::Search provides an API to search for bugs in a Bugzilla database. Any resulting bugs will be returned as instances of WWW::Bugzilla bugs.
34  
35 =head1 INTERFACE
36  
37 =head2 Multiple choice search criteria
38  
39 The following fields are multiple choice fields:
40  
41 classification, component, op_sys, priority, product, resolution, bug_severity, bug_status, target_milestone, version, hardware, rep_platform
42  
43 Available options can be retrieved via:
44
45 $search->field();
46  
47 To choose a given value, use:
48
49 $search->field('value');
50  
51 =head2 Text search criteria
52  
53 The following fields are avaiilable for text searching:
54  
55 assigned_to, reporter, summary
56  
57 To searc using a given field, use:
58  
59 $search->field('value');
60  
61 =head2 Methods
62  
63 =head3 search()
64  
65 Searches Bugzilla with the defined criteria. Returns a list of bugs that match the criteria. Each bug is a seperate instance of WWW::Bugzilla
66  
67 =head3 reset()
68  
69 Resets all search criteria.
70  
71 =over
72  
73 =back
74  
75 =cut
76  
77  
78 #sub AUTOLOAD {
79 # my $self = shift;
80 # my $name = $AUTOLOAD;
81 #
82 # warn Dumper($self->{'_fields'});
83 # if ($self->{'_fields'}{$name}) {
84 # $name =~ s/^WWW::Bugzilla::Search:://;
85 # my $out =$self->_field_values($name);
86 # # warn Dumper($out);
87 # }
88 #
89 # if (@_) {
90 # return $self->{$name} = shift;
91 # } else {
92 # if (defined($self->{$name})) {
93 # return $self->{$name};
94 # } else {
95 # return;
96 # }
97 # }
98 #}
99  
100 1 my $_SETUP;
101  
102 1 ▶sub new {
Subroutine new: called CC=9 SCAR=25.0
Pod undocumented
103 1 my $that = shift;
104 1 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
105 1 my $self = {
106 search_keys => {},
107 protocol => '',
108 server=> '',
109 };
110 1 bless $self, $class;
111  
112 1 ▶ if (!$_SETUP) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_SETUP) 1 0 1
113 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 # accessors
115 1 foreach my $field (qw(mech protocol server email password)) {
116 5 ▶ *{ $class . '::' . $field } = sub { my ($self, $value) = @_; if (defined $value) { $self->{$field} = $value; } return $self->{$field} }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 3 7
117 5 }
118
119 # search fields
120 1 foreach my $field (qw(classification component op_sys priority product resolution bug_severity bug_status target_milestone version hardware rep_platform)) {
121 12 ▶ *{ $class . '::' . $field } = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
122 0 my ($self, $value) = @_;
123 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
124 0 $self->{'search_keys'}{$field} = $value;
125 0 return $value;
126 } else {
127 0 return $self->_field_values($field);
128 }
129 }
130 12 }
131  
132 # search fields that are used as accessors
133 1 foreach my $field (qw(assigned_to reporter summary)) {
134 0 ▶ *{ $class . '::' . $field } = sub { my ($self, $value) = @_; if (defined $value) { $self->{'search_keys'}{$field} = $value; } return $self->{'search_keys'}{$field} }
Subroutine __ANON__: not called CC=2 SCAR=15.1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
135 3 }
136 1 $_SETUP++;
137 }
138  
139 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
140 1 my %conf = @_;
141 1 ▶ while (my ($k, $v) = each %conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %conf) 4 1 5
142 4 $self->$k($v);
143 }
144 }
145  
146 1 ▶ $self->protocol('http') if !$self->protocol();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->protocol 0 1 1
147  
148 1 $self->{'mech'} = WWW::Mechanize->new();
149 1 $self->_login();
150
151 0 return $self;
152 }
153  
154 0 ▶sub _field_values {
Subroutine _field_values: not called CC=6 SCAR=37.4
155 0 my ($self, $name) = @_;
156 0 my $url = $self->protocol().'://'.$self->server().'/query.cgi?format=advanced';
157 0 my $mech = $self->{'mech'};
158 0 ▶ if ($mech->{'uri'} ne $url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->{"uri"} ne $url) 0 0 0
159 0 $mech->get( $url);
160 }
161 0 $mech->form_name('queryform');
162  
163 0 my @values;
164  
165 0 my $form = $mech->current_form();
166 0 foreach my $field ($form->inputs()) {
167 0 ▶ if ($field->name && $field->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->name and $field->name eq $name) 0 0 0
Condition0%0/3
$field->name and $field->name eq $name
!ll&&!rl&&r
000
MC/DC0%0/2
$field->name and $field->name eq $name
$field->name $field->name eq $name
Truth table0%0/3
A and B
A= $field->nameB= $field->name eq $name
ABresult
0X0
100
111
168 0 push (@values, grep { defined $_ }$field->possible_values());
169 }
170 }
171 0 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
172 0 return grep { defined $_ } @values;
173 }
174 0 warn "no values for $name";
175 0 return;
176 }
177  
178 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
179 0 my ($self) = @_;
180  
181 0 $self->{'search_keys'} = {};
182 }
183  
184 0 ▶sub search {
Subroutine search: not called CC=17 SCAR=57.2
Pod documented
185 0 my ($self) = @_;
186 0 my $mech = $self->{'mech'};
187 0 my $login_page = $self->protocol().'://'.$self->server().'/query.cgi?format=advanced';
188 0 $mech->get( $login_page );
189 0 $mech->form_name('queryform');
190  
191 0 foreach my $key (keys %{ $self->{'search_keys'} }) {
192 0 my $value = $self->{'search_keys'}{$key};
193 0 ▶ if ($key eq 'summary') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'summary') { } 0 0 0
elsif ($key eq 'assigned_to') { } 0 0 0
elsif ($key eq 'reporter') { } 0 0 0
194 0 $mech->field('short_desc', $value, 1);
195 } elsif ($key eq 'assigned_to') {
196 0 $mech->field('email1', $value);
197 0 ▶ $mech->field('emailtype1', 'regexp') if (ref($value) eq 'Regexp');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "Regexp" 0 0 0
198 } elsif ($key eq 'reporter') {
199 0 $mech->field('email2', $value);
200 0 ▶ $mech->field('emailtype2', 'regexp') if (ref($value) eq 'Regexp');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "Regexp" 0 0 0
201 0 $mech->tick('emailreporter2', 1);
202 0 map($mech->untick($_, 1), qw(emailqa_contact2 emailassigned_to2 emailcc2));
203 } else {
204 0 ▶ if ($self->_field_values($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_field_values($key)) { } 0 0 0
205 # ghetto hack. Grr, Mechanize is making each of the form elements a seperate entry.
206 0 my $i = 1;
207 0 foreach my $input ($mech->current_form()->inputs()) {
208 0 ▶ next if (defined $input->name && $input->name ne $key);
Branch: 1 branch
BranchTrueFalseTotal
if defined $input->name and $input->name ne $key 0 0 0
Condition0%0/3
defined $input->name and $input->name ne $key
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input->name and $input->name ne $key
defined $input->name $input->name ne $key
Truth table0%0/3
A and B
A= defined $input->nameB= $input->name ne $key
ABresult
0X0
100
111
209 0 foreach my $val ($input->possible_values) {
210 0 ▶ next if !defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
211 0 ▶ if ($value eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $val) 0 0 0
212 0 $input->value($value);
213 # $mech->field($key, $value, $i);
214 }
215 }
216 0 $i++;
217 }
218 } else {
219 0 $mech->field($key, $self->{'search_keys'}{$key});
220 }
221 }
222 }
223  
224 0 $mech->submit();
225 0 my @bugs;
226 0 foreach my $link ($mech->links()) {
227 0 ▶ if ($link->url() =~ /^show_bug\.cgi\?id=(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->url =~ /^show_bug\.cgi\?id=(\d+)$/) 0 0 0
228 0 ▶ push (@bugs, WWW::Bugzilla->new( 'server' => $self->{'server'}, 'email' => $self->{'email'}, 'password' => $self->{'password'}, 'bug_number' => $1, 'use_ssl' => ($self->protocol() eq 'https') ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$self->protocol eq 'https' ? : 0 0 0
229 }
230 }
231 0 return @bugs;
232 }
233  
234 # based on the current page, set the current form to the first form with a specified field
235 1 ▶sub _get_form_by_field {
Subroutine _get_form_by_field: called CC=4 SCAR=17.9
236 1 my ($self, $field) = @_;
237 1 ▶ croak("invalid field") if !$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 1 1
238  
239 1 my $mech = $self->{'mech'};
240 1 my $i = 1;
241 1 foreach my $form ($mech->forms()) {
242 0 ▶ if ($form->find_input($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->find_input($field)) 0 0 0
243 0 $mech->form_number($i);
244 0 return 1;
245 }
246 0 $i++;
247 }
248 1 return;
249 }
250  
251 1 ▶sub _login {
Subroutine _login: called CC=5 SCAR=22.1
252 1 my $self = shift;
253 1 my $mech = $self->{'mech'};
254 1 my $login_page = $self->protocol().'://'.$self->server().'/query.cgi?GoAheadAndLogIn=1';
255 1 $mech->get( $login_page );
256  
257 # bail unless OK or Redirect happens
258 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
259 1 ▶ croak("Login form is missing") if !$self->_get_form_by_field('Bugzilla_login');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_form_by_field("Bugzilla_login") 1 0 1
260 0 $mech->field('Bugzilla_login', $self->email());
261 0 $mech->field('Bugzilla_password', $self->password());
262 0 $mech->submit_form();
263 0 $mech->get( $login_page );
264 0 ▶ croak("Login failed") if $self->_get_form_by_field('Bugzilla_login');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_get_form_by_field("Bugzilla_login") 0 0 0
265 }
266  
267 =head1 BUGS, IMPROVEMENTS
268  
269 There may well be bugs in this module. Using it as I have, I just have not run
270 into any. In addition, this module does not support ALL of Bugzilla's
271 features. I will consider any patches or improvements, just send me an email
272 at the address listed below.
273
274 =head1 AUTHOR
275  
276 Written by:
277 Brian Caswell (bmc@shmoo.com)
278  
279 Portions taken from WWW::Bugzilla, originally written by:
280 Matthew C. Vella (the_mcv@yahoo.com)
281  
282 =head1 LICENSE
283
284 WWW::Bugzilla::Search - Module providing API to search Bugzilla bugs.
285 Copyright (C) 2006 Brian Caswell (bmc@shmoo.com)
286  
287 Portions Copyright (C) 2003 Matthew C. Vella (the_mcv@yahoo.com)
288
289 This module is free software; you can redistribute it and/or modify it
290 under the terms of either:
291
292 a) the GNU General Public License as published by the Free Software
293 Foundation; either version 1, or (at your option) any later version,
294 or
295
296 b) the "Artistic License" which comes with this module.
297
298 This program is distributed in the hope that it will be useful,
299 but WITHOUT ANY WARRANTY; without even the implied warranty of
300 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
301 the GNU General Public License or the Artistic License for more details.
302
303 You should have received a copy of the Artistic License with this
304 module, in the file ARTISTIC. If not, I'll be glad to provide one.
305
306 You should have received a copy of the GNU General Public License
307 along with this program; if not, write to the Free Software
308 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
309 USA
310  
311 =cut
312  
313 1 1;
« Bugzilla.pm ↑ Summary