Skip to content

Commit 495358c

Browse files
authored
Merge pull request #2 from gmessner/master
Update to commit b7b2e3a
2 parents 7feed39 + b7b2e3a commit 495358c

File tree

5 files changed

+310
-3
lines changed

5 files changed

+310
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.53'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.54'
1515
}
1616
```
1717

@@ -22,7 +22,7 @@ dependencies {
2222
<dependency>
2323
<groupId>org.gitlab4j</groupId>
2424
<artifactId>gitlab4j-api</artifactId>
25-
<version>4.8.53</version>
25+
<version>4.8.54</version>
2626
</dependency>
2727
```
2828

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>org.gitlab4j</groupId>
66
<artifactId>gitlab4j-api</artifactId>
77
<packaging>jar</packaging>
8-
<version>4.8.54-SNAPSHOT</version>
8+
<version>4.8.55-SNAPSHOT</version>
99
<name>GitLab API Java Client</name>
1010
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API.</description>
1111
<url>https://github.com/gmessner/gitlab4j-api</url>

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
*
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.Constants.ProjectOrderBy;
4+
import org.gitlab4j.api.Constants.SortOrder;
5+
import org.gitlab4j.api.Constants;
6+
import org.gitlab4j.api.GitLabApiForm;
7+
8+
/**
9+
* This class is used to filter Projects when getting lists of projects for a specified user.
10+
*/
11+
public class ProjectFilter {
12+
13+
private Boolean archived;
14+
private Visibility visibility;
15+
private ProjectOrderBy orderBy;
16+
private SortOrder sort;
17+
private String search;
18+
private Boolean simple;
19+
private Boolean owned;
20+
private Boolean membership;
21+
private Boolean starred;
22+
private Boolean statistics;
23+
private Boolean withCustomAttributes;
24+
private Boolean withIssuesEnabled;
25+
private Boolean withMergeRequestsEnabled;
26+
private AccessLevel minAccessLevel;
27+
28+
/**
29+
* Limit by archived status.
30+
*
31+
* @param archived if true will only return archived projects
32+
* @return the reference to this ProjectFilter instance
33+
*/
34+
public ProjectFilter withArchived(Boolean archived) {
35+
this.archived = archived;
36+
return (this);
37+
}
38+
39+
/**
40+
* Limit by visibility public, internal, or private.
41+
*
42+
* @param visibility the visibility to match
43+
* @return the reference to this ProjectFilter instance
44+
*/
45+
public ProjectFilter withVisibility(Visibility visibility) {
46+
this.visibility = visibility;
47+
return (this);
48+
}
49+
50+
/**
51+
* Return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields. Default is created_at.
52+
*
53+
* @param orderBy specifies what field to order by
54+
* @return the reference to this ProjectFilter instance
55+
*/
56+
public ProjectFilter withOrderBy(ProjectOrderBy orderBy) {
57+
this.orderBy = orderBy;
58+
return (this);
59+
}
60+
61+
/**
62+
* Return projects sorted in asc or desc order. Default is desc.
63+
*
64+
* @param sort sort direction, ASC or DESC
65+
* @return the reference to this ProjectFilter instance
66+
*/
67+
public ProjectFilter withSortOder(SortOrder sort) {
68+
this.sort = sort;
69+
return (this);
70+
}
71+
72+
/**
73+
* Return list of projects matching the search criteria.
74+
*
75+
* @param search the search criteria
76+
* @return the reference to this ProjectFilter instance
77+
*/
78+
public ProjectFilter withSearch(String search) {
79+
this.search = search;
80+
return (this);
81+
}
82+
83+
/**
84+
* Return only limited fields for each project. This is a no-op without
85+
* authentication as then only simple fields are returned.
86+
*
87+
* @param simple if true, return only limited fields for each project
88+
* @return the reference to this ProjectFilter instance
89+
*/
90+
public ProjectFilter withSimple(Boolean simple) {
91+
this.simple = simple;
92+
return (this);
93+
}
94+
95+
/**
96+
* Limit by projects explicitly owned by the current user
97+
*
98+
* @param owned if true, limit to projects explicitly owned by the current user
99+
* @return the reference to this ProjectFilter instance
100+
*/
101+
public ProjectFilter withOwned(Boolean owned) {
102+
this.owned = owned;
103+
return (this);
104+
}
105+
106+
/**
107+
* Limit by projects that the current user is a member of
108+
*
109+
* @param membership if true, limit by projects that the current user is a member of
110+
* @return the reference to this ProjectFilter instance
111+
*/
112+
public ProjectFilter withMembership(Boolean membership) {
113+
this.membership = membership;
114+
return (this);
115+
}
116+
117+
/**
118+
* Limit by projects starred by the current user.
119+
*
120+
* @param starred if true, limit by projects starred by the current user
121+
* @return the reference to this ProjectFilter instance
122+
*/
123+
public ProjectFilter withStarred(Boolean starred) {
124+
this.starred = starred;
125+
return (this);
126+
}
127+
128+
/**
129+
* Include project statistics.
130+
*
131+
* @param statistics if true, include project statistics
132+
* @return the reference to this ProjectFilter instance
133+
*/
134+
public ProjectFilter withStatistics(Boolean statistics) {
135+
this.statistics = statistics;
136+
return (this);
137+
}
138+
139+
/**
140+
* Include custom attributes in response (admins only).
141+
*
142+
* @param withCustomAttributes if true, include custom attributes in the repsonse
143+
* @return the reference to this ProjectFilter instance
144+
*/
145+
public ProjectFilter withCustomAttributes(Boolean withCustomAttributes) {
146+
this.withCustomAttributes = withCustomAttributes;
147+
return (this);
148+
}
149+
150+
/**
151+
* Limit by enabled issues feature
152+
*
153+
* @param withIssuesEnabled if true, limit by enabled issues feature
154+
* @return the reference to this ProjectFilter instance
155+
*/
156+
public ProjectFilter withIssuesEnabled(Boolean withIssuesEnabled) {
157+
this.withIssuesEnabled = withIssuesEnabled;
158+
return (this);
159+
}
160+
161+
/**
162+
* Limit by enabled merge requests feature
163+
*
164+
* @param withMergeRequestsEnabled if true, imit by enabled merge requests feature
165+
* @return the reference to this ProjectFilter instance
166+
*/
167+
public ProjectFilter withMergeRequestsEnabled(Boolean withMergeRequestsEnabled) {
168+
this.withMergeRequestsEnabled = withMergeRequestsEnabled;
169+
return (this);
170+
}
171+
172+
/**
173+
* Limit by current user minimal access level
174+
*
175+
* @param minAccessLevel limit by current user minimal access level
176+
* @return the reference to this ProjectFilter instance
177+
*/
178+
public ProjectFilter minAccessLevel(AccessLevel minAccessLevel) {
179+
this.minAccessLevel = minAccessLevel;
180+
return (this);
181+
}
182+
183+
/**
184+
* Get the query params specified by this filter.
185+
*
186+
* @param page specifies the page number
187+
* @param perPage specifies the number of items per page
188+
* @return a GitLabApiForm instance holding the query parameters for this ProjectFilter instance
189+
*/
190+
public GitLabApiForm getQueryParams(int page, int perPage) {
191+
return (getQueryParams()
192+
.withParam(Constants.PAGE_PARAM, page)
193+
.withParam(Constants.PER_PAGE_PARAM, perPage));
194+
}
195+
196+
/**
197+
* Get the query params specified by this filter.
198+
*
199+
* @return a GitLabApiForm instance holding the query parameters for this ProjectFilter instance
200+
*/
201+
public GitLabApiForm getQueryParams() {
202+
return (new GitLabApiForm()
203+
.withParam("archived", archived)
204+
.withParam("visibility", visibility)
205+
.withParam("order_by", orderBy)
206+
.withParam("sort", sort)
207+
.withParam("search", search)
208+
.withParam("simple", simple)
209+
.withParam("owned", owned)
210+
.withParam("membership", membership)
211+
.withParam("starred", starred)
212+
.withParam("statistics", statistics)
213+
.withParam("with_custom_attributes", withCustomAttributes)
214+
.withParam("with_issues_enabled", withIssuesEnabled)
215+
.withParam("with_merge_requests_enabled ", withMergeRequestsEnabled))
216+
.withParam("min_access_level ", (minAccessLevel != null ? minAccessLevel.toValue() : null)
217+
);
218+
}
219+
}

0 commit comments

Comments
 (0)