View Javadoc

1   package org.apache.maven.plugin.trac;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Goal which downloads issues from the Issue Tracking System and generates a
40   * report.
41   *
42   * @goal trac-report
43   * @author Noriko Kinugasa
44   * @version $Id: TracMojo.html 816592 2012-05-08 12:40:21Z hboutemy $
45   * @since 2.1
46   */
47  public class TracMojo
48      extends AbstractChangesReport
49  {
50      /**
51       * Defines the Trac username for authentication into a private Trac
52       * installation.
53       *
54       * @parameter default-value=""
55       */
56      private String tracUser;
57  
58      /**
59       * Defines the Trac password for authentication into a private Trac
60       * installation.
61       *
62       * @parameter default-value=""
63       */
64      private String tracPassword;
65  
66      /**
67       * Defines the Trac query for searching ticket.
68       *
69       * @parameter default-value="order=id"
70       */
71      private String query;
72  
73      /**
74       * Sets the column names that you want to show in the report. The columns
75       * will appear in the report in the same order as you specify them here.
76       * Multiple values can be separated by commas.
77       * <p>
78       * Valid columns are: <code>id</code>, <code>type</code>,
79       * <code>summary</code>, <code>status</code>, <code>resolution</code>,
80       * <code>milestone</code>, <code>owner</code>, <code>priority</code>,
81       * <code>reporter</code>, <code>component</code>, <code>created</code>,
82       * <code>changed</code>.
83       * </p>
84       *
85       * @parameter default-value="id,type,summary,owner,reporter,priority,status,resolution,created,changed"
86       * @since 2.2
87       */
88      private String columnNames;
89  
90      /**
91       * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
92       */
93      public boolean canGenerateReport()
94      {
95          return validateIfIssueManagementComplete();
96      }
97  
98      public void executeReport( Locale locale )
99          throws MavenReportException
100     {
101         if ( !canGenerateReport() )
102         {
103             throw new MavenReportException( "Issue Management is out of order." );
104         }
105 
106         parseTracUrl();
107 
108         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
109 
110         try
111         {
112             config.setServerURL( new URL( project.getIssueManagement().getUrl() + "/login/xmlrpc" ) );
113         }
114         catch ( MalformedURLException e1 )
115         {
116 
117             throw new MavenReportException( "The Trac URL is incorrect." );
118 
119         }
120         config.setBasicUserName( tracUser );
121         config.setBasicPassword( tracPassword );
122 
123         Object[] queryResult = null;
124         XmlRpcClient client = new XmlRpcClient();
125 
126         client.setConfig( config );
127 
128         String qstr = "";
129 
130         if ( !StringUtils.isEmpty( query ) )
131         {
132             qstr = query;
133         }
134 
135         Object[] params = new Object[] { new String( qstr ) };
136         try
137         {
138             queryResult = (Object[]) client.execute( "ticket.query", params );
139         }
140         catch ( XmlRpcException e )
141         {
142             throw new MavenReportException( "XmlRpc Error.", e );
143         }
144 
145         ArrayList ticketList = new ArrayList();
146         TracTicket matchTicket;
147 
148         TracReportGenerator report = new TracReportGenerator( columnNames );
149 
150         if ( queryResult.length == 0 )
151         {
152 
153             report.doGenerateEmptyReport( getBundle( locale ), getSink() );
154             getLog().warn( "No ticket has matched." );
155 
156         }
157         else
158         {
159 
160             for ( int i = 0; i < queryResult.length; i++ )
161             {
162                 params = new Object[] { queryResult[i] };
163                 try
164                 {
165                     Object[] ticketresult = null;
166                     matchTicket = new TracTicket();
167                     ticketresult = (Object[]) client.execute( "ticket.get", params );
168                     ticketList.add( setQueryResult( ticketresult, matchTicket ) );
169 
170                 }
171                 catch ( XmlRpcException e )
172                 {
173                     throw new MavenReportException( "XmlRpc Error.", e );
174                 }
175             }
176             try
177             {
178 
179                 report.doGenerateReport( getBundle( locale ), getSink(), ticketList );
180 
181             }
182             catch ( Exception e )
183 
184             {
185                 e.printStackTrace();
186 
187             }
188 
189         }
190 
191     }
192 
193     public String getName( Locale locale )
194     {
195         return "Trac Report";
196     }
197 
198     public String getDescription( Locale locale )
199     {
200         return "Report on Ticket from the Trac.";
201     }
202 
203     protected Renderer getSiteRenderer()
204     {
205         return siteRenderer;
206     }
207 
208     protected MavenProject getProject()
209     {
210         return project;
211     }
212 
213     public String getOutputName()
214     {
215         return "trac-report";
216     }
217 
218     private ResourceBundle getBundle( Locale locale )
219     {
220         return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() );
221     }
222 
223     private void parseTracUrl()
224     {
225 
226         String tracUrl = project.getIssueManagement().getUrl();
227 
228         if ( tracUrl.endsWith( "/" ) )
229         {
230             project.getIssueManagement().setUrl( tracUrl.substring( 0, tracUrl.length() - 1 ) );
231         }
232 
233     }
234 
235     private TracTicket setQueryResult( Object[] ticketObj, TracTicket ticket )
236     {
237 
238         ticket.setId( String.valueOf( ticketObj[0] ) );
239 
240         ticket.setLink( project.getIssueManagement().getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) );
241 
242         ticket.setTimeCreated( String.valueOf( ticketObj[1] ) );
243 
244         ticket.setTimeChanged( String.valueOf( ticketObj[2] ) );
245 
246         Map attributes = (Map) ticketObj[3];
247 
248         ticket.setType( (String) attributes.get( "type" ) );
249 
250         ticket.setSummary( (String) attributes.get( "summary" ) );
251 
252         ticket.setStatus( (String) attributes.get( "status" ) );
253 
254         ticket.setResolution( (String) attributes.get( "resolution" ) );
255 
256         ticket.setOwner( (String) attributes.get( "owner" ) );
257 
258         ticket.setMilestone( (String) attributes.get( "milestone" ) );
259 
260         ticket.setPriority( (String) attributes.get( "priority" ) );
261 
262         ticket.setReporter( (String) attributes.get( "reporter" ) );
263 
264         ticket.setComponent( (String) attributes.get( "component" ) );
265 
266         return ticket;
267     }
268 
269     private boolean validateIfIssueManagementComplete()
270     {
271         if ( project.getIssueManagement() == null )
272         {
273             getLog().error( "No Issue Management set. No Trac Report will be generated." );
274 
275             return false;
276         }
277         else if ( ( project.getIssueManagement().getUrl() == null )
278             || ( project.getIssueManagement().getUrl().trim().equals( "" ) ) )
279         {
280             getLog().error( "No URL set in Issue Management. No Trac Report will be generated." );
281 
282             return false;
283         }
284         else if ( ( project.getIssueManagement().getSystem() != null )
285             && !( project.getIssueManagement().getSystem().equalsIgnoreCase( "trac" ) ) )
286         {
287             getLog().error( "The Trac Report only supports Trac.  No Trac Report will be generated." );
288 
289             return false;
290         }
291         return true;
292     }
293 
294 }