Skip to content

Commit 18ff06d

Browse files
committed
Added deleteRepositoryTags() and getOptionalRepositoryTag() (#368).
1 parent b1ce952 commit 18ff06d

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

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

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323

2424
package org.gitlab4j.api;
2525

26-
import org.gitlab4j.api.models.RegistryRepository;
27-
import org.gitlab4j.api.models.RegistryRepositoryTag;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import java.util.stream.Stream;
2829

2930
import javax.ws.rs.core.GenericType;
3031
import javax.ws.rs.core.Response;
31-
import java.util.List;
32-
import java.util.stream.Stream;
32+
33+
import org.gitlab4j.api.models.RegistryRepository;
34+
import org.gitlab4j.api.models.RegistryRepositoryTag;
3335

3436
/**
3537
* <p>This class implements the client side API for the GitLab Container Registry API.
36-
* See <a href="https://docs.gitlab.com/ee/api/container_registry.html">Container Registry API at GitLab</a> for more information.</p>
38+
* See <a href="https://docs.gitlab.com/ee/api/container_registry.html">Container Registry API at GitLab</a>
39+
* for more information.</p>
3740
*/
3841
public class ContainerRegistryApi extends AbstractApi {
3942

@@ -55,7 +58,7 @@ public List<RegistryRepository> getRepositories(Object projectIdOrPath) throws G
5558
}
5659

5760
/**
58-
* Get a list of registry repositories in a project.
61+
* Get a list of registry repositories in a project that fall within the specified page parameters.
5962
*
6063
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
6164
*
@@ -73,12 +76,12 @@ public List<RegistryRepository> getRepositories(Object projectIdOrPath, int page
7376
}
7477

7578
/**
76-
* Get a list of registry repositories in a project.
79+
* Get a Pager of registry repositories in a project.
7780
*
7881
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
7982
*
8083
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
81-
* @param itemsPerPage the number of Package instances per page
84+
* @param itemsPerPage the number of RegistryRepository instances per page
8285
* @return a Pager of registry repositories for the specified range
8386
* @throws GitLabApiException if any exception occurs
8487
*/
@@ -88,7 +91,7 @@ public Pager<RegistryRepository> getRepositories(Object projectIdOrPath, int ite
8891
}
8992

9093
/**
91-
* Get a list of registry repositories in a project.
94+
* Get a Stream of registry repositories in a project.
9295
*
9396
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
9497
*
@@ -114,7 +117,7 @@ public Stream<RegistryRepository> getRepositoriesStream(Object projectIdOrPath)
114117
public void deleteRepository(Object projectIdOrPath, Integer repositoryId) throws GitLabApiException {
115118

116119
if (repositoryId == null) {
117-
throw new RuntimeException("packageId cannot be null");
120+
throw new RuntimeException("repositoryId cannot be null");
118121
}
119122

120123
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId);
@@ -126,29 +129,42 @@ public void deleteRepository(Object projectIdOrPath, Integer repositoryId) throw
126129
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags</code></pre>
127130
*
128131
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
129-
* @param repositoryId the ID of registry repository
132+
* @param repositoryId the ID of registry repository
130133
* @return a list of Repository Tags for the specified repository ID
131134
* @throws GitLabApiException if any exception occurs
132135
*/
133136
public List<RegistryRepositoryTag> getRepositoryTags(Object projectIdOrPath, Integer repositoryId) throws GitLabApiException {
134-
Response response = get(Response.Status.OK, null,
135-
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags");
136-
return response.readEntity(new GenericType<List<RegistryRepositoryTag>>() {
137-
});
137+
return getRepositoryTags(projectIdOrPath, repositoryId, getDefaultPerPage()).all();
138138
}
139139

140140
/**
141-
* Get a list of tags for given registry repository.
141+
* Get a Pager of tags for given registry repository.
142142
*
143143
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags</code></pre>
144144
*
145145
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
146-
* @param repositoryId the ID of registry repository
146+
* @param repositoryId the ID of registry repository
147+
* @param itemsPerPage the number of RegistryRepositoryTag instances per page
148+
* @return a Pager of Repository Tags for the specified repository ID
149+
* @throws GitLabApiException if any exception occurs
150+
*/
151+
public Pager<RegistryRepositoryTag> getRepositoryTags(Object projectIdOrPath, Integer repositoryId, int itemsPerPage) throws GitLabApiException {
152+
return (new Pager<>(this, RegistryRepositoryTag.class, itemsPerPage, null,
153+
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags"));
154+
}
155+
156+
/**
157+
* Get a Stream of tags for given registry repository.
158+
*
159+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags</code></pre>
160+
*
161+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
162+
* @param repositoryId the ID of registry repository
147163
* @return a list of Repository Tags for the specified repository ID
148164
* @throws GitLabApiException if any exception occurs
149165
*/
150166
public Stream<RegistryRepositoryTag> getRepositoryTagsStream(Object projectIdOrPath, Integer repositoryId) throws GitLabApiException {
151-
return getRepositoryTags(projectIdOrPath, repositoryId).stream();
167+
return getRepositoryTags(projectIdOrPath, repositoryId, getDefaultPerPage()).stream();
152168
}
153169

154170
/**
@@ -169,19 +185,38 @@ public RegistryRepositoryTag getRepositoryTag(Object projectIdOrPath, Integer re
169185
});
170186
}
171187

188+
/**
189+
* Get details of a registry repository tag as the value of an Optional.
190+
*
191+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags/:tag_name</code></pre>
192+
*
193+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
194+
* @param repositoryId the ID of registry repository
195+
* @param tagName the name of tag
196+
* @return the Repository Tag for the specified repository ID as the value of the Optional
197+
*/
198+
public Optional<RegistryRepositoryTag> getOptionalRepositoryTag(Object projectIdOrPath, Integer repositoryId, String tagName) {
199+
try {
200+
return (Optional.ofNullable(getRepositoryTag(projectIdOrPath, repositoryId, tagName)));
201+
} catch (GitLabApiException glae) {
202+
return (GitLabApi.createOptionalFromException(glae));
203+
}
204+
}
205+
172206
/**
173207
* Delete a registry repository tag.
174208
*
175209
* <pre><code>GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id/tags/:tag_name</code></pre>
176210
*
177211
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
178-
* @param repositoryId the ID of registry repository
212+
* @param repositoryId the ID of registry repository
213+
* @param tagName the name of the tag to delete
179214
* @throws GitLabApiException if any exception occurs
180215
*/
181216
public void deleteRepositoryTag(Object projectIdOrPath, Integer repositoryId, String tagName) throws GitLabApiException {
182217

183218
if (repositoryId == null) {
184-
throw new RuntimeException("packageId cannot be null");
219+
throw new RuntimeException("repositoryId cannot be null");
185220
}
186221

187222
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags", tagName);
@@ -207,20 +242,20 @@ public void deleteRepositoryTag(Object projectIdOrPath, Integer repositoryId, St
207242
* once an hour for a given container repository.
208243
*
209244
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
210-
* @param repositoryId the ID of registry repository
211-
* @param nameRegex The regex of the name to delete. To delete all tags specify <code>.*</code>.
212-
* @param keepN The amount of latest tags of given name to keep.
213-
* @param olderThan Tags to delete that are older than the given time, written in human readable form
245+
* @param repositoryId the ID of registry repository
246+
* @param nameRegex the regex of the name to delete. To delete all tags specify <code>.*</code>.
247+
* @param keepN the amount of latest tags of given name to keep.
248+
* @param olderThan tags to delete that are older than the given time, written in human readable form
214249
* <code>1h</code>, <code>1d</code>, <code>1month</code>.
215250
* @throws GitLabApiException if any exception occurs
216251
*/
217-
@SuppressWarnings({"RedundantThrows", "unused"})
218-
public void deleteRepositoryTagBulk(Object projectIdOrPath, Integer repositoryId, String nameRegex, Integer keepN, String olderThan) throws GitLabApiException {
219-
/*
220-
This function is not implemented because we need the library to support FormData on DELETE requests.
221-
See: https://docs.gitlab.com/ee/api/container_registry.html#delete-repository-tags-in-bulk
222-
*/
223-
throw new UnsupportedOperationException("Not implemented yet");
224-
}
252+
public void deleteRepositoryTags(Object projectIdOrPath, Integer repositoryId, String nameRegex, Integer keepN, String olderThan) throws GitLabApiException {
225253

254+
GitLabApiForm formData = new GitLabApiForm()
255+
.withParam("name_regex", nameRegex, true)
256+
.withParam("keep_n", keepN)
257+
.withParam("older_than", olderThan);
258+
259+
delete(Response.Status.NO_CONTENT, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags");
260+
}
226261
}

0 commit comments

Comments
 (0)