Merge pull request #272 from noctarius/master

Added support for the extended stargazers API in Github V3 API
This commit is contained in:
Kohsuke Kawaguchi
2016-06-03 14:04:24 +09:00
2 changed files with 73 additions and 1 deletions

View File

@@ -935,12 +935,36 @@ public class GHRepository extends GHObject {
}
/**
* Lists all the users who have starred this repo.
* Lists all the users who have starred this repo based on the old version of the API. For
* additional information, like date when the repository was starred, see {@link #listExtendedStargazers()}
*/
public PagedIterable<GHUser> listStargazers() {
return listUsers("stargazers");
}
/**
* Lists all the users who have starred this repo based on new version of the API, having extended
* information like the time when the repository was starred. For compatibility with the old API
* see {@link #listStargazers()}
*/
public PagedIterable<GHStargazer> listExtendedStargazers() {
return new PagedIterable<GHStargazer>() {
@Override
public PagedIterator<GHStargazer> _iterator(int pageSize) {
Requester requester = root.retrieve();
requester.setHeader("Accept", "application/vnd.github.v3.star+json");
return new PagedIterator<GHStargazer>(requester.asIterator(getApiTailUrl("stargazers"), GHStargazer[].class, pageSize)) {
@Override
protected void wrapUp(GHStargazer[] page) {
for (GHStargazer c : page) {
c.wrapUp(GHRepository.this);
}
}
};
}
};
}
private PagedIterable<GHUser> listUsers(final String suffix) {
return new PagedIterable<GHUser>() {
public PagedIterator<GHUser> _iterator(int pageSize) {

View File

@@ -0,0 +1,48 @@
package org.kohsuke.github;
import java.util.Date;
/**
* A stargazer at a repository on GitHub.
*
* @author noctarius
*/
public class GHStargazer {
private GHRepository repository;
private String starred_at;
private GHUser user;
/**
* Gets the repository that is stargazed
*
* @return the starred repository
*/
public GHRepository getRepository() {
return repository;
}
/**
* Gets the date when the repository was starred, however old stars before
* August 2012, will all show the date the API was changed to support starred_at.
*
* @return the date the stargazer was added
*/
public Date getStarredAt() {
return GitHub.parseDate(starred_at);
}
/**
* Gets the user that starred the repository
*
* @return the stargazer user
*/
public GHUser getUser() {
return user;
}
void wrapUp(GHRepository repository) {
this.repository = repository;
user.wrapUp(repository.root);
}
}