Commit 52483ef8 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for searching for tags (#417).

parent 1737dd48
...@@ -221,6 +221,28 @@ public interface Constants { ...@@ -221,6 +221,28 @@ public interface Constants {
} }
} }
/** Enum to use for ordering the results of getTags(). */
public enum TagOrderBy {
NAME, UPDATED;
private static JacksonJsonEnumHelper<TagOrderBy> enumHelper = new JacksonJsonEnumHelper<>(TagOrderBy.class);
@JsonCreator
public static TagOrderBy forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
/** Enum to use for specifying the scope when calling getPipelines(). */ /** Enum to use for specifying the scope when calling getPipelines(). */
public enum PipelineScope { public enum PipelineScope {
......
...@@ -85,6 +85,89 @@ public class TagsApi extends AbstractApi { ...@@ -85,6 +85,89 @@ public class TagsApi extends AbstractApi {
return (getTags(projectIdOrPath, getDefaultPerPage()).stream()); return (getTags(projectIdOrPath, getDefaultPerPage()).stream());
} }
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param orderBy return tags ordered by name or updated fields. Default is updated
* @param sortOrder return tags sorted in asc or desc order. Default is desc
* @param search return list of tags matching the search criteria
* @return the list of tags for the specified project ID
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.8
*/
public List<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search) throws GitLabApiException {
return (getTags(projectIdOrPath, orderBy, sortOrder, search, getDefaultPerPage()).all());
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order and in the specified page range.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param orderBy return tags ordered by name or updated fields. Default is updated
* @param sortOrder return tags sorted in asc or desc order. Default is desc
* @param search return list of tags matching the search criteria
* @param page the page to get
* @param perPage the number of Tag instances per page
* @return the list of tags for the specified project ID
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.8
*/
public List<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search, int page, int perPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("order_by", orderBy)
.withParam("sort", sortOrder)
.withParam("search", search)
.withParam(PAGE_PARAM, page)
.withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(),
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags");
return (response.readEntity(new GenericType<List<Tag>>() { }));
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param orderBy return tags ordered by name or updated fields. Default is updated
* @param sortOrder return tags sorted in asc or desc order. Default is desc
* @param search return list of tags matching the search criteria
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return the Pager of tags for the specified project ID
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.8
*/
public Pager<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("order_by", orderBy)
.withParam("sort", sortOrder)
.withParam("search", search);
return (new Pager<Tag>(this, Tag.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"));
}
/**
* Get a Stream of repository tags from a project, sorted by name in reverse alphabetical order.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param orderBy return tags ordered by name or updated fields. Default is updated
* @param sortOrder return tags sorted in asc or desc order. Default is desc
* @param search return list of tags matching the search criteria
* @return a Stream of tags for the specified project ID
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.8
*/
public Stream<Tag> getTagsStream(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search) throws GitLabApiException {
return (getTags(projectIdOrPath, orderBy, sortOrder, search, getDefaultPerPage()).stream());
}
/** /**
* Get a specific repository tag determined by its name. * Get a specific repository tag determined by its name.
* *
......
package org.gitlab4j.api; package org.gitlab4j.api;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -9,6 +10,7 @@ import static org.junit.Assume.assumeTrue; ...@@ -9,6 +10,7 @@ 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.Constants.SortOrder;
import org.gitlab4j.api.models.AccessLevel; 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.ProtectedTag;
...@@ -158,6 +160,35 @@ public class TestTagsApi extends AbstractIntegrationTest { ...@@ -158,6 +160,35 @@ public class TestTagsApi extends AbstractIntegrationTest {
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName()); assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
} }
@Test
public void testGetTagsInAscOrder() throws GitLabApiException {
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject, null, SortOrder.ASC, null);
assertNotNull(tags);
assertTrue(tags.size() > 1);
assertTrue(tags.get(0).getName().compareTo(tags.get(1).getName()) < 0);
}
@Test
public void testGetTagsInDescOrder() throws GitLabApiException {
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject, null, SortOrder.DESC, null);
assertNotNull(tags);
assertTrue(tags.size() > 1);
assertTrue(tags.get(0).getName().compareTo(tags.get(1).getName()) > 0);
}
@Test
public void testGetTagsSearch() throws GitLabApiException {
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject);
assertNotNull(tags);
assertTrue(tags.size() > 0);
String tagName = tags.get(0).getName();
tags = gitLabApi.getTagsApi().getTags(testProject, null, null, tagName);
assertNotNull(tags);
assertTrue(tags.size() > 0);
assertEquals(tagName, tags.get(0).getName());
}
@Test @Test
public void testProtectedTags() throws GitLabApiException { public void testProtectedTags() throws GitLabApiException {
......
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