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.issues.Issue;
23  import org.apache.maven.project.MavenProject;
24  import org.eclipse.egit.github.core.Label;
25  import org.eclipse.egit.github.core.client.GitHubClient;
26  import org.eclipse.egit.github.core.service.IssueService;
27  
28  import java.io.IOException;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * @since 2.8
38   */
39  public class GitHubDownloader
40  {
41  
42      /**
43       * The github client.
44       */
45      private GitHubClient client;
46  
47      /**
48       * A boolean to indicate if we should include open issues as well
49       */
50      private boolean includeOpenIssues;
51  
52      /**
53       * A boolean to indicate if we should only include issues with milestones
54       */
55      private boolean onlyMilestoneIssues;
56  
57      /**
58       * The owner/organization of the github repo.
59       */
60      private String githubOwner;
61  
62      /**
63       * The name of the github repo.
64       */
65      private String githubRepo;
66  
67      /**
68       * The url to the github repo's issue management
69       */
70      private String githubIssueURL;
71  
72      public GitHubDownloader( MavenProject project, String githubScheme, int githubPort, boolean includeOpenIssues,
73                               boolean onlyMilestoneIssues )
74          throws MalformedURLException
75      {
76          this.includeOpenIssues = includeOpenIssues;
77          this.onlyMilestoneIssues = onlyMilestoneIssues;
78  
79          URL githubURL = new URL( project.getIssueManagement().getUrl() );
80  
81          // The githubclient prefers to connect to 'github.com' using the api domain, unlike github enterprise
82          // which can connect fine using its domain, so for github.com use empty constructor
83          if ( githubURL.getHost().equalsIgnoreCase( "github.com" ) )
84          {
85              this.client = new GitHubClient();
86          }
87          else
88          {
89              this.client = new GitHubClient( githubURL.getHost(), githubPort, githubScheme );
90          }
91  
92          this.githubIssueURL = project.getIssueManagement().getUrl();
93          if ( !this.githubIssueURL.endsWith( "/" ) )
94          {
95              this.githubIssueURL = this.githubIssueURL + "/";
96          }
97  
98          String urlPath = githubURL.getPath();
99          if ( urlPath.startsWith( "/" ) )
100         {
101             urlPath = urlPath.substring( 1 );
102         }
103 
104         if ( urlPath.endsWith( "/" ) )
105         {
106             urlPath = urlPath.substring( 0, urlPath.length() - 2 );
107         }
108 
109         String[] urlPathParts = urlPath.split( "/" );
110 
111         if ( urlPathParts.length != 3 )
112         {
113             throw new MalformedURLException(
114                 "GitHub issue management URL must look like, [GITHUB_DOMAIN]/[OWNER]/[REPO]/issues" );
115         }
116 
117         this.githubOwner = urlPathParts[0];
118         this.githubRepo = urlPathParts[1];
119     }
120 
121     private Issue createIssue( org.eclipse.egit.github.core.Issue githubIssue )
122     {
123         Issue issue = new Issue();
124 
125         issue.setId( String.valueOf( githubIssue.getNumber() ) );
126 
127         issue.setLink( this.githubIssueURL + githubIssue.getNumber() );
128 
129         issue.setCreated( githubIssue.getCreatedAt() );
130 
131         issue.setUpdated( githubIssue.getUpdatedAt() );
132 
133         if ( githubIssue.getAssignee() != null )
134         {
135             if ( githubIssue.getAssignee().getName() != null )
136             {
137                 issue.setAssignee( githubIssue.getAssignee().getName() );
138             }
139             else
140             {
141                 issue.setAssignee( githubIssue.getAssignee().getLogin() );
142             }
143         }
144 
145         issue.setTitle( githubIssue.getTitle() );
146 
147         issue.setSummary( githubIssue.getTitle() );
148 
149         if ( githubIssue.getMilestone() != null )
150         {
151             issue.addFixVersion( githubIssue.getMilestone().getTitle() );
152         }
153 
154         issue.setReporter( githubIssue.getUser().getLogin() );
155 
156         if ( githubIssue.getClosedAt() != null )
157         {
158             issue.setStatus( "closed" );
159         }
160         else
161         {
162             issue.setStatus( "open" );
163         }
164 
165         List<Label> labels = githubIssue.getLabels();
166         if ( labels != null && !labels.isEmpty() )
167         {
168             issue.setType( labels.get( 0 ).getName() );
169         }
170 
171         return issue;
172     }
173 
174     public List<Issue> getIssueList()
175         throws IOException
176     {
177         List<Issue> issueList = new ArrayList<Issue>();
178 
179         IssueService service = new IssueService( client );
180         Map<String, String> issueFilter = new HashMap<String, String>();
181 
182         if ( includeOpenIssues )
183         {
184             // Adding open issues
185 
186             for ( org.eclipse.egit.github.core.Issue issue : service.getIssues( githubOwner, githubRepo, issueFilter ) )
187             {
188                 if ( !onlyMilestoneIssues || issue.getMilestone() != null )
189                 {
190                     issueList.add( createIssue( issue ) );
191                 }
192             }
193         }
194 
195         // Adding closed issues
196 
197         issueFilter.put( "state", "closed" );
198 
199         for ( org.eclipse.egit.github.core.Issue issue : service.getIssues( githubOwner, githubRepo, issueFilter ) )
200         {
201             if ( !onlyMilestoneIssues || issue.getMilestone() != null )
202             {
203                 issueList.add( createIssue( issue ) );
204             }
205         }
206 
207         return issueList;
208     }
209 
210 }