view translate.pl @ 5:d2efec56373f

working buglist transform and bugs fetching
author cin
date Tue, 01 Sep 2015 19:47:30 +0300
parents d1400de5832b
children 2a5f38eb25a9
line wrap: on
line source

#!/usr/bin/perl -w

use JSON;
use YAML::XS qw(LoadFile Dump);
use URI;
use XML::Writer;
use IPC::Run qw(run);

our @ClassPath = qw(
  /usr/share/java/xalan-j2-serializer.jar
  /usr/share/java/xalan-j2.jar
  /usr/share/java/xerces-j2.jar
  /usr/share/java/xml-commons-resolver.jar
  .
);

my $config = LoadFile("config.yaml");

if ( !( $config->{bugzilla}{url} =~ /\/$/ ) ) {
	$config->{bugzilla}{url} .= "/";
}

my $bz = BzRest->new(
	url    => $config->{bugzilla}{url},
	apikey => $config->{bugzilla}{apikey}
);

my $bugs = $bz->GetBugs( { ids => [ 283, 284 ] } );

my @fields = qw(
    id
    creation_time
    last_change_time
    creator
    assigned_to
    
    status
    resolution
    
    priority
    severity
    url
    
    blocks
    depends_on
    cc
    
    component
    product
    classification
    version
    
    actual_time
    estimated_time
    remainig_time
    deadline
);

my %fieldsMap = (
	id => 'bug_id',
	creator => 'reporter',
	status => 'bug_status',
	severity => 'bug_severity',
	blocks => 'blocked',
	depends_on => 'dependson',
	creation_time => 'creation_ts',
	last_change_time => 'delta_ts'
);

my $writer = XML::Writer->new( OUTPUT => \*STDOUT, ENCODING => 'utf-8' );

$writer->xmlDecl("UTF-8");
$writer->startTag("bugzilla");

foreach my $bug ( @$bugs ) {
	$writer->startTag("bug");
	foreach my $field ( @fields ) {
		next unless $bug->{$field};
		
		my $tagName = $fieldsMap{$field} || $field;
		my @values = ref($bug->{$field}) && ref($bug->{$field}) eq 'ARRAY' ? @{$bug->{$field}} : $bug->{$field};
		
		foreach my $v (@values) {
			$writer->dataElement($tagName, $v);
		}
	}
	$writer->endTag();
}

$writer->endTag();



#xalan(
#	-IN          => "bug-list2.xml",
#	-XSL         => "bug-list.xsl",
#	-URIRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver",
#	-ENTITYRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver",
#	-PARAM => (chargeset => "dev")
#);

sub xalan {
	my @params = @_;
	return system 'java',
	  -cp => join( ':', @ClassPath ),
	  "org.apache.xalan.xslt.Process", @params;
}

package BzRest;
use fields qw(url apikey);
use LWP::UserAgent;
use XMLRPC::Lite;
use YAML::XS qw(Dump);

use constant { SELF => __PACKAGE__ };

sub new {
	my $class = shift;
	$class = ref $class || $class;

	my $inst = fields::new($class);
	$inst->CTOR(@_);

	return $inst;
}

sub CTOR {
	my SELF $this = shift;
	my %params = @_;

	$this->{url} = $params{url} or die "An url is required";
	$this->{apikey} = $params{apikey} if $params{apikey};
}

sub GetBug {
	my SELF $this = shift;
	my $id        = shift;
	my %params    = @_;

	$params{api_key} = $this->{apikey};

	my $bugurl = URI->new_abs( 'rest/bug/' . $id, $this->{url} );
	$bugurl->query_form( \%params );

	my $agent = LWP::UserAgent->new();
	my $res   = $agent->get($bugurl);

	return $this->_AssertResponse( $res, $bugurl );
}

sub GetBugs {
	my SELF $this = shift;

	return $this->CallXMLRPC( 'Bug.get', shift )->{bugs};
}

sub CallXMLRPC {
	my SELF $this = shift;
	my ( $method, $params ) = @_;

	die "Method must be specified" unless $method;
	$params ||= {};

	$params->{api_key} = $this->{apikey};
	my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );

	my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );

	die $result->fault if $result->fault;
	return $result->result;
}

sub _AssertResponse {
	my SELF $this = shift;
	my ( $res, $url ) = @_;

	die "Failed to get any response: " . $url unless $res;

	die "Failed to fetch: " . $url . ": " . $res->code unless $res->is_success;

	my $bug = JSON->new()->utf8()->decode( $res->content );

	die "Bugzilla failed: " . $bug->{message} if $bug->{error};

	return $bug->{bugs};
}

__END__

=pod

=head1 NAME

C<translate.pl> - translates bugzilla xml buglist to TaskJuggler format

=head1 METHODS

=head2 xalan(%args)

=over

=item * -IN

Input file

=item * -OUT

Output file

=item * -XSL

XSLT file

=back

=cut