Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 7b436ff

Browse files
committed
support on-demand data population for the use in code search API.
1 parent 1ee2ec3 commit 7b436ff

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/main/java/org/kohsuke/github/GHContent.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
*/
1717
@SuppressWarnings({"UnusedDeclaration"})
1818
public class GHContent {
19-
private GHRepository owner;
19+
/*
20+
In normal use of this class, repository field is set via wrap(),
21+
but in the code search API, there's a nested 'repository' field that gets populated from JSON.
22+
*/
23+
private GHRepository repository;
24+
25+
private GitHub root;
2026

2127
private String type;
2228
private String encoding;
@@ -31,7 +37,7 @@ public class GHContent {
3137
private String download_url;
3238

3339
public GHRepository getOwner() {
34-
return owner;
40+
return repository;
3541
}
3642

3743
public String getType() {
@@ -107,13 +113,16 @@ public String getHtmlUrl() {
107113
* Retrieves the actual content stored here.
108114
*/
109115
public InputStream read() throws IOException {
110-
return new Requester(owner.root).read(getDownloadUrl());
116+
return new Requester(root).read(getDownloadUrl());
111117
}
112118

113119
/**
114120
* URL to retrieve the raw content of the file. Null if this is a directory.
115121
*/
116-
public String getDownloadUrl() { return download_url; }
122+
public String getDownloadUrl() throws IOException {
123+
populate();
124+
return download_url;
125+
}
117126

118127
public boolean isFile() {
119128
return "file".equals(type);
@@ -123,6 +132,16 @@ public boolean isDirectory() {
123132
return "dir".equals(type);
124133
}
125134

135+
/**
136+
* Fully populate the data by retrieving missing data.
137+
*
138+
* Depending on the original API call where this object is created, it may not contain everything.
139+
*/
140+
protected synchronized void populate() throws IOException {
141+
if (download_url!=null) return; // already populated
142+
root.retrieve().to(url, this);
143+
}
144+
126145
/**
127146
* List immediate children of this directory.
128147
*/
@@ -132,10 +151,10 @@ public PagedIterable<GHContent> listDirectoryContent() throws IOException {
132151

133152
return new PagedIterable<GHContent>() {
134153
public PagedIterator<GHContent> iterator() {
135-
return new PagedIterator<GHContent>(owner.root.retrieve().asIterator(url, GHContent[].class)) {
154+
return new PagedIterator<GHContent>(root.retrieve().asIterator(url, GHContent[].class)) {
136155
@Override
137156
protected void wrapUp(GHContent[] page) {
138-
GHContent.wrap(page,owner);
157+
GHContent.wrap(page, repository);
139158
}
140159
};
141160
}
@@ -157,7 +176,7 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa
157176
public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessage, String branch) throws IOException {
158177
String encodedContent = DatatypeConverter.printBase64Binary(newContentBytes);
159178

160-
Requester requester = new Requester(owner.root)
179+
Requester requester = new Requester(root)
161180
.with("path", path)
162181
.with("message", commitMessage)
163182
.with("sha", sha)
@@ -170,8 +189,8 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa
170189

171190
GHContentUpdateResponse response = requester.to(getApiRoute(), GHContentUpdateResponse.class);
172191

173-
response.getContent().wrap(owner);
174-
response.getCommit().wrapUp(owner);
192+
response.getContent().wrap(repository);
193+
response.getCommit().wrapUp(repository);
175194

176195
this.content = encodedContent;
177196
return response;
@@ -182,7 +201,7 @@ public GHContentUpdateResponse delete(String message) throws IOException {
182201
}
183202

184203
public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException {
185-
Requester requester = new Requester(owner.root)
204+
Requester requester = new Requester(root)
186205
.with("path", path)
187206
.with("message", commitMessage)
188207
.with("sha", sha)
@@ -194,18 +213,26 @@ public GHContentUpdateResponse delete(String commitMessage, String branch) throw
194213

195214
GHContentUpdateResponse response = requester.to(getApiRoute(), GHContentUpdateResponse.class);
196215

197-
response.getCommit().wrapUp(owner);
216+
response.getCommit().wrapUp(repository);
198217
return response;
199218
}
200219

201220
private String getApiRoute() {
202-
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/contents/" + path;
221+
return "/repos/" + repository.getOwnerName() + "/" + repository.getName() + "/contents/" + path;
203222
}
204223

205224
GHContent wrap(GHRepository owner) {
206-
this.owner = owner;
225+
this.repository = owner;
226+
this.root = owner.root;
207227
return this;
208228
}
229+
GHContent wrap(GitHub root) {
230+
this.root = root;
231+
if (repository!=null)
232+
repository.wrap(root);
233+
return this;
234+
}
235+
209236

210237
public static GHContent[] wrap(GHContent[] contents, GHRepository repository) {
211238
for (GHContent unwrappedContent : contents) {

0 commit comments

Comments
 (0)