diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index e4459e84d6..502e0fb6c6 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; } @@ -505,6 +509,43 @@ public void delete() throws IOException { } } + /** + * Sort orders for listing forks + */ + public static enum ForkSort { NEWEST, OLDEST, STARGAZERS } + + /** + * 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); + } + + /** + * Lists all the direct forks of this repository, sorted by the given sort order. + * @param sort the sort order. If null, defaults to github api default, + * currently {@link ForkSort#NEWEST ForkSort.NEWEST}. + */ + public PagedIterable listForks(final ForkSort sort) { + return new PagedIterable() { + public PagedIterator iterator() { + 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) { + c.wrap(root); + } + } + }; + } + }; + } + /** * Forks this repository as your repository. *