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