Skip to content

Commit ae9200f

Browse files
committed
Mods to use enums for archive format.
1 parent 1f358da commit ae9200f

File tree

2 files changed

+97
-77
lines changed

2 files changed

+97
-77
lines changed

src/main/java/org/gitlab4j/api/Constants.java

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.gitlab4j.api;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
47

58
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -438,4 +441,41 @@ public String toString() {
438441
return (enumHelper.toString(this));
439442
}
440443
}
444+
445+
/** Enum to specify the format of a downloaded archive. */
446+
public enum ArchiveFormat {
447+
448+
BZ2, TAR, TAR_BZ2, TAR_GZ, TB2, TBZ, TBZ2, ZIP;
449+
450+
private final String value;
451+
452+
ArchiveFormat() {
453+
this.value = name().toLowerCase().replace('_', '.');
454+
}
455+
456+
private static Map<String, ArchiveFormat> valuesMap = new HashMap<String, ArchiveFormat>(8);
457+
static {
458+
for (ArchiveFormat archiveFormat : ArchiveFormat.values())
459+
valuesMap.put(archiveFormat.value, archiveFormat);
460+
}
461+
462+
public static ArchiveFormat forValue(String value) throws GitLabApiException {
463+
464+
if (value == null || value.trim().isEmpty()) {
465+
return (null);
466+
}
467+
468+
ArchiveFormat archiveFormat = valuesMap.get(value);
469+
if (archiveFormat != null) {
470+
return (archiveFormat);
471+
}
472+
473+
throw new GitLabApiException("Invalid format! Options are tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, and zip.");
474+
}
475+
476+
@Override
477+
public String toString() {
478+
return (value);
479+
}
480+
}
441481
}

src/main/java/org/gitlab4j/api/RepositoryApi.java

+57-77
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import java.net.URLEncoder;
88
import java.nio.file.Files;
99
import java.nio.file.StandardCopyOption;
10-
import java.util.HashSet;
1110
import java.util.List;
1211

13-
import java.util.Set;
1412
import javax.ws.rs.core.Form;
1513
import javax.ws.rs.core.GenericType;
1614
import javax.ws.rs.core.MediaType;
@@ -29,19 +27,6 @@
2927
*/
3028
public class RepositoryApi extends AbstractApi {
3129

32-
private static final Set<String> validFormat = new HashSet<String>(){
33-
{
34-
add("tar.bz2");
35-
add("tbz");
36-
add("tbz2");
37-
add("tb2");
38-
add("bz2");
39-
add("tar");
40-
add("zip");
41-
add("tar.gz");
42-
}
43-
};
44-
4530
public RepositoryApi(GitLabApi gitLabApi) {
4631
super(gitLabApi);
4732
}
@@ -451,29 +436,44 @@ public InputStream getRepositoryArchive(Integer projectId, String sha) throws Gi
451436
*
452437
* GET /projects/:id/repository/archive
453438
*
454-
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
455-
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
456-
* So we must check format if it is valid.
439+
* @param projectId the ID of the project
440+
* @param sha the SHA of the archive to get
441+
* @param format The archive format, defaults to "tar.gz" if null
442+
* @return an input stream that can be used to save as a file or to read the content of the archive
443+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
444+
*/
445+
public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException {
446+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
447+
return (getRepositoryArchive(projectId, sha, archiveFormat));
448+
}
449+
450+
/**
451+
* Get an archive of the complete repository by SHA (optional).
457452
*
458-
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
459-
* https://gitlab.com/gitlab-com/support-forum/issues/3067
453+
* GET /projects/:id/repository/archive
460454
*
461455
* @param projectId the ID of the project
462456
* @param sha the SHA of the archive to get
463-
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
464-
* tb2, bz2, tar, zip
465-
* @return an input stream that can be used to save as a file
466-
* or to read the content of the archive
457+
* @param format The archive format, defaults to TAR_GZ if null
458+
* @return an input stream that can be used to save as a file or to read the content of the archive
467459
* @throws GitLabApiException if any exception occurs
468460
*/
469-
public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException {
461+
public InputStream getRepositoryArchive(Integer projectId, String sha, ArchiveFormat format) throws GitLabApiException {
470462

471-
// Throws a GitLabApiException if format is invalid
472-
format = checkFormat(format);
463+
if (format == null) {
464+
format = ArchiveFormat.TAR_GZ;
465+
}
473466

467+
/*
468+
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
469+
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
470+
*
471+
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
472+
* https://gitlab.com/gitlab-com/support-forum/issues/3067
473+
*/
474474
Form formData = new GitLabApiForm().withParam("sha", sha);
475475
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
476-
"projects", projectId, "repository", "archive", ".", format);
476+
"projects", projectId, "repository", "archive", ".", format.toString());
477477
return (response.readEntity(InputStream.class));
478478
}
479479

