Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 511f156

Browse filesBrowse files
committed
Added the membership API for the authenticated user.
1 parent 3f223b1 commit 511f156
Copy full SHA for 511f156

File tree

Expand file treeCollapse file tree

5 files changed

+138
-1
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+138
-1
lines changed
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.Locale;
6+
7+
/**
8+
* Represents a membership of a user in an organization.
9+
*
10+
* @author Kohsuke Kawaguchi
11+
* @see GHMyself#listOrgMemberships()
12+
*/
13+
public class GHMembership /* extends GHObject --- but it doesn't have id, created_at, etc. */ {
14+
GitHub root;
15+
16+
String url;
17+
String state;
18+
String role;
19+
GHUser user;
20+
GHOrganization organization;
21+
22+
public URL getUrl() {
23+
return GitHub.parseURL(url);
24+
}
25+
26+
public State getState() {
27+
return Enum.valueOf(State.class, state.toUpperCase(Locale.ENGLISH));
28+
}
29+
30+
public Role getRole() {
31+
return Enum.valueOf(Role.class, role.toUpperCase(Locale.ENGLISH));
32+
}
33+
34+
public GHUser getUser() {
35+
return user;
36+
}
37+
38+
public GHOrganization getOrganization() {
39+
return organization;
40+
}
41+
42+
/**
43+
* Accepts a pending invitation to an organization.
44+
*
45+
* @see GHMyself#getMembership(GHOrganization)
46+
*/
47+
public void activate() throws IOException {
48+
root.retrieve().method("PATCH").with("state",State.ACTIVE).to(url,this);
49+
}
50+
51+
/*package*/ GHMembership wrap(GitHub root) {
52+
this.root = root;
53+
if (user!=null) user = root.getUser(user.wrapUp(root));
54+
if (organization!=null) organization.wrapUp(root);
55+
return this;
56+
}
57+
58+
/*package*/ static void wrap(GHMembership[] page, GitHub root) {
59+
for (GHMembership m : page)
60+
m.wrap(root);
61+
}
62+
63+
/**
64+
* Role of a user in an organization.
65+
*/
66+
public enum Role {
67+
/**
68+
* Organization owner.
69+
*/
70+
ADMIN,
71+
/**
72+
* Non-owner organization member.
73+
*/
74+
MEMBER;
75+
}
76+
77+
/**
78+
* Whether a role is currently active or waiting for acceptance (pending)
79+
*/
80+
public enum State {
81+
ACTIVE,
82+
PENDING;
83+
}
84+
}

‎src/main/java/org/kohsuke/github/GHMyself.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHMyself.java
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,39 @@ public PagedIterable<GHRepository> listAllRepositories() {
178178
return listRepositories();
179179
}
180180

181+
/**
182+
* List your organization memberships
183+
*/
184+
public PagedIterable<GHMembership> listOrgMemberships() {
185+
return listOrgMemberships(null);
186+
}
187+
188+
/**
189+
* List your organization memberships
190+
*
191+
* @param state
192+
* Filter by a specific state
193+
*/
194+
public PagedIterable<GHMembership> listOrgMemberships(final GHMembership.State state) {
195+
return new PagedIterable<GHMembership>() {
196+
public PagedIterator<GHMembership> _iterator(int pageSize) {
197+
return new PagedIterator<GHMembership>(root.retrieve().with("state",state).asIterator("/user/memberships/orgs", GHMembership[].class, pageSize)) {
198+
@Override
199+
protected void wrapUp(GHMembership[] page) {
200+
GHMembership.wrap(page,root);
201+
}
202+
};
203+
}
204+
};
205+
}
206+
207+
/**
208+
* Gets your membership in a specific organization.
209+
*/
210+
public GHMembership getMembership(GHOrganization o) throws IOException {
211+
return root.retrieve().to("/user/memberships/orgs/"+o.getLogin(),GHMembership.class).wrap(root);
212+
}
213+
181214
// public void addEmails(Collection<String> emails) throws IOException {
182215
//// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails");
183216
// root.retrieveWithAuth3()

‎src/main/java/org/kohsuke/github/GHUser.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHUser.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,9 @@ String getApiTailUrl(String tail) {
214214
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
215215
return "/users/" + login + tail;
216216
}
217+
218+
/*package*/ GHUser wrapUp(GitHub root) {
219+
super.wrapUp(root);
220+
return this;
221+
}
217222
}

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public void refreshCache() {
340340
/**
341341
* Interns the given {@link GHUser}.
342342
*/
343-
protected GHUser getUser(GHUser orig) throws IOException {
343+
protected GHUser getUser(GHUser orig) {
344344
GHUser u = users.get(orig.getLogin());
345345
if (u==null) {
346346
orig.root = this;

‎src/test/java/org/kohsuke/github/AppTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AppTest.java
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,21 @@ public void reactions() throws Exception {
888888
a.delete();
889889
}
890890

891+
@Test
892+
public void listOrgMemberships() throws Exception {
893+
GHMyself me = gitHub.getMyself();
894+
for (GHMembership m : me.listOrgMemberships()) {
895+
assertThat(m.getUser(), is((GHUser)me));
896+
assertNotNull(m.getState());
897+
assertNotNull(m.getRole());
898+
899+
System.out.printf("%s %s %s\n",
900+
m.getOrganization().getLogin(),
901+
m.getState(),
902+
m.getRole());
903+
}
904+
}
905+
891906
private void kohsuke() {
892907
String login = getUser().getLogin();
893908
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.