Commit d9c722ae authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support and test Discussions API (#279).

parent 4bd96b69
......@@ -554,6 +554,15 @@ public abstract class AbstractApi implements Constants {
return (new GitLabApiException(thrown));
}
/**
* Creates a MultivaluedMap instance containing the "per_page" param.
*
* @param perPage the number of projects per page
* @return a MultivaluedMap instance containing the "per_page" param
*/
protected MultivaluedMap<String, String> getPerPageQueryParam(int perPage) {
return (new GitLabApiForm().withParam(PER_PAGE_PARAM, perPage).asMap());
}
/**
* Creates a MultivaluedMap instance containing "page" and "per_page" params.
......
......@@ -10,7 +10,6 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Discussion;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestFilter;
......@@ -280,44 +279,6 @@ public class MergeRequestApi extends AbstractApi {
"projects", projectId, "merge_requests", mergeRequestIid, "commits"));
}
/**
* Get a list of merge request discussions.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
*
* @param projectId the project ID for the merge request
* @param mergeRequestIid the internal ID of the merge request
* @param page the page to get
* @param perPage the number of commits per page
* @return a list containing the discussions for the specified merge request
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<Discussion> getDiscussions(int projectId, int mergeRequestIid, int page, int perPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("owned", false).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid, "discussions");
return (response.readEntity(new GenericType<List<Discussion>>() {}));
}
/**
* Get a Pager of merge request discussions.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* GET /projects/:id/merge_requests/:merge_request_iid/discussions
*
* @param projectId the project ID for the merge request
* @param mergeRequestIid the internal ID of the merge request
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager containing the discussions for the specified merge request
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<Discussion> getDiscussions(int projectId, int mergeRequestIid, int itemsPerPage) throws GitLabApiException {
return (new Pager<Discussion>(this, Discussion.class, itemsPerPage, null,
"projects", projectId, "merge_requests", mergeRequestIid, "discussions"));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
*
......@@ -361,7 +322,6 @@ public class MergeRequestApi extends AbstractApi {
return (response.readEntity(MergeRequest.class));
}
/**
* Creates a merge request and optionally assigns a reviewer to it.
*
......
......@@ -3,7 +3,6 @@ package org.gitlab4j.api;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
......@@ -87,7 +86,7 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
try {
currentItems = mapper.readValue((InputStream) response.getEntity(), javaType);
} catch (IOException e) {
} catch (Exception e) {
throw new GitLabApiException(e);
}
......@@ -310,8 +309,8 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
// regardless of what page the instance is currently on.
currentPage = 0;
// Create a Stream.Builder to ontain all the items. This is more efficient than
// getting a List with all() and streaming that List
// Create a Stream.Builder to contain all the items. This is more efficient than
// getting a List with all() and streaming that List
Stream.Builder<T> streamBuilder = Stream.builder();
// Iterate through the pages and append each page of items to the stream builder
......
......@@ -3,8 +3,6 @@ package org.gitlab4j.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
......
......@@ -12,37 +12,36 @@ import org.gitlab4j.api.utils.JacksonJson;
@XmlAccessorType(XmlAccessType.FIELD)
public class Discussion {
private String id;
private Boolean individualNote;
private List<Note> notes;
public String getId() {
return id;
}
public Boolean getIndividualNote() {
return individualNote;
}
public List<Note> getNotes() {
return notes;
}
public void setId(String id) {
this.id = id;
}
public void setIndividualNote(Boolean individualNote) {
this.individualNote = individualNote;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
private String id;
private Boolean individualNote;
private List<Note> notes;
public String getId() {
return id;
}
public Boolean getIndividualNote() {
return individualNote;
}
public List<Note> getNotes() {
return notes;
}
public void setId(String id) {
this.id = id;
}
public void setIndividualNote(Boolean individualNote) {
this.individualNote = individualNote;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
......@@ -40,7 +40,7 @@ public class Note {
public static enum NoteableType {
COMMIT, ISSUE, MERGE_REQUEST, SNIPPET;
COMMIT, EPIC, ISSUE, MERGE_REQUEST, SNIPPET;
private static JacksonJsonEnumHelper<NoteableType> enumHelper = new JacksonJsonEnumHelper<>(NoteableType.class, true, true);
@JsonCreator
......@@ -93,7 +93,7 @@ public class Note {
private Integer noteableIid;
private Boolean system;
private String title;
private String updatedAt;
private Date updatedAt;
private Boolean upvote;
private Boolean resolved;
private Boolean resolvable;
......@@ -204,11 +204,11 @@ public class Note {
this.title = title;
}
public String getUpdatedAt() {
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
......
......@@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.ws.rs.Produces;
......@@ -23,6 +24,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
......@@ -87,12 +89,12 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
/**
* Unmarshal the JSON data on the specified Reader instance to an instance of the provided class.
*
*
* @param <T> the generics type for the return value
* @param returnType an instance of this type class will be returned
* @param reader the Reader instance that contains the JSON data
* @return an instance of the provided class containing the parsed data from the Reader
* @throws JsonParseException when an error occurs paresing the provided JSON
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
......@@ -103,12 +105,12 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
/**
* Unmarshal the JSON data contained by the string and populate an instance of the provided returnType class.
*
*
* @param <T> the generics type for the return value
* @param returnType an instance of this type class will be returned
* @param postData a String holding the POST data
* @return an instance of the provided class containing the parsed data from the string
* @throws JsonParseException when an error occurs paresing the provided JSON
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
......@@ -117,6 +119,70 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
return (objectMapper.readValue(postData, returnType));
}
/**
* Unmarshal the JSON data on the specified Reader instance and populate a List of instances of the provided returnType class.
*
* @param <T> the generics type for the List
* @param returnType an instance of this type class will be contained in the returned List
* @param reader the Reader instance that contains the JSON data
* @return a List of the provided class containing the parsed data from the Reader
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> List<T> unmarshalList(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(reader, new TypeReference<List<T>>() {}));
}
/**
* Unmarshal the JSON data contained by the string and populate a List of instances of the provided returnType class.
*
* @param <T> the generics type for the List
* @param returnType an instance of this type class will be contained in the returned List
* @param postData a String holding the POST data
* @return a List of the provided class containing the parsed data from the string
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> List<T> unmarshalList(Class<T> returnType, String postData) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return objectMapper.readValue(postData, new TypeReference<List<T>>() {});
}
/**
* Unmarshal the JSON data on the specified Reader instance and populate a Map of String keys and values of the provided returnType class.
*
* @param <T> the generics type for the Map value
* @param returnType an instance of this type class will be contained the values of the Map
* @param reader the Reader instance that contains the JSON data
* @return a Map containing the parsed data from the Reader
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> Map<String, T> unmarshalMap(Class<T> returnType, Reader reader) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(reader, new TypeReference<Map<String, T>>() {}));
}
/**
* Unmarshal the JSON data and populate a Map of String keys and values of the provided returnType class.
*
* @param <T> the generics type for the Map value
* @param returnType an instance of this type class will be contained the values of the Map
* @param jsonData the String containing the JSON data
* @return a Map containing the parsed data from the String
* @throws JsonParseException when an error occurs parsing the provided JSON
* @throws JsonMappingException if a JSON error occurs
* @throws IOException if an error occurs reading the JSON data
*/
public <T> Map<String, T> unmarshalMap(Class<T> returnType, String jsonData) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = getContext(null);
return (objectMapper.readValue(jsonData, new TypeReference<Map<String, T>>() {}));
}
/**
* Marshals the supplied object out as a formatted JSON string.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment