|
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; |
11 | 10 | import java.util.List; |
12 | 11 |
|
13 | | -import java.util.Set; |
14 | 12 | import javax.ws.rs.core.Form; |
15 | 13 | import javax.ws.rs.core.GenericType; |
16 | 14 | import javax.ws.rs.core.MediaType; |
|
29 | 27 | */ |
30 | 28 | public class RepositoryApi extends AbstractApi { |
31 | 29 |
|
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 | | - |
45 | 30 | public RepositoryApi(GitLabApi gitLabApi) { |
46 | 31 | super(gitLabApi); |
47 | 32 | } |
@@ -451,29 +436,44 @@ public InputStream getRepositoryArchive(Integer projectId, String sha) throws Gi |
451 | 436 | * |
452 | 437 | * GET /projects/:id/repository/archive |
453 | 438 | * |
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). |
457 | 452 | * |
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 |
460 | 454 | * |
461 | 455 | * @param projectId the ID of the project |
462 | 456 | * @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 |
467 | 459 | * @throws GitLabApiException if any exception occurs |
468 | 460 | */ |
469 | | - public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException { |
| 461 | + public InputStream getRepositoryArchive(Integer projectId, String sha, ArchiveFormat format) throws GitLabApiException { |
470 | 462 |
|
471 | | - // Throws a GitLabApiException if format is invalid |
472 | | - format = checkFormat(format); |
| 463 | + if (format == null) { |
| 464 | + format = ArchiveFormat.TAR_GZ; |
| 465 | + } |
473 | 466 |
|
| 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 | + */ |
474 | 474 | Form formData = new GitLabApiForm().withParam("sha", sha); |
475 | 475 | 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()); |
477 | 477 | return (response.readEntity(InputStream.class)); |
478 | 478 | } |
479 | 479 |
|
@@ -518,29 +518,47 @@ public File getRepositoryArchive(Integer projectId, String sha, File directory) |
518 | 518 | * |
519 | 519 | * GET /projects/:id/repository/archive |
520 | 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. |
| 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. |
524 | 536 | * |
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 |
527 | 538 | * |
528 | 539 | * @param projectId the ID of the project |
529 | 540 | * @param sha the SHA of the archive to get |
530 | 541 | * @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 |
533 | 543 | * @return a File instance pointing to the downloaded instance |
534 | 544 | * @throws GitLabApiException if any exception occurs |
535 | 545 | */ |
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 { |
537 | 547 |
|
538 | | - // Throws a GitLabApiException if format is invalid |
539 | | - format = checkFormat(format); |
| 548 | + if (format == null) { |
| 549 | + format = ArchiveFormat.TAR_GZ; |
| 550 | + } |
540 | 551 |
|
| 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 | + */ |
541 | 559 | Form formData = new GitLabApiForm().withParam("sha", sha); |
542 | 560 | 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()); |
544 | 562 |
|
545 | 563 | try { |
546 | 564 |
|
@@ -642,42 +660,4 @@ public List<Contributor> getContributors(Integer projectId, int page, int perPag |
642 | 660 | public Pager<Contributor> getContributors(Integer projectId, int itemsPerPage) throws GitLabApiException { |
643 | 661 | return new Pager<Contributor>(this, Contributor.class, itemsPerPage, null, "projects", projectId, "repository", "contributors"); |
644 | 662 | } |
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 | | - } |
683 | 663 | } |
0 commit comments