Commit b90c8189 authored by zhengrenjie's avatar zhengrenjie Committed by Greg Messner
Browse files

Added getProject() methods which return a single project with statistics and...

Added getProject() methods which return a single project with statistics and methods for retrieving project archives with a specified format. (#200)
parent 97e21a81
......@@ -477,6 +477,22 @@ public class ProjectApi extends AbstractApi implements Constants {
return (response.readEntity(Project.class));
}
/**
* Get a specific project, which is owned by the authentication user.
*
* GET /projects/:id
*
* @param projectId the ID of the project to get
* @param statistics include project statistics
* @return the specified project
* @throws GitLabApiException if any exception occurs
*/
public Project getProject(Integer projectId, Boolean statistics) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("statistics", statistics);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId);
return (response.readEntity(Project.class));
}
/**
* Get an Optional instance with the value for the specific project, which is owned by the authentication user.
*
......@@ -524,6 +540,39 @@ public class ProjectApi extends AbstractApi implements Constants {
return (response.readEntity(Project.class));
}
/**
* Get a specific project, which is owned by the authentication user.
*
* GET /projects/:id
*
* @param namespace the name of the project namespace or group
* @param project the name of the project to get
* @param statistics include project statistics
* @return the specified project
* @throws GitLabApiException if any exception occurs
*/
public Project getProject(String namespace, String project, Boolean statistics) throws GitLabApiException {
if (namespace == null) {
throw new RuntimeException("namespace cannot be null");
}
if (project == null) {
throw new RuntimeException("project cannot be null");
}
String projectPath = null;
try {
projectPath = URLEncoder.encode(namespace + "/" + project, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw (new GitLabApiException(uee));
}
Form formData = new GitLabApiForm().withParam("statistics", statistics);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectPath);
return (response.readEntity(Project.class));
}
/**
* Get an Optional instance with the value for the specific project, which is owned by the authentication user.
*
......
......@@ -425,6 +425,26 @@ public class RepositoryApi extends AbstractApi {
return (response.readEntity(InputStream.class));
}
/**
* Get an archive of the complete repository by SHA (optional).
*
* GET /projects/:id/repository/archive
*
* @param projectId the ID of the project
* @param sha the SHA of the archive to get
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @return an input stream that can be used to save as a file
* or to read the content of the archive
* @throws GitLabApiException if any exception occurs
*/
public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("sha", sha).withParam("format", format);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive");
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.
......@@ -460,6 +480,43 @@ public class RepositoryApi extends AbstractApi {
}
}
/**
* 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 the ID of the project
* @param sha the SHA of the archive to get
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
*/
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("sha", sha).withParam("format", format);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive");
try {
if (directory == null)
directory = new File(System.getProperty("java.io.tmpdir"));
String filename = FileUtils.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);
}
}
/**
* Compare branches, tags or commits. This can be accessed without authentication
* if the repository is publicly accessible.
......
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