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 | |
public boolean canGenerateReport() |
77 | |
{ |
78 | 0 | return validateIfIssueManagementComplete(); |
79 | |
} |
80 | |
|
81 | |
public void executeReport( Locale locale ) |
82 | |
throws MavenReportException |
83 | |
{ |
84 | 0 | if ( !canGenerateReport() ) |
85 | |
{ |
86 | 0 | throw new MavenReportException( "Issue Management is out of order." ); |
87 | |
} |
88 | |
|
89 | 0 | parseTracUrl(); |
90 | |
|
91 | 0 | XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 0 | config.setServerURL( new URL( project.getIssueManagement().getUrl() + "/login/xmlrpc" ) ); |
96 | |
} |
97 | 0 | catch ( MalformedURLException e1 ) |
98 | |
{ |
99 | |
|
100 | 0 | throw new MavenReportException( "The Trac URL is incorrect." ); |
101 | |
|
102 | 0 | } |
103 | 0 | config.setBasicUserName( tracUser ); |
104 | 0 | config.setBasicPassword( tracPassword ); |
105 | |
|
106 | 0 | Object[] queryResult = null; |
107 | 0 | XmlRpcClient client = new XmlRpcClient(); |
108 | |
|
109 | 0 | client.setConfig( config ); |
110 | |
|
111 | 0 | String qstr = ""; |
112 | |
|
113 | 0 | if ( !StringUtils.isEmpty( query ) ) |
114 | |
{ |
115 | 0 | qstr = query; |
116 | |
} |
117 | |
|
118 | 0 | Object[] params = new Object[] { new String( qstr ) }; |
119 | |
try |
120 | |
{ |
121 | 0 | queryResult = (Object[]) client.execute( "ticket.query", params ); |
122 | |
} |
123 | 0 | catch ( XmlRpcException e ) |
124 | |
{ |
125 | 0 | throw new MavenReportException( "XmlRpc Error.", e ); |
126 | 0 | } |
127 | |
|
128 | 0 | ArrayList ticketList = new ArrayList(); |
129 | |
TracTicket matchTicket; |
130 | |
|
131 | 0 | TracReportGenerator report = new TracReportGenerator(); |
132 | |
|
133 | 0 | if ( queryResult.length == 0 ) |
134 | |
{ |
135 | |
|
136 | 0 | report.doGenerateEmptyReport( getBundle( locale ), getSink() ); |
137 | 0 | getLog().warn( "No ticket has matched." ); |
138 | |
|
139 | |
} |
140 | |
else |
141 | |
{ |
142 | |
|
143 | 0 | for ( int i = 0; i < queryResult.length; i++ ) |
144 | |
{ |
145 | 0 | params = new Object[] { queryResult[i] }; |
146 | |
try |
147 | |
{ |
148 | 0 | Object[] Ticketresult = null; |
149 | 0 | matchTicket = new TracTicket(); |
150 | 0 | Ticketresult = (Object[]) client.execute( "ticket.get", params ); |
151 | 0 | ticketList.add( setQueryResult( Ticketresult, matchTicket ) ); |
152 | |
|
153 | |
} |
154 | 0 | catch ( XmlRpcException e ) |
155 | |
{ |
156 | 0 | throw new MavenReportException( "XmlRpc Error.", e ); |
157 | 0 | } |
158 | |
} |
159 | |
try |
160 | |
{ |
161 | |
|
162 | 0 | report.doGenerateReport( getBundle( locale ), getSink(), ticketList ); |
163 | |
|
164 | |
} |
165 | 0 | catch ( Exception e ) |
166 | |
|
167 | |
{ |
168 | 0 | e.printStackTrace(); |
169 | |
|
170 | 0 | } |
171 | |
|
172 | |
} |
173 | |
|
174 | 0 | } |
175 | |
|
176 | |
public String getName( Locale locale ) |
177 | |
{ |
178 | 0 | return "Trac Report"; |
179 | |
} |
180 | |
|
181 | |
public String getDescription( Locale locale ) |
182 | |
{ |
183 | 0 | return "Report on Ticket from the Trac."; |
184 | |
} |
185 | |
|
186 | |
protected Renderer getSiteRenderer() |
187 | |
{ |
188 | 0 | return siteRenderer; |
189 | |
} |
190 | |
|
191 | |
protected MavenProject getProject() |
192 | |
{ |
193 | 0 | return project; |
194 | |
} |
195 | |
|
196 | |
public String getOutputName() |
197 | |
{ |
198 | 0 | return "trac-report"; |
199 | |
} |
200 | |
|
201 | |
private ResourceBundle getBundle( Locale locale ) |
202 | |
{ |
203 | 0 | return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() ); |
204 | |
} |
205 | |
|
206 | |
private void parseTracUrl() |
207 | |
{ |
208 | |
|
209 | 0 | String tracUrl = project.getIssueManagement().getUrl(); |
210 | |
|
211 | 0 | if ( tracUrl.endsWith( "/" ) ) |
212 | |
{ |
213 | 0 | project.getIssueManagement().setUrl( tracUrl.substring( 0, tracUrl.length() - 1 ) ); |
214 | |
} |
215 | |
|
216 | 0 | } |
217 | |
|
218 | |
private TracTicket setQueryResult( Object[] ticketObj, TracTicket ticket ) |
219 | |
{ |
220 | |
|
221 | 0 | ticket.setId( String.valueOf( ticketObj[0] ) ); |
222 | |
|
223 | 0 | ticket.setLink( project.getIssueManagement().getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) ); |
224 | |
|
225 | 0 | ticket.setTimeCreated( String.valueOf( ticketObj[1] ) ); |
226 | |
|
227 | 0 | ticket.setTimeChanged( String.valueOf( ticketObj[2] ) ); |
228 | |
|
229 | 0 | Map attributes = (Map) ticketObj[3]; |
230 | |
|
231 | 0 | ticket.setType( (String) attributes.get( "type" ) ); |
232 | |
|
233 | 0 | ticket.setSummary( (String) attributes.get( "summary" ) ); |
234 | |
|
235 | 0 | ticket.setStatus( (String) attributes.get( "status" ) ); |
236 | |
|
237 | 0 | ticket.setResolution( (String) attributes.get( "resolution" ) ); |
238 | |
|
239 | 0 | ticket.setOwner( (String) attributes.get( "owner" ) ); |
240 | |
|
241 | 0 | ticket.setMilestone( (String) attributes.get( "milestone" ) ); |
242 | |
|
243 | 0 | ticket.setPriority( (String) attributes.get( "priority" ) ); |
244 | |
|
245 | 0 | ticket.setReporter( (String) attributes.get( "reporter" ) ); |
246 | |
|
247 | 0 | ticket.setComponent( (String) attributes.get( "component" ) ); |
248 | |
|
249 | 0 | return ticket; |
250 | |
} |
251 | |
|
252 | |
private boolean validateIfIssueManagementComplete() |
253 | |
{ |
254 | 0 | if ( project.getIssueManagement() == null ) |
255 | |
{ |
256 | 0 | getLog().error( "No Issue Management set. No Trac Report will be generated." ); |
257 | |
|
258 | 0 | return false; |
259 | |
} |
260 | 0 | else if ( ( project.getIssueManagement().getUrl() == null ) |
261 | |
|| ( project.getIssueManagement().getUrl().trim().equals( "" ) ) ) |
262 | |
{ |
263 | 0 | getLog().error( "No URL set in Issue Management. No Trac Report will be generated." ); |
264 | |
|
265 | 0 | return false; |
266 | |
} |
267 | 0 | else if ( ( project.getIssueManagement().getSystem() != null ) |
268 | |
&& !( project.getIssueManagement().getSystem().equalsIgnoreCase( "trac" ) ) ) |
269 | |
{ |
270 | 0 | getLog().error( "The Trac Report only supports Trac. No Trac Report will be generated." ); |
271 | |
|
272 | 0 | return false; |
273 | |
} |
274 | 0 | return true; |
275 | |
} |
276 | |
|
277 | |
} |