Commit 3e9426f3 authored by Tim Olshansky's avatar Tim Olshansky

Fixing issue caused by Jackson throwing an error for unknown properties in...

Fixing issue caused by Jackson throwing an error for unknown properties in timols/jenkins-gitlab-merge-request-builder-plugin#3
parent f106195c
......@@ -4,7 +4,7 @@
<groupId>org.gitlab</groupId>
<artifactId>java-gitlab-api</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Gitlab Java API Wrapper</name>
<description>A Java wrapper for the Gitlab Git Hosting Server API</description>
......
package org.gitlab.api;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.gitlab.api.http.GitlabHTTPRequestor;
import org.gitlab.api.models.GitlabCommit;
......@@ -12,6 +8,13 @@ import org.gitlab.api.models.GitlabMergeRequest;
import org.gitlab.api.models.GitlabNote;
import org.gitlab.api.models.GitlabProject;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Gitlab API Wrapper class
*
......@@ -22,7 +25,7 @@ public class GitlabAPI {
private final String _apiToken;
private boolean _ignoreCertificateErrors = false;
private static final String API_NAMESPACE = "/api/v3";
public static final ObjectMapper MAPPER = new ObjectMapper();
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private GitlabAPI(String hostUrl, String apiToken) {
_hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
......@@ -74,8 +77,30 @@ public class GitlabAPI {
return retrieve().to(tailUrl, GitlabProject.class);
}
public List<GitlabProject> getProjects() throws IOException {
String tailUrl = GitlabProject.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabProject[].class));
}
public List<GitlabProject> getAllProjects() throws IOException {
String tailUrl = GitlabProject.URL;
List<GitlabProject> results = new ArrayList<GitlabProject>();
Iterator<GitlabProject[]> iterator = retrieve().asIterator(tailUrl, GitlabProject[].class);
while (iterator.hasNext()) {
GitlabProject[] projects = iterator.next();
if (projects.length > 0) {
results.addAll(Arrays.asList(projects));
}
}
return results;
}
public List<GitlabMergeRequest> getOpenMergeRequests(GitlabProject project) throws IOException {
List<GitlabMergeRequest> allMergeRequests = getMergeRequests(project);
List<GitlabMergeRequest> allMergeRequests = getAllMergeRequests(project);
List<GitlabMergeRequest> openMergeRequests = new ArrayList<GitlabMergeRequest>();
for (GitlabMergeRequest mergeRequest : allMergeRequests) {
......@@ -99,6 +124,22 @@ public class GitlabAPI {
return fetchMergeRequests(tailUrl);
}
public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL;
List<GitlabMergeRequest> results = new ArrayList<GitlabMergeRequest>();
Iterator<GitlabMergeRequest[]> iterator = retrieve().asIterator(tailUrl, GitlabMergeRequest[].class);
while (iterator.hasNext()) {
GitlabMergeRequest[] requests = iterator.next();
if (requests.length > 0) {
results.addAll(Arrays.asList(requests));
}
}
return results;
}
public GitlabMergeRequest getMergeRequest(GitlabProject project, Integer mergeRequestId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + "/merge_request/" + mergeRequestId;
return retrieve().to(tailUrl, GitlabMergeRequest.class);
......@@ -106,8 +147,8 @@ public class GitlabAPI {
public List<GitlabNote> getNotes(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() +
GitlabNote.URL;
GitlabMergeRequest.URL + "/" + mergeRequest.getId() +
GitlabNote.URL;
GitlabNote[] notes = retrieve().to(tailUrl, GitlabNote[].class);
return Arrays.asList(notes);
......@@ -115,7 +156,7 @@ public class GitlabAPI {
public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
"/repository" + GitlabCommit.URL + "?ref_name=" + mergeRequest.getSourceBranch();
"/repository" + GitlabCommit.URL + "?ref_name=" + mergeRequest.getSourceBranch();
GitlabCommit[] commits = retrieve().to(tailUrl, GitlabCommit[].class);
return Arrays.asList(commits);
......@@ -123,7 +164,7 @@ public class GitlabAPI {
public GitlabNote createNote(GitlabMergeRequest mergeRequest, String body) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabNote.URL;
GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabNote.URL;
return dispatch().with("body", body).to(tailUrl, GitlabNote.class);
}
......
......@@ -11,10 +11,12 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
import org.gitlab.api.GitlabAPI;
......@@ -122,6 +124,102 @@ public class GitlabHTTPRequestor {
return null;
}
public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) {
method("GET"); // Ensure we only use iterators for GET requests
// Ensure that we don't submit any data and alert the user
if (!_data.isEmpty()) {
throw new IllegalStateException();
}
return new Iterator<T>() {
T _next;
URL _url;
{
try {
_url = _root.getAPIUrl(tailApiUrl);
} catch (IOException e) {
throw new Error(e);
}
}
@Override
public boolean hasNext() {
fetch();
if (_next.getClass().isArray()) {
Object[] arr = (Object[]) _next;
return arr.length != 0;
} else {
return _next != null;
}
}
@Override
public T next() {
fetch();
T record = _next;
if (record == null) {
throw new NoSuchElementException();
}
_next = null;
return record;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void fetch() {
if (_next != null) {
return;
}
if (_url == null) {
return;
}
try {
HttpURLConnection connection = setupConnection(_url);
try {
_next = parse(connection, type, null);
assert _next != null;
findNextUrl(connection);
} catch (IOException e) {
handleAPIError(e, connection);
}
} catch (IOException e) {
throw new Error(e);
}
}
private void findNextUrl(HttpURLConnection connection) throws MalformedURLException {
String url = _url.toString();
_url = null;
/* Increment the page number for the url if a "page" property exists,
* otherwise, add the page property and increment it.
* The Gitlab API is not a compliant hypermedia REST api, so we use
* a naive implementation.
*/
Pattern pattern = Pattern.compile("([&|?])page=(\\d+)");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
Integer page = Integer.parseInt(matcher.group(2)) + 1;
_url = new URL(matcher.replaceAll(matcher.group(1) + "page=" + page));
} else {
// Since the page query was not present, its safe to assume that we just
// currently used the first page, so we can default to page 2
_url = new URL(url + "&page=2");
}
}
};
}
private void submitData(HttpURLConnection connection) throws IOException {
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
......
......@@ -8,6 +8,7 @@ public class GitlabNamespace {
private Integer _id;
private String _name;
private String _path;
private String _description;
@JsonProperty("created_at")
private Date _createdAt;
......@@ -65,4 +66,12 @@ public class GitlabNamespace {
public void setPath(String path) {
_path = path;
}
public String getDescription() {
return _description;
}
public void setDescription(String description) {
_description = description;
}
}
package org.gitlab.api;
import java.io.IOException;
import java.net.URL;
import org.gitlab.api.models.GitlabProject;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import static org.junit.Assert.assertEquals;
public class GitlabAPITest {
......
Markdown is supported
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