Skip to content

Commit

Permalink
Merge pull request #510 from awittha/get-user-orgs
Browse files Browse the repository at this point in the history
Added getUserPublicOrganizations method
  • Loading branch information
bitwiseman authored Oct 25, 2019
2 parents 72d4b9b + c763c02 commit 1d004a3
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static GitHub connect() throws IOException {
* @deprecated
* Use {@link #connectToEnterpriseWithOAuth(String, String, String)}
*/
@Deprecated
public static GitHub connectToEnterprise(String apiUrl, String oauthAccessToken) throws IOException {
return connectToEnterpriseWithOAuth(apiUrl,null,oauthAccessToken);
}
Expand All @@ -192,6 +193,7 @@ public static GitHub connectToEnterpriseWithOAuth(String apiUrl, String login, S
* @deprecated
* Use with caution. Login with password is not a preferred method.
*/
@Deprecated
public static GitHub connectToEnterprise(String apiUrl, String login, String password) throws IOException {
return new GitHubBuilder().withEndpoint(apiUrl).withPassword(login, password).build();
}
Expand All @@ -205,6 +207,7 @@ public static GitHub connect(String login, String oauthAccessToken) throws IOExc
* Either OAuth token or password is sufficient, so there's no point in passing both.
* Use {@link #connectUsingPassword(String, String)} or {@link #connectUsingOAuth(String)}.
*/
@Deprecated
public static GitHub connect(String login, String oauthAccessToken, String password) throws IOException {
return new GitHubBuilder().withOAuthToken(oauthAccessToken, login).withPassword(login, password).build();
}
Expand Down Expand Up @@ -378,7 +381,7 @@ public GHMyself getMyself() throws IOException {
requireCredential();
synchronized (this) {
if (this.myself != null) return myself;

GHMyself u = retrieve().to("/user", GHMyself.class);

u.root = this;
Expand All @@ -400,7 +403,7 @@ public GHUser getUser(String login) throws IOException {
return u;
}


/**
* clears all cached data in order for external changes (modifications and del) to be reflected
*/
Expand Down Expand Up @@ -535,6 +538,32 @@ public Map<String, GHOrganization> getMyOrganizations() throws IOException {
}
return r;
}

/**
* Alias for {@link #getUserPublicOrganizations(String)}.
*/
public Map<String, GHOrganization> getUserPublicOrganizations(GHUser user) throws IOException {
return getUserPublicOrganizations( user.getLogin() );
}

/**
* This method returns a shallowly populated organizations.
*
* To retrieve full organization details, you need to call {@link #getOrganization(String)}
*
* @param user the user to retrieve public Organization membership information for
*
* @return the public Organization memberships for the user
*/
public Map<String, GHOrganization> getUserPublicOrganizations(String login) throws IOException {
GHOrganization[] orgs = retrieve().to("/users/" + login + "/orgs", GHOrganization[].class);
Map<String, GHOrganization> r = new HashMap<String, GHOrganization>();
for (GHOrganization o : orgs) {
// don't put 'o' into orgs because they are shallow
r.put(o.getLogin(),o.wrapUp(this));
}
return r;
}

/**
* Gets complete map of organizations/teams that current user belongs to.
Expand Down Expand Up @@ -607,6 +636,7 @@ public <T extends GHEventPayload> T parseEventPayload(Reader r, Class<T> type) t
* @deprecated
* Use {@link #createRepository(String)} that uses a builder pattern to let you control every aspect.
*/
@Deprecated
public GHRepository createRepository(String name, String description, String homepage, boolean isPublic) throws IOException {
return createRepository(name).description(description).homepage(homepage).private_(!isPublic).create();
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ public void testMyTeamsShouldIncludeMyself() throws IOException {
}
}
}

@Test
public void testUserPublicOrganizationsWhenThereAreSome() throws IOException {
// kohsuke had some public org memberships at the time Wiremock recorded the GitHub API responses
GHUser user = new GHUser();
user.login = "kohsuke";

Map<String, GHOrganization> orgs = gitHub.getUserPublicOrganizations( user );
assertFalse(orgs.isEmpty());
}

@Test
public void testUserPublicOrganizationsWhenThereAreNone() throws IOException {
// bitwiseman had no public org memberships at the time Wiremock recorded the GitHub API responses
GHUser user = new GHUser();
user.login = "bitwiseman";

Map<String, GHOrganization> orgs = gitHub.getUserPublicOrganizations( user );
assertTrue(orgs.isEmpty());
}

private boolean shouldBelongToTeam(String organizationName, String teamName) throws IOException {
GHOrganization org = gitHub.getOrganization(organizationName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "94ce7391-a312-48e7-b869-ce2f16838e60",
"name": "users_bitwiseman_orgs",
"request": {
"url": "/users/bitwiseman/orgs",
"method": "GET"
},
"response": {
"status": 200,
"body": "[]",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 24 Oct 2019 15:49:23 GMT",
"Content-Type": "application/json; charset=utf-8",
"Status": "200 OK",
"X-RateLimit-Limit": "60",
"X-RateLimit-Remaining": "27",
"X-RateLimit-Reset": "1571934805",
"Cache-Control": "public, max-age=60, s-maxage=60",
"Vary": "Accept",
"ETag": "\"80b190627d4c87e9a37c34e20ea246a1\"",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "6970:40D4:19AB38:31CA07:5DB1C803"
}
},
"uuid": "94ce7391-a312-48e7-b869-ce2f16838e60",
"persistent": true,
"insertionIndex": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
[
{
"login": "jenkinsci",
"id": 107424,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjEwNzQyNA==",
"url": "https://api.github.com/orgs/jenkinsci",
"repos_url": "https://api.github.com/orgs/jenkinsci/repos",
"events_url": "https://api.github.com/orgs/jenkinsci/events",
"hooks_url": "https://api.github.com/orgs/jenkinsci/hooks",
"issues_url": "https://api.github.com/orgs/jenkinsci/issues",
"members_url": "https://api.github.com/orgs/jenkinsci/members{/member}",
"public_members_url": "https://api.github.com/orgs/jenkinsci/public_members{/member}",
"avatar_url": "https://avatars0.githubusercontent.com/u/107424?v=4",
"description": "Jenkins is an open source automation server with an unparalleled plugin ecosystem to support practically every tool as part of your delivery pipelines"
},
{
"login": "cloudbees",
"id": 235526,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjIzNTUyNg==",
"url": "https://api.github.com/orgs/cloudbees",
"repos_url": "https://api.github.com/orgs/cloudbees/repos",
"events_url": "https://api.github.com/orgs/cloudbees/events",
"hooks_url": "https://api.github.com/orgs/cloudbees/hooks",
"issues_url": "https://api.github.com/orgs/cloudbees/issues",
"members_url": "https://api.github.com/orgs/cloudbees/members{/member}",
"public_members_url": "https://api.github.com/orgs/cloudbees/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/235526?v=4",
"description": ""
},
{
"login": "infradna",
"id": 266075,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjI2NjA3NQ==",
"url": "https://api.github.com/orgs/infradna",
"repos_url": "https://api.github.com/orgs/infradna/repos",
"events_url": "https://api.github.com/orgs/infradna/events",
"hooks_url": "https://api.github.com/orgs/infradna/hooks",
"issues_url": "https://api.github.com/orgs/infradna/issues",
"members_url": "https://api.github.com/orgs/infradna/members{/member}",
"public_members_url": "https://api.github.com/orgs/infradna/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/266075?v=4",
"description": null
},
{
"login": "stapler",
"id": 700341,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjcwMDM0MQ==",
"url": "https://api.github.com/orgs/stapler",
"repos_url": "https://api.github.com/orgs/stapler/repos",
"events_url": "https://api.github.com/orgs/stapler/events",
"hooks_url": "https://api.github.com/orgs/stapler/hooks",
"issues_url": "https://api.github.com/orgs/stapler/issues",
"members_url": "https://api.github.com/orgs/stapler/members{/member}",
"public_members_url": "https://api.github.com/orgs/stapler/public_members{/member}",
"avatar_url": "https://avatars1.githubusercontent.com/u/700341?v=4",
"description": null
},
{
"login": "java-schema-utilities",
"id": 709081,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjcwOTA4MQ==",
"url": "https://api.github.com/orgs/java-schema-utilities",
"repos_url": "https://api.github.com/orgs/java-schema-utilities/repos",
"events_url": "https://api.github.com/orgs/java-schema-utilities/events",
"hooks_url": "https://api.github.com/orgs/java-schema-utilities/hooks",
"issues_url": "https://api.github.com/orgs/java-schema-utilities/issues",
"members_url": "https://api.github.com/orgs/java-schema-utilities/members{/member}",
"public_members_url": "https://api.github.com/orgs/java-schema-utilities/public_members{/member}",
"avatar_url": "https://avatars0.githubusercontent.com/u/709081?v=4",
"description": null
},
{
"login": "CloudBees-community",
"id": 1225740,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjEyMjU3NDA=",
"url": "https://api.github.com/orgs/CloudBees-community",
"repos_url": "https://api.github.com/orgs/CloudBees-community/repos",
"events_url": "https://api.github.com/orgs/CloudBees-community/events",
"hooks_url": "https://api.github.com/orgs/CloudBees-community/hooks",
"issues_url": "https://api.github.com/orgs/CloudBees-community/issues",
"members_url": "https://api.github.com/orgs/CloudBees-community/members{/member}",
"public_members_url": "https://api.github.com/orgs/CloudBees-community/public_members{/member}",
"avatar_url": "https://avatars0.githubusercontent.com/u/1225740?v=4",
"description": ""
},
{
"login": "cloudbeers",
"id": 4181899,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQxODE4OTk=",
"url": "https://api.github.com/orgs/cloudbeers",
"repos_url": "https://api.github.com/orgs/cloudbeers/repos",
"events_url": "https://api.github.com/orgs/cloudbeers/events",
"hooks_url": "https://api.github.com/orgs/cloudbeers/hooks",
"issues_url": "https://api.github.com/orgs/cloudbeers/issues",
"members_url": "https://api.github.com/orgs/cloudbeers/members{/member}",
"public_members_url": "https://api.github.com/orgs/cloudbeers/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/4181899?v=4",
"description": ""
},
{
"login": "jenkins-infra",
"id": 7422698,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc0MjI2OTg=",
"url": "https://api.github.com/orgs/jenkins-infra",
"repos_url": "https://api.github.com/orgs/jenkins-infra/repos",
"events_url": "https://api.github.com/orgs/jenkins-infra/events",
"hooks_url": "https://api.github.com/orgs/jenkins-infra/hooks",
"issues_url": "https://api.github.com/orgs/jenkins-infra/issues",
"members_url": "https://api.github.com/orgs/jenkins-infra/members{/member}",
"public_members_url": "https://api.github.com/orgs/jenkins-infra/public_members{/member}",
"avatar_url": "https://avatars2.githubusercontent.com/u/7422698?v=4",
"description": "Repositories owned by the Jenkins project infrastructure team"
},
{
"login": "LegoMatterhorn",
"id": 9170491,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNzA0OTE=",
"url": "https://api.github.com/orgs/LegoMatterhorn",
"repos_url": "https://api.github.com/orgs/LegoMatterhorn/repos",
"events_url": "https://api.github.com/orgs/LegoMatterhorn/events",
"hooks_url": "https://api.github.com/orgs/LegoMatterhorn/hooks",
"issues_url": "https://api.github.com/orgs/LegoMatterhorn/issues",
"members_url": "https://api.github.com/orgs/LegoMatterhorn/members{/member}",
"public_members_url": "https://api.github.com/orgs/LegoMatterhorn/public_members{/member}",
"avatar_url": "https://avatars2.githubusercontent.com/u/9170491?v=4",
"description": ""
},
{
"login": "jenkinsci-cert",
"id": 9285726,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkyODU3MjY=",
"url": "https://api.github.com/orgs/jenkinsci-cert",
"repos_url": "https://api.github.com/orgs/jenkinsci-cert/repos",
"events_url": "https://api.github.com/orgs/jenkinsci-cert/events",
"hooks_url": "https://api.github.com/orgs/jenkinsci-cert/hooks",
"issues_url": "https://api.github.com/orgs/jenkinsci-cert/issues",
"members_url": "https://api.github.com/orgs/jenkinsci-cert/members{/member}",
"public_members_url": "https://api.github.com/orgs/jenkinsci-cert/public_members{/member}",
"avatar_url": "https://avatars2.githubusercontent.com/u/9285726?v=4",
"description": "Private repositories for the Jenkins CERT team"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "7ef9c5df-d48e-44be-ba15-e880b8e9558d",
"name": "users_kohsuke_orgs",
"request": {
"url": "/users/kohsuke/orgs",
"method": "GET"
},
"response": {
"status": 200,
"bodyFileName": "users_kohsuke_orgs-7ef9c5df-d48e-44be-ba15-e880b8e9558d.json",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 24 Oct 2019 15:50:04 GMT",
"Content-Type": "application/json; charset=utf-8",
"Status": "200 OK",
"X-RateLimit-Limit": "60",
"X-RateLimit-Remaining": "25",
"X-RateLimit-Reset": "1571934805",
"Cache-Control": "public, max-age=60, s-maxage=60",
"Vary": "Accept",
"ETag": "W/\"5c5a7fc59f333fbeda60ffeefc546a8a\"",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "5EE8:1F70:C0C53:1D07EC:5DB1C82C"
}
},
"uuid": "7ef9c5df-d48e-44be-ba15-e880b8e9558d",
"persistent": true,
"insertionIndex": 1
}

0 comments on commit 1d004a3

Please sign in to comment.