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

Fix Repository Archive API Format Bug (#244)

parent 1047d7b1
......@@ -7,8 +7,10 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
......@@ -27,6 +29,19 @@ import org.gitlab4j.api.utils.FileUtils;
*/
public class RepositoryApi extends AbstractApi {
private static final Set<String> validFormat = new HashSet<String>(){
{
add("tar.bz2");
add("tbz");
add("tbz2");
add("tb2");
add("bz2");
add("tar");
add("zip");
add("tar.gz");
}
};
public RepositoryApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
......@@ -436,6 +451,13 @@ public class RepositoryApi extends AbstractApi {
*
* GET /projects/:id/repository/archive
*
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* So we must check format if it is valid.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*
* @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,
......@@ -445,9 +467,13 @@ public class RepositoryApi extends AbstractApi {
* @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);
// Throws a GitLabApiException if format is invalid
format = checkFormat(format);
Form formData = new GitLabApiForm().withParam("sha", sha);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive");
"projects", projectId, "repository", "archive", ".", format);
return (response.readEntity(InputStream.class));
}
......@@ -492,6 +518,13 @@ public class RepositoryApi extends AbstractApi {
*
* GET /projects/:id/repository/archive
*
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* So we must check format if it is valid.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*
* @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"
......@@ -502,9 +535,12 @@ public class RepositoryApi extends AbstractApi {
*/
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("sha", sha).withParam("format", format);
// Throws a GitLabApiException if format is invalid
format = checkFormat(format);
Form formData = new GitLabApiForm().withParam("sha", sha);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive");
"projects", projectId, "repository", "archive", ".", format);
try {
......@@ -606,4 +642,42 @@ public class RepositoryApi extends AbstractApi {
public Pager<Contributor> getContributors(Integer projectId, int itemsPerPage) throws GitLabApiException {
return new Pager<Contributor>(this, Contributor.class, itemsPerPage, null, "projects", projectId, "repository", "contributors");
}
/* gitlab-ce/lib/gitlab/git/repository.rb #386 */
/*
extension =
case format
when "tar.bz2", "tbz", "tbz2", "tb2", "bz2"
"tar.bz2"
when "tar"
"tar"
when "zip"
"zip"
else
# everything else should fall back to tar.gz
"tar.gz"
end
*/
/**
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* We must check format if it is valid.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @return A valid format. Default is tar.gz.
*/
private String checkFormat(String format) throws GitLabApiException {
if(format == null || format.isEmpty())
return "tar.gz";
if(!validFormat.contains(format))
throw new GitLabApiException("Invalid format! Options are tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, zip.");
return format;
}
}
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