Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify creation of PagedIterables from requests #563

Merged
merged 3 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,11 @@ public void setPermissions(Map<String, String> permissions) {
*/
@Preview @Deprecated
public PagedIterable<GHAppInstallation> listInstallations() {
return new PagedIterable<GHAppInstallation>() {
public PagedIterator<GHAppInstallation> _iterator(int pageSize) {
return new PagedIterator<GHAppInstallation>(root.retrieve().withPreview(MACHINE_MAN).asIterator("/app/installations", GHAppInstallation[].class, pageSize)) {
protected void wrapUp(GHAppInstallation[] page) {
for (GHAppInstallation appInstallation : page) {
appInstallation.wrapUp(root);
}
}
};
}
};
return root.retrieve().withPreview(MACHINE_MAN)
.asPagedIterable(
"/app/installations",
GHAppInstallation[].class,
item -> item.wrapUp(root) );
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,11 @@ private GHUser resolveUser(User author) throws IOException {
* Lists up all the commit comments in this repository.
*/
public PagedIterable<GHCommitComment> listComments() {
return new PagedIterable<GHCommitComment>() {
public PagedIterator<GHCommitComment> _iterator(int pageSize) {
return new PagedIterator<GHCommitComment>(owner.root.retrieve().asIterator(String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha), GHCommitComment[].class, pageSize)) {
@Override
protected void wrapUp(GHCommitComment[] page) {
for (GHCommitComment c : page)
c.wrap(owner);
}
};
}
};
return owner.root.retrieve()
.asPagedIterable(
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha),
GHCommitComment[].class,
item -> item.wrap(owner) );
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/kohsuke/github/GHCommitComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException {

@Preview @Deprecated
public PagedIterable<GHReaction> listReactions() {
return new PagedIterable<GHReaction>() {
public PagedIterator<GHReaction> _iterator(int pageSize) {
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiTail()+"/reactions", GHReaction[].class, pageSize)) {
@Override
protected void wrapUp(GHReaction[] page) {
for (GHReaction c : page)
c.wrap(owner.root);
}
};
}
};
return owner.root.retrieve().withPreview(SQUIRREL_GIRL)
.asPagedIterable(
getApiTail()+"/reactions",
GHReaction[].class,
item -> item.wrap(owner.root) );
}

/**
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,10 @@ public GHCommitQueryBuilder until(long timestamp) {
* Lists up the commits with the criteria built so far.
*/
public PagedIterable<GHCommit> list() {
return new PagedIterable<GHCommit>() {
public PagedIterator<GHCommit> _iterator(int pageSize) {
return new PagedIterator<GHCommit>(req.asIterator(repo.getApiTailUrl("commits"), GHCommit[].class, pageSize)) {
protected void wrapUp(GHCommit[] page) {
for (GHCommit c : page)
c.wrapUp(repo);
}
};
}
};
return req
.asPagedIterable(
repo.getApiTailUrl("commits"),
GHCommit[].class,
item -> item.wrapUp(repo) );
}
}
15 changes: 5 additions & 10 deletions src/main/java/org/kohsuke/github/GHContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,11 @@ public PagedIterable<GHContent> listDirectoryContent() throws IOException {
if (!isDirectory())
throw new IllegalStateException(path+" is not a directory");

return new PagedIterable<GHContent>() {
public PagedIterator<GHContent> _iterator(int pageSize) {
return new PagedIterator<GHContent>(root.retrieve().asIterator(url, GHContent[].class, pageSize)) {
@Override
protected void wrapUp(GHContent[] page) {
GHContent.wrap(page, repository);
}
};
}
};
return root.retrieve()
.asPagedIterable(
url,
GHContent[].class,
item -> item.wrap(repository) );
}

@SuppressFBWarnings("DM_DEFAULT_ENCODING")
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/kohsuke/github/GHDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,11 @@ public GHDeploymentStatusBuilder createStatus(GHDeploymentState state) {
}

public PagedIterable<GHDeploymentStatus> listStatuses() {
return new PagedIterable<GHDeploymentStatus>() {
public PagedIterator<GHDeploymentStatus> _iterator(int pageSize) {
return new PagedIterator<GHDeploymentStatus>(root.retrieve().asIterator(statuses_url, GHDeploymentStatus[].class, pageSize)) {
@Override
protected void wrapUp(GHDeploymentStatus[] page) {
for (GHDeploymentStatus c : page)
c.wrap(owner);
}
};
}
};
return root.retrieve()
.asPagedIterable(
statuses_url,
GHDeploymentStatus[].class,
item -> item.wrap(owner) );
}

}
16 changes: 5 additions & 11 deletions src/main/java/org/kohsuke/github/GHGist.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,11 @@ public GHGist fork() throws IOException {
}

public PagedIterable<GHGist> listForks() {
return new PagedIterable<GHGist>() {
public PagedIterator<GHGist> _iterator(int pageSize) {
return new PagedIterator<GHGist>(root.retrieve().asIterator(getApiTailUrl("forks"), GHGist[].class, pageSize)) {
@Override
protected void wrapUp(GHGist[] page) {
for (GHGist c : page)
c.wrapUp(root);
}
};
}
};
return root.retrieve()
.asPagedIterable(
getApiTailUrl("forks"),
GHGist[].class,
item -> item.wrapUp(root) );
}

