Howto: Connect TWiki to Bugzilla API
Preparation
Following perl moduls are needed to use the bugzilla API:
Install them from
cpan.org
.
Also
bz_webservice_demo.pl is needed, get the file from the bugzilla package (download
bugzilla-3.?.?.tar.gz from:
bugzilla.org
, file in folder:
bugzilla-3.0.4/contrib)
First steps
At first, we will check if a basic connect to the bugzilla api is possible:
# perl bz_webservice_demo.pl --uri http://connection.to/bugzilla/xmlrpc.cgi --login username --password mypassword
Should return:
Connecting to a Bugzilla of version 3.?.?.
Bugzilla's timezone is ????.
Login successful.
If not, check if url is not correct, or if there is a firewall who blocks ...
Create a new bug entry
Basic code
use File::Basename qw(dirname);
use File::Spec;
use HTTP::Cookies;
use XMLRPC::Lite;
...
my %bug_values = (
product => 'ExampleProd',
component => 'Prod8',
summary => 'API Test',
version => '1.0',
description => 'This is a description',
op_sys => 'All',
platform => 'All',
severity => 'normal',
priority => 'P5'
); # all these field are required
$soapresult = $proxy->call('Bug.create', \%bug_values);
_die_on_fault($soapresult);
$result = $soapresult->result;
if (ref($result) eq 'HASH') {
foreach (keys(%$result)) {
print "$_: $$result{$_}\n";
}
}
else {
print "$result\n";
}
Use TWiki for bug creation
Please note, code has written very rough and isn't a very good example for a twiki extension, but it will show you how TWiki and Bugzilla API work together and that's a beginning.
Frontend
At first we start with the twiki site (all files are uploaded, see attachment):
TWiki bin/publishbug
The connector between form and plugin (see
bin/view for more informations)
...
use TWiki::Plugins;
use TWiki::Plugins::BugzillaAPIPlugin;
TWiki::UI::run( \&TWiki::Plugins::BugzillaAPIPlugin::createBug );
TWiki lib/TWiki/Plugins/BugzillaAPIPlugin.pm
The main code, there are code comments in
BugzillaAPIPlugin.pm which might help you.
...
# connection to bugzilla api
my $Bugzilla_uri = "http://myaddress.to/bugzilla/xmlrpc.cgi";
my $cookie_jar = new HTTP::Cookies('file' => File::Spec->catdir(dirname($0), 'cookies.txt'), 'autosave' => 1);
my $proxy = XMLRPC::Lite->proxy($Bugzilla_uri, 'cookie_jar' => $cookie_jar);
# try to login to bugzilla
my $bugzilla_user = "<user>\@<domain>";
my $bugzilla_pwd = "<pwd>";
# Log in.
my $soapresult = $proxy->call('User.login',
{ login => $bugzilla_user,
password => $bugzilla_pwd,
remember => 0 } );
# If login failed
if ($soapresult->fault) {
my ($package, $filename, $line) = caller;
&TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: Login failed: " . $soapresult->faultcode . " " . $soapresult->faultstring ) if $localdebug;
throw TWiki::OopsException(
'attention',
def=>'SOAP_error',
web => $session->{webName},
topic => $session->{topicName},
params => [ $soapresult->faultcode . ' ' . $soapresult->faultstring . " in SOAP call near $filename line $line.\n" ] );
return;
}
&TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: Login successful" ) if $localdebug;
# let's create a bug
$soapresult = $proxy->call('Bug.create', \%createInfo);
# if creation failed:
if ($soapresult->fault) {
my ($package, $filename, $line) = caller;
&TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: Create Bug failed: " . $soapresult->faultcode . " " . $soapresult->faultstring ) if $localdebug;
throw TWiki::OopsException(
'attention',
def=>'SOAP_error',
web => $session->{webName},
topic => $session->{topicName},
params => [ $soapresult->faultcode . ' ' . $soapresult->faultstring . " in SOAP call near $filename line $line.\n" ] );
return;
}
my $result = $soapresult->result;
my $bugid;
if (ref($result) eq 'HASH') {
foreach (keys(%$result)) {
&TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: $_: $$result{$_}\n") if $localdebug;
$bugid = $$result{$_};
}
}
else {
&TWiki::Func::writeDebug("BugzillaAPIPlugin::createBug: unexpected result\n") if $localdebug;
}
Add comment
under construction
--
Contributors: SaschaVetter - 08 Aug 2008
Discussion