org.aoju.bus.gitlab.ContainerRegistryApi Maven / Gradle / Ivy
/*********************************************************************************
* *
* The MIT License (MIT) *
* *
* Copyright (c) 2015-2022 aoju.org Greg Messner and other contributors. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
* *
********************************************************************************//*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.aoju.bus.gitlab;
import org.aoju.bus.gitlab.models.RegistryRepository;
import org.aoju.bus.gitlab.models.RegistryRepositoryTag;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
/**
* This class implements the client side API for the GitLab Container Registry API.
* See Container Registry API at GitLab
* for more information.
*/
public class ContainerRegistryApi extends AbstractApi {
public ContainerRegistryApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of registry repositories in a project.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @return a list of pages in the project's registry repositories
* @throws GitLabApiException if any exception occurs
*/
public List getRepositories(Object projectIdOrPath) throws GitLabApiException {
return (getRepositories(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a list of registry repositories in a project that fall within the specified page parameters.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param page the page to get
* @param perPage the number of Package instances per page
* @return a list of registry repositories for the specified range
* @throws GitLabApiException if any exception occurs
*/
public List getRepositories(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories");
return response.readEntity(new GenericType>() {
});
}
/**
* Get a Pager of registry repositories in a project.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param itemsPerPage the number of RegistryRepository instances per page
* @return a Pager of registry repositories for the specified range
* @throws GitLabApiException if any exception occurs
*/
public Pager getRepositories(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<>(this, RegistryRepository.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories"));
}
/**
* Get a Stream of registry repositories in a project.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @return a Stream of pages in the project's registry repositories
* @throws GitLabApiException if any exception occurs
*/
public Stream getRepositoriesStream(Object projectIdOrPath) throws GitLabApiException {
return (getRepositories(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Delete a repository in registry.
*
* This operation is executed asynchronously and might take some time to get executed.
*
*
GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @throws GitLabApiException if any exception occurs
*/
public void deleteRepository(Object projectIdOrPath, Long repositoryId) throws GitLabApiException {
if (repositoryId == null) {
throw new RuntimeException("repositoryId cannot be null");
}
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId);
}
/**
* Get a list of tags for given registry repository.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @return a list of Repository Tags for the specified repository ID
* @throws GitLabApiException if any exception occurs
*/
public List getRepositoryTags(Object projectIdOrPath, Long repositoryId) throws GitLabApiException {
return getRepositoryTags(projectIdOrPath, repositoryId, getDefaultPerPage()).all();
}
/**
* Get a Pager of tags for given registry repository.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @param itemsPerPage the number of RegistryRepositoryTag instances per page
* @return a Pager of Repository Tags for the specified repository ID
* @throws GitLabApiException if any exception occurs
*/
public Pager getRepositoryTags(Object projectIdOrPath, Long repositoryId, int itemsPerPage) throws GitLabApiException {
return (new Pager<>(this, RegistryRepositoryTag.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags"));
}
/**
* Get a Stream of tags for given registry repository.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @return a list of Repository Tags for the specified repository ID
* @throws GitLabApiException if any exception occurs
*/
public Stream getRepositoryTagsStream(Object projectIdOrPath, Long repositoryId) throws GitLabApiException {
return getRepositoryTags(projectIdOrPath, repositoryId, getDefaultPerPage()).stream();
}
/**
* Get details of a registry repository tag.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags/:tag_name
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @param tagName the name of tag
* @return the Repository Tag for the specified repository ID
* @throws GitLabApiException if any exception occurs
*/
public RegistryRepositoryTag getRepositoryTag(Object projectIdOrPath, Long repositoryId, String tagName) throws GitLabApiException {
Response response = get(Response.Status.OK, null,
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags", tagName);
return response.readEntity(new GenericType() {
});
}
/**
* Get details of a registry repository tag as the value of an Optional.
*
* GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags/:tag_name
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @param tagName the name of tag
* @return the Repository Tag for the specified repository ID as the value of the Optional
*/
public Optional getOptionalRepositoryTag(Object projectIdOrPath, Long repositoryId, String tagName) {
try {
return (Optional.ofNullable(getRepositoryTag(projectIdOrPath, repositoryId, tagName)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Delete a registry repository tag.
*
* GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id/tags/:tag_name
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @param tagName the name of the tag to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteRepositoryTag(Object projectIdOrPath, Long repositoryId, String tagName) throws GitLabApiException {
if (repositoryId == null) {
throw new RuntimeException("repositoryId cannot be null");
}
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags", tagName);
}
/**
* Delete repository tags in bulk based on given criteria.
*
* GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id/tags
*
* This API call performs the following operations:
*
* - It orders all tags by creation date. The creation date is the time of the manifest creation,
* not the time of tag push.
* - It removes only the tags matching the given name_regex.
* - It never removes the tag named latest.
* - It keeps N latest matching tags (if keep_n is specified).
* - It only removes tags that are older than X amount of time (if older_than is specified).
* - It schedules the asynchronous job to be executed in the background.
*
*
* These operations are executed asynchronously and it might take time to get executed. You can run this at most
* once an hour for a given container repository.
*
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
* @param repositoryId the ID of registry repository
* @param nameRegex the regex of the name to delete. To delete all tags specify .*
.
* @param keepN the amount of latest tags of given name to keep.
* @param olderThan tags to delete that are older than the given time, written in human readable form
* 1h
, 1d
, 1month
.
* @throws GitLabApiException if any exception occurs
*/
public void deleteRepositoryTags(Object projectIdOrPath, Long repositoryId, String nameRegex, Integer keepN, String olderThan) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("name_regex", nameRegex, true)
.withParam("keep_n", keepN)
.withParam("older_than", olderThan);
delete(Response.Status.NO_CONTENT, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags");
}
}