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 | 0 | { |
76 | 0 | this.includeOpenIssues = includeOpenIssues; |
77 | 0 | this.onlyMilestoneIssues = onlyMilestoneIssues; |
78 | |
|
79 | 0 | URL githubURL = new URL( project.getIssueManagement().getUrl() ); |
80 | |
|
81 | |
|
82 | |
|
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 | |
|
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 | |
|
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 | |
} |