Skip to content

Commit

Permalink
Bug 1024987 - contrib/bz_webservice_demo.pl fails after User Token lo…
Browse files Browse the repository at this point in the history
…gin update

r=sgreen, a=justdave
  • Loading branch information
rojanu authored and Simon Green committed Jun 16, 2014
1 parent ac11ce1 commit 660b7bf
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions contrib/bz_webservice_demo.pl
Expand Up @@ -24,7 +24,6 @@ =head1 SYNOPSIS
use Pod::Usage;
use File::Basename qw(dirname);
use File::Spec;
use HTTP::Cookies;
use XMLRPC::Lite;

# If you want, say “use Bugzilla::WebService::Constants” here to get access
Expand All @@ -36,7 +35,8 @@ =head1 SYNOPSIS
my $Bugzilla_uri;
my $Bugzilla_login;
my $Bugzilla_password;
my $Bugzilla_remember;
my $Bugzilla_restrict;
my $Bugzilla_token;
my $bug_id;
my $product_name;
my $create_file_name;
Expand All @@ -51,7 +51,7 @@ =head1 SYNOPSIS
'uri=s' => \$Bugzilla_uri,
'login:s' => \$Bugzilla_login,
'password=s' => \$Bugzilla_password,
'rememberlogin!' => \$Bugzilla_remember,
'restrictlogin!' => \$Bugzilla_restrict,
'bug_id:s' => \$bug_id,
'product_name:s' => \$product_name,
'create:s' => \$create_file_name,
Expand Down Expand Up @@ -86,14 +86,14 @@ =head1 OPTIONS
Bugzilla password. Specify this together with B<--login> in order to log in.
=item --rememberlogin
=item --restrictlogin
Gives access to Bugzilla's "Bugzilla_remember" option.
Specify this option while logging in to do the same thing as ticking the
C<Bugzilla_remember> box on Bugilla's log in form.
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
Don't specify this option to do the same thing as unchecking the box.
See Bugzilla's rememberlogin parameter for details.
See Bugzilla's restrictlogin parameter for details.
=item --bug_id
Expand Down Expand Up @@ -151,17 +151,6 @@ =head1 DESCRIPTION
# We will use this variable for function call results.
my $result;

# Open our cookie jar. We save it into a file so that we may re-use cookies
# to avoid the need of logging in every time. You're encouraged, but not
# required, to do this in your applications, too.
# Cookies are only saved if Bugzilla's rememberlogin parameter is set to one of
# - on
# - defaulton (and you didn't pass 0 as third parameter to User.login)
# - defaultoff (and you passed 1 as third parameter to User.login)
my $cookie_jar =
new HTTP::Cookies('file' => File::Spec->catdir(dirname($0), 'cookies.txt'),
'autosave' => 1);

=head2 Initialization
Using the XMLRPC::Lite class, you set up a proxy, as shown in this script.
Expand All @@ -170,8 +159,7 @@ =head2 Initialization
=cut

my $proxy = XMLRPC::Lite->proxy($Bugzilla_uri,
'cookie_jar' => $cookie_jar);
my $proxy = XMLRPC::Lite->proxy($Bugzilla_uri);

=head2 Debugging
Expand Down Expand Up @@ -205,25 +193,6 @@ =head2 Checking Bugzilla's timezone
_die_on_fault($soapresult);
print 'Bugzilla\'s timezone is ' . $soapresult->result()->{timezone} . ".\n";

=head2 Getting Extension Information
Returns all the information any extensions have decided to provide to the webservice.
=cut

if ($fetch_extension_info) {
$soapresult = $proxy->call('Bugzilla.extensions');
_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";
}
}
}

=head2 Logging In and Out
=head3 Using Bugzilla's Environment Authentication
Expand All @@ -238,21 +207,20 @@ =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.
The C<Bugzilla_remember> parameter is optional.
If omitted, Bugzilla's defaults apply (as specified by its C<rememberlogin>
The C<Bugzilla_restrictlogin> parameter is optional.
If omitted, Bugzilla's defaults apply (as specified by its C<restrictlogin>
parameter).
Bugzilla hands back cookies you'll need to pass along during your work calls.
=cut

if (defined($Bugzilla_login)) {
if ($Bugzilla_login ne '') {
# Log in.
$soapresult = $proxy->call('User.login',
{ login => $Bugzilla_login,
{ login => $Bugzilla_login,
password => $Bugzilla_password,
remember => $Bugzilla_remember } );
restrict_login => $Bugzilla_restrict } );
$Bugzilla_token = $soapresult->result->{token};
_die_on_fault($soapresult);
print "Login successful.\n";
}
Expand All @@ -264,17 +232,36 @@ =head3 Using Bugzilla's CGI Variable Authentication
}
}

=head2 Getting Extension Information
Returns all the information any extensions have decided to provide to the webservice.
=cut

if ($fetch_extension_info) {
$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";
}
}
}

=head2 Retrieving Bug Information
Call C<Bug.get> with the ID of the bug you want to know more of.
The call will return a C<Bugzilla::Bug> object.
The call will return a C<Bugzilla::Bug> object.
Note: You can also use "Bug.get_bugs" for compatibility with Bugzilla 3.0 API.
=cut

if ($bug_id) {
$soapresult = $proxy->call('Bug.get', { ids => [$bug_id] });
$soapresult = $proxy->call('Bug.get', { ids => [$bug_id], token => $Bugzilla_token});
_die_on_fault($soapresult);
$result = $soapresult->result;
my $bug = $result->{bugs}->[0];
Expand All @@ -299,7 +286,7 @@ =head2 Retrieving Product Information
=cut

if ($product_name) {
$soapresult = $proxy->call('Product.get', {'names' => [$product_name]});
$soapresult = $proxy->call('Product.get', {'names' => [$product_name], token => $Bugzilla_token});
_die_on_fault($soapresult);
$result = $soapresult->result()->{'products'}->[0];

Expand All @@ -325,14 +312,16 @@ =head2 Retrieving Product Information
=head2 Creating A Bug
Call C<Bug.create> with the settings read from the file indicated on
the command line. The file must contain a valid anonymous hash to use
the command line. The file must contain a valid anonymous hash to use
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) {
$soapresult = $proxy->call('Bug.create', do "$create_file_name" );
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;

Expand All @@ -356,7 +345,7 @@ =head2 Getting Legal Field Values
=cut

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

Expand All @@ -374,7 +363,7 @@ =head2 Adding a comment to a bug
if ($add_comment) {
if ($bug_id) {
$soapresult = $proxy->call('Bug.add_comment', {id => $bug_id,
comment => $add_comment, private => $private, work_time => $work_time});
comment => $add_comment, private => $private, work_time => $work_time, token => $Bugzilla_token});
_die_on_fault($soapresult);
print "Comment added.\n";
}
Expand Down

0 comments on commit 660b7bf

Please sign in to comment.