Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getUserPublicOrganizations method #510

Merged
merged 7 commits into from
Oct 25, 2019
37 changes: 35 additions & 2 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @Deprecated annotation to match @deprecated javadoc; this is best-practice but may start causing IDE or compile warnings for existing users of the methods.

If the method is truly deprecated, this is fine.

return connectToEnterpriseWithOAuth(apiUrl,null,oauthAccessToken);
}
Expand All @@ -194,6 +195,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 +209,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 +383,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 +405,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 @@ -493,6 +496,7 @@ public GHRepository getRepositoryById(String id) throws IOException {
@Preview @Deprecated
public PagedIterable<GHLicense> listLicenses() throws IOException {
return new PagedIterable<GHLicense>() {
@Override
public PagedIterator<GHLicense> _iterator(int pageSize) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @Override annotation to overridden methods as a matter of lint/best-practice.

return new PagedIterator<GHLicense>(retrieve().withPreview(DRAX).asIterator("/licenses", GHLicense[].class, pageSize)) {
@Override
Expand All @@ -510,6 +514,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 @@ -562,6 +567,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() );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One method takes a GH* object, here, a GHUser.

}

/**
* 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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other method takes a plain old String, so that users' code doesn't have to construct a GHUser just to look up organizations.

GHOrganization[] orgs = retrieve().to("/users/" + login + "/orgs", GHOrganization[].class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @@ -634,6 +665,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 @@ -869,6 +901,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