Commit cd037b61 authored by Tim Olshansky's avatar Tim Olshansky

Merge pull request #7 from pweingardt/master

Issues, ProjectMembers, and notes (for issues, not just MergeRequests)
parents 5daa647d 19eeb591
package org.gitlab.api; package org.gitlab.api;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.gitlab.api.http.GitlabHTTPRequestor; import org.gitlab.api.http.GitlabHTTPRequestor;
import org.gitlab.api.models.GitlabBranch; import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabCommit; import org.gitlab.api.models.GitlabCommit;
import org.gitlab.api.models.GitlabIssue;
import org.gitlab.api.models.GitlabMergeRequest; import org.gitlab.api.models.GitlabMergeRequest;
import org.gitlab.api.models.GitlabMilestone;
import org.gitlab.api.models.GitlabNote; import org.gitlab.api.models.GitlabNote;
import org.gitlab.api.models.GitlabProject; import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabProjectHook; import org.gitlab.api.models.GitlabProjectHook;
import org.gitlab.api.models.GitlabSession;
import java.io.IOException; import org.gitlab.api.models.GitlabProjectMember;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/** /**
* Gitlab API Wrapper class * Gitlab API Wrapper class
...@@ -34,6 +37,13 @@ public class GitlabAPI { ...@@ -34,6 +37,13 @@ public class GitlabAPI {
_hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl; _hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
_apiToken = apiToken; _apiToken = apiToken;
} }
public static GitlabSession connect(String hostUrl, String username, String password) throws IOException {
String tailUrl = GitlabSession.URL;
GitlabAPI api = connect(hostUrl, null);
return api.dispatch().with("login", username).with("password", password)
.to(tailUrl, GitlabSession.class);
}
public static GitlabAPI connect(String hostUrl, String apiToken) { public static GitlabAPI connect(String hostUrl, String apiToken) {
return new GitlabAPI(hostUrl, apiToken); return new GitlabAPI(hostUrl, apiToken);
...@@ -87,19 +97,7 @@ public class GitlabAPI { ...@@ -87,19 +97,7 @@ public class GitlabAPI {
public List<GitlabProject> getAllProjects() throws IOException { public List<GitlabProject> getAllProjects() throws IOException {
String tailUrl = GitlabProject.URL; String tailUrl = GitlabProject.URL;
List<GitlabProject> results = new ArrayList<GitlabProject>(); return retrieve().getAll(tailUrl, GitlabProject[].class);
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 { public List<GitlabMergeRequest> getOpenMergeRequests(GitlabProject project) throws IOException {
...@@ -128,18 +126,7 @@ public class GitlabAPI { ...@@ -128,18 +126,7 @@ public class GitlabAPI {
public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) throws IOException { public List<GitlabMergeRequest> getAllMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL; String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL;
List<GitlabMergeRequest> results = new ArrayList<GitlabMergeRequest>(); return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
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 { public GitlabMergeRequest getMergeRequest(GitlabProject project, Integer mergeRequestId) throws IOException {
...@@ -160,19 +147,8 @@ public class GitlabAPI { ...@@ -160,19 +147,8 @@ public class GitlabAPI {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() + String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabMergeRequest.URL + "/" + mergeRequest.getId() +
GitlabNote.URL; GitlabNote.URL;
List<GitlabNote> results = new ArrayList<GitlabNote>(); return retrieve().getAll(tailUrl, GitlabNote[].class);
Iterator<GitlabNote[]> iterator = retrieve().asIterator(tailUrl, GitlabNote[].class);
while (iterator.hasNext()) {
GitlabNote[] projects = iterator.next();
if (projects.length > 0) {
results.addAll(Arrays.asList(projects));
}
}
return results;
} }
...@@ -244,4 +220,84 @@ public class GitlabAPI { ...@@ -244,4 +220,84 @@ public class GitlabAPI {
GitlabMergeRequest[] mergeRequests = retrieve().to(tailUrl, GitlabMergeRequest[].class); GitlabMergeRequest[] mergeRequests = retrieve().to(tailUrl, GitlabMergeRequest[].class);
return Arrays.asList(mergeRequests); return Arrays.asList(mergeRequests);
} }
public List<GitlabIssue> getIssues(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabIssue.URL;
return retrieve().getAll(tailUrl, GitlabIssue[].class);
}
public GitlabIssue getIssue(Integer projectId, Integer issueId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabIssue.URL + "/" + issueId;
return retrieve().to(tailUrl, GitlabIssue.class);
}
public GitlabIssue createIssue(int projectId, int assigneeId, int milestoneId, String labels,
String description, String title) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabIssue.URL;
GitlabHTTPRequestor requestor = dispatch();
applyIssue(requestor, projectId, assigneeId, milestoneId, labels, description, title);
return requestor.to(tailUrl, GitlabIssue.class);
}
public GitlabIssue editIssue(int projectId, int issueId, int assigneeId, int milestoneId, String labels,
String description, String title, GitlabIssue.Action action) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabIssue.URL + "/" + issueId;
GitlabHTTPRequestor requestor = retrieve().method("PUT");
applyIssue(requestor, projectId, assigneeId, milestoneId, labels, description, title);
if(action != GitlabIssue.Action.LEAVE) {
requestor.with("state_event", action.toString().toLowerCase());
}
return requestor.to(tailUrl, GitlabIssue.class);
}
private void applyIssue(GitlabHTTPRequestor requestor, int projectId,
int assigneeId, int milestoneId, String labels, String description,
String title) {
requestor.with("title", title)
.with("description", description)
.with("labels", labels)
.with("milestone_id", milestoneId);
if(assigneeId != 0) {
requestor.with("assignee_id", assigneeId == -1 ? 0 : assigneeId);
}
}
public List<GitlabNote> getNotes(GitlabIssue issue) throws IOException {
String tailUrl = GitlabProject.URL + "/" + issue.getProjectId() + GitlabIssue.URL + "/"
+ issue.getId() + GitlabNote.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabNote[].class));
}
public GitlabNote createNote(Integer projectId, Integer issueId, String message) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabIssue.URL
+ "/" + issueId + GitlabNote.URL;
return dispatch().with("body", message).to(tailUrl, GitlabNote.class);
}
public GitlabNote createNote(GitlabIssue issue, String message) throws IOException {
return createNote(issue.getProjectId(), issue.getId(), message);
}
public List<GitlabMilestone> getMilestones(GitlabProject project) throws IOException {
return getMilestones(project.getId());
}
public List<GitlabMilestone> getMilestones(Integer projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabMilestone.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabMilestone[].class));
}
public List<GitlabProjectMember> getProjectMembers(GitlabProject project) throws IOException {
return getProjectMembers(project.getId());
}
public List<GitlabProjectMember> getProjectMembers(Integer projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabProjectMember.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class));
}
} }
package org.gitlab.api.http; package org.gitlab.api.http;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -14,10 +9,23 @@ import java.net.HttpURLConnection; ...@@ -14,10 +9,23 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.ProtocolException; import java.net.ProtocolException;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.gitlab.api.GitlabAPI; import org.gitlab.api.GitlabAPI;
...@@ -128,6 +136,19 @@ public class GitlabHTTPRequestor { ...@@ -128,6 +136,19 @@ public class GitlabHTTPRequestor {
return null; return null;
} }
public <T> List<T> getAll(final String tailUrl, final Class<T[]> type) {
List<T> results = new ArrayList<T>();
Iterator<T[]> iterator = asIterator(tailUrl, type);
while (iterator.hasNext()) {
T[] requests = iterator.next();
if (requests.length > 0) {
results.addAll(Arrays.asList(requests));
}
}
return results;
}
public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) { public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) {
method("GET"); // Ensure we only use iterators for GET requests method("GET"); // Ensure we only use iterators for GET requests
...@@ -149,7 +170,6 @@ public class GitlabHTTPRequestor { ...@@ -149,7 +170,6 @@ public class GitlabHTTPRequestor {
} }
} }
@Override
public boolean hasNext() { public boolean hasNext() {
fetch(); fetch();
if (_next.getClass().isArray()) { if (_next.getClass().isArray()) {
...@@ -160,7 +180,6 @@ public class GitlabHTTPRequestor { ...@@ -160,7 +180,6 @@ public class GitlabHTTPRequestor {
} }
} }
@Override
public T next() { public T next() {
fetch(); fetch();
T record = _next; T record = _next;
......
package org.gitlab.api.models;
import java.util.Date;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabIssue {
public enum Action {
LEAVE, CLOSE, REOPEN
}
public static final String StateClosed = "closed";
public static final String StateOpened = "opened";
public static final String URL = "/issues";
private int _id;
private int _iid;
@JsonProperty("project_id")
private int _projectId;
private String _title;
private String _description;
private String[] _labels;
private GitlabMilestone _milestone;
private GitlabUser _assignee;
private GitlabUser _author;
private String _state;
@JsonProperty("updated_at")
private Date _updatedAt;
@JsonProperty("created_at")
private Date _createdAt;
public int getId() {
return _id;
}
public void setId(int id) {
_id = id;
}
public int getIid() {
return _iid;
}
public void setIid(int iid) {
_iid = iid;
}
public int getProjectId() {
return _projectId;
}
public void setProjectId(int projectId) {
_projectId = projectId;
}
public String getTitle() {
return _title;
}
public void setTitle(String title) {
_title = title;
}
public String getDescription() {
return _description;
}
public void setDescription(String description) {
_description = description;
}
public String[] getLabels() {
return _labels;
}
public void setLabels(String[] labels) {
_labels = labels;
}
public GitlabMilestone getMilestone() {
return _milestone;
}
public void setMilestone(GitlabMilestone milestone) {
_milestone = milestone;
}
public GitlabUser getAssignee() {
return _assignee;
}
public void setAssignee(GitlabUser assignee) {
_assignee = assignee;
}
public GitlabUser getAuthor() {
return _author;
}
public void setAuthor(GitlabUser author) {
_author = author;
}
public String getState() {
return _state;
}
public void setState(String state) {
_state = state;
}
public Date getUpdatedAt() {
return _updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
_updatedAt = updatedAt;
}
public Date getCreatedAt() {
return _createdAt;
}
public void setCreatedAt(Date createdAt) {
_createdAt = createdAt;
}
}
package org.gitlab.api.models;
import java.util.Date;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabMilestone {
public static final String URL = "/milestones";
private int _id;
private int _iid;
private int _projectId;
private String _title;
private String _description;
@JsonProperty("due_date")
private Date _dueDate;
private String _state;
@JsonProperty("updated_date")
private Date _updatedDate;
@JsonProperty("created_date")
private Date _createdDate;
public int getId() {
return _id;
}
public void setId(int id) {
_id = id;
}
public int getIid() {
return _iid;
}
public void setIid(int iid) {
_iid = iid;
}
public int getProjectId() {
return _projectId;
}
public void setProjectId(int projectId) {
_projectId = projectId;
}
public String getTitle() {
return _title;
}
public void setTitle(String title) {
_title = title;
}
public String getDescription() {
return _description;
}
public void setDescription(String description) {
_description = description;
}
public Date getDueDate() {
return _dueDate;
}
public void setDueDate(Date dueDate) {
_dueDate = dueDate;
}
public String getState() {
return _state;
}
public void setState(String state) {
_state = state;
}
public Date getUpdatedDate() {
return _updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
_updatedDate = updatedDate;
}
public Date getCreatedDate() {
return _createdDate;
}
public void setCreatedDate(Date createdDate) {
_createdDate = createdDate;
}
}
package org.gitlab.api.models;
public class GitlabProjectMember extends GitlabUser {
public static final String URL = "/members";
private int _accessLevel;
public int getAccessLevel() {
return _accessLevel;
}
public void setAccessLevel(int accessLevel) {
_accessLevel = accessLevel;
}
}
package org.gitlab.api.models;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabSession extends GitlabUser {
public static final String URL = "/session";
@JsonProperty("private_token")
private String _privateToken;
public String getPrivateToken() {
return _privateToken;
}
public void setPrivateToken(String privateToken) {
_privateToken = privateToken;
}
}
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