diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index 31be58a70..7a612007a 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -3,7 +3,12 @@ package org.kohsuke.github; import java.io.IOException; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; /** * Represents the account that's logging into GitHub. @@ -35,7 +40,50 @@ public class GHMyself extends GHUser { public List getPublicKeys() throws IOException { return Collections.unmodifiableList(Arrays.asList(root.retrieve().to("/user/keys", GHKey[].class))); } - + + /** + * Gets the organization that this user belongs to. + */ + public GHPersonSet getAllOrganizations() throws IOException { + GHPersonSet orgs = new GHPersonSet(); + Set names = new HashSet(); + for (GHOrganization o : root.retrieve().to("/user/orgs", GHOrganization[].class)) { + if (names.add(o.getLogin())) // in case of rumoured duplicates in the data + orgs.add(root.getOrganization(o.getLogin())); + } + return orgs; + } + + /** + * Gets the all repositories this user owns (public and private). + */ + public synchronized Map getAllRepositories() throws IOException { + Map repositories = new TreeMap(); + for (GHRepository r : listAllRepositories()) { + repositories.put(r.getName(),r); + } + return Collections.unmodifiableMap(repositories); + } + + /** + * Lists up all repositories this user owns (public and private). + * + * Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned. + */ + public PagedIterable listAllRepositories() { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator("/user/repos", GHRepository[].class)) { + @Override + protected void wrapUp(GHRepository[] page) { + for (GHRepository c : page) + c.wrap(root); + } + }; + } + }; + } + // public void addEmails(Collection emails) throws IOException { //// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails"); // root.retrieveWithAuth3()