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

Added support for listing projects for a specified user (#268).

parent 4858f054
...@@ -13,6 +13,7 @@ import javax.ws.rs.core.StreamingOutput; ...@@ -13,6 +13,7 @@ import javax.ws.rs.core.StreamingOutput;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
/** /**
* This class is the base class for all the sub API classes. It provides implementations of * This class is the base class for all the sub API classes. It provides implementations of
...@@ -98,6 +99,42 @@ public abstract class AbstractApi implements Constants { ...@@ -98,6 +99,42 @@ public abstract class AbstractApi implements Constants {
} }
} }
/**
* Returns the user ID or path from the provided Integer, String, or User instance.
*
* @param obj the object to determine the ID or username from
* @return the user ID or username from the provided Integer, String, or User instance
* @throws GitLabApiException if any exception occurs during execution
*/
public Object getUserIdOrUsername(Object obj) throws GitLabApiException {
if (obj == null) {
throw (new RuntimeException("Cannot determine ID or username from null object"));
} else if (obj instanceof Integer) {
return (obj);
} else if (obj instanceof String) {
return (urlEncode(((String) obj).trim()));
} else if (obj instanceof User) {
Integer id = ((User) obj).getId();
if (id != null && id.intValue() > 0) {
return (id);
}
String username = ((User) obj).getUsername();
if (username != null && username.trim().length() > 0) {
return (urlEncode(username.trim()));
}
throw (new RuntimeException("Cannot determine ID or username from provided User instance"));
} else {
throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() +
" instance, must be Integer, String, or a User instance"));
}
}
protected ApiVersion getApiVersion() { protected ApiVersion getApiVersion() {
return (gitLabApi.getApiVersion()); return (gitLabApi.getApiVersion());
} }
......
...@@ -42,6 +42,7 @@ import org.gitlab4j.api.models.FileUpload; ...@@ -42,6 +42,7 @@ import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectFilter;
import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser; import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.PushRules; import org.gitlab4j.api.models.PushRules;
...@@ -464,6 +465,56 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -464,6 +465,56 @@ public class ProjectApi extends AbstractApi implements Constants {
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects")); return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
} }
/**
* Get a list of visible projects owned by the given user.
*
* <pre><code>GET /users/:user_id/projects</code></pre>
*
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
* @param filter the ProjectFilter instance holding the filter values for the query
* @return a list of visible projects owned by the given use
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter) throws GitLabApiException {
return (getUserProjects(userIdOrUsername, filter, 1, getDefaultPerPage()));
}
/**
* Get a list of visible projects owned by the given user in the specified page range.
*
* <pre><code>GET /users/:user_id/projects</code></pre>
*
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
* @param filter the ProjectFilter instance holding the filter values for the query
* @param page the page to get
* @param perPage the number of projects per page
* @return a list of visible projects owned by the given use
* @throws GitLabApiException if any exception occurs
*/
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int page, int perPage) throws GitLabApiException {
GitLabApiForm formData = filter.getQueryParams(page, perPage);
Response response = get(Response.Status.OK, formData.asMap(),
"users", getUserIdOrUsername(userIdOrUsername), "projects");
return (response.readEntity(new GenericType<List<Project>>() {}));
}
/**
* Get a Pager of visible projects owned by the given user.
*
* <pre><code>GET /users/:user_id/projects</code></pre>
*
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
* @param filter the ProjectFilter instance holding the filter values for the query
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a Pager of visible projects owned by the given use
* @throws GitLabApiException if any exception occurs
*/
public Pager<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = filter.getQueryParams();
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(),
"users", getUserIdOrUsername(userIdOrUsername), "projects"));
}
/** /**
* Get a specific project, which is owned by the authentication user. * Get a specific project, which is owned by the authentication user.
* *
......
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