Skip to content

Commit f6d5cff

Browse files
committed
Added support for listing projects for a specified user (gitlab4j#268).
1 parent 4858f05 commit f6d5cff

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.gitlab4j.api.GitLabApi.ApiVersion;
1414
import org.gitlab4j.api.models.Group;
1515
import org.gitlab4j.api.models.Project;
16+
import org.gitlab4j.api.models.User;
1617

1718
/**
1819
* This class is the base class for all the sub API classes. It provides implementations of
@@ -98,6 +99,42 @@ public Object getGroupIdOrPath(Object obj) throws GitLabApiException {
9899
}
99100
}
100101

102+
/**
103+
* Returns the user ID or path from the provided Integer, String, or User instance.
104+
*
105+
* @param obj the object to determine the ID or username from
106+
* @return the user ID or username from the provided Integer, String, or User instance
107+
* @throws GitLabApiException if any exception occurs during execution
108+
*/
109+
public Object getUserIdOrUsername(Object obj) throws GitLabApiException {
110+
111+
if (obj == null) {
112+
throw (new RuntimeException("Cannot determine ID or username from null object"));
113+
} else if (obj instanceof Integer) {
114+
return (obj);
115+
} else if (obj instanceof String) {
116+
return (urlEncode(((String) obj).trim()));
117+
} else if (obj instanceof User) {
118+
119+
Integer id = ((User) obj).getId();
120+
if (id != null && id.intValue() > 0) {
121+
return (id);
122+
}
123+
124+
String username = ((User) obj).getUsername();
125+
if (username != null && username.trim().length() > 0) {
126+
return (urlEncode(username.trim()));
127+
}
128+
129+
throw (new RuntimeException("Cannot determine ID or username from provided User instance"));
130+
131+
} else {
132+
133+
throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() +
134+
" instance, must be Integer, String, or a User instance"));
135+
}
136+
}
137+
101138
protected ApiVersion getApiVersion() {
102139
return (gitLabApi.getApiVersion());
103140
}

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.gitlab4j.api.models.Issue;
4343
import org.gitlab4j.api.models.Member;
4444
import org.gitlab4j.api.models.Project;
45+
import org.gitlab4j.api.models.ProjectFilter;
4546
import org.gitlab4j.api.models.ProjectHook;
4647
import org.gitlab4j.api.models.ProjectUser;
4748
import org.gitlab4j.api.models.PushRules;
@@ -464,6 +465,56 @@ public Pager<Project> getStarredProjects(int itemsPerPage) throws GitLabApiExcep
464465
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
465466
}
466467

468+
/**
469+
* Get a list of visible projects owned by the given user.
470+
*
471+
* <pre><code>GET /users/:user_id/projects</code></pre>
472+
*
473+
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
474+
* @param filter the ProjectFilter instance holding the filter values for the query
475+
* @return a list of visible projects owned by the given use
476+
* @throws GitLabApiException if any exception occurs
477+
*/
478+
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter) throws GitLabApiException {
479+
return (getUserProjects(userIdOrUsername, filter, 1, getDefaultPerPage()));
480+
}
481+
482+
/**
483+
* Get a list of visible projects owned by the given user in the specified page range.
484+
*
485+
* <pre><code>GET /users/:user_id/projects</code></pre>
486+
*
487+
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
488+
* @param filter the ProjectFilter instance holding the filter values for the query
489+
* @param page the page to get
490+
* @param perPage the number of projects per page
491+
* @return a list of visible projects owned by the given use
492+
* @throws GitLabApiException if any exception occurs
493+
*/
494+
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int page, int perPage) throws GitLabApiException {
495+
GitLabApiForm formData = filter.getQueryParams(page, perPage);
496+
Response response = get(Response.Status.OK, formData.asMap(),
497+
"users", getUserIdOrUsername(userIdOrUsername), "projects");
498+
return (response.readEntity(new GenericType<List<Project>>() {}));
499+
}
500+
501+
/**
502+
* Get a Pager of visible projects owned by the given user.
503+
*
504+
* <pre><code>GET /users/:user_id/projects</code></pre>
505+
*
506+
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
507+
* @param filter the ProjectFilter instance holding the filter values for the query
508+
* @param itemsPerPage the number of Project instances that will be fetched per page
509+
* @return a Pager of visible projects owned by the given use
510+
* @throws GitLabApiException if any exception occurs
511+
*/
512+
public Pager<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int itemsPerPage) throws GitLabApiException {
513+
GitLabApiForm formData = filter.getQueryParams();
514+
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(),
515+
"users", getUserIdOrUsername(userIdOrUsername), "projects"));
516+
}
517+
467518
/**
468519
* Get a specific project, which is owned by the authentication user.
469520
*

0 commit comments

Comments
 (0)