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

Issue/1669 #2

Merged
merged 2 commits into from
Jun 30, 2023
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
22 changes: 3 additions & 19 deletions src/main/java/org/kohsuke/github/GHCommit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
@SuppressFBWarnings(value = { "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public class GHCommit {

/**
* Number of files returned in the commit response. If there are more files than this, the response will include
* pagination link headers for the remaining files.
*/
private static final int GH_FILE_LIMIT_PER_COMMIT_PAGE = 300;

private GHRepository owner;

private ShortInfo commit;
Expand Down Expand Up @@ -426,7 +420,7 @@ public URL getUrl() {
*/
@Deprecated
public List<File> getFiles() throws IOException {
return listFiles();
return listFiles().toList();
}

/**
Expand All @@ -439,21 +433,11 @@ public List<File> getFiles() throws IOException {
* @throws IOException
* on error
*/
public List<File> listFiles() throws IOException {
public PagedIterable<File> listFiles() throws IOException {

populate();

if (files != null && files.size() < GH_FILE_LIMIT_PER_COMMIT_PAGE) {
return Collections.unmodifiableList(files);
}

PagedIterable<File> filesIterable = new GHCommitIterable(owner, sha);
if (files == null) {
files = new ArrayList<>();
}
files.clear();
files.addAll(filesIterable.toList());
return Collections.unmodifiableList(files);
return new GHCommitFileIterable(owner, sha, files);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommitFileIterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.kohsuke.github;

import org.kohsuke.github.GHCommit.File;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Nonnull;

/**
* Iterable for commit listing.
*
* @author Stephen Horgan
*/
class GHCommitFileIterable extends PagedIterable<GHCommit.File> {

/**
* Number of files returned in the commit response. If there are more files than this, the response will include
* pagination link headers for the remaining files.
*/
private static final int GH_FILE_LIMIT_PER_COMMIT_PAGE = 300;

private final GHRepository owner;
private final String sha;
private GHCommitFilesPage result;

/**
* Instantiates a new GH commit iterable.
*
* @param owner
* the owner
* @param sha
* the SHA of the commit
* @param files
* the list of files initially populated
*/
public GHCommitFileIterable(GHRepository owner, String sha, List<File> files) {
this.owner = owner;
this.sha = sha;
this.result = new GHCommitFilesPage(files != null ? files.toArray(new File[0]) : new File[0]);
}

/**
* Iterator.
*
* @param pageSize
* the page size
* @return the paged iterator
*/
@Nonnull
@Override
public PagedIterator<GHCommit.File> _iterator(int pageSize) {

Iterator<GHCommit.File[]> pageIterator;

if (result.getFiles().length < GH_FILE_LIMIT_PER_COMMIT_PAGE) {
// create a page iterator that only provides one page
pageIterator = Collections.singleton(result.getFiles()).iterator();
} else {
// page size is controlled by the server for this iterator, do not allow it to be set by the caller
pageSize = 0;

GitHubRequest request = owner.root()
.createRequest()
.withUrlPath(owner.getApiTailUrl("commits/" + sha))
.build();

pageIterator = adapt(
GitHubPageIterator.create(owner.root().getClient(), GHCommitFilesPage.class, request, pageSize));
}

return new PagedIterator<>(pageIterator, null);
}

/**
* Adapt.
*
* @param base
* the base commit page
* @return the iterator
*/
protected Iterator<GHCommit.File[]> adapt(final Iterator<GHCommitFilesPage> base) {
return new Iterator<GHCommit.File[]>() {

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

public GHCommit.File[] next() {
GHCommitFilesPage v = base.next();
if (result == null) {
result = v;
}
return v.getFiles();
}
};
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommitFilesPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
class GHCommitFilesPage {
private File[] files;

public GHCommitFilesPage() {
}

public GHCommitFilesPage(File[] files) {
this.files = files;
}

/**
* Gets the files.
*
Expand Down
72 changes: 0 additions & 72 deletions src/main/java/org/kohsuke/github/GHCommitIterable.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/CommitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void listFilesWhereCommitHasSmallChange() throws Exception {
GHRepository repo = getRepository();
GHCommit commit = repo.getCommit("dabf0e89fe7107d6e294a924561533ecf80f2384");

assertThat(commit.listFiles().size(), equalTo(28));
assertThat(commit.listFiles().toList().size(), equalTo(28));
}

/**
Expand All @@ -72,7 +72,7 @@ public void listFilesWhereCommitHasLargeChange() throws Exception {
GHRepository repo = getRepository();
GHCommit commit = repo.getCommit("b83812aa76bb7c3c43da96fbf8aec1e45db87624");

assertThat(commit.listFiles().size(), equalTo(691));
assertThat(commit.listFiles().toList().size(), equalTo(691));
}

/**
Expand Down