View Javadoc
1   package org.apache.maven.plugin.github;
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.IssueUtils;
33  import org.apache.maven.plugin.issues.IssuesReportGenerator;
34  import org.apache.maven.plugin.issues.IssuesReportHelper;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.reporting.MavenReportException;
38  import org.apache.maven.settings.Settings;
39  
40  /**
41   * Goal which downloads issues from GitHub and generates a report.
42   *
43   * @author Bryan Baugher
44   * @since 2.8
45   */
46  @Mojo( name = "github-report", threadSafe = true )
47  public class GitHubMojo
48      extends AbstractChangesReport
49  {
50  
51      /**
52       * Valid Github columns.
53       */
54      private static Map<String, Integer> githubColumns = new HashMap<String, Integer>();
55  
56      static
57      {
58          githubColumns.put( "Assignee", IssuesReportHelper.COLUMN_ASSIGNEE );
59          githubColumns.put( "Created", IssuesReportHelper.COLUMN_CREATED );
60          githubColumns.put( "Fix Version", IssuesReportHelper.COLUMN_FIX_VERSION );
61          githubColumns.put( "Id", IssuesReportHelper.COLUMN_ID );
62          githubColumns.put( "Reporter", IssuesReportHelper.COLUMN_REPORTER );
63          githubColumns.put( "Status", IssuesReportHelper.COLUMN_STATUS );
64          githubColumns.put( "Summary", IssuesReportHelper.COLUMN_SUMMARY );
65          githubColumns.put( "Type", IssuesReportHelper.COLUMN_TYPE );
66          githubColumns.put( "Updated", IssuesReportHelper.COLUMN_UPDATED );
67      }
68  
69      /**
70       * Sets the column names that you want to show in the report. The columns will appear in the report in the same
71       * order as you specify them here. Multiple values can be separated by commas.
72       * <p>
73       * Valid columns are: <code>Assignee</code>, <code>Created</code>, <code>Fix Version</code>, <code>Id</code>,
74       * <code>Reporter</code>, <code>Status</code>, <code>Summary</code>, <code>Type</code> and <code>Updated</code>.
75       * </p>
76       */
77      @Parameter( defaultValue = "Id,Type,Summary,Assignee,Reporter,Status,Created,Updated,Fix Version" )
78      private String columnNames;
79  
80      /**
81       * The scheme of your github api domain. Only use if using github enterprise.
82       */
83      @Parameter( defaultValue = "http" )
84      private String githubAPIScheme;
85  
86      /**
87       * The port of your github api domain. Only use if using github enterprise.
88       */
89      @Parameter( defaultValue = "80" )
90      private int githubAPIPort;
91  
92      /**
93       * The settings.xml server id to be used to authenticate into github api domain. Only use if using github
94       * enterprise.
95       */
96      @Parameter( defaultValue = "github" )
97      private String githubAPIServerId;
98  
99      /**
100      * Settings XML configuration.
101      */
102     @Parameter( defaultValue = "${settings}", readonly = true, required = true )
103     private Settings settings;
104 
105     /**
106      * Boolean which says if we should include open issues in the report.
107      */
108     @Parameter( defaultValue = "true" )
109     private boolean includeOpenIssues;
110 
111     /**
112      * Boolean which says if we should include only issues with milestones.
113      */
114     @Parameter( defaultValue = "true" )
115     private boolean onlyMilestoneIssues;
116 
117     /**
118      * If you only want to show issues for the current version in the report. The current version being used is
119      * <code>${project.version}</code> minus any "-SNAPSHOT" suffix.
120      */
121     @Parameter( defaultValue = "false" )
122     private boolean onlyCurrentVersion;
123 
124     public String getOutputName()
125     {
126         return "github-report";
127     }
128 
129     public String getName( Locale locale )
130     {
131         return getBundle( locale ).getString( "report.issues.name" );
132     }
133 
134     public String getDescription( Locale locale )
135     {
136         return getBundle( locale ).getString( "report.issues.description" );
137     }
138 
139     /* --------------------------------------------------------------------- */
140     /* Public methods */
141     /* --------------------------------------------------------------------- */
142 
143     /**
144      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
145      */
146     public boolean canGenerateReport()
147     {
148         // Run only at the execution root
149         if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
150         {
151             getLog().info( "Skipping the GitHub Report in this project because it's not the Execution Root" );
152             return false;
153         }
154         String message = ProjectUtils.validateIssueManagement( project, "GitHub", "GitHub Report" );
155         if ( message != null )
156         {
157             getLog().warn( message );
158         }
159         return message == null;
160     }
161 
162     @Override
163     protected void executeReport( Locale locale )
164         throws MavenReportException
165     {
166 
167         // Validate parameters
168         List<Integer> columnIds = IssuesReportHelper.getColumnIds( columnNames, githubColumns );
169         if ( columnIds.size() == 0 )
170         {
171             // This can happen if the user has configured column names and they are all invalid
172             throw new MavenReportException( "maven-changes-plugin: None of the configured columnNames '" + columnNames
173                 + "' are valid." );
174         }
175 
176         try
177         {
178             // Download issues
179             GitHubDownloader issueDownloader =
180                 new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, onlyMilestoneIssues );
181 
182             issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() );
183 
184             List<Issue> issueList = issueDownloader.getIssueList();
185 
186             if ( onlyCurrentVersion )
187             {
188                 issueList = IssueUtils.getIssuesForVersion( issueList, project.getVersion() );
189                 getLog().info( "The GitHub Report will contain issues only for the current version." );
190             }
191 
192             // Generate the report
193             IssuesReportGenerator report = new IssuesReportGenerator( IssuesReportHelper.toIntArray( columnIds ) );
194 
195             if ( issueList.isEmpty() )
196             {
197                 report.doGenerateEmptyReport( getBundle( locale ), getSink() );
198                 getLog().warn( "No issue was matched." );
199             }
200             else
201             {
202                 report.doGenerateReport( getBundle( locale ), getSink(), issueList );
203             }
204         }
205         catch ( MalformedURLException e )
206         {
207             // Rethrow this error so that the build fails
208             throw new MavenReportException( "The Github URL is incorrect." );
209         }
210         catch ( Exception e )
211         {
212             throw new MavenReportException( e.getMessage(), e );
213         }
214     }
215 
216     /* --------------------------------------------------------------------- */
217     /* Private methods */
218     /* --------------------------------------------------------------------- */
219 
220     private ResourceBundle getBundle( Locale locale )
221     {
222         return ResourceBundle.getBundle( "github-report", locale, this.getClass().getClassLoader() );
223     }
224 
225 }