Commit e1a54855 authored by Greg Messner's avatar Greg Messner
Browse files

Added Page and Stream methods for getDiff() (#497)

parent 897e1db0
...@@ -537,8 +537,46 @@ public class CommitsApi extends AbstractApi { ...@@ -537,8 +537,46 @@ public class CommitsApi extends AbstractApi {
* @throws GitLabApiException GitLabApiException if any exception occurs during execution * @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/ */
public List<Diff> getDiff(Object projectIdOrPath, String sha) throws GitLabApiException { 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 (getDiff(projectIdOrPath, sha, getDefaultPerPage()).all());
return (response.readEntity(new GenericType<List<Diff>>() {})); }
/**
* 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());
} }
/** /**
......
...@@ -13,6 +13,7 @@ import java.util.Arrays; ...@@ -13,6 +13,7 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
...@@ -95,6 +96,18 @@ public class TestCommitsApi extends AbstractIntegrationTest { ...@@ -95,6 +96,18 @@ public class TestCommitsApi extends AbstractIntegrationTest {
assertTrue(diffs.size() > 0); 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 @Test
public void testComments() throws GitLabApiException, ParseException { public void testComments() throws GitLabApiException, ParseException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment