# HG changeset patch # User cin # Date 1441126050 -10800 # Node ID d2efec56373f9a2bfcc1b844f8522d9716112a06 # Parent f8f966388b6848f5f1204f72cc5a7c0f95774f00 working buglist transform and bugs fetching diff -r f8f966388b68 -r d2efec56373f bug-list.xsl --- a/bug-list.xsl Mon Aug 24 20:50:23 2015 +0300 +++ b/bug-list.xsl Tue Sep 01 19:47:30 2015 +0300 @@ -4,6 +4,19 @@ extension-element-prefixes="exsl"> + + + + + + + + + + + + @@ -22,7 +35,7 @@ - + @@ -48,11 +61,11 @@ - + - + @@ -74,62 +87,123 @@ - + + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + - - - - - - - - + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -137,47 +211,66 @@ + + + + + + + depends + + + + + + + + + + + + + + - + + + + + - + + + + - - - - - - depends - - - - + + + + + + + + + + + + - - - - - - - - , - - - - - + @@ -200,7 +293,7 @@ + select="exsl:node-set($structure)//bug[@id = $id]/ancestor-or-self::bug"> @@ -250,9 +343,7 @@ task - - - + @@ -275,6 +366,7 @@ + @@ -291,4 +383,26 @@ + + + + + + + + # + + + + + + + + + + + + + + \ No newline at end of file diff -r f8f966388b68 -r d2efec56373f translate.pl --- a/translate.pl Mon Aug 24 20:50:23 2015 +0300 +++ b/translate.pl Tue Sep 01 19:47:30 2015 +0300 @@ -1,5 +1,11 @@ #!/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 @@ -8,18 +14,175 @@ . ); -xalan( - -IN => "bug-list1.xml", - -XSL => "bug-list.xsl", - -URIRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver", - -ENTITYRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver" +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 = @_; + my @params = @_; return system 'java', -cp => join( ':', @ClassPath ), - "org.apache.xalan.xslt.Process", %params; + "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__