Commit f4c0750e authored by Greg Messner's avatar Greg Messner
Browse files

Added support for Protected Tags API (#389).

parent 7ad13e6b
...@@ -11,14 +11,16 @@ import javax.ws.rs.core.GenericType; ...@@ -11,14 +11,16 @@ import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion; 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.Release;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.utils.FileUtils; import org.gitlab4j.api.utils.FileUtils;
/** /**
* This class provides an entry point to all the GitLab Tags API calls. * This class provides an entry point to all the GitLab Tags and Protected Tags API calls.
* * @see <a href="https://docs.gitlab.com/ce/api/tags.html">Tags API at GitLab</a>
* See https://docs.gitlab.com/ce/api/tags.html for more information on the GitLab Tags API. * @see <a href="https://docs.gitlab.com/ce/api/protected_tags.html">Protected Tags API at GitLab</a>
*/ */
public class TagsApi extends AbstractApi { public class TagsApi extends AbstractApi {
...@@ -63,7 +65,7 @@ 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 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 * @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 * @throws GitLabApiException if any exception occurs
*/ */
public Pager<Tag> getTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { public Pager<Tag> getTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
...@@ -237,4 +239,123 @@ public class TagsApi extends AbstractApi { ...@@ -237,4 +239,123 @@ public class TagsApi extends AbstractApi {
return (response.readEntity(Release.class)); return (response.readEntity(Release.class));
} }
/**
* Gets a list of protected tags from a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
*
* @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<ProtectedTag> 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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
*
* @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<ProtectedTag> 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<List<ProtectedTag>>() { }));
}
/**
* Get a Pager of protected tags for a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
*
* @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<ProtectedTag> getProtectedTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<ProtectedTag>(this, ProtectedTag.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags"));
}
/**
* Get a Stream of protected tags for a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
*
* @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<ProtectedTag> getProtectedTagsStream(Object projectIdOrPath) throws GitLabApiException {
return (getProtectedTags(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Gets a single protected tag or wildcard protected tag
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
*
* @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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
*
* @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<ProtectedTag> 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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
*
* @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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags/:name</code></pre>
*
* @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));
}
} }
...@@ -75,6 +75,7 @@ import org.gitlab4j.api.models.Project; ...@@ -75,6 +75,7 @@ import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser; import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch; import org.gitlab4j.api.models.ProtectedBranch;
import org.gitlab4j.api.models.ProtectedTag;
import org.gitlab4j.api.models.PushRules; import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.RegistryRepository; import org.gitlab4j.api.models.RegistryRepository;
import org.gitlab4j.api.models.Runner; import org.gitlab4j.api.models.Runner;
...@@ -338,6 +339,12 @@ public class TestGitLabApiBeans { ...@@ -338,6 +339,12 @@ public class TestGitLabApiBeans {
assertTrue(compareJson(protectedBranch, "protected-branch.json")); assertTrue(compareJson(protectedBranch, "protected-branch.json"));
} }
@Test
public void testProtectedTags() throws Exception {
List<ProtectedTag> protectedTags = unmarshalResourceList(ProtectedTag.class, "protected-tags.json");
assertTrue(compareJson(protectedTags, "protected-tags.json"));
}
@Test @Test
public void testPushRule() throws Exception { public void testPushRule() throws Exception {
PushRules pushRule = unmarshalResource(PushRules.class, "push-rule.json"); PushRules pushRule = unmarshalResource(PushRules.class, "push-rule.json");
......
...@@ -9,7 +9,9 @@ import static org.junit.Assume.assumeTrue; ...@@ -9,7 +9,9 @@ import static org.junit.Assume.assumeTrue;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProtectedTag;
import org.gitlab4j.api.models.Release; import org.gitlab4j.api.models.Release;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.junit.AfterClass; import org.junit.AfterClass;
...@@ -24,6 +26,7 @@ public class TestTagsApi extends AbstractIntegrationTest { ...@@ -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_1 = "test-tag-1";
private static final String TEST_TAG_NAME_0 = "test-tag-0"; 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_TAG_WITH_SLASH = "env/test-tag";
private static final String TEST_PROTECTED_TAG = "protected-tag";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
private static Project testProject; private static Project testProject;
...@@ -39,23 +42,22 @@ public class TestTagsApi extends AbstractIntegrationTest { ...@@ -39,23 +42,22 @@ public class TestTagsApi extends AbstractIntegrationTest {
gitLabApi = baseTestSetup(); gitLabApi = baseTestSetup();
testProject = getTestProject(); testProject = getTestProject();
deleteTestTags();
if (testProject != null) { if (testProject != null) {
try { try {
gitLabApi.getTagsApi().createTag(testProject, TEST_TAG_NAME_0, "master"); gitLabApi.getTagsApi().createTag(testProject, TEST_TAG_NAME_0, "master");
} catch (Exception ignore) {} } 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 @AfterClass
public static void tearDown() { public static void tearDown() {
deleteTestTags();
}
private static final void deleteTestTags() {
if (testProject != null) { if (testProject != null) {
try { try {
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_0); gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_0);
...@@ -68,6 +70,14 @@ public class TestTagsApi extends AbstractIntegrationTest { ...@@ -68,6 +70,14 @@ public class TestTagsApi extends AbstractIntegrationTest {
try { try {
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH); gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
} catch (Exception ignore) {} } 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 { ...@@ -147,4 +157,28 @@ public class TestTagsApi extends AbstractIntegrationTest {
assertNotNull(testTag); assertNotNull(testTag);
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName()); 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<ProtectedTag> tags = gitLabApi.getTagsApi().getProtectedTags(testProject);
assertTrue(tags.stream().map(ProtectedTag::getName).anyMatch(s -> TEST_PROTECTED_TAG.equals(s)));
Optional<ProtectedTag> 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)));
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment