Skip to content

Commit a273ce2

Browse files
committed
Unable to find path for getArchive api (#982)
Added "path" parameter to methods Fixes #982
1 parent 2d9a588 commit a273ce2

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-0
lines changed

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

+176
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.gitlab4j.api.models.Commit;
2323
import org.gitlab4j.api.models.CompareResults;
2424
import org.gitlab4j.api.models.Contributor;
25+
import org.gitlab4j.api.models.RepositoryArchiveParams;
2526
import org.gitlab4j.api.models.TreeItem;
2627
import org.gitlab4j.api.utils.FileUtils;
2728

@@ -443,14 +444,33 @@ public InputStream getRawBlobContent(Object projectIdOrPath, String sha) throws
443444
* @return an input stream that can be used to save as a file
444445
* or to read the content of the archive
445446
* @throws GitLabApiException if any exception occurs
447+
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams)}
446448
*/
449+
@Deprecated
447450
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha) throws GitLabApiException {
448451
Form formData = new GitLabApiForm().withParam("sha", sha);
449452
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
450453
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
451454
return (response.readEntity(InputStream.class));
452455
}
453456

457+
/**
458+
* Get an archive of the complete repository by SHA (optional) and Path (optional).
459+
*
460+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
461+
*
462+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
463+
* @param params params for getting file archive of the repository
464+
* @return an input stream that can be used to save as a file or to read the content of the archive
465+
* @throws GitLabApiException if any exception occurs
466+
*/
467+
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params) throws GitLabApiException {
468+
GitLabApiForm formData = params.getForm();
469+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
470+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
471+
return (response.readEntity(InputStream.class));
472+
}
473+
454474
/**
455475
* Get an archive of the complete repository by SHA (optional).
456476
*
@@ -461,12 +481,30 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha) thro
461481
* @param format The archive format, defaults to "tar.gz" if null
462482
* @return an input stream that can be used to save as a file or to read the content of the archive
463483
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
484+
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, String)}
464485
*/
486+
@Deprecated
465487
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, String format) throws GitLabApiException {
466488
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
467489
return (getRepositoryArchive(projectIdOrPath, sha, archiveFormat));
468490
}
469491

492+
/**
493+
* Get an archive of the complete repository by SHA (optional) and Path (optional).
494+
*
495+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
496+
*
497+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
498+
* @param params params for getting file archive of the repository
499+
* @param format The archive format, defaults to "tar.gz" if null
500+
* @return an input stream that can be used to save as a file or to read the content of the archive
501+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
502+
*/
503+
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, String format) throws GitLabApiException {
504+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
505+
return (getRepositoryArchive(projectIdOrPath, params, archiveFormat));
506+
}
507+
470508
/**
471509
* Get an archive of the complete repository by SHA (optional).
472510
*
@@ -477,7 +515,9 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Stri
477515
* @param format The archive format, defaults to TAR_GZ if null
478516
* @return an input stream that can be used to save as a file or to read the content of the archive
479517
* @throws GitLabApiException if any exception occurs
518+
* @deprecated User {@link #getRepositoryArchive(Object, RepositoryArchiveParams, ArchiveFormat)}
480519
*/
520+
@Deprecated
481521
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, ArchiveFormat format) throws GitLabApiException {
482522

483523
if (format == null) {
@@ -497,6 +537,36 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Arch
497537
return (response.readEntity(InputStream.class));
498538
}
499539

540+
/**
541+
* Get an archive of the complete repository by SHA (optional) and Path (optional).
542+
*
543+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
544+
*
545+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
546+
* @param params params for getting file archive of the repository
547+
* @param format The archive format, defaults to TAR_GZ if null
548+
* @return an input stream that can be used to save as a file or to read the content of the archive
549+
* @throws GitLabApiException if any exception occurs
550+
*/
551+
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, ArchiveFormat format) throws GitLabApiException {
552+
553+
if (format == null) {
554+
format = ArchiveFormat.TAR_GZ;
555+
}
556+
557+
/*
558+
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
559+
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
560+
*
561+
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
562+
* https://gitlab.com/gitlab-com/support-forum/issues/3067
563+
*/
564+
Form formData = params.getForm();
565+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
566+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive" + "." + format);
567+
return (response.readEntity(InputStream.class));
568+
}
569+
500570
/**
501571
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
502572
* If the archive already exists in the directory it will be overwritten.
@@ -508,7 +578,9 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Arch
508578
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
509579
* @return a File instance pointing to the downloaded instance
510580
* @throws GitLabApiException if any exception occurs
581+
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File)}
511582
*/
583+
@Deprecated
512584
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory) throws GitLabApiException {
513585

514586
Form formData = new GitLabApiForm().withParam("sha", sha);
@@ -532,6 +604,41 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
532604
}
533605
}
534606

607+
/**
608+
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
609+
* If the archive already exists in the directory it will be overwritten.
610+
*
611+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
612+
*
613+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
614+
* @param params params for getting file archive of the repository
615+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
616+
* @return a File instance pointing to the downloaded instance
617+
* @throws GitLabApiException if any exception occurs
618+
*/
619+
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory) throws GitLabApiException {
620+
621+
Form formData = params.getForm();
622+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
623+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
624+
625+
try {
626+
627+
if (directory == null)
628+
directory = new File(System.getProperty("java.io.tmpdir"));
629+
630+
String filename = FileUtils.getFilenameFromContentDisposition(response);
631+
File file = new File(directory, filename);
632+
633+
InputStream in = response.readEntity(InputStream.class);
634+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
635+
return (file);
636+
637+
} catch (IOException ioe) {
638+
throw new GitLabApiException(ioe);
639+
}
640+
}
641+
535642
/**
536643
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
537644
* If the archive already exists in the directory it will be overwritten.
@@ -544,12 +651,32 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
544651
* @param format The archive format, defaults to "tar.gz" if null
545652
* @return a File instance pointing to the downloaded instance
546653
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
654+
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File, String)}
547655
*/
656+
@Deprecated
548657
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, String format) throws GitLabApiException {
549658
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
550659
return (getRepositoryArchive(projectIdOrPath, sha, directory, archiveFormat));
551660
}
552661

662+
/**
663+
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
664+
* If the archive already exists in the directory it will be overwritten.
665+
*
666+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
667+
*
668+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
669+
* @param params params for getting file archive of the repository
670+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
671+
* @param format The archive format, defaults to "tar.gz" if null
672+
* @return a File instance pointing to the downloaded instance
673+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
674+
*/
675+
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory, String format) throws GitLabApiException {
676+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
677+
return (getRepositoryArchive(projectIdOrPath, params, directory, archiveFormat));
678+
}
679+
553680
/**
554681
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
555682
* If the archive already exists in the directory it will be overwritten.
@@ -562,7 +689,9 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
562689
* @param format The archive format, defaults to TAR_GZ if null
563690
* @return a File instance pointing to the downloaded instance
564691
* @throws GitLabApiException if any exception occurs
692+
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File, ArchiveFormat)}
565693
*/
694+
@Deprecated
566695
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, ArchiveFormat format) throws GitLabApiException {
567696

568697
if (format == null) {
@@ -597,6 +726,53 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
597726
}
598727
}
599728

729+
/**
730+
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
731+
* If the archive already exists in the directory it will be overwritten.
732+
*
733+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
734+
*
735+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
736+
* @param params params for getting file archive of the repository
737+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
738+
* @param format The archive format, defaults to TAR_GZ if null
739+
* @return a File instance pointing to the downloaded instance
740+
* @throws GitLabApiException if any exception occurs
741+
*/
742+
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory, ArchiveFormat format) throws GitLabApiException {
743+
744+
if (format == null) {
745+
format = ArchiveFormat.TAR_GZ;
746+
}
747+
748+
/*
749+
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
750+
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
751+
*
752+
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
753+
* https://gitlab.com/gitlab-com/support-forum/issues/3067
754+
*/
755+
Form formData = params.getForm();
756+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
757+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive" + "." + format.toString());
758+
759+
try {
760+
761+
if (directory == null)
762+
directory = new File(System.getProperty("java.io.tmpdir"));
763+
764+
String filename = FileUtils.getFilenameFromContentDisposition(response);
765+
File file = new File(directory, filename);
766+
767+
InputStream in = response.readEntity(InputStream.class);
768+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
769+
return (file);
770+
771+
} catch (IOException ioe) {
772+
throw new GitLabApiException(ioe);
773+
}
774+
}
775+
600776
/**
601777
* Compare branches, tags or commits. This can be accessed without authentication
602778
* if the repository is publicly accessible.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.GitLabApiForm;
4+
5+
/**
6+
* Params for getting file archive of the repository.
7+
*/
8+
public class RepositoryArchiveParams {
9+
10+
private String sha;
11+
private String path;
12+
13+
/**
14+
* Add param "The commit SHA to download".
15+
*
16+
* @param sha the commit SHA to download
17+
* @return current params with sha
18+
*/
19+
public RepositoryArchiveParams withSha(String sha) {
20+
this.sha = sha;
21+
return this;
22+
}
23+
24+
/**
25+
* Add param "The subpath of the repository to download".
26+
*
27+
* @param path the subpath of the repository to download
28+
* @return current params with path
29+
*/
30+
public RepositoryArchiveParams withPath(String path) {
31+
this.path = path;
32+
return this;
33+
}
34+
35+
/**
36+
* Get form with params.
37+
*
38+
* @return form with params
39+
*/
40+
public GitLabApiForm getForm() {
41+
return new GitLabApiForm()
42+
.withParam("sha", sha)
43+
.withParam("path", path);
44+
}
45+
46+
47+
}

0 commit comments

Comments
 (0)