From f28edbcf8fcb57a291aaa537bc57ea79c9bcda64 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Sun, 6 Oct 2019 18:45:22 -0700 Subject: [PATCH 1/3] Simplify creation of PagedIterables from requests --- src/main/java/org/kohsuke/github/GHApp.java | 16 +- .../java/org/kohsuke/github/GHCommit.java | 16 +- .../org/kohsuke/github/GHCommitComment.java | 16 +- .../kohsuke/github/GHCommitQueryBuilder.java | 15 +- .../java/org/kohsuke/github/GHContent.java | 15 +- .../java/org/kohsuke/github/GHDeployment.java | 16 +- src/main/java/org/kohsuke/github/GHGist.java | 16 +- src/main/java/org/kohsuke/github/GHIssue.java | 31 +- .../org/kohsuke/github/GHIssueComment.java | 17 +- .../java/org/kohsuke/github/GHMyself.java | 34 +- .../org/kohsuke/github/GHOrganization.java | 82 ++--- .../java/org/kohsuke/github/GHPerson.java | 22 +- .../java/org/kohsuke/github/GHProject.java | 18 +- .../org/kohsuke/github/GHProjectColumn.java | 18 +- .../org/kohsuke/github/GHPullRequest.java | 66 ++-- .../github/GHPullRequestQueryBuilder.java | 15 +- .../kohsuke/github/GHPullRequestReview.java | 18 +- .../github/GHPullRequestReviewComment.java | 17 +- .../java/org/kohsuke/github/GHRepository.java | 293 ++++++------------ .../github/GHRepositoryStatistics.java | 35 +-- src/main/java/org/kohsuke/github/GHTeam.java | 31 +- src/main/java/org/kohsuke/github/GHUser.java | 61 ++-- src/main/java/org/kohsuke/github/GitHub.java | 67 ++-- .../java/org/kohsuke/github/Requester.java | 35 +++ 24 files changed, 334 insertions(+), 636 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHApp.java b/src/main/java/org/kohsuke/github/GHApp.java index 97de867ccf..43ad91c5e0 100644 --- a/src/main/java/org/kohsuke/github/GHApp.java +++ b/src/main/java/org/kohsuke/github/GHApp.java @@ -107,17 +107,11 @@ public void setPermissions(Map permissions) { */ @Preview @Deprecated public PagedIterable listInstallations() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHCommit.java b/src/main/java/org/kohsuke/github/GHCommit.java index a817da2fd6..4891065ad5 100644 --- a/src/main/java/org/kohsuke/github/GHCommit.java +++ b/src/main/java/org/kohsuke/github/GHCommit.java @@ -327,17 +327,11 @@ private GHUser resolveUser(User author) throws IOException { * Lists up all the commit comments in this repository. */ public PagedIterable listComments() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHCommitComment.java b/src/main/java/org/kohsuke/github/GHCommitComment.java index f5ee76e478..a0047c3d15 100644 --- a/src/main/java/org/kohsuke/github/GHCommitComment.java +++ b/src/main/java/org/kohsuke/github/GHCommitComment.java @@ -98,17 +98,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException { @Preview @Deprecated public PagedIterable listReactions() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java b/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java index bac8c89ee6..64b3a0e935 100644 --- a/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java +++ b/src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java @@ -91,15 +91,10 @@ public GHCommitQueryBuilder until(long timestamp) { * Lists up the commits with the criteria built so far. */ public PagedIterable list() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } } diff --git a/src/main/java/org/kohsuke/github/GHContent.java b/src/main/java/org/kohsuke/github/GHContent.java index d7a7bd446f..db4a36d0b3 100644 --- a/src/main/java/org/kohsuke/github/GHContent.java +++ b/src/main/java/org/kohsuke/github/GHContent.java @@ -150,16 +150,11 @@ public PagedIterable listDirectoryContent() throws IOException { if (!isDirectory()) throw new IllegalStateException(path+" is not a directory"); - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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") diff --git a/src/main/java/org/kohsuke/github/GHDeployment.java b/src/main/java/org/kohsuke/github/GHDeployment.java index 42118f3329..7a7febf6ce 100644 --- a/src/main/java/org/kohsuke/github/GHDeployment.java +++ b/src/main/java/org/kohsuke/github/GHDeployment.java @@ -71,17 +71,11 @@ public GHDeploymentStatusBuilder createStatus(GHDeploymentState state) { } public PagedIterable listStatuses() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } } diff --git a/src/main/java/org/kohsuke/github/GHGist.java b/src/main/java/org/kohsuke/github/GHGist.java index 8de2f02585..19a2b626f2 100644 --- a/src/main/java/org/kohsuke/github/GHGist.java +++ b/src/main/java/org/kohsuke/github/GHGist.java @@ -145,17 +145,11 @@ public GHGist fork() throws IOException { } public PagedIterable listForks() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index d7525ff39c..07db9376a0 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -292,16 +292,11 @@ public List getComments() throws IOException { * Obtains all the comments associated with this issue. */ public PagedIterable listComments() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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 @@ -314,17 +309,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException { @Preview @Deprecated public PagedIterable listReactions() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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 { diff --git a/src/main/java/org/kohsuke/github/GHIssueComment.java b/src/main/java/org/kohsuke/github/GHIssueComment.java index 1b7eea5ebc..258718312c 100644 --- a/src/main/java/org/kohsuke/github/GHIssueComment.java +++ b/src/main/java/org/kohsuke/github/GHIssueComment.java @@ -109,17 +109,12 @@ public GHReaction createReaction(ReactionContent content) throws IOException { @Preview @Deprecated public PagedIterable listReactions() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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() { diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index 5ab9b4a270..e059f58226 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -156,17 +156,13 @@ public PagedIterable listRepositories(final int pageSize) { * @param repoType type of repository returned in the listing */ public PagedIterable listRepositories(final int pageSize, final RepositoryListFilter repoType) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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); } /** @@ -191,16 +187,12 @@ public PagedIterable listOrgMemberships() { * Filter by a specific state */ public PagedIterable listOrgMemberships(final GHMembership.State state) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index d79eba0efc..ead656242d 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -70,16 +70,11 @@ public Map getTeams() throws IOException { * List up all the teams. */ public PagedIterable listTeams() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/orgs/%s/teams", login), GHTeam[].class, pageSize)) { - @Override - protected void wrapUp(GHTeam[] page) { - GHTeam.wrapUp(page, GHOrganization.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/orgs/%s/teams", login), + GHTeam[].class, + item -> item.wrapUp(GHOrganization.this) ); } /** @@ -189,17 +184,12 @@ public PagedIterable listMembersWithFilter(String filter) throws IOExcep } private PagedIterable listMembers(final String suffix, final String filter) throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - String filterParams = (filter == null) ? "" : ("?filter=" + filter); - return new PagedIterator(root.retrieve().asIterator(String.format("/orgs/%s/%s%s", login, suffix, filterParams), GHUser[].class, pageSize)) { - @Override - protected void wrapUp(GHUser[] users) { - GHUser.wrap(users, root); - } - }; - } - }; + String filterParams = (filter == null) ? "" : ("?filter=" + filter); + return root.retrieve() + .asPagedIterable( + String.format("/orgs/%s/%s%s", login, suffix, filterParams), + GHUser[].class, + item -> item.wrapUp(root) ); } /** @@ -214,19 +204,12 @@ public void conceal(GHUser u) throws IOException { * @param status The status filter (all, open or closed). */ public PagedIterable listProjects(final GHProject.ProjectStateFilter status) throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().withPreview(INERTIA) + return root.retrieve().withPreview(INERTIA) .with("state", status) - .asIterator(String.format("/orgs/%s/projects", login), GHProject[].class, pageSize)) { - @Override - protected void wrapUp(GHProject[] page) { - for (GHProject c : page) - c.wrap(root); - } - }; - } - }; + .asPagedIterable( + String.format("/orgs/%s/projects", login), + GHProject[].class, + item -> item.wrap(root) ); } /** @@ -299,17 +282,11 @@ public List getPullRequests() throws IOException { * Lists events performed by a user (this includes private events if the caller is authenticated. */ public PagedIterable listEvents() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/orgs/%s/events", login), GHEventInfo[].class, pageSize)) { - @Override - protected void wrapUp(GHEventInfo[] page) { - for (GHEventInfo c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/orgs/%s/events", login), + GHEventInfo[].class, + item -> item.wrapUp(root) ); } /** @@ -321,17 +298,12 @@ protected void wrapUp(GHEventInfo[] page) { */ @Override public PagedIterable listRepositories(final int pageSize) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator("/orgs/" + login + "/repos", GHRepository[].class, pageSize)) { - @Override - protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) - c.wrap(root); - } - }; - } - }.withPageSize(pageSize); + return root.retrieve() + .asPagedIterable( + "/orgs/" + login + "/repos", + GHRepository[].class, + item -> item.wrap(root) + ).withPageSize(pageSize); } /** diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index b1be225965..137dd5b368 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -79,32 +79,28 @@ public PagedIterable listRepositories() { * Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned. */ public PagedIterable listRepositories(final int pageSize) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator("/users/" + login + "/repos", GHRepository[].class, pageSize)) { - @Override - protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) - c.wrap(root); - } - }; - } - }.withPageSize(pageSize); + return root.retrieve() + .asPagedIterable( + "/users/" + login + "/repos", + GHRepository[].class, + item -> item.wrap(root) + ).withPageSize(pageSize); } /** * Loads repository list in a paginated fashion. - * + * *

* For a person with a lot of repositories, GitHub returns the list of repositories in a paginated fashion. * Unlike {@link #getRepositories()}, this method allows the caller to start processing data as it arrives. - * + * * Every {@link Iterator#next()} call results in I/O. Exceptions that occur during the processing is wrapped * into {@link Error}. * * @deprecated * Use {@link #listRepositories()} */ + @Deprecated public synchronized Iterable> iterateRepositories(final int pageSize) { return new Iterable>() { public Iterator> iterator() { diff --git a/src/main/java/org/kohsuke/github/GHProject.java b/src/main/java/org/kohsuke/github/GHProject.java index 457cc17713..6645a05a3e 100644 --- a/src/main/java/org/kohsuke/github/GHProject.java +++ b/src/main/java/org/kohsuke/github/GHProject.java @@ -166,18 +166,12 @@ public void delete() throws IOException { public PagedIterable listColumns() throws IOException { final GHProject project = this; - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().withPreview(INERTIA) - .asIterator(String.format("/projects/%d/columns", id), GHProjectColumn[].class, pageSize)) { - @Override - protected void wrapUp(GHProjectColumn[] page) { - for (GHProjectColumn c : page) - c.wrap(project); - } - }; - } - }; + return root.retrieve() + .withPreview(INERTIA) + .asPagedIterable( + String.format("/projects/%d/columns", id), + GHProjectColumn[].class, + item -> item.wrap(project) ); } public GHProjectColumn createColumn(String name) throws IOException { diff --git a/src/main/java/org/kohsuke/github/GHProjectColumn.java b/src/main/java/org/kohsuke/github/GHProjectColumn.java index 01aeab530c..3b853962ff 100644 --- a/src/main/java/org/kohsuke/github/GHProjectColumn.java +++ b/src/main/java/org/kohsuke/github/GHProjectColumn.java @@ -73,18 +73,12 @@ public void delete() throws IOException { public PagedIterable listCards() throws IOException { final GHProjectColumn column = this; - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().withPreview(INERTIA) - .asIterator(String.format("/projects/columns/%d/cards", id), GHProjectCard[].class, pageSize)) { - @Override - protected void wrapUp(GHProjectCard[] page) { - for (GHProjectCard c : page) - c.wrap(column); - } - }; - } - }; + return root.retrieve() + .withPreview(INERTIA) + .asPagedIterable( + String.format("/projects/columns/%d/cards", id), + GHProjectCard[].class, + item -> item.wrap(column) ); } public GHProjectCard createCard(String note) throws IOException { diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index cff06e5ea5..350d0795bb 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -269,72 +269,44 @@ public void refresh() throws IOException { * Retrieves all the files associated to this pull request. */ public PagedIterable listFiles() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("%s/files", getApiRoute()), - GHPullRequestFileDetail[].class, pageSize)) { - @Override - protected void wrapUp(GHPullRequestFileDetail[] page) { - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("%s/files", getApiRoute()), + GHPullRequestFileDetail[].class, + null); } /** * Retrieves all the reviews associated to this pull request. */ public PagedIterable listReviews() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve() - .asIterator(String.format("%s/reviews", getApiRoute()), - GHPullRequestReview[].class, pageSize)) { - @Override - protected void wrapUp(GHPullRequestReview[] page) { - for (GHPullRequestReview r: page) { - r.wrapUp(GHPullRequest.this); - } - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("%s/reviews", getApiRoute()), + GHPullRequestReview[].class, + item -> item.wrapUp(GHPullRequest.this)); } /** * Obtains all the review comments associated with this pull request. */ public PagedIterable listReviewComments() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiRoute() + COMMENTS_ACTION, - GHPullRequestReviewComment[].class, pageSize)) { - protected void wrapUp(GHPullRequestReviewComment[] page) { - for (GHPullRequestReviewComment c : page) - c.wrapUp(GHPullRequest.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiRoute() + COMMENTS_ACTION, + GHPullRequestReviewComment[].class, + item -> item.wrapUp(GHPullRequest.this) ); } /** * Retrieves all the commits associated to this pull request. */ public PagedIterable listCommits() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator( + return root.retrieve() + .asPagedIterable( String.format("%s/commits", getApiRoute()), - GHPullRequestCommitDetail[].class, pageSize)) { - @Override - protected void wrapUp(GHPullRequestCommitDetail[] page) { - for (GHPullRequestCommitDetail c : page) - c.wrapUp(GHPullRequest.this); - } - }; - } - }; + GHPullRequestCommitDetail[].class, + item -> item.wrapUp(GHPullRequest.this) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java b/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java index 74bae03e2a..70e475671e 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java @@ -46,16 +46,9 @@ public GHPullRequestQueryBuilder direction(GHDirection d) { @Override public PagedIterable list() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(req.asIterator(repo.getApiTailUrl("pulls"), GHPullRequest[].class, pageSize)) { - @Override - protected void wrapUp(GHPullRequest[] page) { - for (GHPullRequest pr : page) - pr.wrapUp(repo); - } - }; - } - }; + return req.asPagedIterable( + repo.getApiTailUrl("pulls"), + GHPullRequest[].class, + item -> item.wrapUp(repo) ); } } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReview.java b/src/main/java/org/kohsuke/github/GHPullRequestReview.java index e5b025e5e3..b61a65ca05 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReview.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReview.java @@ -148,18 +148,10 @@ public void dismiss(String message) throws IOException { * Obtains all the review comments associated with this pull request review. */ public PagedIterable listReviewComments() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator( - owner.root.retrieve() - .asIterator(getApiRoute() + "/comments", - GHPullRequestReviewComment[].class, pageSize)) { - protected void wrapUp(GHPullRequestReviewComment[] page) { - for (GHPullRequestReviewComment c : page) - c.wrapUp(owner); - } - }; - } - }; + return owner.root.retrieve() + .asPagedIterable( + getApiRoute() + "/comments", + GHPullRequestReviewComment[].class, + item -> item.wrapUp(owner) ); } } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java index 5cda4abb0a..8848c28282 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java @@ -148,16 +148,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException { @Preview @Deprecated public PagedIterable listReactions() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 65274c493f..566fd36157 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -118,17 +118,11 @@ public PagedIterable getDeploymentStatuses(final int id) thr public PagedIterable listDeployments(String sha,String ref,String task,String environment){ List params = Arrays.asList(getParam("sha", sha), getParam("ref", ref), getParam("task", task), getParam("environment", environment)); final String deploymentsUrl = getApiTailUrl("deployments") + "?"+ join(params,"&"); - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(deploymentsUrl, GHDeployment[].class, pageSize)) { - @Override - protected void wrapUp(GHDeployment[] page) { - for (GHDeployment c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + deploymentsUrl, + GHDeployment[].class, + item -> item.wrap(GHRepository.this) ); } /** @@ -284,17 +278,11 @@ public List getIssues(GHIssueState state, GHMilestone milestone) throws * Lists up all the issues in this repository. */ public PagedIterable listIssues(final GHIssueState state) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().with("state",state).asIterator(getApiTailUrl("issues"), GHIssue[].class, pageSize)) { - @Override - protected void wrapUp(GHIssue[] page) { - for (GHIssue c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve().with("state",state) + .asPagedIterable( + getApiTailUrl("issues"), + GHIssue[].class, + item -> item.wrap(GHRepository.this) ); } public GHReleaseBuilder createRelease(String tag) { @@ -348,31 +336,19 @@ public GHRelease getLatestRelease() throws IOException { } public PagedIterable listReleases() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("releases"), GHRelease[].class, pageSize)) { - @Override - protected void wrapUp(GHRelease[] page) { - for (GHRelease c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl("releases"), + GHRelease[].class, + item -> item.wrap(GHRepository.this) ); } public PagedIterable listTags() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("tags"), GHTag[].class, pageSize)) { - @Override - protected void wrapUp(GHTag[] page) { - for (GHTag c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl("tags"), + GHTag[].class, + item -> item.wrap(GHRepository.this) ); } /** @@ -714,18 +690,11 @@ public PagedIterable listForks() { * currently {@link ForkSort#NEWEST ForkSort.NEWEST}. */ public PagedIterable listForks(final ForkSort sort) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().with("sort",sort).asIterator(getApiTailUrl("forks"), GHRepository[].class, pageSize)) { - @Override - protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) { - c.wrap(root); - } - } - }; - } - }; + return root.retrieve().with("sort",sort) + .asPagedIterable( + getApiTailUrl("forks"), + GHRepository[].class, + item -> item.wrap(root) ); } /** @@ -920,17 +889,11 @@ public GHRef[] getRefs() throws IOException { */ public PagedIterable listRefs() throws IOException { final String url = String.format("/repos/%s/%s/git/refs", getOwnerName(), name); - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(url, GHRef[].class, pageSize)) { - protected void wrapUp(GHRef[] page) { - for(GHRef p: page) { - p.wrap(root); - } - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + url, + GHRef[].class, + item -> item.wrap(root) ); } /** @@ -952,15 +915,11 @@ public GHRef[] getRefs(String refType) throws IOException { */ public PagedIterable listRefs(String refType) throws IOException { final String url = String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType); - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(url, GHRef[].class, pageSize)) { - protected void wrapUp(GHRef[] page) { - // no-op - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + url, + GHRef[].class, + item -> item.wrap(root)); } /** @@ -991,7 +950,7 @@ public GHRef getRef(String refName) throws IOException { public GHTagObject getTagObject(String sha) throws IOException { return root.retrieve().to(getApiTailUrl("git/tags/" + sha), GHTagObject.class).wrap(this); } - + /** * Retrive a tree of the given type for the current GitHub repository. * @@ -1074,16 +1033,11 @@ public GHCommitBuilder createCommit() { * Lists all the commits. */ public PagedIterable listCommits() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/commits", getOwnerName(), name), GHCommit[].class, pageSize)) { - protected void wrapUp(GHCommit[] page) { - for (GHCommit c : page) - c.wrapUp(GHRepository.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/repos/%s/%s/commits", getOwnerName(), name), + GHCommit[].class, + item -> item.wrapUp(GHRepository.this) ); } /** @@ -1097,17 +1051,11 @@ public GHCommitQueryBuilder queryCommits() { * Lists up all the commit comments in this repository. */ public PagedIterable listCommitComments() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/comments", getOwnerName(), name), GHCommitComment[].class, pageSize)) { - @Override - protected void wrapUp(GHCommitComment[] page) { - for (GHCommitComment c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/repos/%s/%s/comments", getOwnerName(), name), + GHCommitComment[].class, + item -> item.wrap(GHRepository.this) ); } /** @@ -1148,17 +1096,11 @@ private GHContentWithLicense getLicenseContent_() throws IOException { * Lists all the commit statues attached to the given commit, newer ones first. */ public PagedIterable listCommitStatuses(final String sha1) throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/statuses/%s", getOwnerName(), name, sha1), GHCommitStatus[].class, pageSize)) { - @Override - protected void wrapUp(GHCommitStatus[] page) { - for (GHCommitStatus c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/repos/%s/%s/statuses/%s", getOwnerName(), name, sha1), + GHCommitStatus[].class, + item -> item.wrapUp(root) ); } /** @@ -1199,17 +1141,11 @@ public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, Strin * Lists repository events. */ public PagedIterable listEvents() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/events", getOwnerName(), name), GHEventInfo[].class, pageSize)) { - @Override - protected void wrapUp(GHEventInfo[] page) { - for (GHEventInfo c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/repos/%s/%s/events", getOwnerName(), name), + GHEventInfo[].class, + item -> item.wrapUp(root) ); } /** @@ -1218,19 +1154,12 @@ protected void wrapUp(GHEventInfo[] page) { * https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository */ public PagedIterable listLabels() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve() + return root.retrieve() .withPreview(SYMMETRA) - .asIterator(getApiTailUrl("labels"), GHLabel[].class, pageSize)) { - @Override - protected void wrapUp(GHLabel[] page) { - for (GHLabel c : page) - c.wrapUp(GHRepository.this); - } - }; - } - }; + .asPagedIterable( + getApiTailUrl("labels"), + GHLabel[].class, + item -> item.wrapUp(GHRepository.this) ); } public GHLabel getLabel(String name) throws IOException { @@ -1266,16 +1195,11 @@ public GHLabel createLabel(String name, String color, String description) throws * Lists all the invitations. */ public PagedIterable listInvitations() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/invitations", getOwnerName(), name), GHInvitation[].class, pageSize)) { - protected void wrapUp(GHInvitation[] page) { - for (GHInvitation c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/repos/%s/%s/invitations", getOwnerName(), name), + GHInvitation[].class, + item -> item.wrapUp(root) ); } /** @@ -1301,34 +1225,20 @@ public PagedIterable listStargazers() { * see {@link #listStargazers()} */ public PagedIterable listStargazers2() { - return new PagedIterable() { - @Override - public PagedIterator _iterator(int pageSize) { - Requester requester = root.retrieve(); - requester.setHeader("Accept", "application/vnd.github.v3.star+json"); - return new PagedIterator(requester.asIterator(getApiTailUrl("stargazers"), GHStargazer[].class, pageSize)) { - @Override - protected void wrapUp(GHStargazer[] page) { - for (GHStargazer c : page) { - c.wrapUp(GHRepository.this); - } - } - }; - } - }; + return root.retrieve() + .withPreview("application/vnd.github.v3.star+json") + .asPagedIterable( + getApiTailUrl("stargazers"), + GHStargazer[].class, + item -> item.wrapUp(GHRepository.this) ); } private PagedIterable listUsers(final String suffix) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl(suffix), GHUser[].class, pageSize)) { - protected void wrapUp(GHUser[] page) { - for (GHUser c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl(suffix), + GHUser[].class, + item -> item.wrapUp(root) ); } /** @@ -1491,17 +1401,11 @@ public Map getMilestones() throws IOException { * Lists up all the milestones in this repository. */ public PagedIterable listMilestones(final GHIssueState state) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().with("state",state).asIterator(getApiTailUrl("milestones"), GHMilestone[].class, pageSize)) { - @Override - protected void wrapUp(GHMilestone[] page) { - for (GHMilestone c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + return root.retrieve().with("state",state) + .asPagedIterable( + getApiTailUrl("milestones"), + GHMilestone[].class, + item -> item.wrap(GHRepository.this) ); } public GHMilestone getMilestone(int number) throws IOException { @@ -1666,17 +1570,11 @@ public GHSubscription getSubscription() throws IOException { } public PagedIterable listContributors() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("contributors"), Contributor[].class, pageSize)) { - @Override - protected void wrapUp(Contributor[] page) { - for (Contributor c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl("contributors"), + Contributor[].class, + item -> item.wrapUp(root) ); } public static class Contributor extends GHUser { @@ -1724,19 +1622,12 @@ public GHProject createProject(String name, String body) throws IOException { * @param status The status filter (all, open or closed). */ public PagedIterable listProjects(final GHProject.ProjectStateFilter status) throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().withPreview(INERTIA) + return root.retrieve().withPreview(INERTIA) .with("state", status) - .asIterator(getApiTailUrl("projects"), GHProject[].class, pageSize)) { - @Override - protected void wrapUp(GHProject[] page) { - for (GHProject c : page) - c.wrap(GHRepository.this); - } - }; - } - }; + .asPagedIterable( + getApiTailUrl("projects"), + GHProject[].class, + item -> item.wrap(GHRepository.this) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java b/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java index 4280d1e880..3d8187da20 100644 --- a/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java +++ b/src/main/java/org/kohsuke/github/GHRepositoryStatistics.java @@ -68,19 +68,11 @@ public PagedIterable getContributorStats(boolean waitTillReady * are still being cached. */ private PagedIterable getContributorStatsImpl() throws IOException { - return new PagedIterable() { - @Override - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("contributors"), ContributorStats[].class, pageSize)) { - @Override - protected void wrapUp(ContributorStats[] page) { - for (ContributorStats c : page) { - c.wrapUp(root); - } - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl("contributors"), + ContributorStats[].class, + item -> item.wrapUp(root) ); } @SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", @@ -202,18 +194,11 @@ public String toString() { * https://developer.github.com/v3/repos/statistics/#get-the-last-year-of-commit-activity-data */ public PagedIterable getCommitActivity() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("commit_activity"), CommitActivity[].class, pageSize)) { - @Override - protected void wrapUp(CommitActivity[] page) { - for (CommitActivity c : page) { - c.wrapUp(root); - } - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl("commit_activity"), + CommitActivity[].class, + item -> item.wrapUp(root) ); } @SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", diff --git a/src/main/java/org/kohsuke/github/GHTeam.java b/src/main/java/org/kohsuke/github/GHTeam.java index 3a6af444e8..00f43e194b 100644 --- a/src/main/java/org/kohsuke/github/GHTeam.java +++ b/src/main/java/org/kohsuke/github/GHTeam.java @@ -85,16 +85,11 @@ public int getId() { * Retrieves the current members. */ public PagedIterable listMembers() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(api("/members"), GHUser[].class, pageSize)) { - @Override - protected void wrapUp(GHUser[] page) { - GHUser.wrap(page, root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + api("/members"), + GHUser[].class, + item -> item.wrapUp(root) ); } public Set getMembers() throws IOException { @@ -122,17 +117,11 @@ public Map getRepositories() throws IOException { } public PagedIterable listRepositories() { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(api("/repos"), GHRepository[].class, pageSize)) { - @Override - protected void wrapUp(GHRepository[] page) { - for (GHRepository r : page) - r.wrap(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + api("/repos"), + GHRepository[].class, + item -> item.wrap(root) ); } /** diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java index cd365b82bd..d794a8c74f 100644 --- a/src/main/java/org/kohsuke/github/GHUser.java +++ b/src/main/java/org/kohsuke/github/GHUser.java @@ -84,15 +84,11 @@ public PagedIterable listFollowers() { } private PagedIterable listUser(final String suffix) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl(suffix), GHUser[].class, pageSize)) { - protected void wrapUp(GHUser[] page) { - GHUser.wrap(page,root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl(suffix), + GHUser[].class, + item -> item.wrapUp(root) ); } /** @@ -112,16 +108,11 @@ public PagedIterable listStarredRepositories() { } private PagedIterable listRepositories(final String suffix) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl(suffix), GHRepository[].class, pageSize)) { - protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) - c.wrap(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + getApiTailUrl(suffix), + GHRepository[].class, + item -> item.wrap(root) ); } /** @@ -169,34 +160,22 @@ public GHPersonSet getOrganizations() throws IOException { * Lists events performed by a user (this includes private events if the caller is authenticated. */ public PagedIterable listEvents() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/users/%s/events", login), GHEventInfo[].class, pageSize)) { - @Override - protected void wrapUp(GHEventInfo[] page) { - for (GHEventInfo c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/users/%s/events", login), + GHEventInfo[].class, + item -> item.wrapUp(root) ); } /** * Lists Gists created by this user. */ public PagedIterable listGists() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(String.format("/users/%s/gists", login), GHGist[].class, pageSize)) { - @Override - protected void wrapUp(GHGist[] page) { - for (GHGist c : page) - c.wrapUp(GHUser.this); - } - }; - } - }; + return root.retrieve() + .asPagedIterable( + String.format("/users/%s/gists", login), + GHGist[].class, + item -> item.wrapUp(GHUser.this) ); } @Override diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 78098e0ac1..a8118a066f 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -447,19 +447,12 @@ public PagedIterable listOrganizations() { * @see List All Orgs - Parameters */ public PagedIterable listOrganizations(final String since) { - return new PagedIterable() { - @Override - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(retrieve().with("since",since) - .asIterator("/organizations", GHOrganization[].class, pageSize)) { - @Override - protected void wrapUp(GHOrganization[] page) { - for (GHOrganization c : page) - c.wrapUp(GitHub.this); - } - }; - } - }; + return retrieve() + .with("since",since) + .asPagedIterable( + "/organizations", + GHOrganization[].class, + item -> item.wrapUp(GitHub.this) ); } /** @@ -487,34 +480,22 @@ public GHRepository getRepositoryById(String id) throws IOException { * @return a list of popular open source licenses */ public PagedIterable listLicenses() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(retrieve().asIterator("/licenses", GHLicense[].class, pageSize)) { - @Override - protected void wrapUp(GHLicense[] page) { - for (GHLicense c : page) - c.wrap(GitHub.this); - } - }; - } - }; + return retrieve() + .asPagedIterable( + "/licenses", + GHLicense[].class, + item -> item.wrap(GitHub.this) ); } /** * Returns a list of all users. */ public PagedIterable listUsers() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(retrieve().asIterator("/users", GHUser[].class, pageSize)) { - @Override - protected void wrapUp(GHUser[] page) { - for (GHUser u : page) - u.wrapUp(GitHub.this); - } - }; - } - }; + return retrieve() + .asPagedIterable( + "/users", + GHUser[].class, + item -> item.wrapUp(GitHub.this) ); } /** @@ -902,17 +883,11 @@ public PagedIterable listAllPublicRepositories() { * @see documentation */ public PagedIterable listAllPublicRepositories(final String since) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(retrieve().with("since",since).asIterator("/repositories", GHRepository[].class, pageSize)) { - @Override - protected void wrapUp(GHRepository[] page) { - for (GHRepository c : page) - c.wrap(GitHub.this); - } - }; - } - }; + return retrieve().with("since",since) + .asPagedIterable( + "/repositories", + GHRepository[].class, + item -> item.wrap(GitHub.this) ); } /** diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index d79bb48124..b2a2a2f9b8 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -54,6 +54,7 @@ import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; +import java.util.function.Consumer; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -439,6 +440,40 @@ private boolean isMethodWithBody() { return forceBody || !METHODS_WITHOUT_BODY.contains(method); } + /*package*/ PagedIterable asPagedIterable(String tailApiUrl, Class type, Consumer consumer) { + return new PagedIterableWithConsumer(type, this, tailApiUrl, consumer); + } + + private static class PagedIterableWithConsumer extends PagedIterable { + + private final Class clazz; + private final Requester requester; + private final String tailApiUrl; + private final Consumer consumer; + + public PagedIterableWithConsumer(Class clazz, Requester requester, String tailApiUrl, Consumer consumer) { + this.clazz = clazz; + this.tailApiUrl = tailApiUrl; + this.requester = requester; + this.consumer = consumer; + } + + @Override + public PagedIterator _iterator(int pageSize) { + final Iterator iterator = requester.asIterator(tailApiUrl, clazz, pageSize); + return new PagedIterator(iterator) { + @Override + protected void wrapUp(S[] page) { + if (consumer != null) { + for (S item : page) { + consumer.accept(item); + } + } + } + }; + } + } + /** * Loads paginated resources. * From 9dd44c13e45e0688267a2728689580dc8f4578e6 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Mon, 7 Oct 2019 19:17:59 -0700 Subject: [PATCH 2/3] Update for AuthorizationList --- src/main/java/org/kohsuke/github/GitHub.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index a8118a066f..af3d21179f 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -683,17 +683,11 @@ public GHAuthorization resetAuth(@Nonnull String clientId, @Nonnull String acces * @see List your authorizations */ public PagedIterable listMyAuthorizations() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(retrieve().asIterator("/authorizations", GHAuthorization[].class, pageSize)) { - @Override - protected void wrapUp(GHAuthorization[] page) { - for (GHAuthorization u : page) - u.wrap(GitHub.this); - } - }; - } - }; + return retrieve() + .asPagedIterable( + "/authorizations", + GHAuthorization[].class, + item -> item.wrap(GitHub.this) ); } /** From 281c92797105575eab81b7d97539c9af0e1b2c7f Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 11 Oct 2019 13:11:08 -0700 Subject: [PATCH 3/3] Update GHIssueEvent PagedIterable --- src/main/java/org/kohsuke/github/GHIssue.java | 15 ++++----------- .../java/org/kohsuke/github/GHRepository.java | 15 ++++----------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index 07db9376a0..20343266b7 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -425,16 +425,9 @@ public URL getUrl() { * See https://developer.github.com/v3/issues/events/ */ public PagedIterable listEvents() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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) ); } } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 566fd36157..750f7d97b6 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -1701,17 +1701,10 @@ String getApiTailUrl(String tail) { * See https://developer.github.com/v3/issues/events/#list-events-for-a-repository */ public PagedIterable listIssueEvents() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("issues/events"), GHIssueEvent[].class, pageSize)) { - @Override - protected void wrapUp(GHIssueEvent[] page) { - for (GHIssueEvent c : page) - c.wrapUp(root); - } - }; - } - }; + return root.retrieve().asPagedIterable( + getApiTailUrl("issues/events"), + GHIssueEvent[].class, + item -> item.wrapUp(root) ); } /**