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", new Integer( IssuesReportHelper.COLUMN_ASSIGNEE ) );
59          GITHUB_COLUMNS.put( "Created", new Integer( IssuesReportHelper.COLUMN_CREATED ) );
60          GITHUB_COLUMNS.put( "Fix Version", new Integer( IssuesReportHelper.COLUMN_FIX_VERSION ) );
61          GITHUB_COLUMNS.put( "Id", new Integer( IssuesReportHelper.COLUMN_ID ) );
62          GITHUB_COLUMNS.put( "Reporter", new Integer( IssuesReportHelper.COLUMN_REPORTER ) );
63          GITHUB_COLUMNS.put( "Status", new Integer( IssuesReportHelper.COLUMN_STATUS ) );
64          GITHUB_COLUMNS.put( "Summary", new Integer( IssuesReportHelper.COLUMN_SUMMARY ) );
65          GITHUB_COLUMNS.put( "Type", new Integer( IssuesReportHelper.COLUMN_TYPE ) );
66          GITHUB_COLUMNS.put( "Updated", new Integer( 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         return ProjectUtils.validateIfIssueManagementComplete( project, "GitHub", "GitHub Report", getLog() );
140     }
141 
142     @Override
143     protected void executeReport( Locale locale )
144         throws MavenReportException
145     {
146 
147         // Validate parameters
148         List<Integer> columnIds = IssuesReportHelper.getColumnIds( columnNames, GITHUB_COLUMNS );
149         if ( columnIds.size() == 0 )
150         {
151             // This can happen if the user has configured column names and they are all invalid
152             throw new MavenReportException(
153                 "maven-changes-plugin: None of the configured columnNames '" + columnNames + "' are valid." );
154         }
155 
156         try
157         {
158             // Download issues
159             GitHubDownloader issueDownloader =
160                 new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, onlyMilestoneIssues );
161 
162             List<Issue> issueList = issueDownloader.getIssueList();
163 
164             if ( onlyCurrentVersion )
165             {
166                 issueList = IssueUtils.getIssuesForVersion( issueList, project.getVersion() );
167                 getLog().info( "The GitHub Report will contain issues only for the current version." );
168             }
169 
170             // Generate the report
171             IssuesReportGenerator report = new IssuesReportGenerator( IssuesReportHelper.toIntArray( columnIds ) );
172 
173             if ( issueList.isEmpty() )
174             {
175                 report.doGenerateEmptyReport( getBundle( locale ), getSink() );
176                 getLog().warn( "No issue was matched." );
177             }
178             else
179             {
180                 report.doGenerateReport( getBundle( locale ), getSink(), issueList );
181             }
182         }
183         catch ( MalformedURLException e )
184         {
185             // Rethrow this error so that the build fails
186             throw new MavenReportException( "The Github URL is incorrect." );
187         }
188         catch ( Exception e )
189         {
190             e.printStackTrace();
191         }
192     }
193 
194     /* --------------------------------------------------------------------- */
195     /* Private methods                                                       */
196     /* --------------------------------------------------------------------- */
197 
198     private ResourceBundle getBundle( Locale locale )
199     {
200         return ResourceBundle.getBundle( "github-report", locale, this.getClass().getClassLoader() );
201     }
202 
203 }