From fff854e321ea365788fa6cb38489d6b01e888eb8 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Wed, 12 Dec 2018 12:47:59 -0800 Subject: [PATCH] Added support for v3 getRawFile() (#282). --- .../org/gitlab4j/api/RepositoryFileApi.java | 161 +++++++++++++++--- 1 file changed, 133 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/RepositoryFileApi.java b/src/main/java/org/gitlab4j/api/RepositoryFileApi.java index 9e291e33..75228749 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryFileApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryFileApi.java @@ -34,6 +34,7 @@ public class RepositoryFileApi extends AbstractApi { * @param filePath (required) - Full path to the file. Ex. lib/class.rb * @param ref (required) - The name of branch, tag or commit * @return an Optional instance with the specified RepositoryFile as a value + * @since GitLab-11.1.0 */ public Optional getOptionalFileInfo(Object projectIdOrPath, String filePath, String ref) { try { @@ -127,13 +128,14 @@ public class RepositoryFileApi extends AbstractApi { * @throws GitLabApiException if any exception occurs * @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)} */ + @Deprecated public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException { if (isApiVersion(ApiVersion.V3)) { return (getFileV3(filePath, projectId, ref)); + } else { + return (getFile(projectId, filePath, ref, true)); } - - return (getFile(projectId, filePath, ref, true)); } /** @@ -174,6 +176,7 @@ public class RepositoryFileApi extends AbstractApi { * @throws GitLabApiException if any exception occurs * @deprecated Will be removed in version 5.0 */ + @Deprecated protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException { Form form = new Form(); addFormParam(form, "file_path", filePath, true); @@ -193,25 +196,52 @@ public class RepositoryFileApi extends AbstractApi { * content (required) - File content * commit_message (required) - Commit message * + * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path * @param file a ReposityoryFile instance with info for the file to create - * @param projectId the project ID * @param branchName the name of branch * @param commitMessage the commit message * @return a RepositoryFile instance with the created file info * @throws GitLabApiException if any exception occurs */ - public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException { + Form formData = createForm(file, branchName, commitMessage); Response response; if (isApiVersion(ApiVersion.V3)) { - response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files"); + response = post(Response.Status.CREATED, formData, + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files"); } else { - response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files", urlEncode(file.getFilePath())); + response = post(Response.Status.CREATED, formData, + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath())); } 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 a ReposityoryFile instance with info for the file to create + * @param projectId the project ID + * @param branchName the name of branch + * @param commitMessage the commit message + * @return a RepositoryFile instance with the created file info + * @throws GitLabApiException if any exception occurs + * @deprecated Will be removed in version 5.0, replaced by {@link #createFile(Object, RepositoryFile, String, String)} + */ + @Deprecated + public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + return (createFile(projectId, file, branchName, commitMessage)); + } + /** * Update existing file in repository * @@ -223,25 +253,52 @@ public class RepositoryFileApi extends AbstractApi { * content (required) - File content * commit_message (required) - Commit message * + * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path * @param file a ReposityoryFile instance with info for the file to update - * @param projectId the project ID * @param branchName the name of branch * @param commitMessage the commit message * @return a RepositoryFile instance with the updated file info * @throws GitLabApiException if any exception occurs */ - public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException { + Form formData = createForm(file, branchName, commitMessage); Response response; if (isApiVersion(ApiVersion.V3)) { - response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files"); + response = put(Response.Status.OK, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files"); } else { - response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files", urlEncode(file.getFilePath())); + response = put(Response.Status.OK, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath())); } 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 a ReposityoryFile instance with info for the file to update + * @param projectId the project ID + * @param branchName the name of branch + * @param commitMessage the commit message + * @return a RepositoryFile instance with the updated file info + * @throws GitLabApiException if any exception occurs + * @deprecated Will be removed in version 5.0, replaced by {@link #updateFile(Object, RepositoryFile, String, String)} + */ + @Deprecated + public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + return (updateFile(projectId, file, branchName, commitMessage)); + } + /** * Delete existing file in repository * @@ -251,13 +308,13 @@ public class RepositoryFileApi extends AbstractApi { * branch_name (required) - The name of branch * commit_message (required) - Commit message * + * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path * @param filePath full path to new file. Ex. lib/class.rb - * @param projectId the project ID * @param branchName the name of branch * @param commitMessage the commit message * @throws GitLabApiException if any exception occurs */ - public void deleteFile(String filePath, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + public void deleteFile(Object projectIdOrPath, String filePath, String branchName, String commitMessage) throws GitLabApiException { if (filePath == null) { throw new RuntimeException("filePath cannot be null"); @@ -270,17 +327,42 @@ public class RepositoryFileApi extends AbstractApi { if (isApiVersion(ApiVersion.V3)) { addFormParam(form, "file_path", filePath, true); - delete(expectedStatus, form.asMap(), "projects", projectId, "repository", "files"); + delete(expectedStatus, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files"); } else { - delete(expectedStatus, form.asMap(), "projects", projectId, "repository", "files", urlEncode(filePath)); + delete(expectedStatus, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath)); } } + /** + * 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 full path to new file. Ex. lib/class.rb + * @param projectId the project ID + * @param branchName the name of branch + * @param commitMessage the commit message + * @throws GitLabApiException if any exception occurs + * @deprecated Will be removed in version 5.0, replaced by {@link #deleteFile(Object, RepositoryFile, String, String)} + */ + @Deprecated + public void deleteFile(String filePath, Integer projectId, String branchName, String commitMessage) throws GitLabApiException { + deleteFile(projectId, filePath, branchName, commitMessage); + } + /** * Get the raw file for the file by commit sha and path. Thye file will be saved to the specified directory. * If the file already exists in the directory it will be overwritten. * - * GET /projects/:id/repository/archive + * V3: + * GET /projects/:id/repository/blobs/:sha + * + * V4: + * GET /projects/:id/repository/files/:filepath * * @param projectId the ID of the project * @param commitOrBranchName the commit or branch name to get the file for @@ -289,48 +371,71 @@ public class RepositoryFileApi extends AbstractApi { * @return a File instance pointing to the download of the specified file * @throws GitLabApiException if any exception occurs */ - public File getRawFile(Integer projectId, String commitOrBranchName, String filepath, File directory) throws GitLabApiException { + public File getRawFile(Object projectIdOrPath, String commitOrBranchName, String filepath, File directory) throws GitLabApiException { - Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true); - Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD, - "projects", projectId, "repository", "files", urlEncode(filepath), "raw"); + InputStream in = getRawFile(projectIdOrPath, commitOrBranchName, filepath); try { - if (directory == null) + if (directory == null) { directory = new File(System.getProperty("java.io.tmpdir")); + } String filename = new File(filepath).getName(); File file = new File(directory, filename); - InputStream in = response.readEntity(InputStream.class); Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); return (file); } catch (IOException ioe) { throw new GitLabApiException(ioe); + } finally { + try { + in.close(); + } catch (IOException ignore) { + } } } /** * Get the raw file contents for a file by commit sha and path. * + * V3: * GET /projects/:id/repository/blobs/:sha * - * @param projectId the ID of the project + * V4: + * GET /projects/:id/repository/files/:filepath + * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance * @param commitOrBranchName the commit or branch name to get the file contents for * @param filepath the path of the file to get * @return an InputStream to read the raw file from * @throws GitLabApiException if any exception occurs */ - public InputStream getRawFile(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException { - Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true); - Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD, - "projects", projectId, "repository", "files", urlEncode(filepath), "raw"); - return (response.readEntity(InputStream.class)); + public InputStream getRawFile(Object projectIdOrPath, String commitOrBranchName, String filepath) throws GitLabApiException { + + if (isApiVersion(ApiVersion.V3)) { + Form formData = new GitLabApiForm().withParam("file_path", filepath, true); + Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD, + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "blobs", commitOrBranchName); + return (response.readEntity(InputStream.class)); + } else { + Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true); + Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD, + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filepath), "raw"); + return (response.readEntity(InputStream.class)); + } } - private Form createForm(RepositoryFile file, String branchName, String commitMessage) { + /** + * Gets the query params based on the API version. + * + * @param file the RepositoryFile instance with the info for the query params + * @param branchName the branch name + * @param commitMessage the commit message + * @return a Form instance with the correct query params. + */ + protected Form createForm(RepositoryFile file, String branchName, String commitMessage) { Form form = new Form(); if (isApiVersion(ApiVersion.V3)) { -- GitLab