Skip to content

Commit d9c722a

Browse files
committed
Mods to support and test Discussions API (#279).
1 parent 4bd96b6 commit d9c722a

File tree

8 files changed

+241
-210
lines changed

8 files changed

+241
-210
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ protected GitLabApiException handle(Exception thrown) {
554554
return (new GitLabApiException(thrown));
555555
}
556556

557+
/**
558+
* Creates a MultivaluedMap instance containing the "per_page" param.
559+
*
560+
* @param perPage the number of projects per page
561+
* @return a MultivaluedMap instance containing the "per_page" param
562+
*/
563+
protected MultivaluedMap<String, String> getPerPageQueryParam(int perPage) {
564+
return (new GitLabApiForm().withParam(PER_PAGE_PARAM, perPage).asMap());
565+
}
557566

558567
/**
559568
* Creates a MultivaluedMap instance containing "page" and "per_page" params.

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import org.gitlab4j.api.GitLabApi.ApiVersion;
1212
import org.gitlab4j.api.models.Commit;
13-
import org.gitlab4j.api.models.Discussion;
1413
import org.gitlab4j.api.models.Issue;
1514
import org.gitlab4j.api.models.MergeRequest;
1615
import org.gitlab4j.api.models.MergeRequestFilter;
@@ -280,44 +279,6 @@ public Pager<Commit> getCommits(int projectId, int mergeRequestIid, int itemsPer
280279
"projects", projectId, "merge_requests", mergeRequestIid, "commits"));
281280
}
282281

283-
/**
284-
* Get a list of merge request discussions.
285-
*
286-
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
287-
*
288-
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
289-
*
290-
* @param projectId the project ID for the merge request
291-
* @param mergeRequestIid the internal ID of the merge request
292-
* @param page the page to get
293-
* @param perPage the number of commits per page
294-
* @return a list containing the discussions for the specified merge request
295-
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
296-
*/
297-
public List<Discussion> getDiscussions(int projectId, int mergeRequestIid, int page, int perPage) throws GitLabApiException {
298-
Form formData = new GitLabApiForm().withParam("owned", false).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
299-
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid, "discussions");
300-
return (response.readEntity(new GenericType<List<Discussion>>() {}));
301-
}
302-
303-
/**
304-
* Get a Pager of merge request discussions.
305-
*
306-
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
307-
*
308-
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
309-
*
310-
* @param projectId the project ID for the merge request
311-
* @param mergeRequestIid the internal ID of the merge request
312-
* @param itemsPerPage the number of Commit instances that will be fetched per page
313-
* @return a Pager containing the discussions for the specified merge request
314-
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
315-
*/
316-
public Pager<Discussion> getDiscussions(int projectId, int mergeRequestIid, int itemsPerPage) throws GitLabApiException {
317-
return (new Pager<Discussion>(this, Discussion.class, itemsPerPage, null,
318-
"projects", projectId, "merge_requests", mergeRequestIid, "discussions"));
319-
}
320-
321282
/**
322283
* Creates a merge request and optionally assigns a reviewer to it.
323284
*
@@ -361,7 +322,6 @@ public MergeRequest createMergeRequest(Integer projectId, String sourceBranch, S
361322
return (response.readEntity(MergeRequest.class));
362323
}
363324

364-
365325
/**
366326
* Creates a merge request and optionally assigns a reviewer to it.
367327
*

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.util.ArrayList;
6-
import java.util.Collections;
76
import java.util.Iterator;
87
import java.util.List;
98
import java.util.NoSuchElementException;
@@ -87,7 +86,7 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
8786

8887
try {
8988
currentItems = mapper.readValue((InputStream) response.getEntity(), javaType);
90-
} catch (IOException e) {
89+
} catch (Exception e) {
9190
throw new GitLabApiException(e);
9291
}
9392

@@ -310,8 +309,8 @@ public Stream<T> stream() throws GitLabApiException {
310309
// regardless of what page the instance is currently on.
311310
currentPage = 0;
312311

313-
// Create a Stream.Builder to ontain all the items. This is more efficient than
314-
// getting a List with all() and streaming that List
312+
// Create a Stream.Builder to contain all the items. This is more efficient than
313+
// getting a List with all() and streaming that List
315314
Stream.Builder<T> streamBuilder = Stream.builder();
316315

317316
// Iterate through the pages and append each page of items to the stream builder

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6-
import java.io.UnsupportedEncodingException;
7-
import java.net.URLEncoder;
86
import java.nio.file.Files;
97
import java.nio.file.StandardCopyOption;
108
import java.util.List;

src/main/java/org/gitlab4j/api/models/Discussion.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,36 @@
1212
@XmlAccessorType(XmlAccessType.FIELD)
1313
public class Discussion {
1414

15-
private String id;
16-
private Boolean individualNote;
17-
private List<Note> notes;
18-
19-
public String getId() {
20-
return id;
21-
}
22-
23-
public Boolean getIndividualNote() {
24-
return individualNote;
25-
}
26-
27-
public List<Note> getNotes() {
28-
return notes;
29-
}
30-
31-
public void setId(String id) {
32-
this.id = id;
33-
}
34-
35-
public void setIndividualNote(Boolean individualNote) {
36-
this.individualNote = individualNote;
37-
}
38-
39-
public void setNotes(List<Note> notes) {
40-
this.notes = notes;
41-
}
42-
43-
@Override
44-
public String toString() {
45-
return (JacksonJson.toJsonString(this));
46-
}
47-
15+
private String id;
16+
private Boolean individualNote;
17+
private List<Note> notes;
18+
19+
public String getId() {
20+
return id;
21+
}
22+
23+
public Boolean getIndividualNote() {
24+
return individualNote;
25+
}
26+
27+
public List<Note> getNotes() {
28+
return notes;
29+
}
30+
31+
public void setId(String id) {
32+
this.id = id;
33+
}
34+
35+
public void setIndividualNote(Boolean individualNote) {
36+
this.individualNote = individualNote;
37+
}
38+
39+
public void setNotes(List<Note> notes) {
40+
this.notes = notes;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return (JacksonJson.toJsonString(this));
46+
}
4847
}

src/main/java/org/gitlab4j/api/models/Note.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public String toString() {
4040

4141
public static enum NoteableType {
4242

43-
COMMIT, ISSUE, MERGE_REQUEST, SNIPPET;
43+
COMMIT, EPIC, ISSUE, MERGE_REQUEST, SNIPPET;
4444
private static JacksonJsonEnumHelper<NoteableType> enumHelper = new JacksonJsonEnumHelper<>(NoteableType.class, true, true);
4545

4646
@JsonCreator
@@ -93,7 +93,7 @@ public String toString() {
9393
private Integer noteableIid;
9494
private Boolean system;
9595
private String title;
96-
private String updatedAt;
96+
private Date updatedAt;
9797
private Boolean upvote;
9898
private Boolean resolved;
9999
private Boolean resolvable;
@@ -204,11 +204,11 @@ public void setTitle(String title) {
204204
this.title = title;
205205
}
206206

207-
public String getUpdatedAt() {
207+
public Date getUpdatedAt() {
208208
return updatedAt;
209209
}
210210

211-
public void setUpdatedAt(String updatedAt) {
211+
public void setUpdatedAt(Date updatedAt) {
212212
this.updatedAt = updatedAt;
213213
}
214214

src/main/java/org/gitlab4j/api/utils/JacksonJson.java

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.ArrayList;
99
import java.util.Date;
1010
import java.util.List;
11+
import java.util.Map;
1112
import java.util.TimeZone;
1213

1314
import javax.ws.rs.Produces;
@@ -23,6 +24,7 @@
2324
import com.fasterxml.jackson.core.JsonParseException;
2425
import com.fasterxml.jackson.core.JsonParser;
2526
import com.fasterxml.jackson.core.JsonProcessingException;
27+
import com.fasterxml.jackson.core.type.TypeReference;
2628
import com.fasterxml.jackson.databind.DeserializationContext;
2729
import com.fasterxml.jackson.databind.DeserializationFeature;
2830
import com.fasterxml.jackson.databind.JsonDeserializer;
@@ -87,12 +89,12 @@ public ObjectMapper getObjectMapper() {
8789

8890
/**
8991
* Unmarshal the JSON data on the specified Reader instance to an instance of the provided class.
90-
*
92+
*
9193
* @param <T> the generics type for the return value
9294
* @param returnType an instance of this type class will be returned
9395
* @param reader the Reader instance that contains the JSON data
9496
* @return an instance of the provided class containing the parsed data from the Reader
95-
* @throws JsonParseException when an error occurs paresing the provided JSON
97+
* @throws JsonParseException when an error occurs parsing the provided JSON
9698
* @throws JsonMappingException if a JSON error occurs
9799
* @throws IOException if an error occurs reading the JSON data
98100
*/
@@ -103,12 +105,12 @@ public <T> T unmarshal(Class<T> returnType, Reader reader) throws JsonParseExcep
103105

