Skip to content

Commit a4d4c18

Browse files
committed
Added support for fetching projects with a filter (#356).
1 parent c45863b commit a4d4c18

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

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

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,73 @@ public Stream<Project> getStarredProjectsStream() throws GitLabApiException {
479479
return (getStarredProjects(getDefaultPerPage()).stream());
480480
}
481481

482+
/**
483+
* Get a list of all visible projects across GitLab for the authenticated user using the provided filter.
484+
*
485+
* <pre><code>GET /projects</code></pre>
486+
*
487+
* @param filter the ProjectFilter instance holding the filter values for the query
488+
* @return a list of all visible projects across GitLab for the authenticated use
489+
* @throws GitLabApiException if any exception occurs
490+
*/
491+
public List<Project> getProjects(ProjectFilter filter) throws GitLabApiException {
492+
return (getProjects(filter, getDefaultPerPage()).all());
493+
}
494+
495+
/**
496+
* Get a list of all visible projects across GitLab for the authenticated user in the specified page range
497+
* using the provided filter.
498+
*
499+
* <pre><code>GET /projects</code></pre>
500+
*
501+
* @param filter the ProjectFilter instance holding the filter values for the query
502+
* @param page the page to get
503+
* @param perPage the number of projects per page
504+
* @return a list of all visible projects across GitLab for the authenticated use
505+
* @throws GitLabApiException if any exception occurs
506+
*/
507+
public List<Project> getProjects(ProjectFilter filter, int page, int perPage) throws GitLabApiException {
508+
GitLabApiForm formData = filter.getQueryParams(page, perPage);
509+
Response response = get(Response.Status.OK, formData.asMap(), "projects");
510+
return (response.readEntity(new GenericType<List<Project>>() {}));
511+
}
512+
513+
/**
514+
* Get a Pager of all visible projects across GitLab for the authenticated user using the provided filter.
515+
*
516+
* <pre><code>GET /projects</code></pre>
517+
*
518+
* @param filter the ProjectFilter instance holding the filter values for the query
519+
* @param itemsPerPage the number of Project instances that will be fetched per page
520+
* @return a Pager of all visible projects across GitLab for the authenticated use
521+
* @throws GitLabApiException if any exception occurs
522+
*/
523+
public Pager<Project> getProjects(ProjectFilter filter, int itemsPerPage) throws GitLabApiException {
524+
GitLabApiForm formData = filter.getQueryParams();
525+
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
526+
}
527+
528+
/**
529+
* Get a Stream of all visible projects across GitLab for the authenticated user using the provided filter.
530+
*
531+
* <pre><code>GET /projects</code></pre>
532+
*
533+
* @param filter the ProjectFilter instance holding the filter values for the query
534+
* @return a Stream of all visible projects across GitLab for the authenticated use
535+
* @throws GitLabApiException if any exception occurs
536+
*/
537+
public Stream<Project> getProjectsStream(ProjectFilter filter) throws GitLabApiException {
538+
return (getProjects(filter, getDefaultPerPage()).stream());
539+
}
540+
482541
/**
483542
* Get a list of visible projects owned by the given user.
484543
*
485544
* <pre><code>GET /users/:user_id/projects</code></pre>
486545
*
487546
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
488547
* @param filter the ProjectFilter instance holding the filter values for the query
489-
* @return a list of visible projects owned by the given use
548+
* @return a list of visible projects owned by the given user
490549
* @throws GitLabApiException if any exception occurs
491550
*/
492551
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter) throws GitLabApiException {
@@ -502,7 +561,7 @@ public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filt
502561
* @param filter the ProjectFilter instance holding the filter values for the query
503562
* @param page the page to get
504563
* @param perPage the number of projects per page
505-
* @return a list of visible projects owned by the given use
564+
* @return a list of visible projects owned by the given user
506565
* @throws GitLabApiException if any exception occurs
507566
*/
508567
public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int page, int perPage) throws GitLabApiException {
@@ -520,7 +579,7 @@ public List<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filt
520579
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
521580
* @param filter the ProjectFilter instance holding the filter values for the query
522581
* @param itemsPerPage the number of Project instances that will be fetched per page
523-
* @return a Pager of visible projects owned by the given use
582+
* @return a Pager of visible projects owned by the given user
524583
* @throws GitLabApiException if any exception occurs
525584
*/
526585
public Pager<Project> getUserProjects(Object userIdOrUsername, ProjectFilter filter, int itemsPerPage) throws GitLabApiException {
@@ -536,7 +595,7 @@ public Pager<Project> getUserProjects(Object userIdOrUsername, ProjectFilter fil
536595
*
537596
* @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username
538597
* @param filter the ProjectFilter instance holding the filter values for the query
539-
* @return a Stream of visible projects owned by the given use
598+
* @return a Stream of visible projects owned by the given user
540599
* @throws GitLabApiException if any exception occurs
541600
*/
542601
public Stream<Project> getUserProjectsStream(Object userIdOrUsername, ProjectFilter filter) throws GitLabApiException {

src/test/java/org/gitlab4j/api/TestProjectApi.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.gitlab4j.api.models.Group;
4444
import org.gitlab4j.api.models.Member;
4545
import org.gitlab4j.api.models.Project;
46+
import org.gitlab4j.api.models.ProjectFilter;
4647
import org.gitlab4j.api.models.Variable;
4748
import org.gitlab4j.api.models.Visibility;
4849
import org.junit.AfterClass;
@@ -432,6 +433,24 @@ public void testProjects() throws GitLabApiException {
432433
assertTrue(projects != null);
433434
}
434435

436+
@Test
437+
public void testProjectsWithFilter() throws GitLabApiException {
438+
ProjectFilter filter = new ProjectFilter().withOwned(true).withStatistics(false);
439+
List<Project> projects = gitLabApi.getProjectApi().getProjects(filter);
440+
assertTrue(projects != null);
441+
assertTrue(projects.size() > 0);
442+
assertNull(projects.get(0).getStatistics());
443+
}
444+
445+
@Test
446+
public void testProjectsWithFilterAndStatistics() throws GitLabApiException {
447+
ProjectFilter filter = new ProjectFilter().withOwned(true).withStatistics(true);
448+
List<Project> projects = gitLabApi.getProjectApi().getProjects(filter);
449+
assertTrue(projects != null);
450+
assertTrue(projects.size() > 0);
451+
assertNotNull(projects.get(0).getStatistics());
452+
}
453+
435454
@Test
436455
public void testProjectPerPage() throws GitLabApiException {
437456
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);

0 commit comments

Comments
 (0)