Skip to content

Commit

Permalink
Added getUserPublicOrganizations method to get public organization me…
Browse files Browse the repository at this point in the history
…mberships for any user, not just the authenticated user
  • Loading branch information
awittha committed Mar 27, 2019
1 parent fad203a commit 56e3791
Showing 1 changed file with 53 additions and 16 deletions.
69 changes: 53 additions & 16 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@
*/
package org.kohsuke.github;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.FINE;
import static org.kohsuke.github.Previews.DRAX;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -57,10 +52,18 @@
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*;
import static java.net.HttpURLConnection.*;
import static java.util.logging.Level.*;
import static org.kohsuke.github.Previews.*;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;

/**
* Root of the GitHub API.
Expand Down Expand Up @@ -172,6 +175,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 @@ -194,6 +198,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 @@ -207,6 +212,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 @@ -380,7 +386,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 @@ -402,7 +408,7 @@ public GHUser getUser(String login) throws IOException {
return u;
}


/**
* clears all cached data in order for external changes (modifications and del
*/
Expand Down Expand Up @@ -486,6 +492,7 @@ public GHRepository getRepository(String name) throws IOException {
@Preview @Deprecated
public PagedIterable<GHLicense> listLicenses() throws IOException {
return new PagedIterable<GHLicense>() {
@Override
public PagedIterator<GHLicense> _iterator(int pageSize) {
return new PagedIterator<GHLicense>(retrieve().withPreview(DRAX).asIterator("/licenses", GHLicense[].class, pageSize)) {
@Override
Expand All @@ -503,6 +510,7 @@ protected void wrapUp(GHLicense[] page) {
*/
public PagedIterable<GHUser> listUsers() throws IOException {
return new PagedIterable<GHUser>() {
@Override
public PagedIterator<GHUser> _iterator(int pageSize) {
return new PagedIterator<GHUser>(retrieve().asIterator("/users", GHUser[].class, pageSize)) {
@Override
Expand Down Expand Up @@ -556,6 +564,33 @@ 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)}
* TODO: make this automatic.
*
* @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 @@ -620,6 +655,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 Expand Up @@ -855,6 +891,7 @@ public PagedIterable<GHRepository> listAllPublicRepositories() {
*/
public PagedIterable<GHRepository> listAllPublicRepositories(final String since) {
return new PagedIterable<GHRepository>() {
@Override
public PagedIterator<GHRepository> _iterator(int pageSize) {
return new PagedIterator<GHRepository>(retrieve().with("since",since).asIterator("/repositories", GHRepository[].class, pageSize)) {
@Override
Expand Down

0 comments on commit 56e3791

Please sign in to comment.