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