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 org.apache.maven.plugin.changes.AbstractChangesReport;
23  import org.apache.maven.plugin.changes.ProjectUtils;
24  import org.apache.maven.plugin.issues.Issue;
25  import org.apache.maven.plugin.issues.IssueUtils;
26  import org.apache.maven.plugin.issues.IssuesReportGenerator;
27  import org.apache.maven.plugin.issues.IssuesReportHelper;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.reporting.MavenReportException;
31  import org.apache.maven.settings.Settings;
32  
33  import java.net.MalformedURLException;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Locale;
37  import java.util.Map;
38  import java.util.ResourceBundle;
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         return ProjectUtils.validateIfIssueManagementComplete( project, "GitHub", "GitHub Report", getLog() );
155     }
156 
157     @Override
158     protected void executeReport( Locale locale )
159         throws MavenReportException
160     {
161 
162         // Validate parameters
163         List<Integer> columnIds = IssuesReportHelper.getColumnIds( columnNames, githubColumns );
164         if ( columnIds.size() == 0 )
165         {
166             // This can happen if the user has configured column names and they are all invalid
167             throw new MavenReportException( "maven-changes-plugin: None of the configured columnNames '" + columnNames
168                 + "' are valid." );
169         }
170 
171         try
172         {
173             // Download issues
174             GitHubDownloader issueDownloader =
175                 new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, onlyMilestoneIssues );
176 
177             issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() );
178 
179             List<Issue> issueList = issueDownloader.getIssueList();
180 
181             if ( onlyCurrentVersion )
182             {
183                 issueList = IssueUtils.getIssuesForVersion( issueList, project.getVersion() );
184                 getLog().info( "The GitHub Report will contain issues only for the current version." );
185             }
186 
187             // Generate the report
188             IssuesReportGenerator report = new IssuesReportGenerator( IssuesReportHelper.toIntArray( columnIds ) );
189 
190             if ( issueList.isEmpty() )
191             {
192                 report.doGenerateEmptyReport( getBundle( locale ), getSink() );
193                 getLog().warn( "No issue was matched." );
194             }
195             else
196             {
197                 report.doGenerateReport( getBundle( locale ), getSink(), issueList );
198             }
199         }
200         catch ( MalformedURLException e )
201         {
202             // Rethrow this error so that the build fails
203             throw new MavenReportException( "The Github URL is incorrect." );
204         }
205         catch ( Exception e )
206         {
207             throw new MavenReportException( e.getMessage(), e );
208         }
209     }
210 
211     /* --------------------------------------------------------------------- */
212     /* Private methods */
213     /* --------------------------------------------------------------------- */
214 
215     private ResourceBundle getBundle( Locale locale )
216     {
217         return ResourceBundle.getBundle( "github-report", locale, this.getClass().getClassLoader() );
218     }
219 
220 }