1 package org.apache.maven.plugin.github;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39 public class GitHubDownloader
40 {
41
42
43
44
45 private GitHubClient client;
46
47
48
49
50 private boolean includeOpenIssues;
51
52
53
54
55 private boolean onlyMilestoneIssues;
56
57
58
59
60 private String githubOwner;
61
62
63
64
65 private String githubRepo;
66
67
68
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
82
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
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
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 }