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.util.HashMap;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.ResourceBundle;
28  
29  import org.apache.maven.plugin.changes.AbstractChangesReport;
30  import org.apache.maven.plugin.changes.ProjectUtils;
31  import org.apache.maven.plugin.issues.IssuesReportGenerator;
32  import org.apache.maven.plugin.issues.IssuesReportHelper;
33  import org.apache.maven.reporting.MavenReportException;
34  import org.apache.xmlrpc.XmlRpcException;
35  
36  /**
37   * Goal which downloads issues from the Issue Tracking System and generates a
38   * report.
39   *
40   * @goal trac-report
41   * @author Noriko Kinugasa
42   * @version $Id: TracMojo.html 816601 2012-05-08 12:50:18Z hboutemy $
43   * @since 2.1
44   * @threadSafe
45   */
46  public class TracMojo
47      extends AbstractChangesReport
48  {
49      /**
50       * Deprecated Trac columns.
51       */
52      private static Map<String,Integer> DEPRECATED_TRAC_COLUMNS = new HashMap<String,Integer>();
53  
54      /**
55       * Valid Trac columns.
56       */
57      private static Map<String,Integer> TRAC_COLUMNS = new HashMap<String,Integer>();
58  
59      static
60      {
61          DEPRECATED_TRAC_COLUMNS.put( "changed", new Integer( IssuesReportHelper.COLUMN_UPDATED ) );
62          DEPRECATED_TRAC_COLUMNS.put( "component", new Integer( IssuesReportHelper.COLUMN_COMPONENT ) );
63          DEPRECATED_TRAC_COLUMNS.put( "created", new Integer( IssuesReportHelper.COLUMN_CREATED ) );
64          DEPRECATED_TRAC_COLUMNS.put( "id", new Integer( IssuesReportHelper.COLUMN_ID ) );
65          DEPRECATED_TRAC_COLUMNS.put( "milestone", new Integer( IssuesReportHelper.COLUMN_FIX_VERSION ) );
66          DEPRECATED_TRAC_COLUMNS.put( "owner", new Integer( IssuesReportHelper.COLUMN_ASSIGNEE ) );
67          DEPRECATED_TRAC_COLUMNS.put( "priority", new Integer( IssuesReportHelper.COLUMN_PRIORITY ) );
68          DEPRECATED_TRAC_COLUMNS.put( "reporter", new Integer( IssuesReportHelper.COLUMN_REPORTER ) );
69          DEPRECATED_TRAC_COLUMNS.put( "resolution", new Integer( IssuesReportHelper.COLUMN_RESOLUTION ) );
70          DEPRECATED_TRAC_COLUMNS.put( "status", new Integer( IssuesReportHelper.COLUMN_STATUS ) );
71          DEPRECATED_TRAC_COLUMNS.put( "summary", new Integer( IssuesReportHelper.COLUMN_SUMMARY ) );
72          DEPRECATED_TRAC_COLUMNS.put( "type", new Integer( IssuesReportHelper.COLUMN_TYPE ) );
73  
74          TRAC_COLUMNS.put( "Assignee", new Integer( IssuesReportHelper.COLUMN_ASSIGNEE ) );
75          TRAC_COLUMNS.put( "Component", new Integer( IssuesReportHelper.COLUMN_COMPONENT ) );
76          TRAC_COLUMNS.put( "Created", new Integer( IssuesReportHelper.COLUMN_CREATED ) );
77          TRAC_COLUMNS.put( "Fix Version", new Integer( IssuesReportHelper.COLUMN_FIX_VERSION ) );
78          TRAC_COLUMNS.put( "Id", new Integer( IssuesReportHelper.COLUMN_ID ) );
79          TRAC_COLUMNS.put( "Priority", new Integer( IssuesReportHelper.COLUMN_PRIORITY ) );
80          TRAC_COLUMNS.put( "Reporter", new Integer( IssuesReportHelper.COLUMN_REPORTER ) );
81          TRAC_COLUMNS.put( "Resolution", new Integer( IssuesReportHelper.COLUMN_RESOLUTION ) );
82          TRAC_COLUMNS.put( "Status", new Integer( IssuesReportHelper.COLUMN_STATUS ) );
83          TRAC_COLUMNS.put( "Summary", new Integer( IssuesReportHelper.COLUMN_SUMMARY ) );
84          TRAC_COLUMNS.put( "Type", new Integer( IssuesReportHelper.COLUMN_TYPE ) );
85          TRAC_COLUMNS.put( "Updated", new Integer( IssuesReportHelper.COLUMN_UPDATED ) );
86      }
87  
88      /**
89       * Sets the column names that you want to show in the report. The columns
90       * will appear in the report in the same order as you specify them here.
91       * Multiple values can be separated by commas.
92       * <p>
93       * Valid columns are: <code>Assignee</code>, <code>Component</code>,
94       * <code>Created</code>, <code>Fix Version</code>, <code>Id</code>,
95       * <code>Priority</code>, <code>Reporter</code>, <code>Resolution</code>,
96       * <code>Status</code>, <code>Summary</code>, <code>Type</code> and
97       * <code>Updated</code>.
98       * </p>
99       *
100      * @parameter default-value="Id,Type,Summary,Assignee,Reporter,Priority,Status,Resolution,Created,Updated"
101      * @since 2.2
102      */
103     private String columnNames;
104 
105     /**
106      * Defines the Trac query for searching ticket.
107      *
108      * @parameter default-value="order=id"
109      */
110     private String query;
111 
112     /**
113      * Defines the Trac password for authentication into a private Trac
114      * installation.
115      *
116      * @parameter default-value=""
117      */
118     private String tracPassword;
119 
120     /**
121      * Defines the Trac username for authentication into a private Trac
122      * installation.
123      *
124      * @parameter default-value=""
125      */
126     private String tracUser;
127 
128     /* --------------------------------------------------------------------- */
129     /* Public methods                                                        */
130     /* --------------------------------------------------------------------- */
131 
132     /**
133      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
134      */
135     public boolean canGenerateReport()
136     {
137         return ProjectUtils.validateIfIssueManagementComplete( project, "Trac", "Trac Report", getLog() );
138     }
139 
140     public void executeReport( Locale locale )
141         throws MavenReportException
142     {
143         // Validate parameters
144         List<Integer> columnIds = IssuesReportHelper.getColumnIds( columnNames, TRAC_COLUMNS, DEPRECATED_TRAC_COLUMNS,
145                                                                    getLog() );
146         if ( columnIds.size() == 0 )
147         {
148             // This can happen if the user has configured column names and they are all invalid
149             throw new MavenReportException(
150                 "maven-changes-plugin: None of the configured columnNames '" + columnNames + "' are valid." );
151         }
152 
153         try
154         {
155             // Download issues
156             TracDownloader issueDownloader = new TracDownloader();
157             configureIssueDownloader( issueDownloader );
158 
159             List issueList = issueDownloader.getIssueList();
160 
161             // Generate the report
162             IssuesReportGenerator report = new IssuesReportGenerator( IssuesReportHelper.toIntArray( columnIds ) );
163 
164             if ( issueList.isEmpty() )
165             {
166                 report.doGenerateEmptyReport( getBundle( locale ), getSink() );
167                 getLog().warn( "No ticket has matched." );
168             }
169             else
170             {
171                 report.doGenerateReport( getBundle( locale ), getSink(), issueList );
172             }
173         }
174         catch ( MalformedURLException e )
175         {
176             // Rethrow this error so that the build fails
177             throw new MavenReportException( "The Trac URL is incorrect." );
178         }
179         catch ( XmlRpcException e )
180         {
181             // Rethrow this error so that the build fails
182             throw new MavenReportException( "XmlRpc Error.", e );
183         }
184         catch ( Exception e )
185         {
186             e.printStackTrace();
187         }
188     }
189 
190     public String getDescription( Locale locale )
191     {
192         return getBundle( locale ).getString( "report.issues.description" );
193     }
194 
195     public String getName( Locale locale )
196     {
197         return getBundle( locale ).getString( "report.issues.name" );
198     }
199 
200     public String getOutputName()
201     {
202         return "trac-report";
203     }
204 
205     /* --------------------------------------------------------------------- */
206     /* Private methods                                                       */
207     /* --------------------------------------------------------------------- */
208 
209     private ResourceBundle getBundle( Locale locale )
210     {
211         return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() );
212     }
213 
214     private void configureIssueDownloader( TracDownloader issueDownloader )
215     {
216         issueDownloader.setProject( project );
217 
218         issueDownloader.setQuery( query );
219 
220         issueDownloader.setTracPassword( tracPassword );
221 
222         issueDownloader.setTracUser( tracUser );
223     }
224 }