annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
1 #!/usr/bin/perl -w
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
2
5
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
3 use JSON;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
4 use YAML::XS qw(LoadFile Dump);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
5 use URI;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
6 use XML::Writer;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
7 use IPC::Run qw(run);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
8
1
d1400de5832b improved xsl
cin
parents: 0
diff changeset
9 our @ClassPath = qw(
d1400de5832b improved xsl
cin
parents: 0
diff changeset
10 /usr/share/java/xalan-j2-serializer.jar
d1400de5832b improved xsl
cin
parents: 0
diff changeset
11 /usr/share/java/xalan-j2.jar
d1400de5832b improved xsl
cin
parents: 0
diff changeset
12 /usr/share/java/xerces-j2.jar
d1400de5832b improved xsl
cin
parents: 0
diff changeset
13 /usr/share/java/xml-commons-resolver.jar
d1400de5832b improved xsl
cin
parents: 0
diff changeset
14 .
d1400de5832b improved xsl
cin
parents: 0
diff changeset
15 );
d1400de5832b improved xsl
cin
parents: 0
diff changeset
16
5
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
17 my $config = LoadFile("config.yaml");
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
18
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
19 if ( !( $config->{bugzilla}{url} =~ /\/$/ ) ) {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
20 $config->{bugzilla}{url} .= "/";
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
21 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
22
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
23 my $bz = BzRest->new(
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
24 url => $config->{bugzilla}{url},
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
25 apikey => $config->{bugzilla}{apikey}
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
26 );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
27
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
28 my $bugs = $bz->GetBugs( { ids => [ 283, 284 ] } );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
29
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
30 my @fields = qw(
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
31 id
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
32 creation_time
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
33 last_change_time
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
34 creator
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
35 assigned_to
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
36
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
37 status
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
38 resolution
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
39
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
40 priority
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
41 severity
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
42 url
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
43
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
44 blocks
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
45 depends_on
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
46 cc
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
47
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
48 component
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
49 product
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
50 classification
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
51 version
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
52
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
53 actual_time
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
54 estimated_time
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
55 remainig_time
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
56 deadline
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
57 );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
58
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
59 my %fieldsMap = (
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
60 id => 'bug_id',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
61 creator => 'reporter',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
62 status => 'bug_status',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
63 severity => 'bug_severity',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
64 blocks => 'blocked',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
65 depends_on => 'dependson',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
66 creation_time => 'creation_ts',
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
67 last_change_time => 'delta_ts'
1
d1400de5832b improved xsl
cin
parents: 0
diff changeset
68 );
0
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
69
5
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
70 my $writer = XML::Writer->new( OUTPUT => \*STDOUT, ENCODING => 'utf-8' );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
71
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
72 $writer->xmlDecl("UTF-8");
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
73 $writer->startTag("bugzilla");
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
74
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
75 foreach my $bug ( @$bugs ) {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
76 $writer->startTag("bug");
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
77 foreach my $field ( @fields ) {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
78 next unless $bug->{$field};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
79
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
80 my $tagName = $fieldsMap{$field} || $field;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
81 my @values = ref($bug->{$field}) && ref($bug->{$field}) eq 'ARRAY' ? @{$bug->{$field}} : $bug->{$field};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
82
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
83 foreach my $v (@values) {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
84 $writer->dataElement($tagName, $v);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
85 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
86 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
87 $writer->endTag();
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
88 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
89
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
90 $writer->endTag();
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
91
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
92
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
93
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
94 #xalan(
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
95 # -IN => "bug-list2.xml",
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
96 # -XSL => "bug-list.xsl",
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
97 # -URIRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver",
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
98 # -ENTITYRESOLVER => "org.apache.xml.resolver.tools.CatalogResolver",
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
99 # -PARAM => (chargeset => "dev")
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
100 #);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
101
0
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
102 sub xalan {
5
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
103 my @params = @_;
1
d1400de5832b improved xsl
cin
parents: 0
diff changeset
104 return system 'java',
d1400de5832b improved xsl
cin
parents: 0
diff changeset
105 -cp => join( ':', @ClassPath ),
5
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
106 "org.apache.xalan.xslt.Process", @params;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
107 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
108
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
109 package BzRest;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
110 use fields qw(url apikey);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
111 use LWP::UserAgent;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
112 use XMLRPC::Lite;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
113 use YAML::XS qw(Dump);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
114
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
115 use constant { SELF => __PACKAGE__ };
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
116
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
117 sub new {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
118 my $class = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
119 $class = ref $class || $class;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
120
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
121 my $inst = fields::new($class);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
122 $inst->CTOR(@_);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
123
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
124 return $inst;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
125 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
126
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
127 sub CTOR {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
128 my SELF $this = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
129 my %params = @_;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
130
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
131 $this->{url} = $params{url} or die "An url is required";
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
132 $this->{apikey} = $params{apikey} if $params{apikey};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
133 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
134
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
135 sub GetBug {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
136 my SELF $this = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
137 my $id = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
138 my %params = @_;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
139
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
140 $params{api_key} = $this->{apikey};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
141
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
142 my $bugurl = URI->new_abs( 'rest/bug/' . $id, $this->{url} );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
143 $bugurl->query_form( \%params );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
144
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
145 my $agent = LWP::UserAgent->new();
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
146 my $res = $agent->get($bugurl);
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
147
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
148 return $this->_AssertResponse( $res, $bugurl );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
149 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
150
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
151 sub GetBugs {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
152 my SELF $this = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
153
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
154 return $this->CallXMLRPC( 'Bug.get', shift )->{bugs};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
155 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
156
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
157 sub CallXMLRPC {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
158 my SELF $this = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
159 my ( $method, $params ) = @_;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
160
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
161 die "Method must be specified" unless $method;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
162 $params ||= {};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
163
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
164 $params->{api_key} = $this->{apikey};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
165 my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
166
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
167 my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
168
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
169 die $result->fault if $result->fault;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
170 return $result->result;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
171 }
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
172
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
173 sub _AssertResponse {
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
174 my SELF $this = shift;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
175 my ( $res, $url ) = @_;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
176
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
177 die "Failed to get any response: " . $url unless $res;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
178
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
179 die "Failed to fetch: " . $url . ": " . $res->code unless $res->is_success;
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
180
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
181 my $bug = JSON->new()->utf8()->decode( $res->content );
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
182
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
183 die "Bugzilla failed: " . $bug->{message} if $bug->{error};
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
184
d2efec56373f working buglist transform and bugs fetching
cin
parents: 1
diff changeset
185 return $bug->{bugs};
0
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
186 }
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
187
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
188 __END__
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
189
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
190 =pod
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
191
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
192 =head1 NAME
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
193
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
194 C<translate.pl> - translates bugzilla xml buglist to TaskJuggler format
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
195
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
196 =head1 METHODS
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
197
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
198 =head2 xalan(%args)
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
199
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
200 =over
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
201
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
202 =item * -IN
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
203
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
204 Input file
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
205
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
206 =item * -OUT
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
207
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
208 Output file
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
209
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
210 =item * -XSL
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
211
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
212 XSLT file
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
213
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
214 =back
8cae44c166d3 initial xslt and sample xml
cin
parents:
diff changeset
215
1
d1400de5832b improved xsl
cin
parents: 0
diff changeset
216 =cut