Coverage Report - org.apache.maven.plugin.github.GitHubDownloader
 
Classes in this File Line Coverage Branch Coverage Complexity
GitHubDownloader
0%
0/54
0%
0/36
7,333
 
 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  0
     {
 76  0
         this.includeOpenIssues = includeOpenIssues;
 77  0
         this.onlyMilestoneIssues = onlyMilestoneIssues;
 78  
 
 79  0
         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  0
         if ( githubURL.getHost().equalsIgnoreCase( "github.com" ) )
 84  
         {
 85  0
             this.client = new GitHubClient();
 86  
         }
 87  
         else
 88  
         {
 89  0
             this.client = new GitHubClient( githubURL.getHost(), githubPort, githubScheme );
 90  
         }
 91  
 
 92  0
         this.githubIssueURL = project.getIssueManagement().getUrl();
 93  0
         if ( !this.githubIssueURL.endsWith( "/" ) )
 94  
         {
 95  0
             this.githubIssueURL = this.githubIssueURL + "/";
 96  
         }
 97  
 
 98  0
         String urlPath = githubURL.getPath();
 99  0
         if ( urlPath.startsWith( "/" ) )
 100  
         {
 101  0
             urlPath = urlPath.substring( 1 );
 102  
         }
 103  
 
 104  0
         if ( urlPath.endsWith( "/" ) )
 105  
         {
 106  0
             urlPath = urlPath.substring( 0, urlPath.length() - 2 );
 107  
         }
 108  
 
 109  0
         String[] urlPathParts = urlPath.split( "/" );
 110  
 
 111  0
         if ( urlPathParts.length != 3 )
 112  
         {
 113  0
             throw new MalformedURLException(
 114  
                 "GitHub issue management URL must look like, [GITHUB_DOMAIN]/[OWNER]/[REPO]/issues" );
 115  
         }
 116  
 
 117  0
         this.githubOwner = urlPathParts[0];
 118  0
         this.githubRepo = urlPathParts[1];
 119  0
     }
 120  
 
 121  
     private Issue createIssue( org.eclipse.egit.github.core.Issue githubIssue )
 122  
     {
 123  0
         Issue issue = new Issue();
 124  
 
 125  0
         issue.setId( String.valueOf( githubIssue.getNumber() ) );
 126  
 
 127  0
         issue.setLink( this.githubIssueURL + githubIssue.getNumber() );
 128  
 
 129  0
         issue.setCreated( githubIssue.getCreatedAt() );
 130  
 
 131  0
         issue.setUpdated( githubIssue.getUpdatedAt() );
 132  
 
 133  0
         if ( githubIssue.getAssignee() != null )
 134  
         {
 135  0
             if ( githubIssue.getAssignee().getName() != null )
 136  
             {
 137  0
                 issue.setAssignee( githubIssue.getAssignee().getName() );
 138  
             }
 139  
             else
 140  
             {
 141  0
                 issue.setAssignee( githubIssue.getAssignee().getLogin() );
 142  
             }
 143  
         }
 144  
 
 145  0
         issue.setTitle( githubIssue.getTitle() );
 146  
 
 147  0
         issue.setSummary( githubIssue.getTitle() );
 148  
 
 149  0
         if ( githubIssue.getMilestone() != null )
 150  
         {
 151  0
             issue.addFixVersion( githubIssue.getMilestone().getTitle() );
 152  
         }
 153  
 
 154  0
         issue.setReporter( githubIssue.getUser().getLogin() );
 155  
 
 156  0
         if ( githubIssue.getClosedAt() != null )
 157  
         {
 158  0
             issue.setStatus( "closed" );
 159  
         }
 160  
         else
 161  
         {
 162  0
             issue.setStatus( "open" );
 163  
         }
 164  
 
 165  0
         List<Label> labels = githubIssue.getLabels();
 166  0
         if ( labels != null && !labels.isEmpty() )
 167  
         {
 168  0
             issue.setType( labels.get( 0 ).getName() );
 169  
         }
 170  
 
 171  0
         return issue;
 172  
     }
 173  
 
 174  
     public List<Issue> getIssueList()
 175  
         throws IOException
 176  
     {
 177  0
         List<Issue> issueList = new ArrayList<Issue>();
 178  
 
 179  0
         IssueService service = new IssueService( client );
 180  0
         Map<String, String> issueFilter = new HashMap<String, String>();
 181  
 
 182  0
         if ( includeOpenIssues )
 183  
         {
 184  
             // Adding open issues
 185  
 
 186  0
             for ( org.eclipse.egit.github.core.Issue issue : service.getIssues( githubOwner, githubRepo, issueFilter ) )
 187  
             {
 188  0
                 if ( !onlyMilestoneIssues || issue.getMilestone() != null )
 189  
                 {
 190  0
                     issueList.add( createIssue( issue ) );
 191  
                 }
 192  
             }
 193  
         }
 194  
 
 195  
         // Adding closed issues
 196  
 
 197  0
         issueFilter.put( "state", "closed" );
 198  
 
 199  0
         for ( org.eclipse.egit.github.core.Issue issue : service.getIssues( githubOwner, githubRepo, issueFilter ) )
 200  
         {
 201  0
             if ( !onlyMilestoneIssues || issue.getMilestone() != null )
 202  
             {
 203  0
                 issueList.add( createIssue( issue ) );
 204  
             }
 205  
         }
 206  
 
 207  0
         return issueList;
 208  
     }
 209  
 
 210  
 }