view translate.pl @ 6:2a5f38eb25a9

migrated to saxon8 as xslt processor fixed dependency calculation when a container has a dependency on his grandchild
author cin
date Wed, 02 Sep 2015 20:43:38 +0300
parents d2efec56373f
children 29309bc8d932
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(start finish);

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 @fields = qw(
  id
  summary
  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 = (
	summary          => 'short_desc',
	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'
);

local (*HIN);

my $proc = start( [ 'saxon8', '-novw', '-', 'bug-list.xsl' ],
	'<pipe', \*HIN, '>', \*STDOUT )
  or die "failed to create pipe: $!";

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

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

my %visited;
my @queue = (283);

while (@queue) {
	@queue = grep not( $visited{$_}++ ), @queue;

	last unless @queue;

	print "#Fetching: ", join( ', ', @queue ), "\n";

	my $bugs = $bz->GetBugs( { ids => \@queue } );

	@queue = ();

	foreach my $bug (@$bugs) {

		push @queue, @{ $bug->{depends_on} }
		  if ( $bug->{depends_on} );

		$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();

close HIN;
finish($proc);

#xalan(
#    in => \*OUT,
#    out => \*STDOUT,
#    params => [
#
#   ]
#);

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