getProtectedTagsStream(Object projectIdOrPath) throws GitLabApiException {
+ return (getProtectedTags(projectIdOrPath, getDefaultPerPage()).stream());
+ }
+
+ /**
+ * Gets a single protected tag or wildcard protected tag
+ *
+ * GitLab Endpoint: GET /projects/:id/protected_tags/:name
+ *
+ * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
+ * @param name the name of the tag or wildcard
+ * @return a ProtectedTag instance with info on the specified protected tag
+ * @throws GitLabApiException if any exception occurs
+ */
+ public ProtectedTag getProtectedTag(Object projectIdOrPath, String name) throws GitLabApiException {
+ Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags", urlEncode(name));
+ return (response.readEntity(ProtectedTag.class));
+ }
+
+ /**
+ * Get an Optional instance holding a protected tag or wildcard protected tag.
+ *
+ * GitLab Endpoint: GET /projects/:id/protected_tags/:name
+ *
+ * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
+ * @param name the name of the tag or wildcard
+ * @return an Optional instance with the specified protected tag as the value
+ * @throws GitLabApiException if any exception occurs
+ */
+ public Optional getOptionalProtectedTag(Object projectIdOrPath, String name) throws GitLabApiException {
+ try {
+ return (Optional.ofNullable(getProtectedTag(projectIdOrPath, name)));
+ } catch (GitLabApiException glae) {
+ return (GitLabApi.createOptionalFromException(glae));
+ }
+ }
+
+ /**
+ * Protects a single repository tag or several project repository tags using a wildcard protected tag.
+ *
+ * GitLab Endpoint: GET /projects/:id/protected_tags
+ *
+ * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
+ * @param name the name of the tag or wildcard
+ * @param createAccessLevel the access level allowed to create
+ * @return a ProtectedTag instance
+ * @throws GitLabApiException if any exception occurs
+ */
+ public ProtectedTag protectTag(Object projectIdOrPath, String name, AccessLevel createAccessLevel) throws GitLabApiException {
+ Form formData = new GitLabApiForm().withParam("name", name, true).withParam("create_access_Level", createAccessLevel);
+ Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags");
+ return (response.readEntity(ProtectedTag.class));
+ }
+
+ /**
+ * Unprotects the given protected tag or wildcard protected tag.
+ *
+ * GitLab Endpoint: GET /projects/:id/protected_tags/:name
+ *
+ * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
+ * @param name the name of the tag or wildcard
+ * @throws GitLabApiException if any exception occurs
+ */
+ public void unprotectTag(Object projectIdOrPath, String name) throws GitLabApiException {
+ delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags", urlEncode(name));
+ }
}
diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
index 735a6565550d793e0a4239a156e78b68aa033143..011e4523ec595752e7f98777f9355ee6853abfcb 100644
--- a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
+++ b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
@@ -75,6 +75,7 @@ import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch;
+import org.gitlab4j.api.models.ProtectedTag;
import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.RegistryRepository;
import org.gitlab4j.api.models.Runner;
@@ -338,6 +339,12 @@ public class TestGitLabApiBeans {
assertTrue(compareJson(protectedBranch, "protected-branch.json"));
}
+ @Test
+ public void testProtectedTags() throws Exception {
+ List protectedTags = unmarshalResourceList(ProtectedTag.class, "protected-tags.json");
+ assertTrue(compareJson(protectedTags, "protected-tags.json"));
+ }
+
@Test
public void testPushRule() throws Exception {
PushRules pushRule = unmarshalResource(PushRules.class, "push-rule.json");
diff --git a/src/test/java/org/gitlab4j/api/TestTagsApi.java b/src/test/java/org/gitlab4j/api/TestTagsApi.java
index 0f1c65c3216292c6ada18cb4dfe3963492f50193..ac15f83a3ae67de1abbf24d49c7155d1bd341b13 100644
--- a/src/test/java/org/gitlab4j/api/TestTagsApi.java
+++ b/src/test/java/org/gitlab4j/api/TestTagsApi.java
@@ -9,7 +9,9 @@ import static org.junit.Assume.assumeTrue;
import java.util.List;
import java.util.Optional;
+import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Project;
+import org.gitlab4j.api.models.ProtectedTag;
import org.gitlab4j.api.models.Release;
import org.gitlab4j.api.models.Tag;
import org.junit.AfterClass;
@@ -24,6 +26,7 @@ public class TestTagsApi extends AbstractIntegrationTest {
private static final String TEST_TAG_NAME_1 = "test-tag-1";
private static final String TEST_TAG_NAME_0 = "test-tag-0";
private static final String TEST_TAG_WITH_SLASH = "env/test-tag";
+ private static final String TEST_PROTECTED_TAG = "protected-tag";
private static GitLabApi gitLabApi;
private static Project testProject;
@@ -39,23 +42,22 @@ public class TestTagsApi extends AbstractIntegrationTest {
gitLabApi = baseTestSetup();
testProject = getTestProject();
+ deleteTestTags();
+
if (testProject != null) {
try {
gitLabApi.getTagsApi().createTag(testProject, TEST_TAG_NAME_0, "master");
} catch (Exception ignore) {}
-
- try {
- gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_1);
- } catch (Exception ignore) {}
-
- try {
- gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
- } catch (Exception ignore) {}
}
}
@AfterClass
public static void tearDown() {
+ deleteTestTags();
+ }
+
+ private static final void deleteTestTags() {
+
if (testProject != null) {
try {
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_0);
@@ -68,6 +70,14 @@ public class TestTagsApi extends AbstractIntegrationTest {
try {
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
} catch (Exception ignore) {}
+
+ try {
+ gitLabApi.getTagsApi().unprotectTag(testProject, TEST_PROTECTED_TAG);
+ } catch (Exception ignore) {}
+
+ try {
+ gitLabApi.getTagsApi().deleteTag(testProject, TEST_PROTECTED_TAG);
+ } catch (Exception ignore) {}
}
}
@@ -147,4 +157,28 @@ public class TestTagsApi extends AbstractIntegrationTest {
assertNotNull(testTag);
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
}
+
+ @Test
+ public void testProtectedTags() throws GitLabApiException {
+
+ Tag testTag = gitLabApi.getTagsApi().createTag(testProject, TEST_PROTECTED_TAG, "master");
+ assertNotNull(testTag);
+ assertEquals(TEST_PROTECTED_TAG, testTag.getName());
+
+ ProtectedTag protectedTag = gitLabApi.getTagsApi().protectTag(testProject, TEST_PROTECTED_TAG, AccessLevel.DEVELOPER);
+ assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());
+
+ List tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
+ assertTrue(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
+
+ Optional optionalTag = gitLabApi.getTagsApi().getOptionalProtectedTag(testProject, TEST_PROTECTED_TAG);
+ assertTrue(optionalTag.isPresent());
+ assertEquals(TEST_PROTECTED_TAG, optionalTag.get().getName());
+
+ gitLabApi.getTagsApi().unprotectTag(testProject, TEST_PROTECTED_TAG);
+ assertEquals(TEST_PROTECTED_TAG, protectedTag.getName());
+
+ tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
+ assertFalse(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
+ }
}