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 1765156 2016-10-16 13:54:35Z gboue $
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         String message = ProjectUtils.validateIssueManagement( project, "Trac", "Trac Report" );
139         if ( message != null )
140         {
141             getLog().warn( message );
142         }
143         return message == null;
144     }
145 
146     public void executeReport( Locale locale )
147         throws MavenReportException
148     {
149         // Validate parameters
150         List<Integer> columnIds =
151             IssuesReportHelper.getColumnIds( columnNames, TRAC_COLUMNS, DEPRECATED_TRAC_COLUMNS, getLog() );
152         if ( columnIds.size() == 0 )
153         {
154             // This can happen if the user has configured column names and they are all invalid
155             throw new MavenReportException( "maven-changes-plugin: None of the configured columnNames '" + columnNames
156                 + "' are valid." );
157         }
158 
159         try
160         {
161             // Download issues
162             TracDownloader issueDownloader = new TracDownloader();
163             configureIssueDownloader( issueDownloader );
164 
165             List<Issue> issueList = issueDownloader.getIssueList();
166 
167             // Generate the report
168             IssuesReportGenerator report = new IssuesReportGenerator( IssuesReportHelper.toIntArray( columnIds ) );
169 
170             if ( issueList.isEmpty() )
171             {
172                 report.doGenerateEmptyReport( getBundle( locale ), getSink() );
173                 getLog().warn( "No ticket has matched." );
174             }
175             else
176             {
177                 report.doGenerateReport( getBundle( locale ), getSink(), issueList );
178             }
179         }
180         catch ( MalformedURLException e )
181         {
182             // Rethrow this error so that the build fails
183             throw new MavenReportException( "The Trac URL is incorrect." );
184         }
185         catch ( XmlRpcException e )
186         {
187             // Rethrow this error so that the build fails
188             throw new MavenReportException( "XmlRpc Error.", e );
189         }
190         catch ( Exception e )
191         {
192             e.printStackTrace();
193         }
194     }
195 
196     public String getDescription( Locale locale )
197     {
198         return getBundle( locale ).getString( "report.issues.description" );
199     }
200 
201     public String getName( Locale locale )
202     {
203         return getBundle( locale ).getString( "report.issues.name" );
204     }
205 
206     public String getOutputName()
207     {
208         return "trac-report";
209     }
210 
211     /* --------------------------------------------------------------------- */
212     /* Private methods */
213     /* --------------------------------------------------------------------- */
214 
215     private ResourceBundle getBundle( Locale locale )
216     {
217         return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() );
218     }
219 
220     private void configureIssueDownloader( TracDownloader issueDownloader )
221     {
222         issueDownloader.setProject( project );
223 
224         issueDownloader.setQuery( query );
225 
226         issueDownloader.setTracPassword( tracPassword );
227 
228         issueDownloader.setTracUser( tracUser );
229     }
230 }