Skip to content

Commit 5e57e9f

Browse files
committed
Added support fopr filtering when fetching merge requests (#187, #193).
1 parent 677dcf3 commit 5e57e9f

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ public String toString() {
104104
}
105105
}
106106

107+
/** Enum to use for ordering the results of getMergeRequests(). */
108+
public enum MergeRequestOrderBy {
109+
110+
CREATED_AT, UPDATED_AT;
111+
112+
private static JacksonJsonEnumHelper<MergeRequestOrderBy> enumHelper = new JacksonJsonEnumHelper<>(MergeRequestOrderBy.class);
113+
114+
@JsonCreator
115+
public static MergeRequestOrderBy forValue(String value) {
116+
return enumHelper.forValue(value);
117+
}
118+
119+
@JsonValue
120+
public String toValue() {
121+
return (enumHelper.toString(this));
122+
}
123+
124+
@Override
125+
public String toString() {
126+
return (enumHelper.toString(this));
127+
}
128+
}
129+
107130
/** Enum to use for ordering the results of getGroups() and getSubGroups(). */
108131
public enum GroupOrderBy {
109132

@@ -191,6 +214,27 @@ public String toString() {
191214
}
192215
}
193216

217+
/** Enum to use for specifying the scope for getMergeRequests methods. */
218+
public enum MergeRequestScope {
219+
220+
CREATED_BY_ME, ASSIGNED_TO_ME, ALL;
221+
222+
private static JacksonJsonEnumHelper<MergeRequestScope> enumHelper = new JacksonJsonEnumHelper<>(MergeRequestScope.class);
223+
224+
@JsonCreator
225+
public static MergeRequestScope forValue(String value) { return enumHelper.forValue(value); }
226+
227+
@JsonValue
228+
public String toValue() {
229+
return (enumHelper.toString(this));
230+
}
231+
232+
@Override
233+
public String toString() {
234+
return (enumHelper.toString(this));
235+
}
236+
}
237+
194238
/** Enum to use for querying the state of a MergeRequest */
195239
public enum MergeRequestState {
196240

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public GitLabApiForm(MultivaluedHashMap<String, String> map) {
2323
* @param value the value of the field/attribute to add
2424
* @return this GitLabAPiForm instance
2525
*/
26-
protected GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException {
26+
public GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException {
2727
return (withParam(name, value, false));
2828
}
2929

@@ -37,7 +37,7 @@ protected GitLabApiForm withParam(String name, Object value) throws IllegalArgum
3737
* @return this GitLabAPiForm instance
3838
* @throws IllegalArgumentException if a required parameter is null or empty
3939
*/
40-
protected GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException {
40+
public GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException {
4141

4242
if (value == null) {
4343

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import javax.ws.rs.core.Form;
77
import javax.ws.rs.core.GenericType;
8+
import javax.ws.rs.core.MultivaluedMap;
89
import javax.ws.rs.core.Response;
910

1011
import org.gitlab4j.api.GitLabApi.ApiVersion;
1112
import org.gitlab4j.api.models.Commit;
1213
import org.gitlab4j.api.models.MergeRequest;
14+
import org.gitlab4j.api.models.MergeRequestFilter;
1315
import org.gitlab4j.api.models.Participant;
1416

1517
/**
@@ -21,6 +23,52 @@ public MergeRequestApi(GitLabApi gitLabApi) {
2123
super(gitLabApi);
2224
}
2325

26+
/**
27+
* Get all merge requests matching the filter.
28+
*
29+
* GET /merge_requests
30+
*
31+
* @param filter a MergeRequestFilter instance with the filter settings
32+
* @return all merge requests for the specified project matching the filter
33+
* @throws GitLabApiException if any exception occurs
34+
*/
35+
public List<MergeRequest> getMergeRequests(MergeRequestFilter filter) throws GitLabApiException {
36+
return (getMergeRequests(filter, 1, getDefaultPerPage()));
37+
}
38+
39+
/**
40+
* Get all merge requests matching the filter.
41+
*
42+
* GET /merge_requests
43+
*
44+
* @param filter a MergeRequestFilter instance with the filter settings
45+
* @param page the page to get
46+
* @param perPage the number of MergeRequest instances per page
47+
* @return all merge requests for the specified project matching the filter
48+
* @throws GitLabApiException if any exception occurs
49+
*/
50+
public List<MergeRequest> getMergeRequests(MergeRequestFilter filter, int page, int perPage) throws GitLabApiException {
51+
MultivaluedMap<String, String> queryParams = (filter != null ?
52+
filter.getQueryParams(page, perPage).asMap() : getPageQueryParams(page, perPage));
53+
Response response = get(Response.Status.OK, queryParams, "merge_requests");
54+
return (response.readEntity(new GenericType<List<MergeRequest>>() {}));
55+
}
56+
57+
/**
58+
* Get all merge requests matching the filter.
59+
*
60+
* GET /merge_requests
61+
*
62+
* @param filter a MergeRequestFilter instance with the filter settings
63+
* @param itemsPerPage the number of MergeRequest instances that will be fetched per page
64+
* @return all merge requests for the specified project matching the filter
65+
* @throws GitLabApiException if any exception occurs
66+
*/
67+
public Pager<MergeRequest> getMergeRequests(MergeRequestFilter filter, int itemsPerPage) throws GitLabApiException {
68+
MultivaluedMap<String, String> queryParams = (filter != null ? filter.getQueryParams().asMap() : null);
69+
return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, queryParams, "merge_requests"));
70+
}
71+
2472
/**
2573
* Get all merge requests for the specified project.
2674
*

0 commit comments

Comments
 (0)