-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGHOrganizationExt.java
More file actions
257 lines (221 loc) · 9.86 KB
/
GHOrganizationExt.java
File metadata and controls
257 lines (221 loc) · 9.86 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
package org.kohsuke.github;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Extends original GHOrganization class.
*
* @author Hiroyuki Wada
*/
public class GHOrganizationExt extends GHOrganization {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
GHOrganizationExt wrapUp(GitHub root) {
return (GHOrganizationExt) super.wrapUp(root);
}
public GHUser createInvitation(String email, String role) throws IOException {
return root.createRequest()
.method("POST")
.withHeader("Accept", "application/vnd.github.v3+json")
.with("email", email)
.with("role", role)
.withUrlPath(String.format("/orgs/%s/invitations", login))
.fetch(GHUser.class);
}
public Iterable<GHUser> listInvitation() {
return root.createRequest()
.withUrlPath(String.format("/orgs/%s/invitations", login))
.toIterable(GHUser[].class, item -> item.wrapUp(root));
}
public SCIMUser createSCIMUser(SCIMUser newUser) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("userName", newUser.userName);
map.put("emails", newUser.emails);
map.put("name", newUser.name);
map.put("externalId", newUser.externalId);
SCIMUser u = root.createRequest()
.method("POST")
.withHeader(SCIMConstants.HEADER_ACCEPT, SCIMConstants.SCIM_ACCEPT)
.withHeader(SCIMConstants.GITHUB_API_VERSION, SCIMConstants.GITHUB_API_VERSION)
.with(map)
.withUrlPath(String.format("/scim/v2/organizations/%s/Users", login))
.fetch(SCIMUser.class);
return u;
}
public SCIMUser updateSCIMUser(String scimUserId, String scimUserName, String scimEmail, String scimGivenName, String scimFamilyName) throws IOException {
List<SCIMOperation> ops = new ArrayList<>();
if (scimUserName != null) {
ops.add(new SCIMOperation<>("replace", "userName", scimUserName));
}
if (scimEmail != null) {
List<Map<String, String>> emails = new ArrayList<>();
Map<String, String> emailsMap = new HashMap<>();
emailsMap.put("value", scimEmail);
emails.add(emailsMap);
ops.add(new SCIMOperation<>("replace", "emails", emails));
}
if (scimGivenName != null) {
ops.add(new SCIMOperation<>("replace", "name.givenName", scimGivenName));
}
if (scimFamilyName != null) {
ops.add(new SCIMOperation<>("replace", "name.familyName", scimFamilyName));
}
if (ops.isEmpty()) {
return null;
}
Map<String, Object> map = new HashMap<>();
map.put("Operations", ops);
SCIMUser u = root.createRequest()
.method("PATCH")
.withHeader(SCIMConstants.HEADER_ACCEPT, SCIMConstants.SCIM_ACCEPT)
.withHeader(SCIMConstants.GITHUB_API_VERSION, SCIMConstants.GITHUB_API_VERSION)
.with(map)
.withUrlPath(String.format("/scim/v2/organizations/%s/Users/%s", login, scimUserId))
.fetch(SCIMUser.class);
return u;
}
public SCIMUser getSCIMUser(String scimUserId) throws IOException {
SCIMUser u = root.createRequest()
.withHeader(SCIMConstants.HEADER_ACCEPT, SCIMConstants.SCIM_ACCEPT)
.withHeader(SCIMConstants.GITHUB_API_VERSION, SCIMConstants.GITHUB_API_VERSION)
.withUrlPath(String.format("/scim/v2/organizations/%s/Users/%s", login, scimUserId))
.fetch(SCIMUser.class);
return u;
}
public SCIMUser getSCIMUserByUserName(String scimUserName) throws IOException {
SCIMUser u = root.createRequest()
.withHeader(SCIMConstants.HEADER_ACCEPT, SCIMConstants.SCIM_ACCEPT)
.withHeader(SCIMConstants.GITHUB_API_VERSION, SCIMConstants.GITHUB_API_VERSION)
.withUrlPath(String.format("/scim/v2/organizations/%s/Users?filter=userName eq \"%s\"", login, scimUserName))
.fetch(SCIMUser.class);
return u;
}
/**
* Search users.
*
* @return the gh user search builder
*/
public SCIMUserSearchBuilder searchSCIMUsers() {
return new SCIMUserSearchBuilder(root, this);
}
public SCIMPagedSearchIterable<SCIMUser> listSCIMUsers(int pageSize)
throws IOException {
return searchSCIMUsers().list().withPageSize(pageSize);
}
/**
* Search users.
*
* @return the gh user search builder
*/
public GraphQLOrganizationExternalIdentitySearchBuilder searchExternalIdentities() {
return new GraphQLOrganizationExternalIdentitySearchBuilder(root, this);
}
public GraphQLPagedSearchIterable<GraphQLOrganization, GraphQLExternalIdentityEdge> listExternalIdentities(int pageSize)
throws IOException {
return searchExternalIdentities().list().withPageSize(pageSize);
}
public void deleteSCIMUser(String scimUserId) throws IOException {
root.createRequest()
.method("DELETE")
.withHeader(SCIMConstants.HEADER_ACCEPT, SCIMConstants.SCIM_ACCEPT)
.withHeader(SCIMConstants.GITHUB_API_VERSION, SCIMConstants.GITHUB_API_VERSION)
.withUrlPath(String.format("/scim/v2/organizations/%s/Users/%s", login, scimUserId))
.send();
}
public GHTeamExt getTeam(long teamId) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/organizations/%d/team/%d", getId(), teamId))
.fetch(GHTeamExt.class);
}
public GHTeamExt getTeam(String slug) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/organizations/%d/team/%d", login, slug))
.fetch(GHTeamExt.class);
}
public PagedIterable<GHTeamExt> listTeamsExt() throws IOException {
return root.createRequest()
.withUrlPath(String.format("/orgs/%s/teams", login))
.toIterable(GHTeamExt[].class, item -> item.wrapUp(this));
}
public GHTeam updateTeam(long teamId, String name, String description, GHTeam.Privacy privacy, Long parentTeamId,
boolean clearParent) throws IOException {
Requester req = root.createRequest().method("PATCH");
if (name != null) {
req.with("name", name);
}
if (description != null) {
req.with("description", description);
}
if (privacy != null) {
req.with("privacy", privacy);
}
if (parentTeamId != null) {
req.with("parent_team_id", parentTeamId);
} else if (clearParent) {
req.withNullable("parent_team_id", null);
}
GHTeam updated = req.withUrlPath(String.format("/organizations/%d/team/%d", getId(), teamId))
.fetch(GHTeam.class);
return updated;
}
public void deleteTeam(long teamId) throws IOException {
root.createRequest().method("DELETE")
.withUrlPath(String.format("/organizations/%d/team/%d", getId(), teamId))
.send();
}
public GraphQLPagedSearchIterable<GraphQLOrganization, GraphQLTeamEdge> findTeam(String teamName, int pageSize) throws IOException {
return new GraphQLTeamSearchBuilder(root, this, teamName)
.list()
.withPageSize(pageSize);
}
public GraphQLTeamByMemberSearchBuilder searchTeams(String userLogin) {
return new GraphQLTeamByMemberSearchBuilder(root, this, userLogin);
}
public GraphQLPagedSearchIterable<GraphQLOrganization, GraphQLTeamEdge> listTeams(String userLogin, int pageSize)
throws IOException {
return searchTeams(userLogin).list().withPageSize(pageSize);
}
public void addTeamMembership(long teamId, String userLogin, GHTeam.Role teamRole) throws IOException {
root.createRequest().method("PUT")
.with("role", teamRole.name().toLowerCase())
.withUrlPath(String.format("/organizations/%d/team/%d/memberships/%s", getId(), teamId, userLogin))
.send();
}
public void removeTeamMembership(long teamId, String userLogin) throws IOException {
root.createRequest().method("DELETE")
.withUrlPath(String.format("/organizations/%d/team/%d/memberships/%s", getId(), teamId, userLogin))
.send();
}
public boolean isMember(String userLogin) {
try {
root.createRequest()
.withUrlPath(String.format("/orgs/%s/members/%s", login, userLogin))
.send();
return true;
} catch (IOException ignore) {
return false;
}
}
/**
* Set organization role to the user.
* https://docs.github.com/en/rest/reference/orgs#set-organization-membership-for-a-user
*
* @param userLogin GitHub username
* @param organizationRole orgnization role (admin or member)
* @throws IOException API error
*/
public void setOrganizationMembership(String userLogin, Role organizationRole) throws IOException {
root.createRequest().method("PUT")
.with("role", organizationRole.name().toLowerCase())
.withUrlPath(String.format("/orgs/%s/memberships/%s", login, userLogin))
.send();
}
public GHMembership getOrganizationMembership(String userLogin) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/orgs/%s/memberships/%s", login, userLogin))
.fetch(GHMembership.class);
}
}