104106
/**
105107
* Unmarshal the JSON data contained by the string and populate an instance of the provided returnType class.
106-
*
108+
*
107109
* @param <T> the generics type for the return value
108110
* @param returnType an instance of this type class will be returned
109111
* @param postData a String holding the POST data
110112
* @return an instance of the provided class containing the parsed data from the string
111-
* @throws JsonParseException when an error occurs paresing the provided JSON
113+
* @throws JsonParseException when an error occurs parsing the provided JSON
112114
* @throws JsonMappingException if a JSON error occurs
113115
* @throws IOException if an error occurs reading the JSON data
114116
*/
@@ -117,6 +119,70 @@ public <T> T unmarshal(Class<T> returnType, String postData) throws JsonParseExc
117119
return (objectMapper.readValue(postData, returnType));
118120
}
119121

122+
/**
123+
* Unmarshal the JSON data on the specified Reader instance and populate a List of instances of the provided returnType class.
124+
*
125+
* @param <T> the generics type for the List
126+
* @param returnType an instance of this type class will be contained in the returned List
127+
* @param reader the Reader instance that contains the JSON data
128+
* @return a List of the provided class containing the parsed data from the Reader
129+
* @throws JsonParseException when an error occurs parsing the provided JSON
130+
* @throws JsonMappingException if a JSON error occurs
131+
* @throws IOException if an error occurs reading the JSON data
132+
*/
133+
public <T> List<T> unmarshalList(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
134+
ObjectMapper objectMapper = getContext(null);
135+
return (objectMapper.readValue(reader, new TypeReference<List<T>>() {}));
136+
}
137+
138+
/**
139+
* Unmarshal the JSON data contained by the string and populate a List of instances of the provided returnType class.
140+
*
141+
* @param <T> the generics type for the List
142+
* @param returnType an instance of this type class will be contained in the returned List
143+
* @param postData a String holding the POST data
144+
* @return a List of the provided class containing the parsed data from the string
145+
* @throws JsonParseException when an error occurs parsing the provided JSON
146+
* @throws JsonMappingException if a JSON error occurs
147+
* @throws IOException if an error occurs reading the JSON data
148+
*/
149+
public <T> List<T> unmarshalList(Class<T> returnType, String postData) throws JsonParseException, JsonMappingException, IOException {
150+
ObjectMapper objectMapper = getContext(null);
151+
return objectMapper.readValue(postData, new TypeReference<List<T>>() {});
152+
}
153+
154+
/**
155+
* Unmarshal the JSON data on the specified Reader instance and populate a Map of String keys and values of the provided returnType class.
156+
*
157+
* @param <T> the generics type for the Map value
158+
* @param returnType an instance of this type class will be contained the values of the Map
159+
* @param reader the Reader instance that contains the JSON data
160+
* @return a Map containing the parsed data from the Reader
161+
* @throws JsonParseException when an error occurs parsing the provided JSON
162+
* @throws JsonMappingException if a JSON error occurs
163+
* @throws IOException if an error occurs reading the JSON data
164+
*/
165+
public <T> Map<String, T> unmarshalMap(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
166+
ObjectMapper objectMapper = getContext(null);
167+
return (objectMapper.readValue(reader, new TypeReference<Map<String, T>>() {}));
168+
}
169+
170+
/**
171+
* Unmarshal the JSON data and populate a Map of String keys and values of the provided returnType class.
172+
*
173+
* @param <T> the generics type for the Map value
174+
* @param returnType an instance of this type class will be contained the values of the Map
175+
* @param jsonData the String containing the JSON data
176+
* @return a Map containing the parsed data from the String
177+
* @throws JsonParseException when an error occurs parsing the provided JSON
178+
* @throws JsonMappingException if a JSON error occurs
179+
* @throws IOException if an error occurs reading the JSON data
180+
*/
181+
public <T> Map<String, T> unmarshalMap(Class<T> returnType, String jsonData) throws JsonParseException, JsonMappingException, IOException {
182+
ObjectMapper objectMapper = getContext(null);
183+
return (objectMapper.readValue(jsonData, new TypeReference<Map<String, T>>() {}));
184+
}
185+
120186
/**
121187
* Marshals the supplied object out as a formatted JSON string.
122188
*

0 commit comments

Comments
 (0)