diff --git a/pom.xml b/pom.xml index 9ada1d503b..ff3f7d820b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.43 + 1.44 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java diff --git a/src/main/java/org/kohsuke/github/GHCommit.java b/src/main/java/org/kohsuke/github/GHCommit.java index d7bb9492c7..0c02684215 100644 --- a/src/main/java/org/kohsuke/github/GHCommit.java +++ b/src/main/java/org/kohsuke/github/GHCommit.java @@ -5,6 +5,7 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.List; /** @@ -16,6 +17,55 @@ */ public class GHCommit { private GHRepository owner; + + private ShortInfo commit; + + /** + * Short summary of this commit. + */ + public static class ShortInfo { + private GHAuthor author; + private GHAuthor committer; + + private String message; + + private int comment_count; + + public GHAuthor getAuthor() { + return author; + } + + public GHAuthor getCommitter() { + return committer; + } + + /** + * Commit message. + */ + public String getMessage() { + return message; + } + + public int getCommentCount() { + return comment_count; + } + } + + public static class GHAuthor { + private String name,email,date; + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Date getDate() { + return GitHub.parseDate(date); + } + } public static class Stats { int total,additions,deletions; @@ -110,8 +160,14 @@ static class User { Stats stats; List parents; User author,committer; + + - /** + public ShortInfo getCommitShortInfo() { + return commit; + } + + /** * The repository that contains the commit. */ public GHRepository getOwner() { diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index 31be58a707..7a612007ac 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -3,7 +3,12 @@ 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 List getEmails() throws IOException { 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() diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 7cebf53765..b589a58312 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -192,8 +192,16 @@ public static GitHub connectAnonymously() throws IOException { return new GitHub(null,null,null); } + /** + * Is this an anonymous connection + * @return {@code true} if operations that require authentication will fail. + */ + public boolean isAnonymous() { + return login==null && encodedAuthorization==null; + } + /*package*/ void requireCredential() { - if (login==null && encodedAuthorization==null) + if (isAnonymous()) throw new IllegalStateException("This operation requires a credential but none is given to the GitHub constructor"); } diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index efe3dc4ae1..2fbdf02cde 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -332,6 +332,12 @@ public void testCommitStatus() throws Exception { assertEquals("oops!",state.getDescription()); assertEquals("http://jenkins-ci.org/",state.getTargetUrl()); } + + public void testCommitShortInfo() throws Exception { + GHCommit commit = gitHub.getUser("kohsuke").getRepository("test").getCommit("c77360d6f2ff2c2e6dd11828ad5dccf72419fa1b"); + assertEquals(commit.getCommitShortInfo().getAuthor().getName(), "Kohsuke Kawaguchi"); + assertEquals(commit.getCommitShortInfo().getMessage(), "Added a file"); + } public void testPullRequestPopulate() throws Exception { GHRepository r = gitHub.getUser("kohsuke").getRepository("github-api");