Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e45932d

Browse filesBrowse files
frink182Stevebitwiseman
authored
Return all files from a commit (hub4j#1679)
* Fix for hub4j#1669 * Doc updates * Add test data * Spotless * Javadoc changes * Test comment fixes * PR review changes * Add required default constructor * Update GHCommitFileIterable.java --------- Co-authored-by: Steve <steve@slug.home> Co-authored-by: Liam Newman <bitwiseman@gmail.com>
1 parent c494891 commit e45932d
Copy full SHA for e45932d

File tree

Expand file treeCollapse file tree

81 files changed

+14618
-4
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

81 files changed

+14618
-4
lines changed

‎src/main/java/org/kohsuke/github/GHCommit.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHCommit.java
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
@SuppressFBWarnings(value = { "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
2525
public class GHCommit {
26+
2627
private GHRepository owner;
2728

2829
private ShortInfo commit;
@@ -269,7 +270,7 @@ static class User {
269270
}
270271

271272
/** The sha. */
272-
String url, html_url, sha;
273+
String url, html_url, sha, message;
273274

274275
/** The files. */
275276
List<File> files;
@@ -308,6 +309,7 @@ public GHCommit() {
308309
sha = commit.getSha();
309310
url = commit.getUrl();
310311
parents = commit.getParents();
312+
message = commit.getMessage();
311313
}
312314

313315
/**
@@ -414,10 +416,28 @@ public URL getUrl() {
414416
* @return Can be empty but never null.
415417
* @throws IOException
416418
* on error
419+
* @deprecated Use {@link #listFiles()} instead.
417420
*/
421+
@Deprecated
418422
public List<File> getFiles() throws IOException {
423+
return listFiles().toList();
424+
}
425+
426+
/**
427+
* List of files changed/added/removed in this commit. Uses a paginated list if the files returned by GitHub exceed
428+
* 300 in quantity.
429+
*
430+
* @return the List of files
431+
* @see <a href="https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit">Get a
432+
* commit</a>
433+
* @throws IOException
434+
* on error
435+
*/
436+
public PagedIterable<File> listFiles() throws IOException {
437+
419438
populate();
420-
return files != null ? Collections.unmodifiableList(files) : Collections.<File>emptyList();
439+
440+
return new GHCommitFileIterable(owner, sha, files);
421441
}
422442

423443
/**
+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.kohsuke.github;
2+
3+
import org.kohsuke.github.GHCommit.File;
4+
5+
import java.util.Collections;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
import javax.annotation.Nonnull;
10+
11+
/**
12+
* Iterable for commit listing.
13+
*
14+
* @author Stephen Horgan
15+
*/
16+
class GHCommitFileIterable extends PagedIterable<GHCommit.File> {
17+
18+
/**
19+
* Number of files returned in the commit response. If there are more files than this, the response will include
20+
* pagination link headers for the remaining files.
21+
*/
22+
private static final int GH_FILE_LIMIT_PER_COMMIT_PAGE = 300;
23+
24+
private final GHRepository owner;
25+
private final String sha;
26+
private final File[] files;
27+
28+
/**
29+
* Instantiates a new GH commit iterable.
30+
*
31+
* @param owner
32+
* the owner
33+
* @param sha
34+
* the SHA of the commit
35+
* @param files
36+
* the list of files initially populated
37+
*/
38+
public GHCommitFileIterable(GHRepository owner, String sha, List<File> files) {
39+
this.owner = owner;
40+
this.sha = sha;
41+
this.files = files != null ? files.toArray(new File[0]) : null;
42+
}
43+
44+
/**
45+
* Iterator.
46+
*
47+
* @param pageSize
48+
* the page size
49+
* @return the paged iterator
50+
*/
51+
@Nonnull
52+
@Override
53+
public PagedIterator<GHCommit.File> _iterator(int pageSize) {
54+
55+
Iterator<GHCommit.File[]> pageIterator;
56+
57+
if (files != null && files.length < GH_FILE_LIMIT_PER_COMMIT_PAGE) {
58+
// create a page iterator that only provides one page
59+
pageIterator = Collections.singleton(files).iterator();
60+
} else {
61+
// page size is controlled by the server for this iterator, do not allow it to be set by the caller
62+
pageSize = 0;
63+
64+
GitHubRequest request = owner.root()
65+
.createRequest()
66+
.withUrlPath(owner.getApiTailUrl("commits/" + sha))
67+
.build();
68+
69+
pageIterator = adapt(
70+
GitHubPageIterator.create(owner.root().getClient(), GHCommitFilesPage.class, request, pageSize));
71+
}
72+
73+
return new PagedIterator<>(pageIterator, null);
74+
}
75+
76+
/**
77+
* Adapt.
78+
*
79+
* @param base
80+
* the base commit page
81+
* @return the iterator
82+
*/
83+
protected Iterator<GHCommit.File[]> adapt(final Iterator<GHCommitFilesPage> base) {
84+
return new Iterator<GHCommit.File[]>() {
85+
86+
public boolean hasNext() {
87+
return base.hasNext();
88+
}
89+
90+
public GHCommit.File[] next() {
91+
GHCommitFilesPage v = base.next();
92+
return v.getFiles();
93+
}
94+
};
95+
}
96+
}
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.kohsuke.github;
2+
3+
import org.kohsuke.github.GHCommit.File;
4+
5+
/**
6+
* Represents the array of files in a commit returned by github.
7+
*
8+
* @author Stephen Horgan
9+
*/
10+
class GHCommitFilesPage {
11+
private File[] files;
12+
13+
public GHCommitFilesPage() {
14+
}
15+
16+
public GHCommitFilesPage(File[] files) {
17+
this.files = files;
18+
}
19+
20+
/**
21+
* Gets the files.
22+
*
23+
* @param owner
24+
* the owner
25+
* @return the files
26+
*/
27+
File[] getFiles() {
28+
return files;
29+
}
30+
}

‎src/main/java/org/kohsuke/github/GHCompare.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHCompare.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ public PagedIterator<Commit> _iterator(int pageSize) {
186186
/**
187187
* Gets an array of files.
188188
*
189+
* By default, the file array is limited to 300 results. To retrieve the full list of files, iterate over each
190+
* commit returned by {@link GHCompare#listCommits} and use {@link GHCommit#listFiles} to get the files for each
191+
* commit.
192+
*
189193
* @return A copy of the array being stored in the class.
190194
*/
191195
public GHCommit.File[] getFiles() {

‎src/test/java/org/kohsuke/github/CommitTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/CommitTest.java
+60-2Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public void lastStatus() throws IOException {
3232
}
3333

3434
/**
35-
* List files.
35+
* Test get files.
3636
*
3737
* @throws Exception
3838
* the exception
3939
*/
4040
@Test // issue 230
41-
public void listFiles() throws Exception {
41+
public void getFiles() throws Exception {
4242
GHRepository repo = gitHub.getRepository("stapler/stapler");
4343
PagedIterable<GHCommit> commits = repo.queryCommits().path("pom.xml").list();
4444
for (GHCommit commit : Iterables.limit(commits, 10)) {
@@ -47,6 +47,49 @@ public void listFiles() throws Exception {
4747
}
4848
}
4949

50+
/**
51+
* Test list files where there are less than 300 files in a commit.
52+
*
53+
* @throws Exception
54+
* the exception
55+
*/
56+
@Test // issue 1669
57+
public void listFilesWhereCommitHasSmallChange() throws Exception {
58+
GHRepository repo = getRepository();
59+
GHCommit commit = repo.getCommit("dabf0e89fe7107d6e294a924561533ecf80f2384");
60+
61+
assertThat(commit.listFiles().toList().size(), equalTo(28));
62+
}
63+
64+
/**
65+
* Test list files where there are more than 300 files in a commit.
66+
*
67+
* @throws Exception
68+
* the exception
69+
*/
70+
@Test // issue 1669
71+
public void listFilesWhereCommitHasLargeChange() throws Exception {
72+
GHRepository repo = getRepository();
73+
GHCommit commit = repo.getCommit("b83812aa76bb7c3c43da96fbf8aec1e45db87624");
74+
75+
assertThat(commit.listFiles().toList().size(), equalTo(691));
76+
}
77+
78+
/**
79+
* Tests the commit message.
80+
*
81+
* @throws Exception
82+
* the exception
83+
*/
84+
@Test
85+
public void getMessage() throws Exception {
86+
GHRepository repo = getRepository();
87+
GHCommit commit = repo.getCommit("dabf0e89fe7107d6e294a924561533ecf80f2384");
88+
89+
assertThat(commit.getCommitShortInfo().getMessage(), notNullValue());
90+
assertThat(commit.getCommitShortInfo().getMessage(), equalTo("A commit with a few files"));
91+
}
92+
5093
/**
5194
* Test query commits.
5295
*
@@ -288,4 +331,19 @@ public void commitDateNotNull() throws Exception {
288331
assertThat(commit.getCommitShortInfo().getCommitDate(),
289332
equalTo(commit.getCommitShortInfo().getCommitter().getDate()));
290333
}
334+
335+
/**
336+
* Gets the repository.
337+
*
338+
* @return the repository
339+
* @throws IOException
340+
* Signals that an I/O exception has occurred.
341+
*/
342+
protected GHRepository getRepository() throws IOException {
343+
return getRepository(gitHub);
344+
}
345+
346+
private GHRepository getRepository(GitHub gitHub) throws IOException {
347+
return gitHub.getOrganization("hub4j-test-org").getRepository("CommitTest");
348+
}
291349
}
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "hub4jtestorgemail@could.be.null.com",
19+
"twitter_username": null,
20+
"is_verified": false,
21+
"has_organization_projects": true,
22+
"has_repository_projects": true,
23+
"public_repos": 26,
24+
"public_gists": 0,
25+
"followers": 1,
26+
"following": 0,
27+
"html_url": "https://github.com/hub4j-test-org",
28+
"created_at": "2014-05-10T19:39:11Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
30+
"type": "Organization"
31+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.