From 9b750bedef39660d21f8e06165a8ee56df5cc797 Mon Sep 17 00:00:00 2001 From: Marc Guenther Date: Wed, 29 Apr 2015 18:15:04 +0200 Subject: [PATCH 1/4] Fixes #183: added a method listForks() to GHRepository listForks() will list all forks of a repository. An optional sort argument is also supported. --- .../java/org/kohsuke/github/GHRepository.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index e4459e84d6..59d0dead97 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -505,6 +505,35 @@ public void delete() throws IOException { } } + /** + * Sort orders for listing forks + */ + public static enum Sort { NEWEST, OLDEST, STARGAZERS } + + /** + * Lists all the forks of this repository. + */ + public PagedIterable listForks() { + return listForks(null); + } + + /** + * Lists up all the forks of this repository, sorted by the given sort order. + */ + public PagedIterable listForks(final Sort sort) { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("forks" + ((sort == null)?"":("?sort="+sort.toString().toLowerCase(Locale.ENGLISH)))), GHRepository[].class)) { + @Override + protected void wrapUp(GHRepository[] page) { + for (GHRepository c : page) + c.wrap(root); + } + }; + } + }; + } + /** * Forks this repository as your repository. * From a5425a3c716e96b249b4c2410ca710e4caab5e0e Mon Sep 17 00:00:00 2001 From: Marc Guenther Date: Thu, 28 May 2015 22:18:02 +0200 Subject: [PATCH 2/4] improved javadoc for listForks() --- src/main/java/org/kohsuke/github/GHRepository.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 59d0dead97..505ab14711 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -321,6 +321,10 @@ public boolean isFork() { return fork; } + /** + * Returns the number of all forks of this repository. + * This not only counts direct forks, but also forks of forks, and so on. + */ public int getForks() { return forks; } @@ -511,14 +515,15 @@ public void delete() throws IOException { public static enum Sort { NEWEST, OLDEST, STARGAZERS } /** - * Lists all the forks of this repository. + * Lists all the direct forks of this repository, sorted by {@link Sort#NEWEST Sort.NEWEST} */ public PagedIterable listForks() { return listForks(null); } /** - * Lists up all the forks of this repository, sorted by the given sort order. + * Lists all the direct forks of this repository, sorted by the given sort order. + * @param sort the sort order. If null, defaults to {@link Sort#NEWEST Sort.NEWEST}. */ public PagedIterable listForks(final Sort sort) { return new PagedIterable() { From a83aad22cac75f0789c0fe01d466a9e08c76bc06 Mon Sep 17 00:00:00 2001 From: Marc Guenther Date: Fri, 17 Jul 2015 09:33:48 +0200 Subject: [PATCH 3/4] renamed Sort enum, some cleanup, better javadoc --- .../java/org/kohsuke/github/GHRepository.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 505ab14711..f9337a5ef7 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -37,6 +37,7 @@ import java.util.*; import static java.util.Arrays.asList; +import static org.apache.commons.lang.ObjectUtils.defaultIfNull; /** * A repository on GitHub. @@ -512,10 +513,11 @@ public void delete() throws IOException { /** * Sort orders for listing forks */ - public static enum Sort { NEWEST, OLDEST, STARGAZERS } + public static enum ForkSort { NEWEST, OLDEST, STARGAZERS } /** - * Lists all the direct forks of this repository, sorted by {@link Sort#NEWEST Sort.NEWEST} + * Lists all the direct forks of this repository, sorted by + * github api default, currently {@link ForkSort#NEWEST ForkSort.NEWEST}. */ public PagedIterable listForks() { return listForks(null); @@ -523,16 +525,22 @@ public PagedIterable listForks() { /** * Lists all the direct forks of this repository, sorted by the given sort order. - * @param sort the sort order. If null, defaults to {@link Sort#NEWEST Sort.NEWEST}. + * @param sort the sort order. If null, defaults to github api default, + * currently {@link ForkSort#NEWEST ForkSort.NEWEST}. */ - public PagedIterable listForks(final Sort sort) { + public PagedIterable listForks(final ForkSort sort) { return new PagedIterable() { public PagedIterator iterator() { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("forks" + ((sort == null)?"":("?sort="+sort.toString().toLowerCase(Locale.ENGLISH)))), GHRepository[].class)) { + String sortParam = ""; + if (sort != null) { + sortParam = "?sort=" + sort.toString().toLowerCase(Locale.ENGLISH); + } + return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("forks" + sortParam), GHRepository[].class)) { @Override protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) + for (GHRepository c : page) { c.wrap(root); + } } }; } From 23c56ff887c2583c3c3c49d6cc9fd2442c775fe7 Mon Sep 17 00:00:00 2001 From: Marc Guenther Date: Fri, 17 Jul 2015 12:30:49 +0200 Subject: [PATCH 4/4] remove unused import --- src/main/java/org/kohsuke/github/GHRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index f9337a5ef7..502e0fb6c6 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -37,7 +37,6 @@ import java.util.*; import static java.util.Arrays.asList; -import static org.apache.commons.lang.ObjectUtils.defaultIfNull; /** * A repository on GitHub.