Skip to content

Commit

Permalink
Added Page and Stream methods for getDiff() (gitlab4j#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmessner committed Jan 14, 2020
1 parent 897e1db commit e1a5485
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/main/java/org/gitlab4j/api/CommitsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,46 @@ public CommitStatus addCommitStatus(Object projectIdOrPath, String sha, CommitBu
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<Diff> getDiff(Object projectIdOrPath, String sha) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "diff");
return (response.readEntity(new GenericType<List<Diff>>() {}));
return (getDiff(projectIdOrPath, sha, getDefaultPerPage()).all());
}

/**
* Get the Pager of diffs of a commit in a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/commits/:sha/diff</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha a commit hash or name of a branch or tag
* @param itemsPerPage the number of Diff instances that will be fetched per page
* @return a Pager of Diff instances for the specified project ID/sha pair
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<Diff> getDiff(Object projectIdOrPath, String sha, int itemsPerPage) throws GitLabApiException {

if (projectIdOrPath == null) {
throw new RuntimeException("projectIdOrPath cannot be null");
}

if (sha == null || sha.trim().isEmpty()) {
throw new RuntimeException("sha cannot be null");
}

return (new Pager<Diff>(this, Diff.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "diff"));
}

/**
* Get the Diff of diffs of a commit in a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/commits/:sha/diff</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha a commit hash or name of a branch or tag
* @return a Stream of Diff instances for the specified project ID/sha pair
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Stream<Diff> getDiffStream(Object projectIdOrPath, String sha) throws GitLabApiException {
return (getDiff(projectIdOrPath, sha, getDefaultPerPage()).stream());
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/gitlab4j/api/TestCommitsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Branch;
Expand Down Expand Up @@ -95,6 +96,18 @@ public void testDiff() throws GitLabApiException {
assertTrue(diffs.size() > 0);
}

@Test
public void testDiffStream() throws GitLabApiException {

assertNotNull(testProject);

List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject.getId());
assertTrue(commits.size() > 0);

Stream<Diff> diffs = gitLabApi.getCommitsApi().getDiffStream(testProject.getId(), commits.get(0).getId());
assertTrue(diffs.count() > 0);
}

@Test
public void testComments() throws GitLabApiException, ParseException {

Expand Down

0 comments on commit e1a5485

Please sign in to comment.