-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHRelease.java
More file actions
342 lines (307 loc) · 7.61 KB
/
GHRelease.java
File metadata and controls
342 lines (307 loc) · 7.61 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import static java.lang.String.format;
// TODO: Auto-generated Javadoc
/**
* Release in a github repository.
*
* @see GHRepository#listReleases() () GHRepository#listReleases()
* @see GHRepository#createRelease(String) GHRepository#createRelease(String)
*/
public class GHRelease extends GHObject {
/**
* Wrap.
*
* @param releases
* the releases
* @param owner
* the owner
* @return the GH release[]
*/
static GHRelease[] wrap(GHRelease[] releases, GHRepository owner) {
for (GHRelease release : releases) {
release.wrap(owner);
}
return releases;
}
private List<GHAsset> assets;
private String assetsUrl;
private GHUser author;
private String body;
private String bodyHtml;
private String bodyText;
private String discussionUrl;
private boolean draft;
private String htmlUrl;
private int mentionsCount;
private String name;
private boolean prerelease;
private String publishedAt;
private String tagName;
private String tarballUrl;
private String targetCommitish;
private String uploadUrl;
private String zipballUrl;
/** The owner. */
GHRepository owner;
/**
* Create default GHRelease instance
*/
public GHRelease() {
}
/**
* Deletes this release.
*
* @throws IOException
* the io exception
*/
public void delete() throws IOException {
root().createRequest().method("DELETE").withUrlPath(owner.getApiTailUrl("releases/" + getId())).send();
}
/**
* Get the cached assets.
*
* @return the assets
*/
public List<GHAsset> getAssets() {
return Collections.unmodifiableList(assets);
}
/**
* Gets assets url.
*
* @return the assets url
*/
public String getAssetsUrl() {
return assetsUrl;
}
/**
* Gets the author.
*
* @return the author
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHUser getAuthor() {
return author;
}
/**
* Gets body.
*
* @return the body
*/
public String getBody() {
return body;
}
/**
* Gets body html.
*
* @return the body html
*/
public String getBodyHtml() {
return bodyHtml;
}
/**
* Gets body text.
*
* @return the body text
*/
public String getBodyText() {
return bodyText;
}
/**
* Gets discussion url. Only present if a discussion relating to the release exists
*
* @return the discussion url
*/
public String getDiscussionUrl() {
return discussionUrl;
}
/**
* Gets the html url.
*
* @return the html url
*/
public URL getHtmlUrl() {
return GitHubClient.parseURL(htmlUrl);
}
public int getMentionsCount() {
return this.mentionsCount;
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets owner.
*
* @return the owner
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHRepository getOwner() {
return owner;
}
/**
* Gets published at.
*
* @return the published at
*/
public Instant getPublishedAt() {
return GitHubClient.parseInstant(publishedAt);
}
/**
* Gets published at.
*
* @return the published at
* @deprecated Use #getPublishedAt()
*/
@Deprecated
public Date getPublished_at() {
return Date.from(getPublishedAt());
}
/**
* Gets tag name.
*
* @return the tag name
*/
public String getTagName() {
return tagName;
}
/**
* Gets tarball url.
*
* @return the tarball url
*/
public String getTarballUrl() {
return tarballUrl;
}
/**
* Gets target commitish.
*
* @return the target commitish
*/
public String getTargetCommitish() {
return targetCommitish;
}
/**
* Gets upload url.
*
* @return the upload url
*/
public String getUploadUrl() {
return uploadUrl;
}
/**
* Gets zipball url.
*
* @return the zipball url
*/
public String getZipballUrl() {
return zipballUrl;
}
/**
* Is draft boolean.
*
* @return the boolean
*/
public boolean isDraft() {
return draft;
}
/**
* Is prerelease boolean.
*
* @return the boolean
*/
public boolean isPrerelease() {
return prerelease;
}
/**
* Re-fetch the assets of this release.
*
* @return the assets iterable
*/
public PagedIterable<GHAsset> listAssets() {
Requester builder = owner.root().createRequest();
return builder.withUrlPath(getApiTailUrl("assets")).toIterable(GHAsset[].class, item -> item.wrap(this));
}
/**
* Updates this release via a builder.
*
* @return the gh release updater
*/
public GHReleaseUpdater update() {
return new GHReleaseUpdater(this);
}
/**
* Because github relies on SNI (http://en.wikipedia.org/wiki/Server_Name_Indication) this method will only work on
* Java 7 or greater. Options for fixing this for earlier JVMs can be found here
* http://stackoverflow.com/questions/12361090/server-name-indication-sni-on-java but involve more complicated
* handling of the HTTP requests to github's API.
*
* @param file
* the file
* @param contentType
* the content type
* @return the gh asset
* @throws IOException
* the io exception
*/
public GHAsset uploadAsset(File file, String contentType) throws IOException {
FileInputStream s = new FileInputStream(file);
try {
return uploadAsset(file.getName(), s, contentType);
} finally {
s.close();
}
}
/**
* Upload asset gh asset.
*
* @param filename
* the filename
* @param stream
* the stream
* @param contentType
* the content type
* @return the gh asset
* @throws IOException
* the io exception
*/
public GHAsset uploadAsset(String filename, InputStream stream, String contentType) throws IOException {
Requester builder = owner.root().createRequest().method("POST");
String url = getUploadUrl();
// strip the helpful garbage from the url
int endIndex = url.indexOf('{');
if (endIndex != -1) {
url = url.substring(0, endIndex);
}
url += "?name=" + URLEncoder.encode(filename, "UTF-8");
return builder.contentType(contentType).with(stream).withUrlPath(url).fetch(GHAsset.class).wrap(this);
}
private String getApiTailUrl(String end) {
return owner.getApiTailUrl(format("releases/%s/%s", getId(), end));
}
/**
* Wrap.
*
* @param owner
* the owner
* @return the GH release
*/
GHRelease wrap(GHRepository owner) {
this.owner = owner;
return this;
}
}