Commit 194821f1 authored by Greg Messner's avatar Greg Messner
Browse files

Added getRepositoryArchive() method that downloads the archive to a file.

parent 3e341528
package com.messners.gitlab.api; package com.messners.gitlab.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
import javax.ws.rs.core.Form; import javax.ws.rs.core.Form;
...@@ -62,9 +66,9 @@ public class RepositoryApi extends AbstractApi { ...@@ -62,9 +66,9 @@ public class RepositoryApi extends AbstractApi {
*/ */
public Branch createBranch(Integer projectId, String branchName, String ref) throws GitLabApiException { public Branch createBranch(Integer projectId, String branchName, String ref) throws GitLabApiException {
Form formData = new Form(); Form formData = new GitLabApiForm()
addFormParam(formData, "branch_name", branchName, true); .withParam("branch_name", branchName, true)
addFormParam(formData, "ref", ref, true); .withParam("ref", ref, true);
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "branches"); Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "branches");
return (response.readEntity(Branch.class)); return (response.readEntity(Branch.class));
} }
...@@ -180,11 +184,11 @@ public class RepositoryApi extends AbstractApi { ...@@ -180,11 +184,11 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public List<TreeItem> getTree(Integer projectId, String filePath, String refName, Boolean recursive) throws GitLabApiException { public List<TreeItem> getTree(Integer projectId, String filePath, String refName, Boolean recursive) throws GitLabApiException {
Form formData = new Form(); Form formData = new GitLabApiForm()
addFormParam(formData, "id", projectId, true); .withParam("id", projectId, true)
addFormParam(formData, "path", filePath, false); .withParam("path", filePath, false)
addFormParam(formData, "ref_name", refName, false); .withParam("ref_name", refName, false)
addFormParam(formData, "recursive", recursive, false); .withParam("recursive", recursive, false);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "tree"); Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "tree");
return (response.readEntity(new GenericType<List<TreeItem>>() { return (response.readEntity(new GenericType<List<TreeItem>>() {
})); }));
...@@ -201,8 +205,7 @@ public class RepositoryApi extends AbstractApi { ...@@ -201,8 +205,7 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public String getRawFileContent(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException { public String getRawFileContent(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException {
Form formData = new Form(); Form formData = new GitLabApiForm().withParam("filepath", filepath, true);
addFormParam(formData, "filepath", filepath, true);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "blobs", commitOrBranchName); Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "blobs", commitOrBranchName);
return (response.readEntity(String.class)); return (response.readEntity(String.class));
} }
...@@ -234,9 +237,43 @@ public class RepositoryApi extends AbstractApi { ...@@ -234,9 +237,43 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public InputStream getRepositoryArchive(Integer projectId, String sha) throws GitLabApiException { public InputStream getRepositoryArchive(Integer projectId, String sha) throws GitLabApiException {
Form formData = new Form(); Form formData = new GitLabApiForm().withParam("sha", sha);
addFormParam(formData, "sha", sha, false);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "archive"); Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "archive");
return (response.readEntity(InputStream.class)); return (response.readEntity(InputStream.class));
} }
/**
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
*
* GET /projects/:id/repository/archive
*
* @param projectId
* @param sha
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException
*/
public File getRepositoryArchive(Integer projectId, String sha, File directory) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("sha", sha);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "archive");
try {
if (directory == null)
directory = new File(System.getProperty("java.io.tmpdir"));
String filename = Utils.getFilenameFromContentDisposition(response);
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);
}
}
} }
Supports Markdown
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