Skip to content

Commit fb5fe44

Browse files
committed
#982 unable to find path for getArchive api
Added "path" parameter to methods Fixes #982
1 parent 2d9a588 commit fb5fe44

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

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

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,35 @@ public InputStream getRawBlobContent(Object projectIdOrPath, String sha) throws
443443
* @return an input stream that can be used to save as a file
444444
* or to read the content of the archive
445445
* @throws GitLabApiException if any exception occurs
446+
* @deprecated Use {@link #getRepositoryArchiveByPathAndSha(Object, String, String)}
446447
*/
448+
@Deprecated
447449
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha) throws GitLabApiException {
448450
Form formData = new GitLabApiForm().withParam("sha", sha);
449451
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
450452
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
451453
return (response.readEntity(InputStream.class));
452454
}
453455

456+
/**
457+
* Get an archive of the complete repository by SHA (optional).
458+
*
459+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
460+
*
461+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
462+
* @param path The subpath of the repository to download
463+
* @param sha the SHA of the archive to get
464+
* @return an input stream that can be used to save as a file
465+
* or to read the content of the archive
466+
* @throws GitLabApiException if any exception occurs
467+
*/
468+
public InputStream getRepositoryArchiveByPathAndSha(Object projectIdOrPath, String path, String sha) throws GitLabApiException {
469+
Form formData = new GitLabApiForm().withParam("sha", sha).withParam("path", path);
470+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
471+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
472+
return (response.readEntity(InputStream.class));
473+
}
474+
454475
/**
455476
* Get an archive of the complete repository by SHA (optional).
456477
*
@@ -461,12 +482,31 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha) thro
461482
* @param format The archive format, defaults to "tar.gz" if null
462483
* @return an input stream that can be used to save as a file or to read the content of the archive
463484
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
485+
* @deprecated Use {@link #getRepositoryArchiveByPathAndSha(Object, String, String, String)}
464486
*/
487+
@Deprecated
465488
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, String format) throws GitLabApiException {
466489
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
467490
return (getRepositoryArchive(projectIdOrPath, sha, archiveFormat));
468491
}
469492

493+
/**
494+
* Get an archive of the complete repository by SHA (optional).
495+
*
496+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
497+
*
498+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
499+
* @param path The subpath of the repository to download
500+
* @param sha the SHA of the archive to get
501+
* @param format The archive format, defaults to "tar.gz" if null
502+
* @return an input stream that can be used to save as a file or to read the content of the archive
503+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
504+
*/
505+
public InputStream getRepositoryArchiveByPathAndSha(Object projectIdOrPath, String path, String sha, String format) throws GitLabApiException {
506+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
507+
return (getRepositoryArchiveByPathAndSha(projectIdOrPath, path, sha, archiveFormat));
508+
}
509+
470510
/**
471511
* Get an archive of the complete repository by SHA (optional).
472512
*
@@ -477,7 +517,9 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Stri
477517
* @param format The archive format, defaults to TAR_GZ if null
478518
* @return an input stream that can be used to save as a file or to read the content of the archive
479519
* @throws GitLabApiException if any exception occurs
520+
* @deprecated User {@link #getRepositoryArchiveByPathAndSha(Object, String, String, ArchiveFormat)}
480521
*/
522+
@Deprecated
481523
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, ArchiveFormat format) throws GitLabApiException {
482524

483525
if (format == null) {
@@ -497,6 +539,37 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Arch
497539
return (response.readEntity(InputStream.class));
498540
}
499541

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

514589
Form formData = new GitLabApiForm().withParam("sha", sha);
@@ -532,6 +607,42 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
532607
}
533608
}
534609

610+
/**
611+
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
612+
* If the archive already exists in the directory it will be overwritten.
613+
*
614+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
615+
*
616+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
617+
* @param path The subpath of the repository to download
618+
* @param sha the SHA of the archive to get
619+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
620+
* @return a File instance pointing to the downloaded instance
621+
* @throws GitLabApiException if any exception occurs
622+
*/
623+
public File getRepositoryArchiveByPathAndSha(Object projectIdOrPath, String path, String sha, File directory) throws GitLabApiException {
624+
625+
Form formData = new GitLabApiForm().withParam("path", path).withParam("sha", sha);
626+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
627+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
628+
629+
try {
630+
631+
if (directory == null)
632+
directory = new File(System.getProperty("java.io.tmpdir"));
633+
634+
String filename = FileUtils.getFilenameFromContentDisposition(response);
635+
File file = new File(directory, filename);
636+
637+
InputStream in = response.readEntity(InputStream.class);
638+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
639+
return (file);
640+
641+
} catch (IOException ioe) {
642+
throw new GitLabApiException(ioe);
643+
}
644+
}
645+
535646
/**
536647
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
537648
* If the archive already exists in the directory it will be overwritten.
@@ -544,12 +655,33 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
544655
* @param format The archive format, defaults to "tar.gz" if null
545656
* @return a File instance pointing to the downloaded instance
546657
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
658+
* @deprecated Use {@link #getRepositoryArchiveByPathAndSha(Object, String, String, File, String)}
547659
*/
660+
@Deprecated
548661
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, String format) throws GitLabApiException {
549662
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
550663
return (getRepositoryArchive(projectIdOrPath, sha, directory, archiveFormat));
551664
}
552665

