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

Paginator #1730

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
64 changes: 64 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppInstallationsIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public GHAppInstallationsIterable(GitHub root) {
this.root = root;
}

@Nonnull
@Override
public Paginator<GHAppInstallation> _paginator(int pageSize, int startPage) {
final GitHubRequest request = root.createRequest().withUrlPath(APP_INSTALLATIONS_URL).build();

return new Paginator<>(
adapt(GitHubPaginator
.create(root.getClient(), GHAppInstallationsPage.class, request, pageSize, startPage)),
null);
}

/**
* Iterator.
*
Expand Down Expand Up @@ -63,4 +74,57 @@ public GHAppInstallation[] next() {
}
};
}

protected NavigablePageIterator<GHAppInstallation[]> adapt(
final NavigablePageIterator<GHAppInstallationsPage> base) {
return new NavigablePageIterator<GHAppInstallation[]>() {
@Override
public boolean hasPrevious() {
return base.hasPrevious();
}

@Override
public GHAppInstallation[] previous() {
return base.previous().getInstallations();
}

@Override
public GHAppInstallation[] first() {
return base.first().getInstallations();
}

@Override
public GHAppInstallation[] last() {
return base.last().getInstallations();
}

@Override
public int totalCount() {
return base.totalCount();
}

@Override
public int currentPage() {
return base.currentPage();
}

@Override
public GHAppInstallation[] jumpToPage(int page) {
return base.jumpToPage(page).getInstallations();
}

@Override
public void refresh() {
base.refresh();
}

public boolean hasNext() {
return base.hasNext();
}

public GHAppInstallation[] next() {
return base.next().getInstallations();
}
};
}
}
63 changes: 63 additions & 0 deletions src/main/java/org/kohsuke/github/GHArtifactsIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public GHArtifactsIterable(GHRepository owner, GitHubRequest.Builder<?> requestB
this.request = requestBuilder.build();
}

@Nonnull
@Override
public Paginator<GHArtifact> _paginator(int pageSize, int startPage) {
return new Paginator<>(
adapt(GitHubPaginator
.create(owner.root().getClient(), GHArtifactsPage.class, request, pageSize, startPage)),
null);
}

/**
* Iterator.
*
Expand Down Expand Up @@ -64,4 +73,58 @@ public GHArtifact[] next() {
}
};
}

protected NavigablePageIterator<GHArtifact[]> adapt(final NavigablePageIterator<GHArtifactsPage> base) {
return new NavigablePageIterator<GHArtifact[]>() {
@Override
public boolean hasPrevious() {
return base.hasPrevious();
}

@Override
public GHArtifact[] previous() {
return base.previous().getArtifacts(owner);
}

@Override
public GHArtifact[] first() {
return base.first().getArtifacts(owner);
}

@Override
public GHArtifact[] last() {
return base.last().getArtifacts(owner);
}

@Override
public int totalCount() {
return base.totalCount();
}

@Override
public int currentPage() {
return base.currentPage();
}

@Override
public boolean hasNext() {
return base.hasNext();
}

@Override
public GHArtifact[] next() {
return base.next().getArtifacts(owner);
}

@Override
public GHArtifact[] jumpToPage(int page) {
return base.jumpToPage(page).getArtifacts(owner);
}

@Override
public void refresh() {
base.refresh();
}
};
}
}
63 changes: 63 additions & 0 deletions src/main/java/org/kohsuke/github/GHCheckRunsIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public GHCheckRunsIterable(GHRepository owner, GitHubRequest request) {
this.request = request;
}

@Nonnull
@Override
public Paginator<GHCheckRun> _paginator(int pageSize, int startPage) {
return new Paginator<>(
adapt(GitHubPaginator
.create(owner.root().getClient(), GHCheckRunsPage.class, request, pageSize, startPage)),
null);
}

/**
* Iterator.
*
Expand Down Expand Up @@ -64,4 +73,58 @@ public GHCheckRun[] next() {
}
};
}

protected NavigablePageIterator<GHCheckRun[]> adapt(final NavigablePageIterator<GHCheckRunsPage> base) {
return new NavigablePageIterator<GHCheckRun[]>() {
@Override
public boolean hasPrevious() {
return base.hasPrevious();
}

@Override
public GHCheckRun[] previous() {
return base.previous().getCheckRuns(owner);
}

@Override
public GHCheckRun[] first() {
return base.first().getCheckRuns(owner);
}

@Override
public GHCheckRun[] last() {
return base.last().getCheckRuns(owner);
}

@Override
public int totalCount() {
return base.totalCount();
}

@Override
public int currentPage() {
return base.currentPage();
}

@Override
public boolean hasNext() {
return base.hasNext();
}

@Override
public GHCheckRun[] next() {
return base.next().getCheckRuns(owner);
}

@Override
public GHCheckRun[] jumpToPage(int page) {
return base.jumpToPage(page).getCheckRuns(owner);
}

@Override
public void refresh() {
base.refresh();
}
};
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommitFileIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public GHCommitFileIterable(GHRepository owner, String sha, List<File> files) {
this.files = files != null ? files.toArray(new File[0]) : null;
}

@Nonnull
@Override
public Paginator<File> _paginator(int pageSize, int startPage) {
throw new UnsupportedOperationException();
}

/**
* Iterator.
*
Expand Down
89 changes: 84 additions & 5 deletions src/main/java/org/kohsuke/github/GHCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ public PagedIterable<Commit> listCommits() {
} else {
// if not using paginated commits, adapt the returned commits array
return new PagedIterable<Commit>() {
@Nonnull
@Override
public Paginator<Commit> _paginator(int pageSize, int startPage) {
throw new UnsupportedOperationException("Paginator nor supported if not using paginated commits.");
}

@Nonnull
@Override
public PagedIterator<Commit> _iterator(int pageSize) {
Expand Down Expand Up @@ -373,6 +379,21 @@ class GHCompareCommitsIterable extends PagedIterable<Commit> {
public GHCompareCommitsIterable() {
}

@Nonnull
@Override
public Paginator<Commit> _paginator(int pageSize, int startPage) {
GitHubRequest request = getRequest();

// page_size must be set for GHCompare commit pagination
if (pageSize == 0) {
pageSize = 10;
}
return new Paginator<>(
adapt(GitHubPaginator
.create(owner.root().getClient(), GHCompare.class, request, pageSize, startPage)),
null);
}

/**
* Iterator.
*
Expand All @@ -383,11 +404,7 @@ public GHCompareCommitsIterable() {
@Nonnull
@Override
public PagedIterator<Commit> _iterator(int pageSize) {
GitHubRequest request = owner.root()
.createRequest()
.injectMappingValue("GHCompare_usePaginatedCommits", usePaginatedCommits)
.withUrlPath(owner.getApiTailUrl(url.substring(url.lastIndexOf("/compare/"))))
.build();
GitHubRequest request = getRequest();

// page_size must be set for GHCompare commit pagination
if (pageSize == 0) {
Expand All @@ -398,6 +415,14 @@ public PagedIterator<Commit> _iterator(int pageSize) {
item -> item.wrapUp(owner));
}

private GitHubRequest getRequest() {
return owner.root()
.createRequest()
.injectMappingValue("GHCompare_usePaginatedCommits", usePaginatedCommits)
.withUrlPath(owner.getApiTailUrl(url.substring(url.lastIndexOf("/compare/"))))
.build();
}

/**
* Adapt.
*
Expand All @@ -420,5 +445,59 @@ public Commit[] next() {
}
};
}

protected NavigablePageIterator<Commit[]> adapt(final NavigablePageIterator<GHCompare> base) {
return new NavigablePageIterator<Commit[]>() {
@Override
public boolean hasPrevious() {
return base.hasPrevious();
}

@Override
public Commit[] previous() {
return base.previous().commits;
}

@Override
public Commit[] first() {
return base.first().commits;
}

@Override
public Commit[] last() {
return base.last().commits;
}

@Override
public int totalCount() {
return base.totalCount();
}

@Override
public int currentPage() {
return base.currentPage();
}

@Override
public boolean hasNext() {
return base.hasNext();
}

@Override
public Commit[] next() {
return base.next().commits;
}

@Override
public Commit[] jumpToPage(int page) {
return base.jumpToPage(page).commits;
}

@Override
public void refresh() {
base.refresh();
}
};
}
}
}
Loading
Loading