@@ -518,29 +518,47 @@ public File getRepositoryArchive(Integer projectId, String sha, File directory)
518518
*
519519
* GET /projects/:id/repository/archive
520520
*
521-
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
522-
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
523-
* So we must check format if it is valid.
521+
* @param projectId the ID of the project
522+
* @param sha the SHA of the archive to get
523+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
524+
* @param format The archive format, defaults to "tar.gz" if null
525+
* @return a File instance pointing to the downloaded instance
526+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
527+
*/
528+
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
529+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
530+
return (getRepositoryArchive(projectId, sha, directory, archiveFormat));
531+
}
532+
533+
/**
534+
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
535+
* If the archive already exists in the directory it will be overwritten.
524536
*
525-
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
526-
* https://gitlab.com/gitlab-com/support-forum/issues/3067
537+
* GET /projects/:id/repository/archive
527538
*
528539
* @param projectId the ID of the project
529540
* @param sha the SHA of the archive to get
530541
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
531-
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
532-
* tb2, bz2, tar, zip
542+
* @param format The archive format, defaults to TAR_GZ if null
533543
* @return a File instance pointing to the downloaded instance
534544
* @throws GitLabApiException if any exception occurs
535545
*/
536-
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
546+
public File getRepositoryArchive(Integer projectId, String sha, File directory, ArchiveFormat format) throws GitLabApiException {
537547

538-
// Throws a GitLabApiException if format is invalid
539-
format = checkFormat(format);
548+
if (format == null) {
549+
format = ArchiveFormat.TAR_GZ;
550+
}
540551

552+
/*
553+
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
554+
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
555+
*
556+
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
557+
* https://gitlab.com/gitlab-com/support-forum/issues/3067
558+
*/
541559
Form formData = new GitLabApiForm().withParam("sha", sha);
542560
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
543-
"projects", projectId, "repository", "archive", ".", format);
561+
"projects", projectId, "repository", "archive", ".", format.toString());
544562

545563
try {
546564

@@ -642,42 +660,4 @@ public List<Contributor> getContributors(Integer projectId, int page, int perPag
642660
public Pager<Contributor> getContributors(Integer projectId, int itemsPerPage) throws GitLabApiException {
643661
return new Pager<Contributor>(this, Contributor.class, itemsPerPage, null, "projects", projectId, "repository", "contributors");
644662
}
645-
646-
/* gitlab-ce/lib/gitlab/git/repository.rb #386 */
647-
/*
648-
extension =
649-
case format
650-
when "tar.bz2", "tbz", "tbz2", "tb2", "bz2"
651-
"tar.bz2"
652-
when "tar"
653-
"tar"
654-
when "zip"
655-
"zip"
656-
else
657-
# everything else should fall back to tar.gz
658-
"tar.gz"
659-
end
660-
*/
661-
/**
662-
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
663-
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
664-
* We must check format if it is valid.
665-
*
666-
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
667-
* https://gitlab.com/gitlab-com/support-forum/issues/3067
668-
*
669-
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
670-
* tb2, bz2, tar, zip
671-
* @return A valid format. Default is tar.gz.
672-
*/
673-
private String checkFormat(String format) throws GitLabApiException {
674-
675-
if(format == null || format.isEmpty())
676-
return "tar.gz";
677-
678-
if(!validFormat.contains(format))
679-
throw new GitLabApiException("Invalid format! Options are tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, zip.");
680-
681-
return format;
682-
}
683663
}

0 commit comments

Comments
 (0)