# Plugin for TWiki Enterprise Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it # Copyright (C) 2001-2006 Peter Thoeny, peter@thoeny.org # and TWiki Contributors. All Rights Reserved. TWiki Contributors # are listed in the AUTHORS file in the root of this distribution. # NOTE: Please extend that file, not this notice. # # 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. For # more details read LICENSE in the root of this distribution. # # 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. # # For licensing info read LICENSE file in the TWiki root. # # extension developed by Sascha Vetter 2008 (cc/sve) =pod ---+ package TWiki::Plugins::BugzillaAPIPlugin Plugin for connection between Bugzilla API and TWiki. =cut package TWiki::Plugins::BugzillaAPIPlugin; use strict; use Error qw( :try ); use Assert; use File::Basename qw(dirname); use File::Spec; use HTTP::Cookies; # both needed for use XMLRPC::Lite; # connection to bugzilla API require TWiki; require TWiki::UI; my $localdebug = 1; # Dirty debug activation =pod ---++ StaticMethod createBug() Command handler for =createBug= BugzillaAPIPlugin command. =cut sub createBug { my $session = shift; my $query = $session->{cgiQuery}; my $store = $session->{store}; my $user = $session->{user}; &TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: started" ) if $localdebug; # get all parameters from the form my @paramNames = $query->param(); my @formDataName = (); my @formDataValue = (); my @formDataRequired = (); my %createInfo; # Hash with all informations for bugzilla foreach( @paramNames ) { if( /^(Twk)([0-9])(.*)/ ) { my $value = $query->param( "$1$2$3" ); $formDataRequired[@formDataRequired] = $2; my $name = $3; $formDataName[@formDataName] = $name; $formDataValue[@formDataValue] = $value; &TWiki::Func::writeDebug( "BugzillaAPIPlugin::createBug: $name - $value" ) if $localdebug; # add informations to hash $createInfo{$name} = $value; } else { # do something ... } } # check if required fields are filled in my $formLen = @formDataValue; for( my $x = 0; $x < $formLen; $x++ ) { if( ( $formDataRequired[$x] ) && ( ! $formDataValue[$x] ) ) { throw TWiki::OopsException( 'attention', def=>'mandatory_field', web => $session->{webName}, topic => $session->{topicName}, params => [ "ERROR: data of fiel missing: field $x in file " . __FILE__ . " in line" . __LINE__ . "\n\nfield#$x\nformDataRequired: $formDataRequired[$x]\nformdataValue: $formDataValue[$x]\n\n@formDataValue\n\n@formDataRequired" ] ); #_printError( "ERROR: data of fiel missing: field $x in file " . __FILE__ . " in line" . __LINE__ . "\n\nfield#$x\nformDataRequired: $formDataRequired[$x]\nformdataValue: $formDataValue[$x]\n\n@formDataValue\n\n@formDataRequired" ); return; } } # connection to bugzilla api my $Bugzilla_uri = "http://myurl.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 = "\@"; my $bugzilla_pwd = ""; # Log in. my $soapresult = $proxy->call('User.login', { login => $bugzilla_user, password => $bugzilla_pwd, remember => 0 } ); 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 ($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: $result\n") if $localdebug; } # goto bugzilla form site my $url = &TWiki::Func::getViewUrl( $session->{webName}, $session->{topicName} ); &TWiki::Func::redirectCgiQuery( $query, $url . "?bugid=" . $bugid ); } 1;