diff --git a/src/main/java/org/gitlab4j/api/TagsApi.java b/src/main/java/org/gitlab4j/api/TagsApi.java index 152f0ce237b397459a8ae05531db2337716ff3f1..debff59b06b0ee4aff7b5224eacef048a55e7d19 100644 --- a/src/main/java/org/gitlab4j/api/TagsApi.java +++ b/src/main/java/org/gitlab4j/api/TagsApi.java @@ -11,14 +11,16 @@ import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; import org.gitlab4j.api.GitLabApi.ApiVersion; +import org.gitlab4j.api.models.AccessLevel; +import org.gitlab4j.api.models.ProtectedTag; import org.gitlab4j.api.models.Release; import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.utils.FileUtils; /** - * This class provides an entry point to all the GitLab Tags API calls. - * - * See https://docs.gitlab.com/ce/api/tags.html for more information on the GitLab Tags API. + * This class provides an entry point to all the GitLab Tags and Protected Tags API calls. + * @see Tags API at GitLab + * @see Protected Tags API at GitLab */ public class TagsApi extends AbstractApi { @@ -63,7 +65,7 @@ public class TagsApi extends AbstractApi { * * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path * @param itemsPerPage the number of Project instances that will be fetched per page - * @return the list of tags for the specified project ID + * @return the Pager of tags for the specified project ID * @throws GitLabApiException if any exception occurs */ public Pager getTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { @@ -237,4 +239,123 @@ public class TagsApi extends AbstractApi { return (response.readEntity(Release.class)); } + /** + * Gets a list of protected tags from a project. + * + *
GitLab Endpoint: GET /projects/:id/protected_tags
+ * + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path + * @return a List of protected tags for the specified project ID + * @throws GitLabApiException if any exception occurs + */ + public List getProtectedTags(Object projectIdOrPath) throws GitLabApiException { + return (getProtectedTags(projectIdOrPath, getDefaultPerPage()).all()); + } + + /** + * Gets a list of protected tags from a project and in the specified page range. + * + *
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 page the page to get + * @param perPage the number of Tag instances per page + * @return a List of tags for the specified project ID and page range + * @throws GitLabApiException if any exception occurs + */ + public List getProtectedTags(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), + "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags"); + return (response.readEntity(new GenericType>() { })); + } + + /** + * Get a Pager of protected tags for a project. + * + *
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 itemsPerPage the number of Project instances that will be fetched per page + * @return the Pager of protected tags for the specified project ID + * @throws GitLabApiException if any exception occurs + */ + public Pager getProtectedTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { + return (new Pager(this, ProtectedTag.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags")); + } + + /** + * Get a Stream of protected tags for a project. + * + *
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 + * @return a Stream of protected tags for the specified project ID + * @throws GitLabApiException if any exception occurs + */ + public Stream 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))); + } }