-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBugzillaClient.pm
340 lines (260 loc) · 9.46 KB
/
BugzillaClient.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
# ------------------------------------------------------------------------------
# Copyright (C) 2007 Thomas Schmidt <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ----------------------------------------------------------------------------
package BugzillaClient;
use strict;
use Term::ReadKey;
use LWP::UserAgent;
use HTTP::Cookies;
use XML::Parser;
use Text::ParseWords; # For CSV parsing
#use Data::Dumper;
use base qw(Exporter);
our $VERSION = 0.5;
=head1 NAME
SUSE::BugzillaClient - A Client Library for accessing a bugzilla server.
=head1 DESCRIPTION
SUSE::BugzillaClient is a client Library for accessing a bugzilla server.
Note: The server has to support XML output (&ctype=xml).
It can query bugs by ID or the URL of a search. It returns a hash for each bug
that contains the bugs properties.
=head1 SYNOPSIS
use SUSE::BugzillaClient;
#login to bugzilla
my $jar = bzConnect( 'username','pw' );
#call query methods with the $jar object
#do something meaningful with the result, see BugzillaClientTest.pl
=head1 METHODS
=over
=cut
@BugzillaClient::EXPORT = qw( bzConnect getBug getBugs getBugsByIDList getBugsCSV getBugIds $bzDebug $bzHostname $bzBaseUrl $bzCookieDir $bzRecover %bzErrors);
use vars qw(%gbug $gelem $bzDebug $bzHostname $bzBaseUrl $bzCookieDir $bzRecover %bzErrors);
$bzDebug = 0;
$bzCookieDir = "/tmp";
$bzHostname = "bugzilla.suse.com";
$bzBaseUrl = "https://$bzHostname";
my $useragent = "BugzillaClient/0.1";
# ----------------------------------------------------------------------------
=item B<bzConnect>( $user, $pass )
Login to standard bugzilla and get the cookie $jar
=cut
# ----------------------------------------------------------------------------
sub bzConnect {
my ($user, $pass) = @_;
my $ua = LWP::UserAgent->new ( agent => $useragent);
my $jar = HTTP::Cookies->new(file => "$bzCookieDir/lwpcookies.txt",
autosave => 1, ignore_discard => 1 );
$ua->cookie_jar( $jar );
#old variant using POST with basic auth
#my $req = HTTP::Request->new(POST => $bzBaseUrl . '/index.cgi' );
#$req->content_type('application/x-www-form-urlencoded');
#$req->content("Bugzilla_login=$user&Bugzilla_password=$pass");
#new variant using GET with inline auth
my $req = HTTP::Request->new(GET => "https://$user:$pass\@$bzHostname");
my $res = $ua->request($req);
if ( $res->is_success && (my $cookie = $res->header('Set-Cookie')) ) {
return $jar;
} else {
die( "Login failed, Status: " . $res->status_line );
}
}
sub debug {
print STDERR @_ if ($bzDebug);
}
# Start of XML Element, create a new gbug Hash if <bug> Element
sub handle_start {
my ($expat, $elem) = @_;
# initialize new Bug Hash
if ($elem eq "bug"){
debug("Starting new Bug Element: " . $elem . "\n");
%gbug = ();
foreach my $valid ( "bug_id", "creation_ts", "short_desc", "delta_ts",
"classification", "product", "component", "version", "rep_platform", "op_sys",
"bug_status", "priority", "bug_severity", "target_milestone", "resolution",
"infoprovider", "assigned_to", "reporter", "qa_contact", "group", "status_whiteboard", "cf_partnerid") {
$gbug{$valid} = "";
}
} else {
debug("Starting new Element: " . $elem . "\n");
}
$gelem = $elem;
}
sub handle_char {
my ($expat, $val) = @_;
if ( defined $gelem && defined $gbug{$gelem} ) {
debug("Storing <$val> to element <$gelem>\n");
$gbug{$gelem} = $gbug{$gelem} . $val;
}
}
sub handle_end {
my ($expat, $elem) = @_;
my $bugs = $expat->{bugs};
if ($elem eq "bug"){
debug("Adding Bug " . $gbug{short_desc} . " to Array\n");
my %temp = %gbug;
push(@$bugs, \%temp);
}
$gelem = undef;
}
sub handle_xmldec {
my ($expat, $version, $encoding, $standalone) = @_;
debug("XML Info: \nVersion: ".$version."\nEncoding: ".$encoding."\nStandalone: ".$standalone."\n");
}
# ----------------------------------------------------------------------------
=item B<getBug>( $jar, $bugid )
Get a bug by id, returns a bug hash with the following content:
( "bug_id", "creation_ts", "short_desc", "delta_ts", "product", "component", "version", "rep_platform", "op_sys", "bug_status", "priority", "bug_severity", "target_milestone", "resolution" )
=cut
# ----------------------------------------------------------------------------
sub getBug($$) {
my ($jar, $bugid) = @_;
if( $bugid =~ /^\d+$/ ) {
my @bugids;
push @bugids, $bugid;
my @bugs = getBugsByIDList($jar, @bugids);
return $bugs[0];
} else {
print "Please use a decimal BugID\n";
}
}
# ----------------------------------------------------------------------------
=item B<getBugs>( $jar, $url )
Get a list of bug hashes by providing a search url (like https://bugzilla.suse.com/buglist.cgi?query_format=advanced&product=openSUSE+10.3&bug_status=NEEDINFO)
=cut
# ----------------------------------------------------------------------------
sub getBugs($$) {
my ($jar, $url) = @_;
my @bugids = getBugIds($jar, $url);
return getBugsByIDList( $jar, @bugids );
}
# get an array of bug hashes for a list of bug ids
sub getBugsByIDList($@) {
%bzErrors = ();
return _getBugsByIDList(@_);
}
sub _getBugsByIDList {
my ($jar, @buglist) = @_;
if (!@buglist) {
return ();
}
# construct url for the ids
my $url = $bzBaseUrl . "/show_bug.cgi?ctype=xml&excludefield=attachmentdata&excludefield=long_desc";
foreach (@buglist){
$url = $url . "&id=" . $_;
}
my $bugstr = getUrl($jar, $url);
# parse it into $p2->{bugs}
my $p2 = new XML::Parser( Handlers => {
Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char,
XMLDecl => \&handle_xmldec },
ProtocolEncoding => 'UTF-8',
ErrorContext => 1,
);
$p2->{bugs} = [];
#workaround for illegal characters:
$bugstr =~ s/[\x00-\x08\x0B\x0C\x0E-\x1F]/ /g;
eval {
$p2->parse( $bugstr );
if (!@{$p2->{bugs}}) {
die "No bug data returned (too many bugs?)";
}
};
if ($@) {
if (!$bzRecover) {
die $@;
}
# bugzilla choked on one of the bugs or there is a permanent
# error
if (@buglist < 2) {
$bzErrors{$buglist[0] || 0} = $@;
return ();
}
my $last = $#buglist;
debug("Trying to recurse\n");
return (_getBugsByIDList($jar, @buglist[0..$last/2]),
_getBugsByIDList($jar, @buglist[$last/2+1..$last]));
}
return @{$p2->{bugs}};
}
# Get an Array of Bugs that are shown on the provided Query-Page.
# WARNING: This Method relies on the HTML Output Format
# of SUSE Bugzillas Query-Result-Page !!
sub getBugIds($$) {
my ($jar, $url) = @_;
my $bugstr = getUrl($jar, $url);
my @bugids;
if( $bugstr =~ /<form method="post" action="show_bug.cgi">\s*?<input type="hidden" name="ctype" value="xml">(.*?)<\/form>/s ) {
my $ids = $1;
while( $ids =~ m/<input type="hidden" name="id" value="(\d+)">/g ) {
push @bugids, $1;
}
debug("found " . @bugids . " BugIds for your Query.\n");
} elsif ($bugstr =~ /Your search did not return any results/s) {
debug("Your search did not return any results.\n");
} else {
print "Cannot fetch Bugids from Bugzilla Search Page.\n";
print "Maybe the HTML-Code has changed :-(.\n";
}
return @bugids;
}
# ----------------------------------------------------------------------------
=item B<getBugsCSV>( $jar, $url )
Get a list of bug hashes by providing a CSV search url (like https://bugzilla.suse.com/buglist.cgi?query_format=advanced&product=openSUSE+10.3&bug_status=NEEDINFO&ctype=csv, get this URL from the "CSV" link on the bottom of the search result page)
Faster than using getBugs() because not all XML output has to be parsed, but only these fields will be
available in the bug hashes:
bug_id, changeddate, bug_severity, priority, assigned_to, reporter, bug_status, product, short_short_desc (!)
=cut
# ----------------------------------------------------------------------------
sub getBugsCSV($$) {
my ($jar, $url) = @_;
my $content = getUrl($jar, $url);
my @keys;
my @bugs;
foreach my $line (split('\n', $content)) {
chomp $line;
my @field = parse_line(',', 0, $line);
if (!scalar(@keys)) {
@keys = @field;
debug("The following keys are defined:\n");
foreach my $key (@keys) {
debug(" " . $key);
}
debug("\n");
next;
}
my %hash = ();
for (my $count=0;$count<@keys;$count++) {
$hash{$keys[$count]} = $field[$count];
}
debug("Bugid = " . $hash{'bug_id'} . "<br>\n");
push (@bugs, \%hash);
}
return @bugs;
}
# helper for getting url
sub getUrl($$) {
my ($jar, $url) = @_;
debug("Querying URL: $url\n");
my $ua = LWP::UserAgent->new( agent => $useragent);
$ua->cookie_jar( $jar );
my $response = $ua->get( $url );
return $response->content;
}
1;