Skip to content

Commit f4c0750

Browse files
committed
Added support for Protected Tags API (#389).
1 parent 7ad13e6 commit f4c0750

File tree

3 files changed

+174
-12
lines changed

3 files changed

+174
-12
lines changed

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

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
import javax.ws.rs.core.Response;
1212

1313
import org.gitlab4j.api.GitLabApi.ApiVersion;
14+
import org.gitlab4j.api.models.AccessLevel;
15+
import org.gitlab4j.api.models.ProtectedTag;
1416
import org.gitlab4j.api.models.Release;
1517
import org.gitlab4j.api.models.Tag;
1618
import org.gitlab4j.api.utils.FileUtils;
1719

1820
/**
19-
* This class provides an entry point to all the GitLab Tags API calls.
20-
*
21-
* See https://docs.gitlab.com/ce/api/tags.html for more information on the GitLab Tags API.
21+
* This class provides an entry point to all the GitLab Tags and Protected Tags API calls.
22+
* @see <a href="https://docs.gitlab.com/ce/api/tags.html">Tags API at GitLab</a>
23+
* @see <a href="https://docs.gitlab.com/ce/api/protected_tags.html">Protected Tags API at GitLab</a>
2224
*/
2325
public class TagsApi extends AbstractApi {
2426

@@ -63,7 +65,7 @@ public List<Tag> getTags(Object projectIdOrPath, int page, int perPage) throws G
6365
*
6466
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
6567
* @param itemsPerPage the number of Project instances that will be fetched per page
66-
* @return the list of tags for the specified project ID
68+
* @return the Pager of tags for the specified project ID
6769
* @throws GitLabApiException if any exception occurs
6870
*/
6971
public Pager<Tag> getTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
@@ -237,4 +239,123 @@ public Release updateRelease(Object projectIdOrPath, String tagName, String rele
237239
return (response.readEntity(Release.class));
238240
}
239241

242+
/**
243+
* Gets a list of protected tags from a project.
244+
*
245+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
246+
*
247+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
248+
* @return a List of protected tags for the specified project ID
249+
* @throws GitLabApiException if any exception occurs
250+
*/
251+
public List<ProtectedTag> getProtectedTags(Object projectIdOrPath) throws GitLabApiException {
252+
return (getProtectedTags(projectIdOrPath, getDefaultPerPage()).all());
253+
}
254+
255+
/**
256+
* Gets a list of protected tags from a project and in the specified page range.
257+
*
258+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
259+
*
260+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
261+
* @param page the page to get
262+
* @param perPage the number of Tag instances per page
263+
* @return a List of tags for the specified project ID and page range
264+
* @throws GitLabApiException if any exception occurs
265+
*/
266+
public List<ProtectedTag> getProtectedTags(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
267+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
268+
"projects", getProjectIdOrPath(projectIdOrPath), "protected_tags");
269+
return (response.readEntity(new GenericType<List<ProtectedTag>>() { }));
270+
}
271+
272+
/**
273+
* Get a Pager of protected tags for a project.
274+
*
275+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
276+
*
277+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
278+
* @param itemsPerPage the number of Project instances that will be fetched per page
279+
* @return the Pager of protected tags for the specified project ID
280+
* @throws GitLabApiException if any exception occurs
281+
*/
282+
public Pager<ProtectedTag> getProtectedTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
283+
return (new Pager<ProtectedTag>(this, ProtectedTag.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags"));
284+
}
285+
286+
/**
287+
* Get a Stream of protected tags for a project.
288+
*
289+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
290+
*
291+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
292+
* @return a Stream of protected tags for the specified project ID
293+
* @throws GitLabApiException if any exception occurs
294+
*/
295+
public Stream<ProtectedTag> getProtectedTagsStream(Object projectIdOrPath) throws GitLabApiException {
296+
return (getProtectedTags(projectIdOrPath, getDefaultPerPage()).stream());
297+
}
298+
299+
/**
300+
* Gets a single protected tag or wildcard protected tag
301+
*
302+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
303+
*
304+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
305+
* @param name the name of the tag or wildcard
306+
* @return a ProtectedTag instance with info on the specified protected tag
307+
* @throws GitLabApiException if any exception occurs
308+
*/
309+
public ProtectedTag getProtectedTag(Object projectIdOrPath, String name) throws GitLabApiException {
310+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags", urlEncode(name));
311+
return (response.readEntity(ProtectedTag.class));
312+
}
313+
314+
/**
315+
* Get an Optional instance holding a protected tag or wildcard protected tag.
316+
*
317+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
318+
*
319+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
320+
* @param name the name of the tag or wildcard
321+
* @return an Optional instance with the specified protected tag as the value
322+
* @throws GitLabApiException if any exception occurs
323+
*/
324+
public Optional<ProtectedTag> getOptionalProtectedTag(Object projectIdOrPath, String name) throws GitLabApiException {
325+
try {
326+
return (Optional.ofNullable(getProtectedTag(projectIdOrPath, name)));
327+
} catch (GitLabApiException glae) {
328+
return (GitLabApi.createOptionalFromException(glae));
329+
}
330+
}
331+
332+
/**
333+
* Protects a single repository tag or several project repository tags using a wildcard protected tag.
334+
*
335+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
336+
*
337+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
338+
* @param name the name of the tag or wildcard
339+
* @param createAccessLevel the access level allowed to create
340+
* @return a ProtectedTag instance
341+
* @throws GitLabApiException if any exception occurs
342+
*/
343+
public ProtectedTag protectTag(Object projectIdOrPath, String name, AccessLevel createAccessLevel) throws GitLabApiException {
344+
Form formData = new GitLabApiForm().withParam("name", name, true).withParam("create_access_Level", createAccessLevel);
345+
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags");
346+
return (response.readEntity(ProtectedTag.class));
347+
}
348+
349+
/**
350+
* Unprotects the given protected tag or wildcard protected tag.
351+
*
352+
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
353+
*
354+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
355+
* @param name the name of the tag or wildcard
356+
* @throws GitLabApiException if any exception occurs
357+
*/
358+
public void unprotectTag(Object projectIdOrPath, String name) throws GitLabApiException {
359+
delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags", urlEncode(name));
360+
}
240361
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import org.gitlab4j.api.models.ProjectHook;
7676
import org.gitlab4j.api.models.ProjectUser;
7777
import org.gitlab4j.api.models.ProtectedBranch;
78+
import org.gitlab4j.api.models.ProtectedTag;
7879
import org.gitlab4j.api.models.PushRules;
7980
import org.gitlab4j.api.models.RegistryRepository;
8081
import org.gitlab4j.api.models.Runner;
@@ -338,6 +339,12 @@ public void testProtectedBranch() throws Exception {
338339
assertTrue(compareJson(protectedBranch, "protected-branch.json"));
339340
}
340341

342+
@Test
343+
public void testProtectedTags() throws Exception {
344+
List<ProtectedTag> protectedTags = unmarshalResourceList(ProtectedTag.class, "protected-tags.json");
345+
assertTrue(compareJson(protectedTags, "protected-tags.json"));
346+
}
347+
341348
@Test
342349
public void testPushRule() throws Exception {
343350
PushRules pushRule = unmarshalResource(PushRules.class, "push-rule.json");

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.List;
1010
import java.util.Optional;
1111

12+
import org.gitlab4j.api.models.AccessLevel;
1213
import org.gitlab4j.api.models.Project;
14+
import org.gitlab4j.api.models.ProtectedTag;
1315
import org.gitlab4j.api.models.Release;
1416
import org.gitlab4j.api.models.Tag;
1517
import org.junit.AfterClass;
@@ -24,6 +26,7 @@ public class TestTagsApi extends AbstractIntegrationTest {
2426
private static final String TEST_TAG_NAME_1 = "test-tag-1";
2527
private static final String TEST_TAG_NAME_0 = "test-tag-0";
2628
private static final String TEST_TAG_WITH_SLASH = "env/test-tag";
29+
private static final String TEST_PROTECTED_TAG = "protected-tag";
2730

2831
private static GitLabApi gitLabApi;
2932
private static Project testProject;
@@ -39,23 +42,22 @@ public static void testSetup() {
3942
gitLabApi = baseTestSetup();
4043
testProject = getTestProject();
4144

45+
deleteTestTags();
46+
4247
if (testProject != null) {
4348
try {
4449
gitLabApi.getTagsApi().createTag(testProject, TEST_TAG_NAME_0, "master");
4550
} catch (Exception ignore) {}
46-
47-
try {
48-
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_1);
49-
} catch (Exception ignore) {}
50-
51-
try {
52-
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
53-
} catch (Exception ignore) {}
5451
}
5552
}
5653

5754
@AfterClass
5855
public static void tearDown() {
56+
deleteTestTags();
57+
}
58+
59+
private static final void deleteTestTags() {
60+
5961
if (testProject != null) {
6062
try {
6163
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_0);
@@ -68,6 +70,14 @@ public static void tearDown() {
6870
try {
6971
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
7072
} catch (Exception ignore) {}
73+
74+
try {
75+
gitLabApi.getTagsApi().unprotectTag(testProject, TEST_PROTECTED_TAG);
76+
} catch (Exception ignore) {}
77+
78+
try {
79+
gitLabApi.getTagsApi().deleteTag(testProject, TEST_PROTECTED_TAG);
80+
} catch (Exception ignore) {}
7181
}
7282
}
7383

@@ -147,4 +157,28 @@ public void testGetTagWithSpecialCharacersInTagName() throws GitLabApiException
147157
assertNotNull(testTag);
148158
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
149159
}
160+
161+
@Test
162+
public void testProtectedTags() throws GitLabApiException {
163+
164+
Tag testTag = gitLabApi.getTagsApi().createTag(testProject, TEST_PROTECTED_TAG, "master");
165+
assertNotNull(testTag);
166+
assertEquals(TEST_PROTECTED_TAG, testTag.getName());
167+
168+
ProtectedTag protectedTag = gitLabApi.getTagsApi().protectTag(testProject, TEST_PROTECTED_TAG, AccessLevel.DEVELOPER);
169+
assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());
170+
171+
List<ProtectedTag> tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
172+
assertTrue(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
173+
174+
Optional<ProtectedTag> optionalTag = gitLabApi.getTagsApi().getOptionalProtectedTag(testProject, TEST_PROTECTED_TAG);
175+
assertTrue(optionalTag.isPresent());
176+
assertEquals(TEST_PROTECTED_TAG, optionalTag.get().getName());
177+
178+
gitLabApi.getTagsApi().unprotectTag(testProject, TEST_PROTECTED_TAG);
179+
assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());
180+
181+
tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
182+
assertFalse(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
183+
}
150184
}

0 commit comments

Comments
 (0)