Commit c70ceb94 authored by caguilar187's avatar caguilar187

merging in changes from main gitlab java api

parents 3d4071c3 37391356
.DS_Store
.idea/*
*.iml
build/
target/*
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.jackson:jackson-core-asl:1.9.9'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.9'
compile 'commons-io:commons-io:1.4'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'junit:junit:4.11'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
\ No newline at end of file
......@@ -4,7 +4,7 @@
<groupId>org.gitlab</groupId>
<artifactId>java-gitlab-api</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>1.1.6-SNAPSHOT</version>
<name>Gitlab Java API Wrapper</name>
<description>A Java wrapper for the Gitlab Git Hosting Server API</description>
......@@ -22,6 +22,23 @@
<email>tim.olshansky@gmail.com</email>
</developer>
</developers>
<contributors>
<contributor>
<name>Adam Retter</name>
<email>adam.retter@googlemail.com</email>
<organization>Evolved Binary Ltd</organization>
</contributor>
<contributor>
<name>Cesar Aguilar</name>
<email>cesar@fuzzproductions.com</email>
<organization>Fuzz Productions</organization>
</contributor>
<contributor>
<name>Chris Luu</name>
<email>luu@fuzzproductions.com</email>
<organization>Fuzz Productions</organization>
</contributor>
</contributors>
<licenses>
<license>
......@@ -51,19 +68,16 @@
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
......@@ -79,4 +93,17 @@
</dependency>
</dependencies>
</project>
\ No newline at end of file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
......@@ -3,22 +3,30 @@ package org.gitlab.api;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.gitlab.api.http.GitlabHTTPRequestor;
import org.gitlab.api.models.GitlabCommit;
import org.gitlab.api.models.GitlabCommitDiff;
import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabCommit;
import org.gitlab.api.models.GitlabIssue;
import org.gitlab.api.models.GitlabMergeRequest;
import org.gitlab.api.models.GitlabMilestone;
import org.gitlab.api.models.GitlabNamespace;
import org.gitlab.api.models.GitlabNote;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabProjectHook;
import org.gitlab.api.models.GitlabProjectMember;
import org.gitlab.api.models.GitlabSession;
import org.gitlab.api.models.GitlabUser;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.gitlab.api.http.Query;
import org.gitlab.api.models.*;
/**
* Gitlab API Wrapper class
*
......@@ -35,6 +43,13 @@ public class GitlabAPI {
_hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
_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) {
return new GitlabAPI(hostUrl, apiToken);
......@@ -53,10 +68,6 @@ public class GitlabAPI {
return new GitlabHTTPRequestor(this).method("POST");
}
public GitlabHTTPRequestor delete() {
return new GitlabHTTPRequestor(this).method("DELETE");
}
public boolean isIgnoreCertificateErrors() {
return _ignoreCertificateErrors;
}
......@@ -80,84 +91,214 @@ public class GitlabAPI {
return new URL(_hostUrl + tailAPIUrl);
}
private List<GitlabMergeRequest> fetchMergeRequests(String tailUrl) throws IOException {
GitlabMergeRequest[] mergeRequests = retrieve().to(tailUrl, GitlabMergeRequest[].class);
return Arrays.asList(mergeRequests);
public List<GitlabUser> getUsers() throws IOException {
String tailUrl = GitlabUser.URL;
return retrieve().getAll( tailUrl, GitlabUser[].class );
}
// Search users by Email or username
// GET /users?search=:email_or_username
public List<GitlabUser> findUsers(String emailOrUsername) throws IOException {
String tailUrl = GitlabUser.URL + "?search=" + emailOrUsername;
GitlabUser[] users = retrieve().to(tailUrl, GitlabUser[].class);
return Arrays.asList(users);
}
/*
Project APIs
http://api.gitlab.org/projects.html
*/
public GitlabUser getUser(Integer userId) throws IOException {
String tailUrl = GitlabUser.URL + "/" + userId;
return retrieve().to(tailUrl, GitlabUser.class);
}
public GitlabGroup getGroup(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId;
return retrieve().to(tailUrl, GitlabGroup.class);
}
public List<GitlabGroup> getGroups() throws IOException {
String tailUrl = GitlabGroup.URL;
return retrieve().getAll(tailUrl, GitlabGroup[].class);
}
/**
* Gets all members of a Group
*
* @param group The GitLab Group
*
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) throws IOException {
return getGroupMembers(group.getId());
}
/**
* Gets all members of a Group
*
* @param groupId The id of the GitLab Group
*
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabGroupMember.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabGroupMember[].class));
}
/**
* Creates a Group
*
* @param name The name of the group. The
* name will also be used as the path
* of the group.
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name) throws IOException {
return createGroup(name, name);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name, String path) throws IOException {
return createGroup(name, path, null, null);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param ldapCn LDAP Group Name to sync with, null otherwise
* @param ldapAccess Access level for LDAP group members, null otherwise
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAccessLevel ldapAccess) throws IOException {
Query query = new Query()
.append("name", name)
.append("path", path)
.appendIf("ldap_cn", ldapCn)
.appendIf("ldap_access", ldapAccess);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroup.class);
}
// Get single project
// GET /projects/:id
public GitlabProject getProject(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId);
return retrieve().to(tailUrl, GitlabProject.class);
}
// List projects
// GET /projects
public List<GitlabProject> getProjects() throws IOException {
String tailUrl = GitlabProject.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabProject[].class));
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
// List all projects
// GET /projects
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;
}
// Get list of project hooks for a project
// GET /projects/:id/hooks
public List<GitlabProjectHook> getProjectHooks(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabProjectHook.URL;
GitlabProjectHook[] hooks = retrieve().to(tailUrl, GitlabProjectHook[].class);
return Arrays.asList(hooks);
}
// Create a new project hook
// POST /projects/:id/hooks
public GitlabProjectHook createProjectHook(String projectId, String url, boolean pushEvents, boolean issuesEvents, boolean mergeRequestEvents) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabProjectHook.URL;
return dispatch()
.with("url", url)
.with("push_events", pushEvents ? "true" : "false")
.with("issues_events", issuesEvents ? "true" : "false")
.with("merge_requests_events", mergeRequestEvents ? "true" : "false")
.to(tailUrl, GitlabProjectHook.class);
}
// Removes a hook from project
// DELETE /projects/:id/hooks/:hook_id
public void deleteProjectHook(GitlabProjectHook hook) throws IOException {
String tailUrl = GitlabProject.URL + "/" + hook.getProjectId() + GitlabProjectHook.URL + "/" + hook.getId();
delete().to(tailUrl, GitlabProjectHook.class);
String tailUrl = GitlabProject.URL + "/all";
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Creates a private Project
*
* @param name The name of the project
*
* @return The GitLab Project
*/
public GitlabProject createProject(String name) throws IOException {
return createProject(name, null, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a Project
*
* @param name The name of the project
* @param namespaceId The Namespace for the new project, otherwise null indicates to use the GitLab default (user)
* @param description A description for the project, null otherwise
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param publik Whether the project is public or private, if true same as setting visibilityLevel = 20, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
* @param importUrl The Import URL for the project, otherwise null
*
* @return the Gitlab Project
*/
public GitlabProject createProject(String name, Integer namespaceId, String description, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel, String importUrl) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("namespace_id", namespaceId)
.appendIf("description", description)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("public", publik)
.appendIf("visibility_level", visibilityLevel)
.appendIf("import_url", importUrl);
String tailUrl = GitlabProject.URL + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
*
* @return The GitLab Project
*/
public GitlabProject createUserProject(Integer userId, String name) throws IOException {
return createUserProject(userId, name, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
* @param description A description for the project, null otherwise
* @param defaultBranch The default branch for the project, otherwise null indicates to use GitLab default (master)
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param publik Whether the project is public or private, if true same as setting visibilityLevel = 20, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
*
* @return The GitLab Project
*/
public GitlabProject createUserProject(Integer userId, String name, String description, String defaultBranch, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("description", description)
.appendIf("default_branch", defaultBranch)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("public", publik)
.appendIf("visibility_level", visibilityLevel);
String tailUrl = GitlabProject.URL + "/user/" + userId + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
/*
Merge Request APIs
http://api.gitlab.org/merge_requests.html
*/
// List all open merge requests for a project id
// GET /projects/:id/merge_requests
public List<GitlabMergeRequest> getOpenMergeRequests(GitlabProject project) throws IOException {
List<GitlabMergeRequest> allMergeRequests = getAllMergeRequests(project);
List<GitlabMergeRequest> openMergeRequests = new ArrayList<GitlabMergeRequest>();
......@@ -172,52 +313,26 @@ public class GitlabAPI {
return openMergeRequests;
}
// List merge requests for a project id
// GET /projects/:id/merge_requests
public List<GitlabMergeRequest> getMergeRequests(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabMergeRequest.URL;
return fetchMergeRequests(tailUrl);
}
// List merge requests for a project
// GET /projects/:id/merge_requests
public List<GitlabMergeRequest> getMergeRequests(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabMergeRequest.URL;
return fetchMergeRequests(tailUrl);
}
// List all merge requests for a project
// GET /projects/:id/merge_requests
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;
return retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
}
// Get single merge request
// GET /projects/:id/merge_request/:merge_request_id
public GitlabMergeRequest getMergeRequest(GitlabProject project, Integer mergeRequestId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + "/merge_request/" + mergeRequestId;
return retrieve().to(tailUrl, GitlabMergeRequest.class);
}
/*
Notes API
http://api.gitlab.org/notes.html
*/
// List merge request notes
// GET /projects/:id/merge_requests/:merge_request_id/notes
public List<GitlabNote> getNotes(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() +
......@@ -227,41 +342,15 @@ public class GitlabAPI {
return Arrays.asList(notes);
}
// List all merge request notes
// GET /projects/:id/merge_requests/:merge_request_id/notes
public List<GitlabNote> getAllNotes(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() +
GitlabNote.URL;
return retrieve().getAll(tailUrl, GitlabNote[].class);
List<GitlabNote> results = new ArrayList<GitlabNote>();
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;
}
// Create new merge request note
// POST /projects/:id/merge_requests/:merge_request_id/notes
public GitlabNote createNote(GitlabMergeRequest mergeRequest, String body) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabNote.URL;
return dispatch().with("body", body).to(tailUrl, GitlabNote.class);
}
/*
Repository APIs
http://api.gitlab.org/repositories.html
*/
// Get a specific commit identified by the commit hash or name of a branch or tag
// GET /projects/:id/repository/commits/:sha
public GitlabCommit getCommit(String projectId, String commitHash) throws IOException {
......@@ -270,11 +359,17 @@ public class GitlabAPI {
return commit;
}
// List repository commits for a merge request
// GET /projects/:id/repository/commits
public List<GitlabCommit> getCommits(GitlabMergeRequest mergeRequest) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
"/repository" + GitlabCommit.URL + "?ref_name=" + mergeRequest.getSourceBranch();
Integer projectId = mergeRequest.getSourceProjectId();
if (projectId == null) {
projectId = mergeRequest.getProjectId();
}
Query query = new Query()
.append("ref_name", mergeRequest.getSourceBranch());
String tailUrl = GitlabProject.URL + "/" + projectId +
"/repository" + GitlabCommit.URL + query.toString();
GitlabCommit[] commits = retrieve().to(tailUrl, GitlabCommit[].class);
return Arrays.asList(commits);
......@@ -288,64 +383,180 @@ public class GitlabAPI {
return Arrays.asList(diffs);
}
// Get a list of repository branches from a project, sorted by name alphabetically.
// GET /projects/:id/repository/branches
public List<GitlabBranch> getBranches(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + "/repository" + GitlabBranch.URL;
public GitlabNote createNote(GitlabMergeRequest mergeRequest, String body) throws IOException {
String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() +
GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabNote.URL;
return dispatch().with("body", body).to(tailUrl, GitlabNote.class);
}
public List<GitlabBranch> getBranches(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL;
GitlabBranch[] branches = retrieve().to(tailUrl, GitlabBranch[].class);
return Arrays.asList(branches);
}
/*
User APIs
http://api.gitlab.org/users.html
*/
// List of users
// GET /users
public List<GitlabUser> getUsers() throws IOException {
String tailUrl = GitlabUser.URL;
GitlabUser[] users = retrieve().to(tailUrl, GitlabUser[].class);
return Arrays.asList(users);
public GitlabBranch getBranch(GitlabProject project, String branchName) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL + branchName;
GitlabBranch branch = retrieve().to(tailUrl, GitlabBranch.class);
return branch;
}
public void protectBranch(GitlabProject project, String branchName) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL + branchName + "/protect";
retrieve().method("PUT").to(tailUrl, Void.class);
}
public void unprotectBranch(GitlabProject project, String branchName) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL + branchName + "/unprotect";
retrieve().method("PUT").to(tailUrl, Void.class);
}
public List<GitlabProjectHook> getProjectHooks(GitlabProject project) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL;
GitlabProjectHook[] hooks = retrieve().to(tailUrl, GitlabProjectHook[].class);
return Arrays.asList(hooks);
}
// Search users by Email or username
// GET /users?search=:email_or_username
public List<GitlabUser> searchUsers(String emailOrUsername) throws IOException {
String tailUrl = GitlabUser.URL + "?search=" + emailOrUsername;
GitlabUser[] users = retrieve().to(tailUrl, GitlabUser[].class);
return Arrays.asList(users);
public GitlabProjectHook getProjectHook(GitlabProject project, String hookId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId;
GitlabProjectHook hook = retrieve().to(tailUrl, GitlabProjectHook.class);
return hook;
}
public GitlabProjectHook addProjectHook(GitlabProject project, String url) throws IOException {
Query query = new Query()
.append("url", url);
// List all users
// GET /users
public List<GitlabUser> getAllUsers() throws IOException {
String tailUrl = GitlabUser.URL;
List<GitlabUser> results = new ArrayList<GitlabUser>();
Iterator<GitlabUser[]> iterator = retrieve().asIterator(tailUrl, GitlabUser[].class);
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + query.toString();
return dispatch().to(tailUrl, GitlabProjectHook.class);
}
public GitlabProjectHook editProjectHook(GitlabProject project, String hookId, String url) throws IOException {
Query query = new Query()
.append("url", url);
while (iterator.hasNext()) {
GitlabUser[] users = iterator.next();
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId + query.toString();
return retrieve().method("PUT").to(tailUrl, GitlabProjectHook.class);
}
public void deleteProjectHook(GitlabProject project, String hookId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId;
retrieve().method("DELETE").to(tailUrl, Void.class);
}
if (users.length > 0) {
results.addAll(Arrays.asList(users));
}
private List<GitlabMergeRequest> fetchMergeRequests(String tailUrl) throws IOException {
GitlabMergeRequest[] mergeRequests = retrieve().to(tailUrl, GitlabMergeRequest[].class);
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(String projectId, Integer issueId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(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);
}
return results;
}
// Gets currently authenticated user
// GET /user
public GitlabUser getLoggedInUser() throws IOException {
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(String projectId, Integer issueId, String message) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(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(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(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(String projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + sanitizeProjectId(projectId) + GitlabProjectMember.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class));
}
/**
* This will fail, if the given namespace is a user and not a group
* @param namespace
* @return
* @throws IOException
*/
public List<GitlabProjectMember> getNamespaceMembers(GitlabNamespace namespace) throws IOException {
return getNamespaceMembers(namespace.getId());
}
/**
* This will fail, if the given namespace is a user and not a group
* @param namespaceId
* @return
* @throws IOException
*/
public List<GitlabProjectMember> getNamespaceMembers(Integer namespaceId) throws IOException {
String tailUrl = GitlabNamespace.URL + "/" + namespaceId + GitlabProjectMember.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class));
}
public GitlabSession getCurrentSession() throws IOException {
String tailUrl = "/user";
GitlabUser user = retrieve().to(tailUrl, GitlabUser.class);
return user;
return retrieve().to(tailUrl, GitlabSession.class);
}
private String sanitizeProjectId(String projectId) {
return projectId.replace("/","%2F");
}
}
}
\ No newline at end of file
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.IOException;
import java.io.InputStream;
......@@ -14,10 +9,23 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
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.Pattern;
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.gitlab.api.GitlabAPI;
......@@ -113,6 +121,10 @@ public class GitlabHTTPRequestor {
if (hasOutput()) {
submitData(connection);
} else if( "PUT".equals(_method) ) {
// PUT requires Content-Length: 0 even when there is no body (eg: API for protecting a branch)
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(0);
}
try {
......@@ -124,6 +136,19 @@ public class GitlabHTTPRequestor {
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) {
method("GET"); // Ensure we only use iterators for GET requests
......@@ -145,7 +170,6 @@ public class GitlabHTTPRequestor {
}
}
@Override
public boolean hasNext() {
fetch();
if (_next.getClass().isArray()) {
......@@ -156,7 +180,6 @@ public class GitlabHTTPRequestor {
}
}
@Override
public T next() {
fetch();
T record = _next;
......@@ -268,7 +291,7 @@ public class GitlabHTTPRequestor {
return null;
}
} catch (SSLHandshakeException e) {
throw new SSLHandshakeException("You can disable certificate checking by setting ignoreCertificateErrors on GitlabHTTPRequestor");
throw new SSLHandshakeException("You can disable certificate checking by setting ignoreCertificateErrors on GitlabHTTPRequestor. SSL Error: " + e.getMessage());
} finally {
IOUtils.closeQuietly(reader);
}
......
package org.gitlab.api.http;
import org.gitlab.api.models.GitlabAccessLevel;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* Models the Query
* aspect of a URL
*/
public class Query {
private class Tuple<T1, T2> {
T1 _1;
T2 _2;
public Tuple(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
}
}
/**
* The type of params is:
* Tuple<name, Tuple<value, URLEncoder.encode(value, "UTF-8")>>
*/
private final List<Tuple<String, Tuple<String, String>>> params = new ArrayList<Tuple<String, Tuple<String, String>>>();
/**
* Appends a parameter to the query
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query append(final String name, final String value) throws UnsupportedEncodingException {
params.add(new Tuple(name, new Tuple(value, URLEncoder.encode(value, "UTF-8"))));
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final String value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value);
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Integer value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value.toString());
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Boolean value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value.toString());
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final GitlabAccessLevel value) throws UnsupportedEncodingException {
if(value != null) {
append(name, Integer.toString(value.accessValue));
}
return this;
}
/**
* Returns a Query suitable for appending
* to a URI
*/
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for(final Tuple<String, Tuple<String, String>> param : params) {
if(builder.length() == 0) {
builder.append('?');
} else {
builder.append('&');
}
builder.append(param._1);
builder.append('=');
builder.append(param._2._2);
}
return builder.toString();
}
}
package org.gitlab.api.models;
import org.codehaus.jackson.annotate.JsonProperty;
public abstract class GitlabAbstractMember extends GitlabUser {
public static final String URL = "/members";
@JsonProperty("access_level")
private int _accessLevel;
public GitlabAccessLevel getAccessLevel() {
return GitlabAccessLevel.fromAccessValue(_accessLevel);
}
public void setAccessLevel(GitlabAccessLevel accessLevel) {
_accessLevel = accessLevel.accessValue;
}
}
package org.gitlab.api.models;
public enum GitlabAccessLevel {
Guest(10),
Reporter(20),
Developer(30),
Master(40),
Owner(50);
public final int accessValue;
GitlabAccessLevel(int accessValue) {
this.accessValue = accessValue;
}
public static GitlabAccessLevel fromAccessValue(final int accessValue) throws IllegalArgumentException {
for(final GitlabAccessLevel gitlabAccessLevel : GitlabAccessLevel.values()) {
if(gitlabAccessLevel.accessValue == accessValue) {
return gitlabAccessLevel;
}
}
throw new IllegalArgumentException("No GitLab Access Level enum constant with access value: " + accessValue);
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import org.gitlab.api.models.GitlabBranchCommit;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabBranch {
public static String URL = "/branches";
public static String URL = "/repository/branches/";
private String _name;
private GitlabBranchCommit _commit;
......@@ -34,4 +34,4 @@ public class GitlabBranch {
public void setProtected(boolean isProtected) {
_protected = isProtected;
}
}
}
\ No newline at end of file
package org.gitlab.api.models;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabGroup {
public static final String URL = "/groups";
private Integer _id;
private String _name;
private String _path;
@JsonProperty("ldap_cn")
private String _ldapCn;
@JsonProperty("ldap_access")
private Integer _ldapAccess;
public Integer getId() {
return _id;
}
public void setId(Integer id) {
_id = id;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public String getPath() {
return _path;
}
public void setPath(String path) {
_path = path;
}
public String getLdapCn() {
return _ldapCn;
}
public void setLdapCn(String ldapCn) {
this._ldapCn = ldapCn;
}
public GitlabAccessLevel getLdapAccess() {
return GitlabAccessLevel.fromAccessValue(_ldapAccess);
}
public void setLdapAccess(GitlabAccessLevel ldapGitlabAccessLevel) {
this._ldapAccess = ldapGitlabAccessLevel.accessValue;
}
}
package org.gitlab.api.models;
public class GitlabGroupMember extends GitlabAbstractMember {
}
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;
}
}
......@@ -9,6 +9,7 @@ public class GitlabMergeRequest {
private Integer _iid;
private String _title;
private String _state;
private String _description;
private boolean _closed;
private boolean _merged;
private GitlabUser _author;
......@@ -26,6 +27,10 @@ public class GitlabMergeRequest {
@JsonProperty("source_project_id")
private Integer _sourceProjectId;
@JsonProperty("milestone_id")
private Integer _milestone_id;
public Integer getId() {
return _id;
}
......@@ -34,6 +39,9 @@ public class GitlabMergeRequest {
_id = id;
}
public Integer getMilestoneId(){ return _milestone_id; }
public void setMilestoneId(Integer id) { _milestone_id = id; }
public Integer getIid() {
return _iid;
}
......@@ -82,6 +90,10 @@ public class GitlabMergeRequest {
_title = title;
}
public String getDescription() { return _description; }
public void setDescription(String d) { _description = d; }
public boolean isClosed() {
return _closed;
}
......
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;
}
}
......@@ -4,7 +4,8 @@ import java.util.Date;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabNamespace {
public static final String URL = "/groups";
private Integer _id;
private String _name;
private String _path;
......
......@@ -9,6 +9,10 @@ public class GitlabProject {
private Integer _id;
private String _name;
@JsonProperty("name_with_namespace")
private String _nameWithNamespace;
private String _description;
@JsonProperty("default_branch")
......@@ -18,6 +22,9 @@ public class GitlabProject {
private boolean _public;
private String _path;
@JsonProperty("visibility_level")
private Integer _visibilityLevel;
@JsonProperty("path_with_namespace")
private String _pathWithNamespace;
......@@ -27,6 +34,9 @@ public class GitlabProject {
@JsonProperty("merge_requests_enabled")
private boolean _mergeRequestsEnabled;
@JsonProperty("snippets_enabled")
private boolean _snippetsEnabled;
@JsonProperty("wall_enabled")
private boolean _wallEnabled;
......@@ -42,6 +52,9 @@ public class GitlabProject {
@JsonProperty("web_url")
private String _webUrl;
@JsonProperty("http_url_to_repo")
private String _httpUrl;
private GitlabNamespace _namespace;
public Integer getId() {
......@@ -60,6 +73,14 @@ public class GitlabProject {
_name = name;
}
public String getNameWithNamespace() {
return _nameWithNamespace;
}
public void setNameWithNamespace(String nameWithNamespace) {
this._nameWithNamespace = nameWithNamespace;
}
public String getDescription() {
return _description;
}
......@@ -76,6 +97,14 @@ public class GitlabProject {
_defaultBranch = defaultBranch;
}
public Integer getVisibilityLevel() {
return _visibilityLevel;
}
public void setVisibilityLevel(Integer visibilityLevel) {
this._visibilityLevel = visibilityLevel;
}
public GitlabUser getOwner() {
return _owner;
}
......@@ -116,6 +145,14 @@ public class GitlabProject {
_mergeRequestsEnabled = mergeRequestsEnabled;
}
public boolean isSnippetsEnabled() {
return _snippetsEnabled;
}
public void setSnippetsEnabled(boolean snippetsEnabled) {
this._snippetsEnabled = snippetsEnabled;
}
public boolean isWallEnabled() {
return _wallEnabled;
}
......@@ -156,6 +193,14 @@ public class GitlabProject {
_webUrl = webUrl;
}
public String getHttpUrl() {
return _httpUrl;
}
public void setHttpUrl(String httpUrl) {
_httpUrl = httpUrl;
}
public GitlabNamespace getNamespace() {
return _namespace;
}
......
......@@ -80,4 +80,4 @@ public class GitlabProjectHook {
public void setMergeRequestsEvents(boolean mergeRequestsEvents) {
_mergeRequestsEvents = mergeRequestsEvents;
}
}
}
\ No newline at end of file
package org.gitlab.api.models;
public class GitlabProjectMember extends GitlabAbstractMember {
}
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;
}
}
package org.gitlab.api.http;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
public class QueryTest {
@Test
public void mutableStyle_append() throws UnsupportedEncodingException {
Query query = new Query();
query.append("p1", "v1");
query.append("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void fluentStyle_append() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v1")
.append("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void mixedStyle_append() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v1");
query.append("p2", "v2");
query = query.append("p3", "v3");
assertEquals("?p1=v1&p2=v2&p3=v3", query.toString());
}
@Test
public void conditionalAppend_notNull() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", "v1")
.appendIf("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void conditionalAppend_null() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", (String) null);
assertEquals("", query.toString());
}
@Test
public void conditionalAppend_null_notNull() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", (String)null)
.appendIf("p2", "v2");
assertEquals("?p2=v2", query.toString());
}
@Test
public void append_encodes_values() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v 1")
.append("p2", "v 2");
assertEquals("?p1=v+1&p2=v+2", query.toString());
}
}
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