bz_webservice_demo.pl 11 KB
Newer Older
1
#!/usr/bin/perl
2 3 4
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8 9 10 11 12 13 14 15 16 17 18 19 20

=head1 NAME

bz_webservice_demo.pl - Show how to talk to Bugzilla via XMLRPC

=head1 SYNOPSIS

C<bz_webservice_demo.pl [options]>

C<bz_webservice_demo.pl --help> for detailed help

=cut

21
use 5.10.1;
22
use strict;
23 24
use warnings;

25
use lib qw(lib);
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use Getopt::Long;
use Pod::Usage;
use File::Basename qw(dirname);
use File::Spec;
use XMLRPC::Lite;

# If you want, say “use Bugzilla::WebService::Constants” here to get access
# to Bugzilla's web service error code constants.
# If you do this, remember to issue a “use lib” pointing to your Bugzilla
# installation directory, too.

my $help;
my $Bugzilla_uri;
my $Bugzilla_login;
my $Bugzilla_password;
41 42
my $Bugzilla_restrict;
my $Bugzilla_token;
43 44
my $bug_id;
my $product_name;
45
my $create_file_name;
46
my $legal_field_values;
47 48 49
my $add_comment;
my $private;
my $work_time;
50
my $fetch_extension_info = 0;
51
my $debug;
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
GetOptions(
  'help|h|?'       => \$help,
  'uri=s'          => \$Bugzilla_uri,
  'login:s'        => \$Bugzilla_login,
  'password=s'     => \$Bugzilla_password,
  'restrictlogin!' => \$Bugzilla_restrict,
  'bug_id:s'       => \$bug_id,
  'product_name:s' => \$product_name,
  'create:s'       => \$create_file_name,
  'field:s'        => \$legal_field_values,
  'comment:s'      => \$add_comment,
  'private:i'      => \$private,
  'worktime:f'     => \$work_time,
  'extension_info' => \$fetch_extension_info,
  'debug'          => \$debug
) or pod2usage({'-verbose' => 0, '-exitval' => 1});
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

=head1 OPTIONS

=over

=item --help, -h, -?

Print a short help message and exit.

=item --uri

URI to Bugzilla's C<xmlrpc.cgi> script, along the lines of
C<http://your.bugzilla.installation/path/to/bugzilla/xmlrpc.cgi>.

=item --login

Bugzilla login name. Specify this together with B<--password> in order to log in.

Specify this without a value in order to log out.

=item --password

Bugzilla password. Specify this together with B<--login> in order to log in.

93
=item --restrictlogin
94

95 96 97
Gives access to Bugzilla's "Bugzilla_restrictlogin" option.
Specify this option while logging in to restrict the login token to be
only valid from the IP address which called
98 99
Don't specify this option to do the same thing as unchecking the box.

100
See Bugzilla's restrictlogin parameter for details.
101 102 103 104 105 106 107 108 109 110

=item --bug_id

Pass a bug ID to have C<bz_webservice_demo.pl> do some bug-related test calls.

=item --product_name

Pass a product name to have C<bz_webservice_demo.pl> do some product-related
test calls.

111 112 113 114
=item --create

Specify a file that contains settings for the creating of a new bug.

115 116 117 118 119 120
=item --field

Pass a field name to get legal values for this field. It must be either a
global select field (such as bug_status, resolution, rep_platform, op_sys,
priority, bug_severity) or a custom select field.

121 122 123 124 125 126 127 128 129 130 131 132 133
=item --comment

A comment to add to a bug identified by B<--bug_id>. You must also pass a B<--login>
and B<--password> to log in to Bugzilla.

=item --private

An optional non-zero value to specify B<--comment> as private.

=item --worktime

An optional double precision number specifying the work time for B<--comment>.

134 135 136 137
=item --extension_info

If specified on the command line, the script returns the information about the
extensions that are installed.
138

139 140 141 142
=item --debug

Enable tracing at the debug level of XMLRPC requests and responses.

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
=back

=head1 DESCRIPTION

=cut

pod2usage({'-verbose' => 1, '-exitval' => 0}) if $help;
_syntaxhelp('URI unspecified') unless $Bugzilla_uri;

# We will use this variable for SOAP call results.
my $soapresult;

# We will use this variable for function call results.
my $result;

=head2 Initialization

