-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Github.java
322 lines (274 loc) · 11.8 KB
/
Github.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.sdk.github;
import org.apache.tika.Tika;
import org.apache.tika.mime.MediaType;
import org.jreleaser.bundle.RB;
import org.jreleaser.logging.JReleaserLogger;
import org.jreleaser.model.JReleaserVersion;
import org.jreleaser.model.spi.release.Asset;
import org.kohsuke.github.GHAsset;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHDiscussion;
import org.kohsuke.github.GHException;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIOException;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHMilestone;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRelease;
import org.kohsuke.github.GHReleaseBuilder;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.HttpConnector;
import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.extras.ImpatientHttpConnector;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.StreamSupport;
import static org.jreleaser.sdk.git.GitSdk.REFS_TAGS;
import static org.jreleaser.util.StringUtils.isBlank;
/**
* @author Andres Almiray
* @since 0.1.0
*/
class Github {
static final String ENDPOINT = "https://api.github.com";
private final Tika tika = new Tika();
private final JReleaserLogger logger;
private final GitHub github;
Github(JReleaserLogger logger,
String token,
int connectTimeout,
int readTimeout) throws IOException {
this(logger, ENDPOINT, token, connectTimeout, readTimeout);
}
Github(JReleaserLogger logger,
String endpoint,
String token,
int connectTimeout,
int readTimeout) throws IOException {
this.logger = logger;
if (isBlank(endpoint)) {
endpoint = ENDPOINT;
}
github = new GitHubBuilder()
.withConnector(new JReleaserHttpConnector(connectTimeout, readTimeout))
.withEndpoint(endpoint)
.withOAuthToken(token)
.build();
}
PagedIterable<GHRelease> listReleases(String owner, String repo) throws IOException {
logger.debug(RB.$("git.list.releases"), owner, repo);
return github.getRepository(owner + "/" + repo)
.listReleases();
}
Map<String, GHBranch> listBranches(String owner, String repo) throws IOException {
logger.debug(RB.$("git.list.branches"), owner, repo);
return github.getRepository(owner + "/" + repo)
.getBranches();
}
Map<String, GHAsset> listAssets(String owner, String repo, GHRelease release) throws IOException {
logger.debug(RB.$("git.list.assets.github"), owner, repo, release.getId());
Map<String, GHAsset> assets = new LinkedHashMap<>();
for (GHAsset asset : release.listAssets()) {
assets.put(asset.getName(), asset);
}
return assets;
}
GHRepository findRepository(String owner, String repo) throws IOException {
logger.debug(RB.$("git.repository.lookup"), owner, repo);
try {
return github.getRepository(owner + "/" + repo);
} catch (GHFileNotFoundException e) {
// OK, this means the repository does not exist
return null;
}
}
GHRepository createRepository(String owner, String repo) throws IOException {
logger.debug(RB.$("git.repository.create"), owner, repo);
GHOrganization organization = resolveOrganization(owner);
if (null != organization) {
return organization.createRepository(repo)
.create();
}
return github.createRepository(repo)
.create();
}
Optional<GHMilestone> findMilestoneByName(String owner, String repo, String milestoneName) throws IOException {
logger.debug(RB.$("git.milestone.lookup"), milestoneName, owner, repo);
GHRepository repository = findRepository(owner, repo);
PagedIterable<GHMilestone> milestones = repository.listMilestones(GHIssueState.OPEN);
return StreamSupport.stream(milestones.spliterator(), false)
.filter(m -> milestoneName.equals(m.getTitle()))
.findFirst();
}
Optional<GHMilestone> findClosedMilestoneByName(String owner, String repo, String milestoneName) throws IOException {
logger.debug(RB.$("git.milestone.lookup.closed"), milestoneName, owner, repo);
GHRepository repository = findRepository(owner, repo);
PagedIterable<GHMilestone> milestones = repository.listMilestones(GHIssueState.CLOSED);
return StreamSupport.stream(milestones.spliterator(), false)
.filter(m -> milestoneName.equals(m.getTitle()))
.findFirst();
}
void closeMilestone(String owner, String repo, GHMilestone milestone) throws IOException {
logger.debug(RB.$("git.milestone.close"), milestone.getTitle(), owner, repo);
milestone.close();
}
GHRelease findReleaseByTag(String repo, String tagName) throws IOException {
logger.debug(RB.$("git.fetch.release.on.tag"), repo, tagName);
return github.getRepository(repo)
.getReleaseByTagName(tagName);
}
void deleteTag(String repo, String tagName) throws IOException {
logger.debug(RB.$("git.delete.tag.from.repository"), tagName, repo);
github.getRepository(repo)
.getRef(REFS_TAGS + tagName)
.delete();
}
GHReleaseBuilder createRelease(String repo, String tagName) throws IOException {
logger.debug(RB.$("git.create.release.repository"), repo, tagName);
return github.getRepository(repo)
.createRelease(tagName);
}
void uploadAssets(GHRelease release, List<Asset> assets) throws IOException {
for (Asset asset : assets) {
if (0 == Files.size(asset.getPath()) || !Files.exists(asset.getPath())) {
// do not upload empty or non existent files
continue;
}
logger.info(" " + RB.$("git.upload.asset"), asset.getFilename());
try {
GHAsset ghasset = release.uploadAsset(asset.getPath().toFile(), MediaType.parse(tika.detect(asset.getPath())).toString());
if (!"uploaded".equalsIgnoreCase(ghasset.getState())) {
logger.warn(" " + RB.$("git.upload.asset.failure"), asset.getFilename());
}
} catch (GHIOException ghioe) {
logger.trace(ghioe);
if ("Stream Closed".equals(ghioe.getMessage())) {
logger.warn(" " + RB.$("git.upload.asset.stream.closed"), asset.getFilename());
}
}
}
}
void updateAssets(GHRelease release, List<Asset> assets, Map<String, GHAsset> existingAssets) throws IOException {
for (Asset asset : assets) {
if (0 == Files.size(asset.getPath()) || !Files.exists(asset.getPath())) {
// do not upload empty or non existent files
continue;
}
logger.debug(" " + RB.$("git.delete.asset"), asset.getFilename());
try {
existingAssets.get(asset.getFilename()).delete();
} catch (IOException e) {
logger.error(" " + RB.$("git.delete.asset.failure"), asset.getFilename());
throw e;
}
logger.info(" " + RB.$("git.update.asset"), asset.getFilename());
GHAsset ghasset = release.uploadAsset(asset.getPath().toFile(), MediaType.parse(tika.detect(asset.getPath())).toString());
if (!"uploaded".equalsIgnoreCase(ghasset.getState())) {
logger.warn(" " + RB.$("git.update.asset.failure"), asset.getFilename());
}
}
}
Optional<GHDiscussion> findDiscussion(String organization, String team, String title) throws IOException {
GHTeam ghTeam = resolveTeam(organization, team);
try {
return StreamSupport.stream(ghTeam.listDiscussions().spliterator(), false)
.filter(d -> title.equals(d.getTitle()))
.findFirst();
} catch (GHException ghe) {
if (ghe.getCause() instanceof GHFileNotFoundException) {
// OK
return Optional.empty();
}
throw ghe;
}
}
GHDiscussion createDiscussion(String organization, String team, String title, String message) throws IOException {
GHTeam ghTeam = resolveTeam(organization, team);
return ghTeam.createDiscussion(title)
.body(message)
.done();
}
GHLabel getOrCreateLabel(GHRepository repository, String labelName, String color, String description) throws IOException {
logger.debug(RB.$("git.label.fetch", labelName));
try {
return repository.getLabel(labelName);
} catch (FileNotFoundException ok) {
logger.debug(RB.$("git.label.create", labelName));
return repository.createLabel(labelName, color, description);
}
}
Optional<GHIssue> findIssue(GHRepository repository, int issueNumber) throws IOException {
logger.debug(RB.$("git.issue.fetch", issueNumber));
try {
return Optional.of(repository.getIssue(issueNumber));
} catch (FileNotFoundException ok) {
return Optional.empty();
}
}
private GHOrganization resolveOrganization(String name) throws IOException {
try {
return github.getOrganization(name);
} catch (GHFileNotFoundException ignored) {
// OK, means the organization does not exist
return null;
}
}
private GHTeam resolveTeam(String organization, String team) throws IOException {
GHOrganization ghOrganization = null;
try {
ghOrganization = github.getOrganization(organization);
} catch (GHFileNotFoundException e) {
throw new IllegalStateException(RB.$("ERROR_git_organization_not_exist", organization));
}
GHTeam ghTeam = null;
try {
ghTeam = ghOrganization.getTeamByName(team);
} catch (IOException e) {
throw new IllegalStateException(RB.$("ERROR_git_team_not_exist"));
}
if (null == ghTeam) {
throw new IllegalStateException(RB.$("ERROR_git_team_not_exist"));
}
return ghTeam;
}
private static class JReleaserHttpConnector extends ImpatientHttpConnector {
public JReleaserHttpConnector(int connectTimeout, int readTimeout) {
super(HttpConnector.DEFAULT, connectTimeout * 1000, readTimeout * 1000);
}
@Override
public HttpURLConnection connect(URL url) throws IOException {
HttpURLConnection connection = super.connect(url);
connection.addRequestProperty("User-Agent", "JReleaser/" + JReleaserVersion.getPlainVersion());
return connection;
}
}
}