Skip to content

Commit 52483ef

Browse files
committed
Added support for searching for tags (#417).
1 parent 1737dd4 commit 52483ef

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ public String toString() {
221221
}
222222
}
223223

224+
/** Enum to use for ordering the results of getTags(). */
225+
public enum TagOrderBy {
226+
227+
NAME, UPDATED;
228+
private static JacksonJsonEnumHelper<TagOrderBy> enumHelper = new JacksonJsonEnumHelper<>(TagOrderBy.class);
229+
230+
@JsonCreator
231+
public static TagOrderBy forValue(String value) {
232+
return enumHelper.forValue(value);
233+
}
234+
235+
@JsonValue
236+
public String toValue() {
237+
return (enumHelper.toString(this));
238+
}
239+
240+
@Override
241+
public String toString() {
242+
return (enumHelper.toString(this));
243+
}
244+
}
245+
224246
/** Enum to use for specifying the scope when calling getPipelines(). */
225247
public enum PipelineScope {
226248

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,89 @@ public Stream<Tag> getTagsStream(Object projectIdOrPath) throws GitLabApiExcepti
8585
return (getTags(projectIdOrPath, getDefaultPerPage()).stream());
8686
}
8787

88+
/**
89+
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
90+
*
91+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
92+
*
93+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
94+
* @param orderBy return tags ordered by name or updated fields. Default is updated
95+
* @param sortOrder return tags sorted in asc or desc order. Default is desc
96+
* @param search return list of tags matching the search criteria
97+
* @return the list of tags for the specified project ID
98+
* @throws GitLabApiException if any exception occurs
99+
* @since GitLab 11.8
100+
*/
101+
public List<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search) throws GitLabApiException {
102+
return (getTags(projectIdOrPath, orderBy, sortOrder, search, getDefaultPerPage()).all());
103+
}
104+
105+
/**
106+
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order and in the specified page range.
107+
*
108+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
109+
*
110+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
111+
* @param orderBy return tags ordered by name or updated fields. Default is updated
112+
* @param sortOrder return tags sorted in asc or desc order. Default is desc
113+
* @param search return list of tags matching the search criteria
114+
* @param page the page to get
115+
* @param perPage the number of Tag instances per page
116+
* @return the list of tags for the specified project ID
117+
* @throws GitLabApiException if any exception occurs
118+
* @since GitLab 11.8
119+
*/
120+
public List<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search, int page, int perPage) throws GitLabApiException {
121+
Form formData = new GitLabApiForm()
122+
.withParam("order_by", orderBy)
123+
.withParam("sort", sortOrder)
124+
.withParam("search", search)
125+
.withParam(PAGE_PARAM, page)
126+
.withParam(PER_PAGE_PARAM, perPage);
127+
Response response = get(Response.Status.OK, formData.asMap(),
128+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags");
129+
return (response.readEntity(new GenericType<List<Tag>>() { }));
130+
}
131+
132+
/**
133+
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
134+
*
135+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
136+
*
137+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
138+
* @param orderBy return tags ordered by name or updated fields. Default is updated
139+
* @param sortOrder return tags sorted in asc or desc order. Default is desc
140+
* @param search return list of tags matching the search criteria
141+
* @param itemsPerPage the number of Project instances that will be fetched per page
142+
* @return the Pager of tags for the specified project ID
143+
* @throws GitLabApiException if any exception occurs
144+
* @since GitLab 11.8
145+
*/
146+
public Pager<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search, int itemsPerPage) throws GitLabApiException {
147+
Form formData = new GitLabApiForm()
148+
.withParam("order_by", orderBy)
149+
.withParam("sort", sortOrder)
150+
.withParam("search", search);
151+
return (new Pager<Tag>(this, Tag.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"));
152+
}
153+
154+
/**
155+
* Get a Stream of repository tags from a project, sorted by name in reverse alphabetical order.
156+
*
157+
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
158+
*
159+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
160+
* @param orderBy return tags ordered by name or updated fields. Default is updated
161+
* @param sortOrder return tags sorted in asc or desc order. Default is desc
162+
* @param search return list of tags matching the search criteria
163+
* @return a Stream of tags for the specified project ID
164+
* @throws GitLabApiException if any exception occurs
165+
* @since GitLab 11.8
166+
*/
167+
public Stream<Tag> getTagsStream(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search) throws GitLabApiException {
168+
return (getTags(projectIdOrPath, orderBy, sortOrder, search, getDefaultPerPage()).stream());
169+
}
170+
88171
/**
89172
* Get a specific repository tag determined by its name.
90173
*

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.gitlab4j.api;
22

3+
import static org.hamcrest.CoreMatchers.nullValue;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotNull;
@@ -9,6 +10,7 @@
910
import java.util.List;
1011
import java.util.Optional;
1112

13+
import org.gitlab4j.api.Constants.SortOrder;
1214
import org.gitlab4j.api.models.AccessLevel;
1315
import org.gitlab4j.api.models.Project;
1416
import org.gitlab4j.api.models.ProtectedTag;
@@ -158,6 +160,35 @@ public void testGetTagWithSpecialCharacersInTagName() throws GitLabApiException
158160
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
159161
}
160162

163+
@Test
164+
public void testGetTagsInAscOrder() throws GitLabApiException {
165+
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject, null, SortOrder.ASC, null);
166+
assertNotNull(tags);
167+
assertTrue(tags.size() > 1);
168+
assertTrue(tags.get(0).getName().compareTo(tags.get(1).getName()) < 0);
169+
}
170+
171+
@Test
172+
public void testGetTagsInDescOrder() throws GitLabApiException {
173+
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject, null, SortOrder.DESC, null);
174+
assertNotNull(tags);
175+
assertTrue(tags.size() > 1);
176+
assertTrue(tags.get(0).getName().compareTo(tags.get(1).getName()) > 0);
177+
}
178+
179+
@Test
180+
public void testGetTagsSearch() throws GitLabApiException {
181+
List<Tag> tags = gitLabApi.getTagsApi().getTags(testProject);
182+
assertNotNull(tags);
183+
assertTrue(tags.size() > 0);
184+
185+
String tagName = tags.get(0).getName();
186+
tags = gitLabApi.getTagsApi().getTags(testProject, null, null, tagName);
187+
assertNotNull(tags);
188+
assertTrue(tags.size() > 0);
189+
assertEquals(tagName, tags.get(0).getName());
190+
}
191+
161192
@Test
162193
public void testProtectedTags() throws GitLabApiException {
163194

0 commit comments

Comments
 (0)