666+
/**
667+
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
668+
* If the archive already exists in the directory it will be overwritten.
669+
*
670+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
671+
*
672+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
673+
* @param path The subpath of the repository to download
674+
* @param sha the SHA of the archive to get
675+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
676+
* @param format The archive format, defaults to "tar.gz" if null
677+
* @return a File instance pointing to the downloaded instance
678+
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
679+
*/
680+
public File getRepositoryArchiveByPathAndSha(Object projectIdOrPath, String path, String sha, File directory, String format) throws GitLabApiException {
681+
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
682+
return (getRepositoryArchiveByPathAndSha(projectIdOrPath, path, sha, directory, archiveFormat));
683+
}
684+
553685
/**
554686
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
555687
* If the archive already exists in the directory it will be overwritten.
@@ -562,7 +694,9 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
562694
* @param format The archive format, defaults to TAR_GZ if null
563695
* @return a File instance pointing to the downloaded instance
564696
* @throws GitLabApiException if any exception occurs
697+
* @deprecated Use {@link #getRepositoryArchiveByPathAndSha(Object, String, String, File, ArchiveFormat)}
565698
*/
699+
@Deprecated
566700
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, ArchiveFormat format) throws GitLabApiException {
567701

568702
if (format == null) {
@@ -597,6 +731,54 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
597731
}
598732
}
599733

734+
/**
735+
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
736+
* If the archive already exists in the directory it will be overwritten.
737+
*
738+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
739+
*
740+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
741+
* @param path The subpath of the repository to download
742+
* @param sha the SHA of the archive to get
743+
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
744+
* @param format The archive format, defaults to TAR_GZ if null
745+
* @return a File instance pointing to the downloaded instance
746+
* @throws GitLabApiException if any exception occurs
747+
*/
748+
public File getRepositoryArchiveByPathAndSha(Object projectIdOrPath, String path, String sha, File directory, ArchiveFormat format) throws GitLabApiException {
749+
750+
if (format == null) {
751+
format = ArchiveFormat.TAR_GZ;
752+
}
753+
754+
/*
755+
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
756+
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
757+
*
758+
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
759+
* https://gitlab.com/gitlab-com/support-forum/issues/3067
760+
*/
761+
Form formData = new GitLabApiForm().withParam("path", path).withParam("sha", sha);
762+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
763+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive" + "." + format.toString());
764+
765+
try {
766+
767+
if (directory == null)
768+
directory = new File(System.getProperty("java.io.tmpdir"));
769+
770+
String filename = FileUtils.getFilenameFromContentDisposition(response);
771+
File file = new File(directory, filename);
772+
773+
InputStream in = response.readEntity(InputStream.class);
774+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
775+
return (file);
776+
777+
} catch (IOException ioe) {
778+
throw new GitLabApiException(ioe);
779+
}
780+
}
781+
600782
/**
601783
* Compare branches, tags or commits. This can be accessed without authentication
602784
* if the repository is publicly accessible.

src/test/java/org/gitlab4j/api/TestRepositoryApi.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Arrays;
1616
import java.util.List;
1717

18+
import org.gitlab4j.api.Constants.ArchiveFormat;
1819
import org.gitlab4j.api.models.Branch;
1920
import org.gitlab4j.api.models.Commit;
2021
import org.gitlab4j.api.models.CompareResults;
@@ -142,6 +143,39 @@ public void testRepositoryArchiveViaInputStream() throws GitLabApiException, IOE
142143
Files.delete(target);
143144
}
144145

146+
@Test
147+
public void testRepositoryArchiveByPathAndSha() throws IOException, GitLabApiException {
148+
149+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
150+
assertNotNull(project);
151+
152+
InputStream in = gitLabApi.getRepositoryApi()
153+
.getRepositoryArchiveByPathAndSha(project.getId(), project.getPath(), "master");
154+
155+
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-", ".tar.gz");
156+
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
157+
158+
assertTrue(target.toFile().length() > 0);
159+
Files.delete(target);
160+
161+
}
162+
163+
@Test
164+
public void testRepositoryArchiveByPathAndShaAndFormat() throws GitLabApiException, IOException {
165+
166+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
167+
assertNotNull(project);
168+
169+
InputStream in = gitLabApi.getRepositoryApi()
170+
.getRepositoryArchiveByPathAndSha(project.getId(), project.getPath(), "master", ArchiveFormat.TAR_GZ.toString());
171+
172+
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-", ".tar.gz");
173+
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
174+
175+
assertTrue(target.toFile().length() > 0);
176+
Files.delete(target);
177+
}
178+
145179
@Test
146180
public void testRepositoryArchiveViaFile() throws GitLabApiException, IOException {
147181

0 commit comments

Comments
 (0)