/**
Expand Down
46 changes: 14 additions & 32 deletions src/main/java/org/kohsuke/github/GHIssue.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,11 @@ public List<GHIssueComment> getComments() throws IOException {
* Obtains all the comments associated with this issue.
*/
public PagedIterable<GHIssueComment> listComments() throws IOException {
return new PagedIterable<GHIssueComment>() {
public PagedIterator<GHIssueComment> _iterator(int pageSize) {
return new PagedIterator<GHIssueComment>(root.retrieve().asIterator(getIssuesApiRoute() + "/comments", GHIssueComment[].class, pageSize)) {
protected void wrapUp(GHIssueComment[] page) {
for (GHIssueComment c : page)
c.wrapUp(GHIssue.this);
}
};
}
};
return root.retrieve()
.asPagedIterable(
getIssuesApiRoute() + "/comments",
GHIssueComment[].class,
item -> item.wrapUp(GHIssue.this) );
}

@Preview @Deprecated
Expand All @@ -314,17 +309,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException {

@Preview @Deprecated
public PagedIterable<GHReaction> listReactions() {
return new PagedIterable<GHReaction>() {
public PagedIterator<GHReaction> _iterator(int pageSize) {
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
@Override
protected void wrapUp(GHReaction[] page) {
for (GHReaction c : page)
c.wrap(owner.root);
}
};
}
};
return owner.root.retrieve().withPreview(SQUIRREL_GIRL)
.asPagedIterable(
getApiRoute()+"/reactions",
GHReaction[].class,
item -> item.wrap(owner.root) );
}

public void addAssignees(GHUser... assignees) throws IOException {
Expand Down Expand Up @@ -436,16 +425,9 @@ public URL getUrl() {
* See https://developer.github.com/v3/issues/events/
*/
public PagedIterable<GHIssueEvent> listEvents() throws IOException {
return new PagedIterable<GHIssueEvent>() {
public PagedIterator<GHIssueEvent> _iterator(int pageSize) {
return new PagedIterator<GHIssueEvent>(root.retrieve().asIterator(owner.getApiTailUrl(String.format("/issues/%s/events", number)), GHIssueEvent[].class, pageSize)) {
@Override
protected void wrapUp(GHIssueEvent[] page) {
for (GHIssueEvent c : page)
c.wrapUp(GHIssue.this);
}
};
}
};
return root.retrieve().asPagedIterable(
owner.getApiTailUrl(String.format("/issues/%s/events", number)),
GHIssueEvent[].class,
item -> item.wrapUp(GHIssue.this) );
}
}
17 changes: 6 additions & 11 deletions src/main/java/org/kohsuke/github/GHIssueComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,12 @@ public GHReaction createReaction(ReactionContent content) throws IOException {

@Preview @Deprecated
public PagedIterable<GHReaction> listReactions() {
return new PagedIterable<GHReaction>() {
public PagedIterator<GHReaction> _iterator(int pageSize) {
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
@Override
protected void wrapUp(GHReaction[] page) {
for (GHReaction c : page)
c.wrap(owner.root);
}
};
}
};
return owner.root.retrieve()
.withPreview(SQUIRREL_GIRL)
.asPagedIterable(
getApiRoute()+"/reactions",
GHReaction[].class,
item -> item.wrap(owner.root) );
}

private String getApiRoute() {
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/org/kohsuke/github/GHMyself.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,13 @@ public PagedIterable<GHRepository> listRepositories(final int pageSize) {
* @param repoType type of repository returned in the listing
*/
public PagedIterable<GHRepository> listRepositories(final int pageSize, final RepositoryListFilter repoType) {
return new PagedIterable<GHRepository>() {
public PagedIterator<GHRepository> _iterator(int pageSize) {
return new PagedIterator<GHRepository>(root.retrieve().with("type",repoType).asIterator("/user/repos", GHRepository[].class, pageSize)) {
@Override
protected void wrapUp(GHRepository[] page) {
for (GHRepository c : page)
c.wrap(root);
}
};
}
}.withPageSize(pageSize);
return root.retrieve()
.with("type",repoType)
.asPagedIterable(
"/user/repos",
GHRepository[].class,
item -> item.wrap(root)
).withPageSize(pageSize);
}

/**
Expand All @@ -191,16 +187,12 @@ public PagedIterable<GHMembership> listOrgMemberships() {
* Filter by a specific state
*/
public PagedIterable<GHMembership> listOrgMemberships(final GHMembership.State state) {
return new PagedIterable<GHMembership>() {
public PagedIterator<GHMembership> _iterator(int pageSize) {
return new PagedIterator<GHMembership>(root.retrieve().with("state",state).asIterator("/user/memberships/orgs", GHMembership[].class, pageSize)) {
@Override
protected void wrapUp(GHMembership[] page) {
GHMembership.wrap(page,root);
}
};
}
};
return root.retrieve()
.with("state",state)
.asPagedIterable(
"/user/memberships/orgs",
GHMembership[].class,
item -> item.wrap(root) );
}

/**
Expand Down
Loading