Thursday, 6 February 2014

[Perl] Retrieve bug status from Bugzilla server

Bugzilla provides an initial script called bz_webservice_demo.pl which shows how to talk to Bugzilla via XMLRPC.
The option --bug_id can take one bug id as a parameter to the script, such as --bug_id 1212. So if we want to retrieve the status for multiple bugs, we have to call the script multiple times. It’s time consuming. It’s better to call the script once and retrieve the status for a list of bugs.
Look at the original code for the handle of bug_id. The web service method used to get status is called Bug.get.
  • The ids parameter is an array of numbers and strings. So we can pass multiple bugs together in ids.
  • Besides, this method can accept standard include_fields and exclude_fields arguments as well.

if ($bug_id) {
    $soapresult = $proxy->call('Bug.get', { ids => [$bug_id] });
    _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";
            }
        }
        else {
            print "$field: $value\n";
        }
    }
}
My approach is to extend --bug_id to take a comma separate list of bugs, then convert the list to an array for the parameter ids. In addition, since I only want the status, I can use the include_fields to select the field status and id only.
  • Why need id field? Since we return the status for many bugs, we need to check which id is each status linked to.
Another question is about how to get the value of status and id fields from returns. The reference of Bug.get method shows
bugs: An array of hashes that contains information about the bugs with the valid ids. Each hash contains the following items:
id [int]: The unique numeric id of this bug.
status [string]: The current status of the bug.
We can use the perl debug to check the structure of return bugs.
  • run perl script in debug mode
    $ perl -d -e bz_webservice_demo.pl --uri https://mycompany.com/bugzilla/xmlrpc.cgi --bug_id 121,131,141
  • use b to set breakpoint
  • use r to run it
  • use x $result to show the result
Finally I use the following script to display the status for multiple bugs.
my @bugs;

if ($bug_id) {
    @bugs = split(/,/, $bug_id);
    my $b = $_;
    $soapresult = $proxy->call('Bug.get', { ids => [@bugs], include_fields => ['id', 'status']});
    _die_on_fault($soapresult);
    $result = $soapresult->result;
    my $bugs = $result->{bugs};
    foreach my $bug (@$bugs) {
        print "status: $bug->{id} $bug->{status}\n";
    }
}

No comments :

Post a Comment