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