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