Commit f1be856a authored by Apanasevich Dmitrii's avatar Apanasevich Dmitrii

Adds pagination to commit API

parent 1ff2df69
......@@ -11,7 +11,7 @@ sourceCompatibility = 1.7
targetCompatibility = 1.7
group = 'org.gitlab'
version = '1.1.8-SNAPSHOT'
version = '1.1.9-SNAPSHOT'
repositories {
mavenLocal()
......
......@@ -23,6 +23,7 @@ import java.util.List;
*
* @author @timols (Tim O)
*/
@SuppressWarnings("unused")
public class GitlabAPI {
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
......@@ -770,6 +771,10 @@ public class GitlabAPI {
}
public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest) throws IOException {
return getCommits(mergeRequest, new Pagination());
}
public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest, Pagination pagination) throws IOException {
Integer projectId = mergeRequest.getSourceProjectId();
if (projectId == null) {
projectId = mergeRequest.getProjectId();
......@@ -778,6 +783,8 @@ public class GitlabAPI {
Query query = new Query()
.append("ref_name", mergeRequest.getSourceBranch());
query.mergeWith(pagination.asQuery());
String tailUrl = GitlabProject.URL + "/" + projectId +
"/repository" + GitlabCommit.URL + query.toString();
......@@ -787,14 +794,25 @@ public class GitlabAPI {
// gets all commits for a project
public List<GitlabCommit> getAllCommits(Serializable projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + "/repository" + GitlabCommit.URL;
return getAllCommits(projectId, new Pagination());
}
public List<GitlabCommit> getAllCommits(Serializable projectId, Pagination pagination) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) +
"/repository" + GitlabCommit.URL + pagination;
return retrieve().getAll(tailUrl, GitlabCommit[].class);
}
// List commit diffs for a project ID and commit hash
// GET /projects/:id/repository/commits/:sha/diff
public List<GitlabCommitDiff> getCommitDiffs(Serializable projectId, String commitHash) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + "/repository/commits/" + commitHash + GitlabCommitDiff.URL;
return getCommitDiffs(projectId, commitHash, new Pagination());
}
public List<GitlabCommitDiff> getCommitDiffs(Serializable projectId, String commitHash,
Pagination pagination) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + "/repository/commits/" + commitHash +
GitlabCommitDiff.URL + pagination;
GitlabCommitDiff[] diffs = retrieve().to(tailUrl, GitlabCommitDiff[].class);
return Arrays.asList(diffs);
}
......@@ -802,14 +820,21 @@ public class GitlabAPI {
// List commit statuses for a project ID and commit hash
// GET /projects/:id/repository/commits/:sha/statuses
public List<GitlabCommitStatus> getCommitStatuses(GitlabProject project, String commitHash) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + "/repository" + GitlabCommit.URL + "/" + commitHash + GitlabCommitStatus.URL;
return getCommitStatuses(project, commitHash, new Pagination());
}
public List<GitlabCommitStatus> getCommitStatuses(GitlabProject project, String commitHash,
Pagination pagination) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + "/repository" + GitlabCommit.URL + "/" +
commitHash + GitlabCommitStatus.URL + pagination;
GitlabCommitStatus[] statuses = retrieve().to(tailUrl, GitlabCommitStatus[].class);
return Arrays.asList(statuses);
}
// Submit new commit statuses for a project ID and commit hash
// GET /projects/:id/statuses/:sha
public GitlabCommitStatus createCommitStatus(GitlabProject project, String commitHash, String state, String ref, String name, String targetUrl, String description) throws IOException {
public GitlabCommitStatus createCommitStatus(GitlabProject project, String commitHash, String state, String ref,
String name, String targetUrl, String description) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabCommitStatus.URL + "/" + commitHash;
return dispatch()
.with("state", state)
......@@ -956,8 +981,7 @@ public class GitlabAPI {
public GitlabProjectHook getProjectHook(GitlabProject project, String hookId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId;
GitlabProjectHook hook = retrieve().to(tailUrl, GitlabProjectHook.class);
return hook;
return retrieve().to(tailUrl, GitlabProjectHook.class);
}
public GitlabProjectHook addProjectHook(GitlabProject project, String url) throws IOException {
......
package org.gitlab.api;
import org.gitlab.api.http.Query;
import java.io.UnsupportedEncodingException;
public class Pagination {
public static final String PARAM_PAGE = "page";
public static final String PARAM_PER_PAGE = "per_page";
public static final int MAX_ITEMS_PER_PAGE = 100;
private final Query paginationQuery = new Query();
public void setPage(int page) {
try {
paginationQuery.append(PARAM_PAGE, String.valueOf(page));
} catch (UnsupportedEncodingException ignored) {
}
}
public void setPerPage(int perPage) {
if (perPage > MAX_ITEMS_PER_PAGE) {
throw new IllegalArgumentException("Max value for perPage is " + MAX_ITEMS_PER_PAGE);
}
try {
paginationQuery.append(PARAM_PER_PAGE, String.valueOf(perPage));
} catch (UnsupportedEncodingException ignored) {
}
}
public Query asQuery() {
return paginationQuery;
}
@Override
public String toString() {
return paginationQuery.toString();
}
}
......@@ -106,6 +106,10 @@ public class Query {
return this;
}
public boolean mergeWith(Query query) {
return params.addAll(query.params);
}
/**
* Returns a Query suitable for appending
* to a URI
......
package org.gitlab.api;
import org.gitlab.api.http.Query;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.assertEquals;
public class PaginationTest {
@Test
public void emptyPagination() {
Pagination pagination = new Pagination();
final Query expectedQuery = new Query();
assertEquals(expectedQuery.toString(), pagination.toString());
assertEquals(expectedQuery.toString(), pagination.asQuery().toString());
}
@Test
public void pageOnlyPagination() throws UnsupportedEncodingException {
Pagination pagination = new Pagination();
pagination.setPage(1);
final Query expectedQuery = new Query()
.append(Pagination.PARAM_PAGE, "1");
assertEquals(expectedQuery.toString(), pagination.toString());
assertEquals(expectedQuery.toString(), pagination.asQuery().toString());
}
@Test
public void perPageOnlyPagination() throws UnsupportedEncodingException {
Pagination pagination = new Pagination();
pagination.setPerPage(50);
final Query expectedQuery = new Query()
.append(Pagination.PARAM_PER_PAGE, "50");
assertEquals(expectedQuery.toString(), pagination.toString());
assertEquals(expectedQuery.toString(), pagination.asQuery().toString());
}
@Test(expected = IllegalArgumentException.class)
public void perPageException() {
Pagination pagination = new Pagination();
pagination.setPerPage(Pagination.MAX_ITEMS_PER_PAGE + 1);
}
@Test
public void complexPagination() throws UnsupportedEncodingException {
Pagination pagination = new Pagination();
pagination.setPage(1);
pagination.setPerPage(50);
final Query expectedQuery = new Query()
.append(Pagination.PARAM_PAGE, "1")
.append(Pagination.PARAM_PER_PAGE, "50");
assertEquals(expectedQuery.toString(), pagination.toString());
assertEquals(expectedQuery.toString(), pagination.asQuery().toString());
}
}
......@@ -5,6 +5,7 @@ import org.junit.Test;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class QueryTest {
......@@ -74,4 +75,20 @@ public class QueryTest {
assertEquals("?p1=v+1&p2=v+2", query.toString());
}
@Test
public void merge() throws UnsupportedEncodingException{
Query sourceQuery= new Query()
.append("p1", "v1")
.append("p2", "v2");
Query targetQuery = new Query()
.append("p3", "v3")
.append("p2", "v22");
boolean mergeResult = targetQuery.mergeWith(sourceQuery);
assertTrue(mergeResult);
assertEquals("?p3=v3&p2=v22&p1=v1&p2=v2", targetQuery.toString());
}
}
Markdown is supported
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