Using the XMLRPC::Lite class, you set up a proxy, as shown in this script.
Bugzilla's XMLRPC URI ends in C<xmlrpc.cgi>, so your URI looks along the lines
of C<http://your.bugzilla.installation/path/to/bugzilla/xmlrpc.cgi>.

=cut

166
my $proxy = XMLRPC::Lite->proxy($Bugzilla_uri);
167

168 169 170 171 172 173 174
=head2 Debugging

Enable tracing at the debug level of XMLRPC requests and responses if requested.

=cut

if ($debug) {
175
  $proxy->import(+trace => 'debug');
176 177
}

178 179 180
=head2 Checking Bugzilla's version

To make sure the Bugzilla you're connecting to supports the methods you wish to
181
call, you may want to compare the result of C<Bugzilla.version> to the
182 183 184 185
minimum required version your application needs.

=cut

186
$soapresult = $proxy->call('Bugzilla.version');
187
_die_on_fault($soapresult);
188 189
print 'Connecting to a Bugzilla of version '
  . $soapresult->result()->{version} . ".\n";
190 191 192 193 194 195 196 197 198 199

=head2 Checking Bugzilla's timezone

To make sure that you understand the dates and times that Bugzilla returns to you, you may want to call C<Bugzilla.timezone>.

=cut

$soapresult = $proxy->call('Bugzilla.timezone');
_die_on_fault($soapresult);
print 'Bugzilla\'s timezone is ' . $soapresult->result()->{timezone} . ".\n";
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

=head2 Logging In and Out

=head3 Using Bugzilla's Environment Authentication

Use a
C<http://login:password@your.bugzilla.installation/path/to/bugzilla/xmlrpc.cgi>
style URI.
You don't log out if you're using this kind of authentication.

=head3 Using Bugzilla's CGI Variable Authentication

Use the C<User.login> and C<User.logout> calls to log in and out, as shown
in this script.

215 216
The C<Bugzilla_restrictlogin> parameter is optional.
If omitted, Bugzilla's defaults apply (as specified by its C<restrictlogin>
217 218 219 220 221
parameter).

=cut

if (defined($Bugzilla_login)) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  if ($Bugzilla_login ne '') {

    # Log in.
    $soapresult = $proxy->call(
      'User.login',
      {
        login          => $Bugzilla_login,
        password       => $Bugzilla_password,
        restrict_login => $Bugzilla_restrict
      }
    );
    $Bugzilla_token = $soapresult->result->{token};
    _die_on_fault($soapresult);
    print "Login successful.\n";
  }
  else {
    # Log out.
    $soapresult = $proxy->call('User.logout');
    _die_on_fault($soapresult);
    print "Logout successful.\n";
  }
243 244
}

245 246 247 248 249 250 251
=head2 Getting Extension Information

Returns all the information any extensions have decided to provide to the webservice.

=cut

if ($fetch_extension_info) {
252 253 254 255 256 257 258 259
  $soapresult = $proxy->call('Bugzilla.extensions', {token => $Bugzilla_token});
  _die_on_fault($soapresult);
  my $extensions = $soapresult->result()->{extensions};
  foreach my $extensionname (keys(%$extensions)) {
    print "Extension '$extensionname' information\n";
    my $extension = $extensions->{$extensionname};
    foreach my $data (keys(%$extension)) {
      print '  ' . $data . ' => ' . $extension->{$data} . "\n";
260
    }
261
  }
262 263
}

264 265
=head2 Retrieving Bug Information

266
Call C<Bug.get> with the ID of the bug you want to know more of.
267
The call will return a C<Bugzilla::Bug> object.
268

269 270 271
=cut

if ($bug_id) {
272 273 274 275 276 277 278 279 280 281 282
  $soapresult
    = $proxy->call('Bug.get', {ids => [$bug_id], token => $Bugzilla_token});
  _die_on_fault($soapresult);
  $result = $soapresult->result;
  my $bug = $result->{bugs}->[0];
  foreach my $field (keys(%$bug)) {
    my $value = $bug->{$field};
    if (ref($value) eq 'HASH') {
      foreach (keys %$value) {
        print "$_: " . $value->{$_} . "\n";
      }
283
    }
284 285 286 287
    else {
      print "$field: $value\n";
    }
  }
288 289 290 291
}

=head2 Retrieving Product Information

292
Call C<Product.get> with the name of the product you want to know more of.
293 294 295 296 297
The call will return a C<Bugzilla::Product> object.

=cut

if ($product_name) {
298 299 300 301 302 303 304 305 306 307 308 309 310 311
  $soapresult = $proxy->call('Product.get',
    {'names' => [$product_name], token => $Bugzilla_token});
  _die_on_fault($soapresult);
  $result = $soapresult->result()->{'products'}->[0];

  # Iterate all entries, the values may be scalars or array refs with hash refs.
  foreach my $key (sort(keys %$result)) {
    my $value = $result->{$key};

    if (ref($value)) {
      my $counter = 0;
      foreach my $hash (@$value) {
        while (my ($innerKey, $innerValue) = each %$hash) {
          print "$key.$counter.$innerKey: $innerValue\n";
312
        }
313
        ++$counter;
314
      }
315
    }
316 317 318 319
    else {
      print "$key: $value\n";
    }
  }
320 321
}

322 323 324
=head2 Creating A Bug

Call C<Bug.create> with the settings read from the file indicated on
325
the command line. The file must contain a valid anonymous hash to use
326 327 328 329 330 331
as argument for the call to C<Bug.create>.
The call will return a hash with a bug id for the newly created bug.

=cut

if ($create_file_name) {
332 333 334 335 336 337 338 339 340
  my $bug_fields = do "$create_file_name";
  $bug_fields->{Bugzilla_token} = $Bugzilla_token;
  $soapresult = $proxy->call('Bug.create', \%$bug_fields);
  _die_on_fault($soapresult);
  $result = $soapresult->result;

  if (ref($result) eq 'HASH') {
    foreach (keys(%$result)) {
      print "$_: $$result{$_}\n";
341
    }
342 343 344 345
  }
  else {
    print "$result\n";
  }
346 347 348

}

349 350 351 352 353 354 355 356 357
=head2 Getting Legal Field Values

Call C<Bug.legal_values> with the name of the field (including custom
select fields). The call will return a reference to an array with the
list of legal values for this field.

=cut

if ($legal_field_values) {
358 359 360 361
  $soapresult = $proxy->call('Bug.legal_values',
    {field => $legal_field_values, token => $Bugzilla_token});
  _die_on_fault($soapresult);
  $result = $soapresult->result;
362

363
  print join("\n", @{$result->{values}}) . "\n";
364
}
365

366 367 368 369 370 371 372 373 374
=head2 Adding a comment to a bug

Call C<Bug.add_comment> with the bug id, the comment text, and optionally the number
of hours you worked on the bug, and a boolean indicating if the comment is private
or not.

=cut

if ($add_comment) {
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  if ($bug_id) {
    $soapresult = $proxy->call(
      'Bug.add_comment',
      {
        id        => $bug_id,
        comment   => $add_comment,
        private   => $private,
        work_time => $work_time,
        token     => $Bugzilla_token
      }
    );
    _die_on_fault($soapresult);
    print "Comment added.\n";
  }
  else {
    print "A --bug_id must be supplied to add a comment.";
  }
392
}
393

394 395 396 397 398 399 400 401 402
=head1 NOTES

=head2 Character Set Encoding

Make sure that your application either uses the same character set
encoding as Bugzilla does, or that it converts correspondingly when using the
web service API.
By default, Bugzilla uses UTF-8 as its character set encoding.

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
=head2 Format For Create File

The create format file is a piece of Perl code, that should look something like
this:

    {
        product     => "TestProduct", 
        component   => "TestComponent",
        summary     => "TestBug - created from bz_webservice_demo.pl",
        version     => "unspecified",
        description => "This is a description of the bug... hohoho",
        op_sys      => "All",
        platform    => "All",	
        priority    => "P4",
        severity    => "normal"
    };

420 421 422 423 424 425 426 427
=head1 SEE ALSO

There are code comments in C<bz_webservice_demo.pl> which might be of further
help to you.

=cut

sub _die_on_fault {
428 429 430 431 432 433 434 435
  my $soapresult = shift;

  if ($soapresult->fault) {
    my ($package, $filename, $line) = caller;
    die $soapresult->faultcode . ' '
      . $soapresult->faultstring
      . " in SOAP call near $filename line $line.\n";
  }
436 437 438
}

sub _syntaxhelp {
439
  my $msg = shift;
440

441 442
  print "Error: $msg\n";
  pod2usage({'-verbose' => 0, '-exitval' => 1});
443
}