Skip to content

Commit

Permalink
Fixes hub4j#183: added a method listForks() to GHRepository
Browse files Browse the repository at this point in the history
listForks() will list all forks of a repository.
An optional sort argument is also supported.
  • Loading branch information
marc-guenther committed Apr 29, 2015
1 parent b976e0e commit f89c69b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,49 @@ 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<GHRepository> listForks() {
return listForks(null);
}

/**
* Lists up all the forks of this repository, sorted by the given sort order.
*/
public PagedIterable<GHRepository> listForks(final Sort sort) {
return new PagedIterable<GHRepository>() {
public PagedIterator<GHRepository> iterator() {
return new PagedIterator<GHRepository>(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);
}
};
}
};
}

// I do not know when to use list* or get*, also getForks() is already taken (returns the count),
// so I tried this, but than the listForks implementation above also works.
// I have no idea which one is the preferred way of doing things...
// /**
// * Retrieves the forks of this repository.
// */
// public List<GHRepository> getForkedRepositories() throws IOException {
// List<GHRepository> list = new ArrayList<GHRepository>(Arrays.asList(
// root.retrieve().to(getApiTailUrl("forks"), GHRepository[].class)));
// for (GHRepository h : list)
// h.wrap(root);
// return list;
// }

/**
* Forks this repository as your repository.
*
Expand Down

0 comments on commit f89c69b

Please sign in to comment.