Skip to content

Commit 39225d2

Browse files
authored
Merge pull request timols#349 from tristanlins/feature-specialized-queries
Proposal, use specialized query objects instead of parameters
2 parents 198ea33 + 0955ca7 commit 39225d2

File tree

5 files changed

+410
-28
lines changed

5 files changed

+410
-28
lines changed

src/main/java/org/gitlab/api/GitlabAPI.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.gitlab.api.http.GitlabHTTPRequestor;
66
import org.gitlab.api.http.Query;
77
import org.gitlab.api.models.*;
8+
import org.gitlab.api.query.PaginationQuery;
9+
import org.gitlab.api.query.PipelinesQuery;
10+
import org.gitlab.api.query.ProjectsQuery;
811
import org.slf4j.Logger;
912
import org.slf4j.LoggerFactory;
1013

@@ -843,6 +846,16 @@ public List<GitlabProject> getProjectsWithPagination(int page, int perPage) thro
843846
return getProjectsWithPagination(pagination);
844847
}
845848

849+
/**
850+
* Get a list of projects accessible by the authenticated user.
851+
*
852+
* @return A list of gitlab projects
853+
*/
854+
public List<GitlabProject> getProjects(ProjectsQuery projectsQuery) {
855+
String tailUrl = GitlabProject.URL + projectsQuery;
856+
return retrieve().getAll(tailUrl, GitlabProject[].class);
857+
}
858+
846859
/**
847860
* Get a list of projects by pagination accessible by the authenticated user.
848861
*
@@ -999,6 +1012,47 @@ public GitlabPipeline getProjectPipeline(Integer projectId, Integer pipelineId)
9991012
return retrieve().to(tailUrl, GitlabPipeline.class);
10001013
}
10011014

1015+
/**
1016+
* Get a list of a project's pipelines in Gitlab
1017+
*
1018+
* @param project the project
1019+
* @return A list of project pipelines
1020+
*/
1021+
public List<GitlabPipeline> getProjectPipelines(GitlabProject project) {
1022+
return getProjectPipelines(project.getId());
1023+
}
1024+
1025+
/**
1026+
* Get a list of a project's pipelines in Gitlab
1027+
*
1028+
* @param projectId the project id
1029+
* @return A list of project pipelines
1030+
*/
1031+
public List<GitlabPipeline> getProjectPipelines(Integer projectId) {
1032+
return getProjectPipelines(projectId, new PipelinesQuery().withPerPage(PaginationQuery.MAX_ITEMS_PER_PAGE));
1033+
}
1034+
1035+
/**
1036+
* Get a list of a project's pipelines in Gitlab
1037+
*
1038+
* @param project the project
1039+
* @return A list of project pipelines
1040+
*/
1041+
public List<GitlabPipeline> getProjectPipelines(GitlabProject project, PipelinesQuery pipelinesQuery) {
1042+
return getProjectPipelines(project.getId(), pipelinesQuery);
1043+
}
1044+
1045+
/**
1046+
* Get a list of a project's pipelines in Gitlab
1047+
*
1048+
* @param projectId the project id
1049+
* @return A list of project pipelines
1050+
*/
1051+
public List<GitlabPipeline> getProjectPipelines(Integer projectId, PipelinesQuery pipelinesQuery) {
1052+
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabPipeline.URL + pipelinesQuery;
1053+
return retrieve().getAll(tailUrl, GitlabPipeline[].class);
1054+
}
1055+
10021056
/**
10031057
* Gets a list of a project's jobs in Gitlab
10041058
*
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
package org.gitlab.api;
22

33
import org.gitlab.api.http.Query;
4+
import org.gitlab.api.query.PaginationQuery;
45

5-
import java.io.UnsupportedEncodingException;
6+
/**
7+
* @deprecated Use {@link PaginationQuery#PARAM_PAGE} instead.
8+
*/
9+
@Deprecated
10+
public class Pagination extends PaginationQuery {
611

7-
public class Pagination {
8-
public static final String PARAM_PAGE = "page";
9-
public static final String PARAM_PER_PAGE = "per_page";
10-
public static final int MAX_ITEMS_PER_PAGE = 100;
11-
private final Query paginationQuery = new Query();
12+
/**
13+
* @deprecated Use {@link PaginationQuery#PARAM_PAGE} instead.
14+
*/
15+
@Deprecated
16+
public static final String PARAM_PAGE = PaginationQuery.PARAM_PAGE;
1217

13-
public void setPage(int page) {
14-
try {
15-
paginationQuery.append(PARAM_PAGE, String.valueOf(page));
16-
} catch (UnsupportedEncodingException ignored) {
17-
}
18-
}
18+
/**
19+
@deprecated Use {@link PaginationQuery#PARAM_PER_PAGE} instead.
20+
*/
21+
@Deprecated
22+
public static final String PARAM_PER_PAGE = PaginationQuery.PARAM_PER_PAGE;
23+
24+
/**
25+
@deprecated Use {@link PaginationQuery#MAX_ITEMS_PER_PAGE} instead.
26+
*/
27+
@Deprecated
28+
public static final int MAX_ITEMS_PER_PAGE = PaginationQuery.MAX_ITEMS_PER_PAGE;
1929

20-
public void setPerPage(int perPage) {
21-
if (perPage > MAX_ITEMS_PER_PAGE) {
22-
throw new IllegalArgumentException("Max value for perPage is " + MAX_ITEMS_PER_PAGE);
23-
}
24-
try {
25-
paginationQuery.append(PARAM_PER_PAGE, String.valueOf(perPage));
26-
} catch (UnsupportedEncodingException ignored) {
27-
}
28-
}
29-
3030
public Pagination withPage(int page) {
3131
setPage(page);
3232
return this;
3333
}
34-
34+
3535
public Pagination withPerPage(int perPage) {
3636
setPerPage(perPage);
3737
return this;
3838
}
3939

4040
public Query asQuery() {
41-
return paginationQuery;
41+
return this;
4242
}
4343

44-
@Override
45-
public String toString() {
46-
return paginationQuery.toString();
47-
}
4844
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.gitlab.api.query;
2+
3+
import org.gitlab.api.http.Query;
4+
5+
import java.io.UnsupportedEncodingException;
6+
7+
public class PaginationQuery extends Query {
8+
9+
public static final String PARAM_PAGE = "page";
10+
public static final String PARAM_PER_PAGE = "per_page";
11+
public static final int MAX_ITEMS_PER_PAGE = 100;
12+
13+
public void setPage(int page) {
14+
try {
15+
append(PARAM_PAGE, String.valueOf(page));
16+
} catch (UnsupportedEncodingException ignored) {
17+
}
18+
}
19+
20+
public void setPerPage(int perPage) {
21+
if (perPage > MAX_ITEMS_PER_PAGE) {
22+
throw new IllegalArgumentException("Max value for perPage is " + MAX_ITEMS_PER_PAGE);
23+
}
24+
try {
25+
append(PARAM_PER_PAGE, String.valueOf(perPage));
26+
} catch (UnsupportedEncodingException ignored) {
27+
}
28+
}
29+
30+
public PaginationQuery withPage(int page) {
31+
setPage(page);
32+
return this;
33+
}
34+
35+
public PaginationQuery withPerPage(int perPage) {
36+
setPerPage(perPage);
37+
return this;
38+
}
39+
40+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.gitlab.api.query;
2+
3+
import java.io.UnsupportedEncodingException;
4+
5+
public class PipelinesQuery extends PaginationQuery {
6+
7+
public void setScope(String scope) throws UnsupportedEncodingException {
8+
appendIf("scope", scope);
9+
}
10+
11+
public PipelinesQuery withScope(String scope) throws UnsupportedEncodingException {
12+
this.setScope(scope);
13+
return this;
14+
}
15+
16+
public void setStatus(String status) throws UnsupportedEncodingException {
17+
appendIf("status", status);
18+
}
19+
20+
public PipelinesQuery withStatus(String status) throws UnsupportedEncodingException {
21+
this.setStatus(status);
22+
return this;
23+
}
24+
25+
public void setRef(String ref) throws UnsupportedEncodingException {
26+
appendIf("ref", ref);
27+
}
28+
29+
public PipelinesQuery withRef(String ref) throws UnsupportedEncodingException {
30+
this.setRef(ref);
31+
return this;
32+
}
33+
34+
public void setSha(String sha) throws UnsupportedEncodingException {
35+
appendIf("sha", sha);
36+
}
37+
38+
public PipelinesQuery withSha(String sha) throws UnsupportedEncodingException {
39+
this.setSha(sha);
40+
return this;
41+
}
42+
43+
public void setYamlErrors(Boolean yamlErrors) throws UnsupportedEncodingException {
44+
appendIf("yaml_errors", yamlErrors);
45+
}
46+
47+
public PipelinesQuery withYamlErrors(Boolean yamlErrors) throws UnsupportedEncodingException {
48+
this.setYamlErrors(yamlErrors);
49+
return this;
50+
}
51+
52+
public void setName(String name) throws UnsupportedEncodingException {
53+
appendIf("name", name);
54+
}
55+
56+
public PipelinesQuery withName(String name) throws UnsupportedEncodingException {
57+
this.setName(name);
58+
return this;
59+
}
60+
61+
public void setUsername(String username) throws UnsupportedEncodingException {
62+
appendIf("username", username);
63+
}
64+
65+
public PipelinesQuery withUsername(String username) throws UnsupportedEncodingException {
66+
this.setUsername(username);
67+
return this;
68+
}
69+
70+
public void setOrderBy(String orderBy) throws UnsupportedEncodingException {
71+
appendIf("order_by", orderBy);
72+
}
73+
74+
public PipelinesQuery withOrderBy(String orderBy) throws UnsupportedEncodingException {
75+
this.setOrderBy(orderBy);
76+
return this;
77+
}
78+
79+
public void setSort(String sort) throws UnsupportedEncodingException {
80+
appendIf("sort", sort);
81+
}
82+
83+
public PipelinesQuery withSort(String sort) throws UnsupportedEncodingException {
84+
this.setSort(sort);
85+
return this;
86+
}
87+
88+
@Override
89+
public PipelinesQuery withPage(int page) {
90+
super.withPage(page);
91+
return this;
92+
}
93+
94+
@Override
95+
public PipelinesQuery withPerPage(int perPage) {
96+
super.withPerPage(perPage);
97+
return this;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)