-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHContent.java
More file actions
462 lines (415 loc) · 12.1 KB
/
GHContent.java
File metadata and controls
462 lines (415 loc) · 12.1 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
// TODO: Auto-generated Javadoc
/**
* A Content of a repository.
*
* @author Alexandre COLLIGNON
* @see GHRepository#getFileContent(String) GHRepository#getFileContent(String)
*/
@SuppressWarnings({ "UnusedDeclaration" })
public class GHContent extends GitHubInteractiveObject implements Refreshable {
/**
* Gets the api route.
*
* @param repository
* the repository
* @param path
* the path
* @return the api route
*/
static String getApiRoute(GHRepository repository, String path) {
return repository.getApiTailUrl("contents/" + path);
}
private String content;
private String downloadUrl;
private String encoding;
private String gitUrl; // this is the Blob url
private String htmlUrl; // this is the UI
private String name;
private String path;
/*
* In normal use of this class, repository field is set via wrap(), but in the code search API, there's a nested
* 'repository' field that gets populated from JSON.
*/
private GHRepository repository;
private String sha;
private long size;
private String target;
private String type;
private String url; // this is the API url
/**
* Create default GHContent instance
*/
public GHContent() {
}
/**
* Creates a builder that can be used to delete this file.
*
* <p>
* Unlike the {@link #delete(String)} convenience methods, this builder supports setting the author and committer of
* the resulting commit.
*
* @return a content deleter
* @see GHContentDeleter
*/
public GHContentDeleter createDelete() {
return new GHContentDeleter(this);
}
/**
* Creates a builder that can be used to update this file's content.
*
* <p>
* Unlike the {@link #update(String, String)} convenience methods, this builder supports setting the author and
* committer of the resulting commit.
*
* @return a content updater
* @see GHContentUpdater
*/
public GHContentUpdater createUpdate() {
return new GHContentUpdater(this);
}
/**
* Delete gh content update response.
*
* @param message
* the message
* @return the gh content update response
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse delete(String message) throws IOException {
return delete(message, null);
}
/**
* Delete gh content update response.
*
* @param commitMessage
* the commit message
* @param branch
* the branch
* @return the gh content update response
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException {
Requester requester = root().createRequest()
.method("DELETE")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha);
if (branch != null) {
requester.with("branch", branch);
}
GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))
.fetch(GHContentUpdateResponse.class);
response.getCommit().wrapUp(repository);
return response;
}
/**
* Retrieve the decoded content that is stored at this location.
*
* <p>
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may
* trigger network activity, and can throw an IOException.
*
* @return the content
* @throws IOException
* the io exception
* @deprecated Use {@link #read()}
*/
@Deprecated
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
public String getContent() throws IOException {
return new String(readDecodedContent());
}
/**
* URL to retrieve the raw content of the file. Null if this is a directory.
*
* @return the download url
* @throws IOException
* the io exception
*/
public String getDownloadUrl() throws IOException {
refresh(downloadUrl);
return downloadUrl;
}
/**
* Retrieve the base64-encoded content that is stored at this location.
*
* <p>
* Due to the nature of GitHub's API, you're not guaranteed that the content will already be populated, so this may
* trigger network activity, and can throw an IOException.
*
* @return the encoded content
* @throws IOException
* the io exception
* @deprecated Use {@link #read()}
*/
@Deprecated
public String getEncodedContent() throws IOException {
refresh(content);
return content;
}
/**
* Gets encoding.
*
* @return the encoding
*/
public String getEncoding() {
return encoding;
}
/**
* Gets git url.
*
* @return the git url
*/
public String getGitUrl() {
return gitUrl;
}
/**
* Gets html url.
*
* @return the html url
*/
public String getHtmlUrl() {
return htmlUrl;
}
/**
* 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 repository;
}
/**
* Gets path.
*
* @return the path
*/
public String getPath() {
return path;
}
/**
* Gets sha.
*
* @return the sha
*/
public String getSha() {
return sha;
}
/**
* Gets size.
*
* @return the size
*/
public long getSize() {
return size;
}
/**
* Gets target of a symlink. This will only be set if {@code "symlink".equals(getType())}
*
* @return the target
*/
public String getTarget() {
return target;
}
/**
* Gets type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Gets url.
*
* @return the url
*/
public String getUrl() {
return url;
}
/**
* Is directory boolean.
*
* @return the boolean
*/
public boolean isDirectory() {
return "dir".equals(type);
}
/**
* Is file boolean.
*
* @return the boolean
*/
public boolean isFile() {
return "file".equals(type);
}
/**
* List immediate children of this directory.
*
* @return the paged iterable
*/
public PagedIterable<GHContent> listDirectoryContent() {
if (!isDirectory())
throw new IllegalStateException(path + " is not a directory");
return root().createRequest().setRawUrlPath(url).toIterable(GHContent[].class, item -> item.wrap(repository));
}
/**
* Retrieves the actual bytes of the blob.
*
* @return the input stream
* @throws IOException
* the io exception
*/
public InputStream read() throws IOException {
return new ByteArrayInputStream(readDecodedContent());
}
/**
* Fully populate the data by retrieving missing data.
*
* Depending on the original API call where this object is created, it may not contain everything.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Override
public synchronized void refresh() throws IOException {
root().createRequest().setRawUrlPath(url).fetchInto(this);
}
/**
* Update gh content update response.
*
* @param newContent
* the new content
* @param commitMessage
* the commit message
* @return the gh content update response
* @throws IOException
* the io exception
*/
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
return update(newContent.getBytes(), commitMessage, null);
}
/**
* Update gh content update response.
*
* @param newContent
* the new content
* @param commitMessage
* the commit message
* @param branch
* the branch
* @return the gh content update response
* @throws IOException
* the io exception
*/
@SuppressFBWarnings("DM_DEFAULT_ENCODING")
public GHContentUpdateResponse update(String newContent, String commitMessage, String branch) throws IOException {
return update(newContent.getBytes(), commitMessage, branch);
}
/**
* Update gh content update response.
*
* @param newContentBytes
* the new content bytes
* @param commitMessage
* the commit message
* @return the gh content update response
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage) throws IOException {
return update(newContentBytes, commitMessage, null);
}
/**
* Update gh content update response.
*
* @param newContentBytes
* the new content bytes
* @param commitMessage
* the commit message
* @param branch
* the branch
* @return the gh content update response
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage, String branch)
throws IOException {
String encodedContent = Base64.getEncoder().encodeToString(newContentBytes);
Requester requester = root().createRequest()
.method("PUT")
.with("path", path)
.with("message", commitMessage)
.with("sha", sha)
.with("content", encodedContent);
if (branch != null) {
requester.with("branch", branch);
}
GHContentUpdateResponse response = requester.withUrlPath(getApiRoute(repository, path))
.fetch(GHContentUpdateResponse.class);
response.getContent().wrap(repository);
response.getCommit().wrapUp(repository);
this.content = encodedContent;
return response;
}
/**
* Retrieves the decoded bytes of the blob.
*
* @return the input stream
* @throws IOException
* the io exception
*/
private byte[] readDecodedContent() throws IOException {
String encodedContent = getEncodedContent();
if (encoding.equals("base64")) {
try {
Base64.Decoder decoder = Base64.getMimeDecoder();
return decoder.decode(encodedContent.getBytes(StandardCharsets.US_ASCII));
} catch (IllegalArgumentException e) {
throw new AssertionError(e); // US-ASCII is mandatory
}
}
throw new UnsupportedOperationException("Unrecognized encoding: " + encoding);
}
/**
* Fully populate the data by retrieving missing data.
* <p>
* Depending on the original API call where this object is created, it may not contain everything.
*
* @throws IOException
* the io exception
*/
protected synchronized void populate() throws IOException {
root().createRequest().withUrlPath(url).fetchInto(this);
}
/**
* Wrap.
*
* @param owner
* the owner
* @return the GH content
*/
GHContent wrap(GHRepository owner) {
this.repository = owner;
return this;
}
}