diff --git a/src/main/java/com/messners/gitlab/api/GitLabApi.java b/src/main/java/com/messners/gitlab/api/GitLabApi.java index 1aedf0ff85b5031547303cbcdfffd33c9a1cee83..cefdd9ecd72871dd224dc15ad4e9628f3513c096 100644 --- a/src/main/java/com/messners/gitlab/api/GitLabApi.java +++ b/src/main/java/com/messners/gitlab/api/GitLabApi.java @@ -17,6 +17,7 @@ public class GitLabApi { private RepositoryApi repositoryApi; private SessionApi sessoinApi; private UserApi userApi; + private RepositoryFileApi repositoryFileApi; /** * Logs into GitLab using provided {@code username} and {@code password}, and creates a new @@ -48,6 +49,7 @@ public class GitLabApi { repositoryApi = new RepositoryApi(this); sessoinApi = new SessionApi(this); userApi = new UserApi(this); + repositoryFileApi = new RepositoryFileApi(this); } @@ -137,4 +139,26 @@ public class GitLabApi { public UserApi getUserApi () { return (userApi); } + + + /** + * Gets the RepositoryFileApi instance owned by this GitLabApi instance. The RepositoryFileApi is used + * to perform all repository files related API calls. + * + * @return the RepositoryFileApi instance owned by this GitLabApi instance + */ + public RepositoryFileApi getRepositoryFileApi() { + return repositoryFileApi; + } + + /** + * Gets the RepositoryFileApi instance owned by this GitLabApi instance. The RepositoryFileApi is used + * to perform all repository files related API calls. + * + * @return the RepositoryFileApi instance owned by this GitLabApi instance + */ + public void setRepositoryFileApi(RepositoryFileApi repositoryFileApi) { + this.repositoryFileApi = repositoryFileApi; + } + } diff --git a/src/main/java/com/messners/gitlab/api/GitLabApiClient.java b/src/main/java/com/messners/gitlab/api/GitLabApiClient.java index 6fe0b449806057e4c512a5620f783a4aaa909304..16d2dcccb83a63ff85f2e7ba157a5fa7ed6e0aff 100644 --- a/src/main/java/com/messners/gitlab/api/GitLabApiClient.java +++ b/src/main/java/com/messners/gitlab/api/GitLabApiClient.java @@ -250,7 +250,7 @@ public class GitLabApiClient { * @return a ClientResponse instance with the data returned from the endpoint */ protected Response put (MultivaluedMap queryParams, URL url) { - return invocation(url, queryParams).put(null); + return invocation(url, null).put(Entity.entity(queryParams, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); } diff --git a/src/main/java/com/messners/gitlab/api/ProjectApi.java b/src/main/java/com/messners/gitlab/api/ProjectApi.java index e62e9b6a37292f12b3071976eec0b78b32772f25..df919e3b0206c221d91f3fad5e084cb7d74006bb 100644 --- a/src/main/java/com/messners/gitlab/api/ProjectApi.java +++ b/src/main/java/com/messners/gitlab/api/ProjectApi.java @@ -181,6 +181,45 @@ public class ProjectApi extends AbstractApi { return (response.readEntity(Project.class)); } + /** + * 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 + * @throws GitLabApiException + */ + public Project createProject(String name, Integer namespaceId, String description, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel, String importUrl) throws GitLabApiException{ + + if (name == null || name.trim().length() == 0) { + return (null); + } + + Form formData = new Form(); + addFormParam(formData, "name", name, true); + addFormParam(formData, "namespace_id", namespaceId); + addFormParam(formData, "description", description); + addFormParam(formData, "issues_enabled", issuesEnabled); + addFormParam(formData, "wall_enabled", wallEnabled); + addFormParam(formData, "merge_requests_enabled", mergeRequestsEnabled); + addFormParam(formData, "wiki_enabled", wikiEnabled); + addFormParam(formData, "snippets_enabled", snippetsEnabled); + addFormParam(formData, "public", publik); + addFormParam(formData, "visibility_level", visibilityLevel); + addFormParam(formData, "import_url", importUrl); + + Response response = post(Response.Status.CREATED, formData, "projects"); + return (response.readEntity(Project.class)); + } /** * Removes project with all resources(issues, merge requests etc). diff --git a/src/main/java/com/messners/gitlab/api/RepositoryApi.java b/src/main/java/com/messners/gitlab/api/RepositoryApi.java index 8e656d4f4d765fbbe75b4439ebf85d1c5bed2715..b0cd4de07aafacb1d31c5574c90b29ffaba8a443 100644 --- a/src/main/java/com/messners/gitlab/api/RepositoryApi.java +++ b/src/main/java/com/messners/gitlab/api/RepositoryApi.java @@ -128,7 +128,7 @@ public class RepositoryApi extends AbstractApi { * GET /projects/:id/repository/tree * * @param projectId - * @return a tree with the diurectories and files of a project + * @return a tree with the directories and files of a project * @throws GitLabApiException */ public List getTree (Integer projectId) throws GitLabApiException { @@ -154,4 +154,20 @@ public class RepositoryApi extends AbstractApi { Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "blobs", commitOrBranchName); return (response.readEntity(String.class)); } + + /** + * Get the raw file contents for a blob by blob SHA. + * + * GET /projects/:id/repository/raw_blobs/:sha + * + * @param projectId + * @param sha + * @return the raw file contents for the blob + * @throws GitLabApiException + */ + public String getRawBlobCotent(Integer projectId, String sha) throws GitLabApiException{ + Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "raw_blobs", sha); + return (response.readEntity(String.class)); + } + } diff --git a/src/main/java/com/messners/gitlab/api/RepositoryFileApi.java b/src/main/java/com/messners/gitlab/api/RepositoryFileApi.java new file mode 100644 index 0000000000000000000000000000000000000000..8ca731c353a9a121e480ce6097a6f228397a1fe7 --- /dev/null +++ b/src/main/java/com/messners/gitlab/api/RepositoryFileApi.java @@ -0,0 +1,127 @@ +package com.messners.gitlab.api; + +import javax.ws.rs.core.Form; +import javax.ws.rs.core.Response; + +import com.messners.gitlab.api.models.RepositoryFile; + + +/** + * This class provides an entry point to all the GitLab API repository files calls. + * + * @author lonfee88 + */ +public class RepositoryFileApi extends AbstractApi { + + public RepositoryFileApi (GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Get file from repository + * Allows you to receive information about file in repository like name, size, content. + * Note that file content is Base64 encoded. + * + * GET /projects/:id/repository/files + * + * @param file_path (required) - Full path to new file. Ex. lib/class.rb + * @param projectId + * @param ref (required) - The name of branch, tag or commit + * @return + * @throws GitLabApiException + */ + public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException { + Form form = new Form(); + addFormParam(form, "file_path", filePath, true); + addFormParam(form, "ref", ref, true); + Response response = get(Response.Status.OK, form.asMap(),"projects", projectId, "repository", "files"); + return (response.readEntity(RepositoryFile.class)); + } + + /** + * Create new file in repository + * + * POST /projects/:id/repository/files + * + * file_path (required) - Full path to new file. Ex. lib/class.rb + * branch_name (required) - The name of branch + * encoding (optional) - 'text' or 'base64'. Text is default. + * content (required) - File content + * commit_message (required) - Commit message + * + * @param file + * @param projectId + * @param branchName + * @param commitMessage + * @return + * @throws GitLabApiException + */ + public RepositoryFile createFile (RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + Form formData = file2form(file, branchName, commitMessage); + Response response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files"); + return (response.readEntity(RepositoryFile.class)); + } + + /** + * Update existing file in repository + * + * PUT /projects/:id/repository/files + * + * file_path (required) - Full path to new file. Ex. lib/class.rb + * branch_name (required) - The name of branch + * encoding (optional) - 'text' or 'base64'. Text is default. + * content (required) - File content + * commit_message (required) - Commit message + * + * @param file + * @param projectId + * @param branchName + * @param commitMessage + * @return + * @throws GitLabApiException + */ + public RepositoryFile updateFile (RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + Form form = file2form(file, branchName, commitMessage); + Response response = put(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files"); + return (response.readEntity(RepositoryFile.class)); + } + + /** + * Delete existing file in repository + * + * DELETE /projects/:id/repository/files + * + * file_path (required) - Full path to file. Ex. lib/class.rb + * branch_name (required) - The name of branch + * commit_message (required) - Commit message + * + * @param filePath + * @param projectId + * @param branchName + * @param commitMessage + * @throws GitLabApiException + */ + public void deleteFile (String filePath, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + + if (filePath == null) { + throw new RuntimeException("filePath cannot be null"); + } + + Form form = new Form(); + addFormParam(form, "file_path", filePath, true); + addFormParam(form, "branch_name", branchName, true); + addFormParam(form, "commit_message", commitMessage, true); + delete(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files"); + } + + private Form file2form(RepositoryFile file, String branchName, String commitMessage){ + Form form = new Form(); + addFormParam(form, "file_path", file.getFilePath(), true); + addFormParam(form, "branch_name", branchName, true); + addFormParam(form, "encoding", file.getEncoding(), false); + addFormParam(form, "content", file.getContent(), true); + addFormParam(form, "commit_message", commitMessage, true); + return form; + } + +} diff --git a/src/main/java/com/messners/gitlab/api/UserApi.java b/src/main/java/com/messners/gitlab/api/UserApi.java index 0d9f3edca1dcff602f78b23261d24428a8296822..3dc959d29205a3119bc396c7f6f4bb3fb9ae48c2 100644 --- a/src/main/java/com/messners/gitlab/api/UserApi.java +++ b/src/main/java/com/messners/gitlab/api/UserApi.java @@ -1,11 +1,12 @@ package com.messners.gitlab.api; -import com.messners.gitlab.api.models.User; +import java.util.List; import javax.ws.rs.core.Form; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; -import java.util.List; + +import com.messners.gitlab.api.models.User; public class UserApi extends AbstractApi { @@ -62,6 +63,24 @@ public class UserApi extends AbstractApi { return (response.readEntity(User.class)); } + // Search users by Email or username + // GET /users?search=:email_or_username + + /** + * Search users by Email or username + * + * GET /users?search=:email_or_username + * + * @param emailOrUsername + * @return the User List with the email or username like emailOrUsername + * @throws GitLabApiException + */ + public List findUsers(String emailOrUsername) throws GitLabApiException { + Form formData = new Form(); + addFormParam(formData, "search", emailOrUsername, true); + Response response = get(Response.Status.OK, formData.asMap(), "users"); + return (response.readEntity(new GenericType>() {})); + } /** * Creates a new user. Note only administrators can create new users. diff --git a/src/main/java/com/messners/gitlab/api/models/RepositoryFile.java b/src/main/java/com/messners/gitlab/api/models/RepositoryFile.java new file mode 100644 index 0000000000000000000000000000000000000000..dbc45f0d81a66c25b6b009880ea22a01f144ffb1 --- /dev/null +++ b/src/main/java/com/messners/gitlab/api/models/RepositoryFile.java @@ -0,0 +1,85 @@ + +package com.messners.gitlab.api.models; + + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class RepositoryFile { + + private String fileName; //file name only, Ex. lib/class.rb + private String filePath; //full path to file. Ex. lib/class.rb + private Integer size; + private String encoding; + private String content; + private String ref; + private String blobId; + private String commitId; + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + public Integer getSize() { + return size; + } + + public void setSize(Integer size) { + this.size = size; + } + + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + public String getBlobId() { + return blobId; + } + + public void setBlobId(String blobId) { + this.blobId = blobId; + } + + public String getCommitId() { + return commitId; + } + + public void setCommitId(String commitId) { + this.commitId = commitId; + } +}