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

ProjectApi enhancements.

parent bbf3ee69
......@@ -30,6 +30,10 @@ public abstract class AbstractApi {
return (gitLabApi.getApiVersion() == apiVersion);
}
protected int getDefaultPerPage() {
return (gitLabApi.getDefaultPerPage());
}
protected GitLabApiClient getApiClient() {
return (gitLabApi.getApiClient());
}
......
......@@ -10,6 +10,8 @@ import org.gitlab4j.api.models.Session;
*/
public class GitLabApi {
public static final int DEFAULT_PER_PAGE = 9999;
public enum ApiVersion {
V3, V4;
......@@ -20,6 +22,7 @@ public class GitLabApi {
GitLabApiClient apiClient;
private ApiVersion apiVersion;
private int defaultPerPage = DEFAULT_PER_PAGE;
private CommitsApi commitsApi;
private GroupApi groupApi;
private MergeRequestApi mergeRequestApi;
......@@ -201,6 +204,24 @@ public class GitLabApi {
return (apiVersion);
}
/**
* Get the default number per page for calls that return multiple items.
*
* @return the default number per page for calls that return multiple item
*/
public int getDefaultPerPage() {
return (defaultPerPage);
}
/**
* Set the default number per page for calls that return multiple items.
*
* @param defaultPerPage the new default number per page for calls that return multiple item
*/
public void setDefaultPerPage(int defaultPerPage) {
this.defaultPerPage = defaultPerPage;
}
/**
* Return the GitLabApiClient associated with this instance. This is used by all the sub API classes
* to communicate with the GitLab API.
......
......@@ -15,7 +15,6 @@ import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.Visibility;
import org.glassfish.jersey.uri.UriComponent;
/**
* This class provides an entry point to all the GitLab API project calls.
......@@ -24,7 +23,7 @@ public class ProjectApi extends AbstractApi {
public ProjectApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
}
/**
* Get a list of projects accessible by the authenticated user.
......@@ -35,7 +34,66 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getProjects() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects");
Form formData = new GitLabApiForm().withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of projects accessible by the authenticated user and matching the supplied filter parameters.
* All filter parameters are optional.
*
* GET /projects
*
* @param archived limit by archived status
* @param visibility limit by visibility public, internal, or private
* @param orderBy return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields, default is created_at
* @param sort return projects sorted in asc or desc order. Default is desc
* @param search return list of projects matching the search criteria
* @param simple return only the ID, URL, name, and path of each project
* @param owned limit by projects owned by the current user
* @param membership limit by projects that the current user is a member of
* @param starred limit by projects starred by the current user
* @param statistics include project statistics
* @return a list of projects accessible by the authenticated user and matching the supplied parameters
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getProjects(Boolean archived, Visibility visibility, String orderBy,
String sort, String search, Boolean simple, Boolean owned, Boolean membership,
Boolean starred, Boolean statistics) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("archived", archived)
.withParam("visibility", visibility)
.withParam("order_by", orderBy)
.withParam("sort", sort)
.withParam("search", search)
.withParam("simple", simple)
.withParam("owned", owned)
.withParam("membership", membership)
.withParam("starred", starred)
.withParam("statistics", statistics)
.withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of projects accessible by the authenticated user that match the provided search string.
*
* GET /projects?search=search
*
* @param search the project name search criteria
* @return a list of projects accessible by the authenticated user that match the provided search string
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getProjects(String search) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("search", search).withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
......@@ -49,7 +107,7 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getMemberProjects() throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("membership", true);
Form formData = new GitLabApiForm().withParam("membership", true).withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
......@@ -62,24 +120,47 @@ public class ProjectApi extends AbstractApi {
*
* @return a list of all GitLab projects
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed, no longer supported by the GitLab API
*/
public List<Project> getAllProjects() throws GitLabApiException {
Response response = get(Response.Status.OK, UriComponent.decodeQuery("per_page=9999", true), "projects", "all");
if (!isApiVersion(ApiVersion.V3)) {
throw new GitLabApiException("Not supported by GitLab API version " + this.getApiVersion());
}
Form formData = new GitLabApiForm().withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", "all");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of projects owned by the authenticated user.
*
* GET /projects/owned
*
*
* GET /projects
*
* @return a list of projects owned by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getOwnedProjects() throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("owned", true);
Form formData = new GitLabApiForm().withParam("owned", true).withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
}
/**
* Get a list of projects starred by the authenticated user.
*
* GET /projects
*
* @return a list of projects starred by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getStarredProjects() throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("starred", true).withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects");
return (response.readEntity(new GenericType<List<Project>>() {
}));
......@@ -691,7 +772,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
*/
public List<Issue> getIssues(Integer projectId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "issues");
Form formData = new GitLabApiForm().withParam("per_page", getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "issues");
return (response.readEntity(new GenericType<List<Issue>>() {
}));
}
......
......@@ -5,6 +5,8 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
......@@ -127,13 +129,37 @@ public class TestProjectApi {
}
@Test
public void testDelete() throws GitLabApiException {
public void testListProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects();
assertNotNull(projects);
assertTrue(projects.size() >= 2);
int matchCount = 0;
for (Project project : projects) {
if (TEST_PROJECT_NAME.equals(project.getName()))
matchCount++;
else if (TEST_PROJECT_NAME_2.equals(project.getName()))
matchCount++;
}
assertEquals(2, matchCount);
projects = gitLabApi.getProjectApi().getProjects(TEST_PROJECT_NAME);
assertNotNull(projects);
assertEquals(2, projects.size());
assertEquals(TEST_PROJECT_NAME_2, projects.get(0).getName());
assertEquals(TEST_PROJECT_NAME, projects.get(1).getName());
}
@Test
public void testRemoveByDelete() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
gitLabApi.getProjectApi().deleteProject(project);
}
@Test
public void testParameterBasedCreate() throws GitLabApiException {
public void testCreateParameterBased() throws GitLabApiException {
Project newProject = gitLabApi.getProjectApi().createProject(
TEST_PROJECT_NAME_2, null, "GitLab4J test project.", true, true, true, true, Visibility.PUBLIC, null, null);
......@@ -145,10 +171,12 @@ public class TestProjectApi {
assertEquals(true, newProject.getWikiEnabled());
assertEquals(true, newProject.getSnippetsEnabled());
assertTrue(Visibility.PUBLIC == newProject.getVisibility() || Boolean.TRUE == newProject.getPublic());
}
try {
gitLabApi.getProjectApi().deleteProject(newProject);
} catch (Exception ignore) {
}
@Test
public void testRemoveByDeleteParameterBased() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_2);
gitLabApi.getProjectApi().deleteProject(project);
}
}
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