77import java .net .URLEncoder ;
88import java .nio .file .Files ;
99import java .nio .file .StandardCopyOption ;
10+ import java .util .HashSet ;
1011import java .util .List ;
1112
13+ import java .util .Set ;
1214import javax .ws .rs .core .Form ;
1315import javax .ws .rs .core .GenericType ;
1416import javax .ws .rs .core .MediaType ;
2729 */
2830public class RepositoryApi extends AbstractApi {
2931
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+
3045 public RepositoryApi (GitLabApi gitLabApi ) {
3146 super (gitLabApi );
3247 }
@@ -436,6 +451,13 @@ public InputStream getRepositoryArchive(Integer projectId, String sha) throws Gi
436451 *
437452 * GET /projects/:id/repository/archive
438453 *
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.
457+ *
458+ * Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
459+ * https://gitlab.com/gitlab-com/support-forum/issues/3067
460+ *
439461 * @param projectId the ID of the project
440462 * @param sha the SHA of the archive to get
441463 * @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
@@ -445,9 +467,13 @@ public InputStream getRepositoryArchive(Integer projectId, String sha) throws Gi
445467 * @throws GitLabApiException if any exception occurs
446468 */
447469 public InputStream getRepositoryArchive (Integer projectId , String sha , String format ) throws GitLabApiException {
448- Form formData = new GitLabApiForm ().withParam ("sha" , sha ).withParam ("format" , format );
470+
471+ // Throws a GitLabApiException if format is invalid
472+ format = checkFormat (format );
473+
474+ Form formData = new GitLabApiForm ().withParam ("sha" , sha );
449475 Response response = getWithAccepts (Response .Status .OK , formData .asMap (), MediaType .MEDIA_TYPE_WILDCARD ,
450- "projects" , projectId , "repository" , "archive" );
476+ "projects" , projectId , "repository" , "archive" , "." , format );
451477 return (response .readEntity (InputStream .class ));
452478 }
453479
@@ -492,6 +518,13 @@ public File getRepositoryArchive(Integer projectId, String sha, File directory)
492518 *
493519 * GET /projects/:id/repository/archive
494520 *
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.
524+ *
525+ * Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
526+ * https://gitlab.com/gitlab-com/support-forum/issues/3067
527+ *
495528 * @param projectId the ID of the project
496529 * @param sha the SHA of the archive to get
497530 * @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 File getRepositoryArchive(Integer projectId, String sha, File directory)
502535 */
503536 public File getRepositoryArchive (Integer projectId , String sha , File directory , String format ) throws GitLabApiException {
504537
505- Form formData = new GitLabApiForm ().withParam ("sha" , sha ).withParam ("format" , format );
538+ // Throws a GitLabApiException if format is invalid
539+ format = checkFormat (format );
540+
541+ Form formData = new GitLabApiForm ().withParam ("sha" , sha );
506542 Response response = getWithAccepts (Response .Status .OK , formData .asMap (), MediaType .MEDIA_TYPE_WILDCARD ,
507- "projects" , projectId , "repository" , "archive" );
543+ "projects" , projectId , "repository" , "archive" , "." , format );
508544
509545 try {
510546
@@ -606,4 +642,42 @@ public List<Contributor> getContributors(Integer projectId, int page, int perPag
606642 public Pager <Contributor > getContributors (Integer projectId , int itemsPerPage ) throws GitLabApiException {
607643 return new Pager <Contributor >(this , Contributor .class , itemsPerPage , null , "projects" , projectId , "repository" , "contributors" );
608644 }
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+ }
609683}
0 commit comments