1 | |
package org.apache.maven.plugin.trac; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.net.MalformedURLException; |
23 | |
import java.net.URL; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Locale; |
26 | |
import java.util.Map; |
27 | |
import java.util.ResourceBundle; |
28 | |
|
29 | |
import org.apache.maven.doxia.siterenderer.Renderer; |
30 | |
import org.apache.maven.plugin.changes.AbstractChangesReport; |
31 | |
import org.apache.maven.project.MavenProject; |
32 | |
import org.apache.maven.reporting.MavenReportException; |
33 | |
import org.apache.xmlrpc.XmlRpcException; |
34 | |
import org.apache.xmlrpc.client.XmlRpcClient; |
35 | |
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; |
36 | |
import org.codehaus.plexus.util.StringUtils; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class TracMojo |
48 | |
extends AbstractChangesReport |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private String tracUser; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private String tracPassword; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private String query; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private String columnNames; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public boolean canGenerateReport() |
94 | |
{ |
95 | 0 | return validateIfIssueManagementComplete(); |
96 | |
} |
97 | |
|
98 | |
public void executeReport( Locale locale ) |
99 | |
throws MavenReportException |
100 | |
{ |
101 | 0 | if ( !canGenerateReport() ) |
102 | |
{ |
103 | 0 | throw new MavenReportException( "Issue Management is out of order." ); |
104 | |
} |
105 | |
|
106 | 0 | parseTracUrl(); |
107 | |
|
108 | 0 | XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); |
109 | |
|
110 | |
try |
111 | |
{ |
112 | 0 | config.setServerURL( new URL( project.getIssueManagement().getUrl() + "/login/xmlrpc" ) ); |
113 | |
} |
114 | 0 | catch ( MalformedURLException e1 ) |
115 | |
{ |
116 | |
|
117 | 0 | throw new MavenReportException( "The Trac URL is incorrect." ); |
118 | |
|
119 | 0 | } |
120 | 0 | config.setBasicUserName( tracUser ); |
121 | 0 | config.setBasicPassword( tracPassword ); |
122 | |
|
123 | 0 | Object[] queryResult = null; |
124 | 0 | XmlRpcClient client = new XmlRpcClient(); |
125 | |
|
126 | 0 | client.setConfig( config ); |
127 | |
|
128 | 0 | String qstr = ""; |
129 | |
|
130 | 0 | if ( !StringUtils.isEmpty( query ) ) |
131 | |
{ |
132 | 0 | qstr = query; |
133 | |
} |
134 | |
|
135 | 0 | Object[] params = new Object[] { new String( qstr ) }; |
136 | |
try |
137 | |
{ |
138 | 0 | queryResult = (Object[]) client.execute( "ticket.query", params ); |
139 | |
} |
140 | 0 | catch ( XmlRpcException e ) |
141 | |
{ |
142 | 0 | throw new MavenReportException( "XmlRpc Error.", e ); |
143 | 0 | } |
144 | |
|
145 | 0 | ArrayList ticketList = new ArrayList(); |
146 | |
TracTicket matchTicket; |
147 | |
|
148 | 0 | TracReportGenerator report = new TracReportGenerator( columnNames ); |
149 | |
|
150 | 0 | if ( queryResult.length == 0 ) |
151 | |
{ |
152 | |
|
153 | 0 | report.doGenerateEmptyReport( getBundle( locale ), getSink() ); |
154 | 0 | getLog().warn( "No ticket has matched." ); |
155 | |
|
156 | |
} |
157 | |
else |
158 | |
{ |
159 | |
|
160 | 0 | for ( int i = 0; i < queryResult.length; i++ ) |
161 | |
{ |
162 | 0 | params = new Object[] { queryResult[i] }; |
163 | |
try |
164 | |
{ |
165 | 0 | Object[] ticketresult = null; |
166 | 0 | matchTicket = new TracTicket(); |
167 | 0 | ticketresult = (Object[]) client.execute( "ticket.get", params ); |
168 | 0 | ticketList.add( setQueryResult( ticketresult, matchTicket ) ); |
169 | |
|
170 | |
} |
171 | 0 | catch ( XmlRpcException e ) |
172 | |
{ |
173 | 0 | throw new MavenReportException( "XmlRpc Error.", e ); |
174 | 0 | } |
175 | |
} |
176 | |
try |
177 | |
{ |
178 | |
|
179 | 0 | report.doGenerateReport( getBundle( locale ), getSink(), ticketList ); |
180 | |
|
181 | |
} |
182 | 0 | catch ( Exception e ) |
183 | |
|
184 | |
{ |
185 | 0 | e.printStackTrace(); |
186 | |
|
187 | 0 | } |
188 | |
|
189 | |
} |
190 | |
|
191 | 0 | } |
192 | |
|
193 | |
public String getName( Locale locale ) |
194 | |
{ |
195 | 0 | return "Trac Report"; |
196 | |
} |
197 | |
|
198 | |
public String getDescription( Locale locale ) |
199 | |
{ |
200 | 0 | return "Report on Ticket from the Trac."; |
201 | |
} |
202 | |
|
203 | |
protected Renderer getSiteRenderer() |
204 | |
{ |
205 | 0 | return siteRenderer; |
206 | |
} |
207 | |
|
208 | |
protected MavenProject getProject() |
209 | |
{ |
210 | 0 | return project; |
211 | |
} |
212 | |
|
213 | |
public String getOutputName() |
214 | |
{ |
215 | 0 | return "trac-report"; |
216 | |
} |
217 | |
|
218 | |
private ResourceBundle getBundle( Locale locale ) |
219 | |
{ |
220 | 0 | return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() ); |
221 | |
} |
222 | |
|
223 | |
private void parseTracUrl() |
224 | |
{ |
225 | |
|
226 | 0 | String tracUrl = project.getIssueManagement().getUrl(); |
227 | |
|
228 | 0 | if ( tracUrl.endsWith( "/" ) ) |
229 | |
{ |
230 | 0 | project.getIssueManagement().setUrl( tracUrl.substring( 0, tracUrl.length() - 1 ) ); |
231 | |
} |
232 | |
|
233 | 0 | } |
234 | |
|
235 | |
private TracTicket setQueryResult( Object[] ticketObj, TracTicket ticket ) |
236 | |
{ |
237 | |
|
238 | 0 | ticket.setId( String.valueOf( ticketObj[0] ) ); |
239 | |
|
240 | 0 | ticket.setLink( project.getIssueManagement().getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) ); |
241 | |
|
242 | 0 | ticket.setTimeCreated( String.valueOf( ticketObj[1] ) ); |
243 | |
|
244 | 0 | ticket.setTimeChanged( String.valueOf( ticketObj[2] ) ); |
245 | |
|
246 | 0 | Map attributes = (Map) ticketObj[3]; |
247 | |
|
248 | 0 | ticket.setType( (String) attributes.get( "type" ) ); |
249 | |
|
250 | 0 | ticket.setSummary( (String) attributes.get( "summary" ) ); |
251 | |
|
252 | 0 | ticket.setStatus( (String) attributes.get( "status" ) ); |
253 | |
|
254 | 0 | ticket.setResolution( (String) attributes.get( "resolution" ) ); |
255 | |
|
256 | 0 | ticket.setOwner( (String) attributes.get( "owner" ) ); |
257 | |
|
258 | 0 | ticket.setMilestone( (String) attributes.get( "milestone" ) ); |
259 | |
|
260 | 0 | ticket.setPriority( (String) attributes.get( "priority" ) ); |
261 | |
|
262 | 0 | ticket.setReporter( (String) attributes.get( "reporter" ) ); |
263 | |
|
264 | 0 | ticket.setComponent( (String) attributes.get( "component" ) ); |
265 | |
|
266 | 0 | return ticket; |
267 | |
} |
268 | |
|
269 | |
private boolean validateIfIssueManagementComplete() |
270 | |
{ |
271 | 0 | if ( project.getIssueManagement() == null ) |
272 | |
{ |
273 | 0 | getLog().error( "No Issue Management set. No Trac Report will be generated." ); |
274 | |
|
275 | 0 | return false; |
276 | |
} |
277 | 0 | else if ( ( project.getIssueManagement().getUrl() == null ) |
278 | |
|| ( project.getIssueManagement().getUrl().trim().equals( "" ) ) ) |
279 | |
{ |
280 | 0 | getLog().error( "No URL set in Issue Management. No Trac Report will be generated." ); |
281 | |
|
282 | 0 | return false; |
283 | |
} |
284 | 0 | else if ( ( project.getIssueManagement().getSystem() != null ) |
285 | |
&& !( project.getIssueManagement().getSystem().equalsIgnoreCase( "trac" ) ) ) |
286 | |
{ |
287 | 0 | getLog().error( "The Trac Report only supports Trac. No Trac Report will be generated." ); |
288 | |
|
289 | 0 | return false; |
290 | |
} |
291 | 0 | return true; |
292 | |
} |
293 | |
|
294 | |
} |