From 3fd50335b5af61a3f1e077fe6262c473797335d6 Mon Sep 17 00:00:00 2001 From: zacker Date: Wed, 16 Apr 2014 17:49:02 +0800 Subject: [PATCH 1/3] add getUser(username) and getProject(projectName) method --- src/main/java/org/gitlab/api/GitlabAPI.java | 214 +++++++++--------- .../gitlab/api/http/GitlabHTTPRequestor.java | 59 ++--- .../java/org/gitlab/api/GitlabAPITest.java | 13 +- 3 files changed, 152 insertions(+), 134 deletions(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index 958329cc..956491fa 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -10,17 +10,7 @@ import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.gitlab.api.http.GitlabHTTPRequestor; -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.*; /** * Gitlab API Wrapper class @@ -38,12 +28,12 @@ private GitlabAPI(String hostUrl, String apiToken) { _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); + 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) { @@ -91,6 +81,18 @@ public GitlabProject getProject(Integer projectId) throws IOException { return retrieve().to(tailUrl, GitlabProject.class); } + public GitlabProject getProject(String NAMESPACE_PROJECT_NAME) throws IOException { + String tailUrl = GitlabProject.URL + "/" + NAMESPACE_PROJECT_NAME; + return retrieve().to(tailUrl, GitlabProject.class); + } + + public GitlabUser getUser(String username) throws IOException { + String tailUrl = GitlabUser.URL + "?search=" + username; + GitlabUser[] arrays = retrieve().to(tailUrl, GitlabUser[].class); + if (null == arrays || arrays.length == 0) return null; + return arrays[0]; + } + public List getProjects() throws IOException { String tailUrl = GitlabProject.URL; return retrieve().getAll(tailUrl, GitlabProject[].class); @@ -148,7 +150,7 @@ public List getAllNotes(GitlabMergeRequest mergeRequest) throws IOEx String tailUrl = GitlabProject.URL + "/" + mergeRequest.getProjectId() + GitlabMergeRequest.URL + "/" + mergeRequest.getId() + GitlabNote.URL; - + return retrieve().getAll(tailUrl, GitlabNote[].class); } @@ -171,164 +173,166 @@ public GitlabNote createNote(GitlabMergeRequest mergeRequest, String body) throw return dispatch().with("body", body).to(tailUrl, GitlabNote.class); } - + public List getBranches(GitlabProject project) throws IOException { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL; + String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL; GitlabBranch[] branches = retrieve().to(tailUrl, GitlabBranch[].class); return Arrays.asList(branches); } - + public GitlabBranch getBranch(GitlabProject project, String branchName) throws IOException { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabBranch.URL + branchName; + 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 getProjectHooks(GitlabProject project) throws IOException { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL; - GitlabProjectHook[] hooks = retrieve().to(tailUrl, GitlabProjectHook[].class); + String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL; + GitlabProjectHook[] hooks = retrieve().to(tailUrl, GitlabProjectHook[].class); return Arrays.asList(hooks); } - + 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); + 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 { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "?url=" + URLEncoder.encode(url, "UTF-8"); - return dispatch().to(tailUrl, GitlabProjectHook.class); + String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "?url=" + URLEncoder.encode(url, "UTF-8"); + return dispatch().to(tailUrl, GitlabProjectHook.class); } - + public GitlabProjectHook editProjectHook(GitlabProject project, String hookId, String url) throws IOException { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId + "?url=" + URLEncoder.encode(url, "UTF-8"); - return retrieve().method("PUT").to(tailUrl, GitlabProjectHook.class); + String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId + "?url=" + URLEncoder.encode(url, "UTF-8"); + 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); + String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId; + retrieve().method("DELETE").to(tailUrl, Void.class); } private List fetchMergeRequests(String tailUrl) throws IOException { GitlabMergeRequest[] mergeRequests = retrieve().to(tailUrl, GitlabMergeRequest[].class); return Arrays.asList(mergeRequests); } - + public List getIssues(GitlabProject project) throws IOException { - String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabIssue.URL; - return retrieve().getAll(tailUrl, GitlabIssue[].class); + 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); + 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 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); - } - } - + 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 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)); + 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); + 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); + return createNote(issue.getProjectId(), issue.getId(), message); } - + public List getMilestones(GitlabProject project) throws IOException { - return getMilestones(project.getId()); + return getMilestones(project.getId()); } - + public List getMilestones(Integer projectId) throws IOException { - String tailUrl = GitlabProject.URL + "/" + projectId + GitlabMilestone.URL; - return Arrays.asList(retrieve().to(tailUrl, GitlabMilestone[].class)); + String tailUrl = GitlabProject.URL + "/" + projectId + GitlabMilestone.URL; + return Arrays.asList(retrieve().to(tailUrl, GitlabMilestone[].class)); } - + public List getProjectMembers(GitlabProject project) throws IOException { - return getProjectMembers(project.getId()); + return getProjectMembers(project.getId()); } - + public List getProjectMembers(Integer projectId) throws IOException { - String tailUrl = GitlabProject.URL + "/" + projectId + GitlabProjectMember.URL; - return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class)); + String tailUrl = GitlabProject.URL + "/" + 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 getNamespaceMembers(GitlabNamespace namespace) throws IOException { - return getNamespaceMembers(namespace.getId()); + 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 getNamespaceMembers(Integer namespaceId) throws IOException { - String tailUrl = GitlabNamespace.URL + "/" + namespaceId + GitlabProjectMember.URL; - return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class)); + String tailUrl = GitlabNamespace.URL + "/" + namespaceId + GitlabProjectMember.URL; + return Arrays.asList(retrieve().to(tailUrl, GitlabProjectMember[].class)); } - + public GitlabSession getCurrentSession() throws IOException { - String tailUrl = "/user"; - return retrieve().to(tailUrl, GitlabSession.class); + String tailUrl = "/user"; + return retrieve().to(tailUrl, GitlabSession.class); } } diff --git a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java index 084fc654..1222c4c2 100644 --- a/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java +++ b/src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java @@ -31,7 +31,7 @@ /** * Gitlab HTTP Requestor - * + *

* Responsible for handling HTTP requests to the Gitlab API * * @author @timols @@ -66,11 +66,11 @@ public GitlabHTTPRequestor(GitlabAPI root) { /** * Sets the HTTP Request method for the request. - * + *

* Has a fluent api for method chaining. * - * @param method The HTTP method - * @return this + * @param method The HTTP method + * @return this */ public GitlabHTTPRequestor method(String method) { try { @@ -84,12 +84,12 @@ public GitlabHTTPRequestor method(String method) { /** * Sets the HTTP Form Post parameters for the request - * + *

* Has a fluent api for method chaining * - * @param key - * @param value - * @return this + * @param key + * @param value + * @return this */ public GitlabHTTPRequestor with(String key, Object value) { if (value != null && key != null) { @@ -109,22 +109,23 @@ public T to(String tailAPIUrl, Class type) throws IOException { /** * Opens the HTTP(S) connection, submits any data and parses the response. * Will throw an error - * @param tailAPIUrl The url to open a connection to (after the host and namespace) - * @param type The type of the response to be deserialized from - * @param instance The instance to update from the response * - * @return An object of type T + * @param tailAPIUrl The url to open a connection to (after the host and namespace) + * @param type The type of the response to be deserialized from + * @param instance The instance to update from the response + * @return An object of type T * @throws java.io.IOException */ public T to(String tailAPIUrl, Class type, T instance) throws IOException { + System.out.println(_root.getAPIUrl(tailAPIUrl)); HttpURLConnection connection = setupConnection(_root.getAPIUrl(tailAPIUrl)); 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); + } 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 { @@ -137,8 +138,8 @@ public T to(String tailAPIUrl, Class type, T instance) throws IOException } public List getAll(final String tailUrl, final Class type) { - List results = new ArrayList(); - Iterator iterator = asIterator(tailUrl, type); + List results = new ArrayList(); + Iterator iterator = asIterator(tailUrl, type); while (iterator.hasNext()) { T[] requests = iterator.next(); @@ -328,17 +329,19 @@ private void handleAPIError(IOException e, HttpURLConnection connection) throws private void ignoreCertificateErrors() { TrustManager[] trustAllCerts = new TrustManager[]{ - new X509TrustManager() { - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } - public void checkClientTrusted( - java.security.cert.X509Certificate[] certs, String authType) { - } - public void checkServerTrusted( - java.security.cert.X509Certificate[] certs, String authType) { + new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + } } - } }; try { diff --git a/src/test/java/org/gitlab/api/GitlabAPITest.java b/src/test/java/org/gitlab/api/GitlabAPITest.java index 6753a906..777aa1b9 100644 --- a/src/test/java/org/gitlab/api/GitlabAPITest.java +++ b/src/test/java/org/gitlab/api/GitlabAPITest.java @@ -7,6 +7,7 @@ import java.net.URL; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; public class GitlabAPITest { @@ -14,7 +15,17 @@ public class GitlabAPITest { @Before public void setup() { - _api = GitlabAPI.connect("http://localhost", "token"); + _api = GitlabAPI.connect("http://192.168.1.104:9002/", "token"); + } + + @Test + public void testName() throws Exception { + System.out.println(_api.getUser("00sj6za4pq")); + } + + @Test + public void testGetProject() throws IOException { + assertNotNull(_api.getProject("root%2F00ic94qju1")); } @Test From 6dd870acd1b379a1a8e0ce5ba6a7ca80d1492d41 Mon Sep 17 00:00:00 2001 From: zacker Date: Thu, 17 Apr 2014 11:10:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?add=20some=20methods=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/gitlab/api/GitlabAPI.java | 60 ++++++++++++++++++- .../java/org/gitlab/api/GitlabAPITest.java | 31 +++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index 956491fa..5c109b76 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -1,6 +1,7 @@ package org.gitlab.api; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; @@ -49,6 +50,10 @@ public GitlabHTTPRequestor retrieve() { return new GitlabHTTPRequestor(this); } + public GitlabHTTPRequestor delete() { + return new GitlabHTTPRequestor(this).method("DELETE"); + } + public GitlabHTTPRequestor dispatch() { return new GitlabHTTPRequestor(this).method("POST"); } @@ -81,9 +86,21 @@ public GitlabProject getProject(Integer projectId) throws IOException { return retrieve().to(tailUrl, GitlabProject.class); } - public GitlabProject getProject(String NAMESPACE_PROJECT_NAME) throws IOException { - String tailUrl = GitlabProject.URL + "/" + NAMESPACE_PROJECT_NAME; - return retrieve().to(tailUrl, GitlabProject.class); + public GitlabProject getProject(String namespace_project_name) throws IOException { + String tailUrl = GitlabProject.URL + "/" + urlEncode(namespace_project_name); + try { + return retrieve().to(tailUrl, GitlabProject.class); + } catch (java.io.FileNotFoundException e) { + return null; + } + } + + private String urlEncode(String str) { + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } public GitlabUser getUser(String username) throws IOException { @@ -93,6 +110,34 @@ public GitlabUser getUser(String username) throws IOException { return arrays[0]; } + public GitlabProject createProject(GitlabProject project) throws IOException { + return dispatch().with("name", project.getName()) + .with("namespace_id", project.getNamespace()) + .with("description", project.getDescription()) + .with("issues_enabled", project.isIssuesEnabled()) + .with("wall_enabled", project.isWallEnabled()) + .with("merge_requests_enabled", project.isMergeRequestsEnabled()) + .with("wiki_enabled", project.isWikiEnabled()) + .with("public", project.isPublic()) + .to(GitlabProject.URL, GitlabProject.class); + } + + public GitlabUser createUser(GitlabUser user, String password) throws IOException { + + return dispatch().with("email", user.getEmail()) + .with("username", user.getUsername()) + .with("name", user.getName()) + .with("password", password) + .with("skype", user.getSkype()) + .with("linkedin", user.getLinkedin()) + .with("twitter", user.getTwitter()) + .with("bio", user.getBio()) + .with("admin", user.isAdmin()) + .with("can_create_group", user.isCanCreateGroup()) + .to(GitlabUser.URL, GitlabUser.class); + + } + public List getProjects() throws IOException { String tailUrl = GitlabProject.URL; return retrieve().getAll(tailUrl, GitlabProject[].class); @@ -103,6 +148,15 @@ public List getAllProjects() throws IOException { return retrieve().getAll(tailUrl, GitlabProject[].class); } + public void deleteProject(String namespace_project_name) throws IOException { + String tailUrl = GitlabProject.URL + "/" + urlEncode(namespace_project_name); + try { + delete().to(tailUrl, GitlabProject.class); + } catch (java.io.FileNotFoundException e) { + return; + } + } + public List getOpenMergeRequests(GitlabProject project) throws IOException { List allMergeRequests = getAllMergeRequests(project); List openMergeRequests = new ArrayList(); diff --git a/src/test/java/org/gitlab/api/GitlabAPITest.java b/src/test/java/org/gitlab/api/GitlabAPITest.java index 777aa1b9..e65beaa3 100644 --- a/src/test/java/org/gitlab/api/GitlabAPITest.java +++ b/src/test/java/org/gitlab/api/GitlabAPITest.java @@ -1,6 +1,9 @@ package org.gitlab.api; +import org.gitlab.api.models.GitlabProject; +import org.gitlab.api.models.GitlabUser; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -9,13 +12,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +@Ignore public class GitlabAPITest { GitlabAPI _api; @Before public void setup() { - _api = GitlabAPI.connect("http://192.168.1.104:9002/", "token"); + _api = GitlabAPI.connect("http://192.168.1.104:9002/", "cULf7pfeELg4uMPRNqFV"); } @Test @@ -23,9 +27,32 @@ public void testName() throws Exception { System.out.println(_api.getUser("00sj6za4pq")); } + @Test + public void testDeleteProject() throws Exception { + _api.deleteProject("root/aobl0oxwqj"); + } + + @Test + public void testCreateProject() throws Exception { + GitlabProject project = new GitlabProject(); + project.setName("okokokokoko"); + assertNotNull(_api.createProject(project).getId()); + } + + @Test + public void testCreateUser() throws Exception { + GitlabUser user = new GitlabUser(); + user.setName("okokokok"); + user.setEmail("okokok@173.com"); + user.setUsername("okokokokok"); + assertNotNull(_api.createUser(user, "password").getId()); + + + } + @Test public void testGetProject() throws IOException { - assertNotNull(_api.getProject("root%2F00ic94qju1")); + assertNotNull(_api.getProject("root/aobl0oxwqj")); } @Test From ded8d76486236a46af5972650c62ec324d9aa18e Mon Sep 17 00:00:00 2001 From: zacker Date: Thu, 17 Apr 2014 11:33:24 +0800 Subject: [PATCH 3/3] deleteUser --- src/main/java/org/gitlab/api/GitlabAPI.java | 10 +++++++++- src/test/java/org/gitlab/api/GitlabAPITest.java | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab/api/GitlabAPI.java b/src/main/java/org/gitlab/api/GitlabAPI.java index 5c109b76..c8d53874 100644 --- a/src/main/java/org/gitlab/api/GitlabAPI.java +++ b/src/main/java/org/gitlab/api/GitlabAPI.java @@ -123,7 +123,6 @@ public GitlabProject createProject(GitlabProject project) throws IOException { } public GitlabUser createUser(GitlabUser user, String password) throws IOException { - return dispatch().with("email", user.getEmail()) .with("username", user.getUsername()) .with("name", user.getName()) @@ -135,7 +134,16 @@ public GitlabUser createUser(GitlabUser user, String password) throws IOExceptio .with("admin", user.isAdmin()) .with("can_create_group", user.isCanCreateGroup()) .to(GitlabUser.URL, GitlabUser.class); + } + public void deleteUser(String userName) throws IOException { + try { + GitlabUser user = getUser(userName); + if (user == null) return; + delete().to(GitlabUser.URL + "/" + user.getId(), GitlabUser.class); + } catch (java.io.FileNotFoundException e) { + return; + } } public List getProjects() throws IOException { diff --git a/src/test/java/org/gitlab/api/GitlabAPITest.java b/src/test/java/org/gitlab/api/GitlabAPITest.java index e65beaa3..59863800 100644 --- a/src/test/java/org/gitlab/api/GitlabAPITest.java +++ b/src/test/java/org/gitlab/api/GitlabAPITest.java @@ -32,6 +32,11 @@ public void testDeleteProject() throws Exception { _api.deleteProject("root/aobl0oxwqj"); } + @Test + public void testDeleteUser() throws Exception { + _api.deleteUser("okokokokok"); + } + @Test public void testCreateProject() throws Exception { GitlabProject project = new GitlabProject();