Skip to content

Commit 7eb08e7

Browse files
PhilippeViennegmessner
authored andcommitted
Add Container Registry API (#368)
* Add Container Registry API regarding #367
1 parent 1b352ba commit 7eb08e7

File tree

4 files changed

+392
-0
lines changed

4 files changed

+392
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Greg Messner <[email protected]>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package org.gitlab4j.api;
25+
26+
import org.gitlab4j.api.models.RegistryRepository;
27+
import org.gitlab4j.api.models.RegistryRepositoryTag;
28+
29+
import javax.ws.rs.core.GenericType;
30+
import javax.ws.rs.core.Response;
31+
import java.util.List;
32+
import java.util.stream.Stream;
33+
34+
/**
35+
* <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>
37+
*/
38+
public class ContainerRegistryApi extends AbstractApi {
39+
40+
public ContainerRegistryApi(GitLabApi gitLabApi) {
41+
super(gitLabApi);
42+
}
43+
44+
/**
45+
* Get a list of registry repositories in a project.
46+
*
47+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
48+
*
49+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
50+
* @return a list of pages in the project's registry repositories
51+
* @throws GitLabApiException if any exception occurs
52+
*/
53+
public List<RegistryRepository> getRepositories(Object projectIdOrPath) throws GitLabApiException {
54+
return (getRepositories(projectIdOrPath, getDefaultPerPage()).all());
55+
}
56+
57+
/**
58+
* Get a list of registry repositories in a project.
59+
*
60+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
61+
*
62+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
63+
* @param page the page to get
64+
* @param perPage the number of Package instances per page
65+
* @return a list of registry repositories for the specified range
66+
* @throws GitLabApiException if any exception occurs
67+
*/
68+
public List<RegistryRepository> getRepositories(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
69+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
70+
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories");
71+
return response.readEntity(new GenericType<List<RegistryRepository>>() {
72+
});
73+
}
74+
75+
/**
76+
* Get a list of registry repositories in a project.
77+
*
78+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
79+
*
80+
* @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
82+
* @return a Pager of registry repositories for the specified range
83+
* @throws GitLabApiException if any exception occurs
84+
*/
85+
public Pager<RegistryRepository> getRepositories(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
86+
return (new Pager<>(this, RegistryRepository.class, itemsPerPage, null,
87+
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories"));
88+
}
89+
90+
/**
91+
* Get a list of registry repositories in a project.
92+
*
93+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories</code></pre>
94+
*
95+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
96+
* @return a Stream of pages in the project's registry repositories
97+
* @throws GitLabApiException if any exception occurs
98+
*/
99+
public Stream<RegistryRepository> getRepositoriesStream(Object projectIdOrPath) throws GitLabApiException {
100+
return (getRepositories(projectIdOrPath, getDefaultPerPage()).stream());
101+
}
102+
103+
/**
104+
* Delete a repository in registry.
105+
* <p>
106+
* This operation is executed asynchronously and might take some time to get executed.
107+
*
108+
* <pre><code>GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id</code></pre>
109+
*
110+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
111+
* @param repositoryId the ID of registry repository
112+
* @throws GitLabApiException if any exception occurs
113+
*/
114+
public void deleteRepository(Object projectIdOrPath, Integer repositoryId) throws GitLabApiException {
115+
116+
if (repositoryId == null) {
117+
throw new RuntimeException("packageId cannot be null");
118+
}
119+
120+
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId);
121+
}
122+
123+
/**
124+
* Get a list of tags for given registry repository.
125+
*
126+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags</code></pre>
127+
*
128+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
129+
* @param repositoryId the ID of registry repository
130+
* @return a list of Repository Tags for the specified repository ID
131+
* @throws GitLabApiException if any exception occurs
132+
*/
133+
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+
});
138+
}
139+
140+
/**
141+
* Get a list of tags for given registry repository.
142+
*
143+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags</code></pre>
144+
*
145+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
146+
* @param repositoryId the ID of registry repository
147+
* @return a list of Repository Tags for the specified repository ID
148+
* @throws GitLabApiException if any exception occurs
149+
*/
150+
public Stream<RegistryRepositoryTag> getRepositoryTagsStream(Object projectIdOrPath, Integer repositoryId) throws GitLabApiException {
151+
return getRepositoryTags(projectIdOrPath, repositoryId).stream();
152+
}
153+
154+
/**
155+
* Get details of a registry repository tag.
156+
*
157+
* <pre><code>GitLab Endpoint: GET /projects/:id/registry/repositories/:repository_id/tags/:tag_name</code></pre>
158+
*
159+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
160+
* @param repositoryId the ID of registry repository
161+
* @param tagName the name of tag
162+
* @return the Repository Tag for the specified repository ID
163+
* @throws GitLabApiException if any exception occurs
164+
*/
165+
public RegistryRepositoryTag getRepositoryTag(Object projectIdOrPath, Integer repositoryId, String tagName) throws GitLabApiException {
166+
Response response = get(Response.Status.OK, null,
167+
"projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags", tagName);
168+
return response.readEntity(new GenericType<RegistryRepositoryTag>() {
169+
});
170+
}
171+
172+
/**
173+
* Delete a registry repository tag.
174+
*
175+
* <pre><code>GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id/tags/:tag_name</code></pre>
176+
*
177+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
178+
* @param repositoryId the ID of registry repository
179+
* @throws GitLabApiException if any exception occurs
180+
*/
181+
public void deleteRepositoryTag(Object projectIdOrPath, Integer repositoryId, String tagName) throws GitLabApiException {
182+
183+
if (repositoryId == null) {
184+
throw new RuntimeException("packageId cannot be null");
185+
}
186+
187+
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "registry", "repositories", repositoryId, "tags", tagName);
188+
}
189+
190+
/**
191+
* Delete repository tags in bulk based on given criteria.
192+
*
193+
* <pre><code>GitLab Endpoint: DELETE /projects/:id/registry/repositories/:repository_id/tags</code></pre>
194+
* <p>
195+
* This API call performs the following operations:
196+
* <ol>
197+
* <li>It orders all tags by creation date. The creation date is the time of the manifest creation,
198+
* not the time of tag push.</li>
199+
* <li>It removes only the tags matching the given name_regex.</li>
200+
* <li>It never removes the tag named latest.</li>
201+
* <li>It keeps N latest matching tags (if keep_n is specified).</li>
202+
* <li>It only removes tags that are older than X amount of time (if older_than is specified).</li>
203+
* <li>It schedules the asynchronous job to be executed in the background.</li>
204+
* </ol>
205+
* <p>
206+
* These operations are executed asynchronously and it might take time to get executed. You can run this at most
207+
* once an hour for a given container repository.
208+
*
209+
* @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
214+
* <code>1h</code>, <code>1d</code>, <code>1month</code>.
215+
* @throws GitLabApiException if any exception occurs
216+
*/
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+
}
225+
226+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public String getApiNamespace() {
5656
private AwardEmojiApi awardEmojiApi;
5757
private BoardsApi boardsApi;
5858
private CommitsApi commitsApi;
59+
private ContainerRegistryApi containerRegistryApi;
5960
private DiscussionsApi discussionsApi;
6061
private DeployKeysApi deployKeysApi;
6162
private EpicsApi epicsApi;
@@ -989,6 +990,25 @@ public CommitsApi getCommitsApi() {
989990
return (commitsApi);
990991
}
991992

993+
/**
994+
* Gets the ContainerRegistryApi instance owned by this GitLabApi instance. The ContainerRegistryApi is used
995+
* to perform all Docker Registry related API calls.
996+
*
997+
* @return the ContainerRegistryApi instance owned by this GitLabApi instance
998+
*/
999+
public ContainerRegistryApi getContainerRegistryApi() {
1000+
1001+
if (containerRegistryApi == null) {
1002+
synchronized (this) {
1003+
if (containerRegistryApi == null) {
1004+
containerRegistryApi = new ContainerRegistryApi(this);
1005+
}
1006+
}
1007+
}
1008+
1009+
return (containerRegistryApi);
1010+
}
1011+
9921012
/**
9931013
* Gets the DeployKeysApi instance owned by this GitLabApi instance. The DeployKeysApi is used
9941014
* to perform all deploy key related API calls.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
import java.util.Date;
6+
7+
public class RegistryRepository {
8+
9+
private Integer id;
10+
private String name;
11+
private String path;
12+
private String location;
13+
private Date createdAt;
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getPath() {
32+
return path;
33+
}
34+
35+
public void setPath(String path) {
36+
this.path = path;
37+
}
38+
39+
public String getLocation() {
40+
return location;
41+
}
42+
43+
public void setLocation(String location) {
44+
this.location = location;
45+
}
46+
47+
public Date getCreatedAt() {
48+
return createdAt;
49+
}
50+
51+
public void setCreatedAt(Date createdAt) {
52+
this.createdAt = createdAt;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return (JacksonJson.toJsonString(this));
58+
}
59+
60+
}

0 commit comments

Comments
 (0)