forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHMyself.java
More file actions
242 lines (225 loc) · 8.01 KB
/
GHMyself.java
File metadata and controls
242 lines (225 loc) · 8.01 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
package org.kohsuke.github;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Represents the account that's logging into GitHub.
*
* @author Kohsuke Kawaguchi
*/
public class GHMyself extends GHUser {
/**
* Type of repositories returned during listing.
*/
public enum RepositoryListFilter {
/**
* All public and private repositories that current user has access or collaborates to
*/
ALL,
/**
* Public and private repositories owned by current user
*/
OWNER,
/**
* Public repositories that current user has access or collaborates to
*/
PUBLIC,
/**
* Private repositories that current user has access or collaborates to
*/
PRIVATE,
/**
* Public and private repositories that current user is a member
*/
MEMBER;
}
/**
* Gets emails.
*
* @return the emails
* @throws IOException
* the io exception
* @deprecated Use {@link #getEmails2()}
*/
public List<String> getEmails() throws IOException {
List<GHEmail> src = getEmails2();
List<String> r = new ArrayList<String>(src.size());
for (GHEmail e : src) {
r.add(e.getEmail());
}
return r;
}
/**
* Returns the read-only list of e-mail addresses configured for you.
* <p>
* This corresponds to the stuff you configure in https://github.com/settings/emails, and not to be confused with
* {@link #getEmail()} that shows your public e-mail address set in https://github.com/settings/profile
*
* @return Always non-null.
* @throws IOException
* the io exception
*/
public List<GHEmail> getEmails2() throws IOException {
return root.createRequest().withUrlPath("/user/emails").toIterable(GHEmail[].class, null).toList();
}
/**
* Returns the read-only list of all the pulic keys of the current user.
* <p>
* NOTE: When using OAuth authenticaiton, the READ/WRITE User scope is required by the GitHub APIs, otherwise you
* will get a 404 NOT FOUND.
*
* @return Always non-null.
* @throws IOException
* the io exception
*/
public List<GHKey> getPublicKeys() throws IOException {
return root.createRequest().withUrlPath("/user/keys").toIterable(GHKey[].class, null).toList();
}
/**
* Returns the read-only list of all the public verified keys of the current user.
* <p>
* Differently from the getPublicKeys() method, the retrieval of the user's verified public keys does not require
* any READ/WRITE OAuth Scope to the user's profile.
*
* @return Always non-null.
* @throws IOException
* the io exception
*/
public List<GHVerifiedKey> getPublicVerifiedKeys() throws IOException {
return root.createRequest()
.withUrlPath("/users/" + getLogin() + "/keys")
.toIterable(GHVerifiedKey[].class, null)
.toList();
}
/**
* Gets the organization that this user belongs to.
*
* @return the all organizations
* @throws IOException
* the io exception
*/
public GHPersonSet<GHOrganization> getAllOrganizations() throws IOException {
GHPersonSet<GHOrganization> orgs = new GHPersonSet<GHOrganization>();
Set<String> names = new HashSet<String>();
for (GHOrganization o : root.createRequest()
.withUrlPath("/user/orgs")
.toIterable(GHOrganization[].class, null)
.toArray()) {
if (names.add(o.getLogin())) // in case of rumoured duplicates in the data
orgs.add(root.getOrganization(o.getLogin()));
}
return orgs;
}
/**
* Gets the all repositories this user owns (public and private).
*
* @return the all repositories
* @throws IOException
* the io exception
*/
public synchronized Map<String, GHRepository> getAllRepositories() throws IOException {
Map<String, GHRepository> repositories = new TreeMap<String, GHRepository>();
for (GHRepository r : listAllRepositories()) {
repositories.put(r.getName(), r);
}
return Collections.unmodifiableMap(repositories);
}
/**
* Lists up all repositories this user owns (public and private).
*
* Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned. Repositories
* are returned by GitHub API with a 30 items per page.
*/
@Override
public PagedIterable<GHRepository> listRepositories() {
return listRepositories(30);
}
/**
* List repositories that are accessible to the authenticated user (public and private) using the specified page
* size.
*
* This includes repositories owned by the authenticated user, repositories that belong to other users where the
* authenticated user is a collaborator, and other organizations' repositories that the authenticated user has
* access to through an organization membership.
*
* @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.
*/
public PagedIterable<GHRepository> listRepositories(final int pageSize) {
return listRepositories(pageSize, RepositoryListFilter.ALL);
}
/**
* List repositories of a certain type that are accessible by current authenticated user using the specified page
* size.
*
* @param pageSize
* size for each page of items returned by GitHub. Maximum page size is 100.
* @param repoType
* type of repository returned in the listing
* @return the paged iterable
*/
public PagedIterable<GHRepository> listRepositories(final int pageSize, final RepositoryListFilter repoType) {
return root.createRequest()
.with("type", repoType)
.withUrlPath("/user/repos")
.toIterable(GHRepository[].class, item -> item.wrap(root))
.withPageSize(pageSize);
}
/**
* List all repositories paged iterable.
*
* @return the paged iterable
* @deprecated Use {@link #listRepositories()}
*/
@Deprecated
public PagedIterable<GHRepository> listAllRepositories() {
return listRepositories();
}
/**
* List your organization memberships
*
* @return the paged iterable
*/
public PagedIterable<GHMembership> listOrgMemberships() {
return listOrgMemberships(null);
}
/**
* List your organization memberships
*
* @param state
* Filter by a specific state
* @return the paged iterable
*/
public PagedIterable<GHMembership> listOrgMemberships(final GHMembership.State state) {
return root.createRequest()
.with("state", state)
.withUrlPath("/user/memberships/orgs")
.toIterable(GHMembership[].class, item -> item.wrap(root));
}
/**
* Gets your membership in a specific organization.
*
* @param o
* the o
* @return the membership
* @throws IOException
* the io exception
*/
public GHMembership getMembership(GHOrganization o) throws IOException {
return root.createRequest()
.withUrlPath("/user/memberships/orgs/" + o.getLogin())
.fetch(GHMembership.class)
.wrap(root);
}
// public void addEmails(Collection<String> emails) throws IOException {
//// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails");
// root.retrieveWithAuth3()
// }
}