forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHPerson.java
More file actions
370 lines (336 loc) · 10 KB
/
GHPerson.java
File metadata and controls
370 lines (336 loc) · 10 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
package org.kohsuke.github;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
/**
* Common part of {@link GHUser} and {@link GHOrganization}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class GHPerson extends GHObject {
// core data fields that exist even for "small" user data (such as the user info in pull request)
protected String login, avatar_url;
// other fields (that only show up in full data)
protected String location, blog, email, bio, name, company, type, twitter_username;
protected String html_url;
protected int followers, following, public_repos, public_gists;
protected boolean site_admin, hireable;
// other fields (that only show up in full data) that require privileged scope
protected Integer total_private_repos;
GHPerson wrapUp(GitHub root) {
this.root = root;
return this;
}
/**
* 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 {
if (super.getCreatedAt() != null) {
return; // already populated
}
if (root == null || root.isOffline()) {
return; // cannot populate, will have to live with what we have
}
URL url = getUrl();
if (url != null) {
root.createRequest().setRawUrlPath(url.toString()).fetchInto(this);
}
}
/**
* Gets the public repositories this user owns.
*
* <p>
* To list your own repositories, including private repositories, use {@link GHMyself#listRepositories()}
*
* @return the repositories
* @throws IOException
* the io exception
*/
public synchronized Map<String, GHRepository> getRepositories() throws IOException {
Map<String, GHRepository> repositories = new TreeMap<String, GHRepository>();
for (GHRepository r : listRepositories(100)) {
repositories.put(r.getName(), r);
}
return Collections.unmodifiableMap(repositories);
}
/**
* Lists up all the repositories using a 30 items page size.
* <p>
* Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned.
*
* @return the paged iterable
*/
public PagedIterable<GHRepository> listRepositories() {
return listRepositories(30);
}
/**
* Lists up all the repositories using the specified page size.
*
* @param pageSize
* size for each page of items returned by GitHub. Maximum page size is 100. Unlike
* {@link #getRepositories()}, this does not wait until all the repositories are returned.
* @return the paged iterable
*/
public PagedIterable<GHRepository> listRepositories(final int pageSize) {
return root.createRequest()
.withUrlPath("/users/" + login + "/repos")
.toIterable(GHRepository[].class, item -> item.wrap(root))
.withPageSize(pageSize);
}
/**
* Loads repository list in a paginated fashion.
*
* <p>
* For a person with a lot of repositories, GitHub returns the list of repositories in a paginated fashion. Unlike
* {@link #getRepositories()}, this method allows the caller to start processing data as it arrives.
* <p>
* Every {@link Iterator#next()} call results in I/O. Exceptions that occur during the processing is wrapped into
* {@link Error}.
*
* @param pageSize
* the page size
* @return the iterable
* @deprecated Use {@link #listRepositories()}
*/
@Deprecated
public synchronized Iterable<List<GHRepository>> iterateRepositories(final int pageSize) {
return () -> {
final PagedIterator<GHRepository> pager;
try {
GitHubPageIterator<GHRepository[]> iterator = GitHubPageIterator.create(root.getClient(),
GHRepository[].class,
root.createRequest().withUrlPath("users", login, "repos").build(),
pageSize);
pager = new PagedIterator<>(iterator, item -> item.wrap(root));
} catch (MalformedURLException e) {
throw new GHException("Unable to build GitHub API URL", e);
}
return new Iterator<List<GHRepository>>() {
public boolean hasNext() {
return pager.hasNext();
}
public List<GHRepository> next() {
return pager.nextPage();
}
};
};
}
/**
* Gets repository.
*
* @param name
* the name
* @return null if the repository was not found
* @throws IOException
* the io exception
*/
public GHRepository getRepository(String name) throws IOException {
try {
return GHRepository.read(root, login, name);
} catch (FileNotFoundException e) {
return null;
}
}
/**
* Lists events for an organization or an user.
*
* @return the paged iterable
* @throws IOException
* the io exception
*/
public abstract PagedIterable<GHEventInfo> listEvents() throws IOException;
/**
* Gravatar ID of this user, like 0cb9832a01c22c083390f3c5dcb64105
*
* @return the gravatar id
* @deprecated No longer available in the v3 API.
*/
@Deprecated
public String getGravatarId() {
return "";
}
/**
* Returns a string of the avatar image URL.
*
* @return the avatar url
*/
public String getAvatarUrl() {
return avatar_url;
}
/**
* Gets the login ID of this user, like 'kohsuke'
*
* @return the login
*/
public String getLogin() {
return login;
}
/**
* Gets the human-readable name of the user, like "Kohsuke Kawaguchi"
*
* @return the name
* @throws IOException
* the io exception
*/
public String getName() throws IOException {
populate();
return name;
}
/**
* Gets the company name of this user, like "Sun Microsystems, Inc."
*
* @return the company
* @throws IOException
* the io exception
*/
public String getCompany() throws IOException {
populate();
return company;
}
/**
* Gets the location of this user, like "Santa Clara, California"
*
* @return the location
* @throws IOException
* the io exception
*/
public String getLocation() throws IOException {
populate();
return location;
}
/**
* Gets the Twitter Username of this user, like "GitHub"
*
* @return the Twitter username
* @throws IOException
* the io exception
*/
public String getTwitterUsername() throws IOException {
populate();
return twitter_username;
}
public Date getCreatedAt() throws IOException {
populate();
return super.getCreatedAt();
}
public Date getUpdatedAt() throws IOException {
populate();
return super.getUpdatedAt();
}
/**
* Gets the blog URL of this user.
*
* @return the blog
* @throws IOException
* the io exception
*/
public String getBlog() throws IOException {
populate();
return blog;
}
@Override
public URL getHtmlUrl() {
return GitHubClient.parseURL(html_url);
}
/**
* Gets the e-mail address of the user.
*
* @return the email
* @throws IOException
* the io exception
*/
public String getEmail() throws IOException {
populate();
return email;
}
/**
* Gets public gist count.
*
* @return the public gist count
* @throws IOException
* the io exception
*/
public int getPublicGistCount() throws IOException {
populate();
return public_gists;
}
/**
* Gets public repo count.
*
* @return the public repo count
* @throws IOException
* the io exception
*/
public int getPublicRepoCount() throws IOException {
populate();
return public_repos;
}
/**
* Gets following count.
*
* @return the following count
* @throws IOException
* the io exception
*/
public int getFollowingCount() throws IOException {
populate();
return following;
}
/**
* Gets followers count.
*
* @return the followers count
* @throws IOException
* the io exception
*/
public int getFollowersCount() throws IOException {
populate();
return followers;
}
/**
* Gets the type. This is either "User" or "Organization".
*
* @return the type
* @throws IOException
* the io exception
*/
public String getType() throws IOException {
populate();
return type;
}
/**
* Gets the site_admin field
*
* @return the site_admin field
* @throws IOException
* the io exception
*/
public boolean isSiteAdmin() throws IOException {
populate();
return site_admin;
}
/**
* Gets total private repo count.
*
* @return the total private repo count
* @throws IOException
* the io exception
*/
public Optional<Integer> getTotalPrivateRepoCount() throws IOException {
populate();
return Optional.ofNullable(total_private_repos);
}
}