7
7
import java .net .URLEncoder ;
8
8
import java .nio .file .Files ;
9
9
import java .nio .file .StandardCopyOption ;
10
+ import java .util .HashSet ;
10
11
import java .util .List ;
11
12
13
+ import java .util .Set ;
12
14
import javax .ws .rs .core .Form ;
13
15
import javax .ws .rs .core .GenericType ;
14
16
import javax .ws .rs .core .MediaType ;
27
29
*/
28
30
public class RepositoryApi extends AbstractApi {
29
31
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
+
30
45
public RepositoryApi (GitLabApi gitLabApi ) {
31
46
super (gitLabApi );
32
47
}
@@ -436,6 +451,13 @@ public InputStream getRepositoryArchive(Integer projectId, String sha) throws Gi
436
451
*
437
452
* GET /projects/:id/repository/archive
438
453
*
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
+ *
439
461
* @param projectId the ID of the project
440
462
* @param sha the SHA of the archive to get
441
463
* @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
445
467
* @throws GitLabApiException if any exception occurs
446
468
*/
447
469
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 );
449
475
Response response = getWithAccepts (Response .Status .OK , formData .asMap (), MediaType .MEDIA_TYPE_WILDCARD ,
450
- "projects" , projectId , "repository" , "archive" );
476
+ "projects" , projectId , "repository" , "archive" , "." , format );
451
477
return (response .readEntity (InputStream .class ));
452
478
}
453
479
@@ -492,6 +518,13 @@ public File getRepositoryArchive(Integer projectId, String sha, File directory)
492
518
*
493
519
* GET /projects/:id/repository/archive
494
520
*
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
+ *
495
528
* @param projectId the ID of the project
496
529
* @param sha the SHA of the archive to get
497
530
* @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)
502
535
*/
503
536
public File getRepositoryArchive (Integer projectId , String sha , File directory , String format ) throws GitLabApiException {
504
537
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 );
506
542
Response response = getWithAccepts (Response .Status .OK , formData .asMap (), MediaType .MEDIA_TYPE_WILDCARD ,
507
- "projects" , projectId , "repository" , "archive" );
543
+ "projects" , projectId , "repository" , "archive" , "." , format );
508
544
509
545
try {
510
546
@@ -606,4 +642,42 @@ public List<Contributor> getContributors(Integer projectId, int page, int perPag
606
642
public Pager <Contributor > getContributors (Integer projectId , int itemsPerPage ) throws GitLabApiException {
607
643
return new Pager <Contributor >(this , Contributor .class , itemsPerPage , null , "projects" , projectId , "repository" , "contributors" );
608
644
}
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
+ }
609
683
}
0 commit comments