Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#982 unable to find path for getArchive api #1036

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions src/main/java/org/gitlab4j/api/RepositoryApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Contributor;
import org.gitlab4j.api.models.RepositoryArchiveParams;
import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.utils.FileUtils;

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

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional).
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if any exception occurs
*/
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params) throws GitLabApiException {
GitLabApiForm formData = params.getForm();
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");
return (response.readEntity(InputStream.class));
}

/**
* Get an archive of the complete repository by SHA (optional).
*
Expand All @@ -461,12 +481,30 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha) thro
* @param format The archive format, defaults to "tar.gz" if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, String)}
*/
@Deprecated
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectIdOrPath, sha, archiveFormat));
}

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional).
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @param format The archive format, defaults to "tar.gz" if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
*/
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectIdOrPath, params, archiveFormat));
}

/**
* Get an archive of the complete repository by SHA (optional).
*
Expand All @@ -477,7 +515,9 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Stri
* @param format The archive format, defaults to TAR_GZ if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if any exception occurs
* @deprecated User {@link #getRepositoryArchive(Object, RepositoryArchiveParams, ArchiveFormat)}
*/
@Deprecated
public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, ArchiveFormat format) throws GitLabApiException {

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

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional).
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @param format The archive format, defaults to TAR_GZ if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if any exception occurs
*/
public InputStream getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, ArchiveFormat format) throws GitLabApiException {

if (format == null) {
format = ArchiveFormat.TAR_GZ;
}

/*
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*/
Form formData = params.getForm();
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive" + "." + format);
return (response.readEntity(InputStream.class));
}

/**
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
Expand All @@ -508,7 +578,9 @@ public InputStream getRepositoryArchive(Object projectIdOrPath, String sha, Arch
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File)}
*/
@Deprecated
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory) throws GitLabApiException {

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

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
*/
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory) throws GitLabApiException {

Form formData = params.getForm();
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive");

try {

if (directory == null)
directory = new File(System.getProperty("java.io.tmpdir"));

String filename = FileUtils.getFilenameFromContentDisposition(response);
File file = new File(directory, filename);

InputStream in = response.readEntity(InputStream.class);
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return (file);

} catch (IOException ioe) {
throw new GitLabApiException(ioe);
}
}

/**
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
Expand All @@ -544,12 +651,32 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
* @param format The archive format, defaults to "tar.gz" if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File, String)}
*/
@Deprecated
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectIdOrPath, sha, directory, archiveFormat));
}

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @param format The archive format, defaults to "tar.gz" if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
*/
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectIdOrPath, params, directory, archiveFormat));
}

/**
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
Expand All @@ -562,7 +689,9 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
* @param format The archive format, defaults to TAR_GZ if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
* @deprecated Use {@link #getRepositoryArchive(Object, RepositoryArchiveParams, File, ArchiveFormat)}
*/
@Deprecated
public File getRepositoryArchive(Object projectIdOrPath, String sha, File directory, ArchiveFormat format) throws GitLabApiException {

if (format == null) {
Expand Down Expand Up @@ -597,6 +726,53 @@ public File getRepositoryArchive(Object projectIdOrPath, String sha, File direct
}
}

/**
* Get an archive of the complete repository by SHA (optional) and Path (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/archive</code></pre>
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param params params for getting file archive of the repository
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @param format The archive format, defaults to TAR_GZ if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
*/
public File getRepositoryArchive(Object projectIdOrPath, RepositoryArchiveParams params, File directory, ArchiveFormat format) throws GitLabApiException {

if (format == null) {
format = ArchiveFormat.TAR_GZ;
}

/*
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*/
Form formData = params.getForm();
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.WILDCARD,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "archive" + "." + format.toString());

try {

if (directory == null)
directory = new File(System.getProperty("java.io.tmpdir"));

String filename = FileUtils.getFilenameFromContentDisposition(response);
File file = new File(directory, filename);

InputStream in = response.readEntity(InputStream.class);
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return (file);

} catch (IOException ioe) {
throw new GitLabApiException(ioe);
}
}

/**
* Compare branches, tags or commits. This can be accessed without authentication
* if the repository is publicly accessible.
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/gitlab4j/api/models/RepositoryArchiveParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.GitLabApiForm;

/**
* Params for getting file archive of the repository.
*/
public class RepositoryArchiveParams {

private String sha;
private String path;

/**
* Add param "The commit SHA to download".
*
* @param sha the commit SHA to download
* @return current params with sha
*/
public RepositoryArchiveParams withSha(String sha) {
this.sha = sha;
return this;
}

/**
* Add param "The subpath of the repository to download".
*
* @param path the subpath of the repository to download
* @return current params with path
*/
public RepositoryArchiveParams withPath(String path) {
this.path = path;
return this;
}

/**
* Get form with params.
*
* @return form with params
*/
public GitLabApiForm getForm() {
return new GitLabApiForm()
.withParam("sha", sha)
.withParam("path", path);
}


}
Loading
Loading