diff --git a/src/main/java/org/gitlab4j/api/models/Group.java b/src/main/java/org/gitlab4j/api/models/Group.java index 037a0331a..91e026ea8 100644 --- a/src/main/java/org/gitlab4j/api/models/Group.java +++ b/src/main/java/org/gitlab4j/api/models/Group.java @@ -66,6 +66,7 @@ public void setJobArtifactsSize(Long jobArtifactsSize) { private List sharedProjects; private Date createdAt; private List sharedWithGroups; + private List customAttributes; private String runnersToken; private Boolean preventSharingGroupsOutsideHierarchy; private Boolean preventForkingOutsideGroup; @@ -228,6 +229,14 @@ public void setDefaultBranchProtection(DefaultBranchProtectionLevel defaultBranc this.defaultBranchProtection = defaultBranchProtection; } + public List getCustomAttributes() { + return customAttributes; + } + + public void setCustomAttributes(List customAttributes) { + this.customAttributes = customAttributes; + } + public Group withPath(String path) { this.path = path; return this; @@ -303,6 +312,11 @@ public Group withDefaultBranchProtection(DefaultBranchProtectionLevel defaultBra return this; } + public Group withCustomAttributes(List customAttributes) { + this.customAttributes = customAttributes; + return this; + } + @Override public String toString() { return (JacksonJson.toJsonString(this)); diff --git a/src/main/java/org/gitlab4j/api/models/GroupFilter.java b/src/main/java/org/gitlab4j/api/models/GroupFilter.java index 6d8ebb2a8..2856ce670 100644 --- a/src/main/java/org/gitlab4j/api/models/GroupFilter.java +++ b/src/main/java/org/gitlab4j/api/models/GroupFilter.java @@ -6,6 +6,7 @@ import org.gitlab4j.api.utils.JacksonJson; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; /** @@ -24,6 +25,7 @@ public class GroupFilter implements Serializable { private Boolean owned; private AccessLevel accessLevel; private Boolean topLevelOnly; + private List customAttributesFilter = new ArrayList<>(); /** * Do not include the provided groups IDs. @@ -111,6 +113,18 @@ public GroupFilter withCustomAttributes(Boolean withCustomAttributes) { return (this); } + /** + * Results must have custom attribute (admins only). Can be chained to combine multiple attribute checks. + * + * @param key the assets returned must have the specified custom attribute key + * @param value the assets returned must have the specified value for the custom attribute key + * @return the reference to this GroupFilter instance + */ + public GroupFilter withCustomAttributeFilter(String key, String value) { + this.customAttributesFilter.add(new CustomAttribute().withKey(key).withValue(value)); + return (this); + } + /** * Limit by groups explicitly owned by the current user * @@ -150,18 +164,20 @@ public GroupFilter withTopLevelOnly(Boolean topLevelOnly) { * @return a GitLabApiForm instance holding the query parameters for this GroupFilter instance */ public GitLabApiForm getQueryParams() { - return (new GitLabApiForm() - .withParam("skip_groups", skipGroups) - .withParam("all_available", allAvailable) - .withParam("search", search) - .withParam("order_by", orderBy) - .withParam("sort", sort) - .withParam("statistics", statistics) - .withParam("with_custom_attributes", withCustomAttributes) - .withParam("owned", owned) - .withParam("min_access_level", accessLevel) - .withParam("top_level_only", topLevelOnly) - ); + GitLabApiForm form = new GitLabApiForm().withParam("skip_groups", skipGroups) + .withParam("all_available", allAvailable) + .withParam("search", search) + .withParam("order_by", orderBy) + .withParam("sort", sort) + .withParam("statistics", statistics) + .withParam("with_custom_attributes", withCustomAttributes) + .withParam("owned", owned) + .withParam("min_access_level", accessLevel) + .withParam("top_level_only", topLevelOnly); + for (CustomAttribute customAttribute : customAttributesFilter) { + form.withParam(String.format("custom_attributes[%s]", customAttribute.getKey()), customAttribute.getValue()); + } + return form; } @Override diff --git a/src/test/java/org/gitlab4j/api/TestGroupApi.java b/src/test/java/org/gitlab4j/api/TestGroupApi.java index cd716f0a5..0c8cc8ef6 100644 --- a/src/test/java/org/gitlab4j/api/TestGroupApi.java +++ b/src/test/java/org/gitlab4j/api/TestGroupApi.java @@ -21,6 +21,7 @@ import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.AccessRequest; import org.gitlab4j.api.models.Group; +import org.gitlab4j.api.models.GroupFilter; import org.gitlab4j.api.models.GroupParams; import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.User; @@ -193,6 +194,21 @@ public void getGroup() throws GitLabApiException { assertNotNull(group); } + @Test + public void getGroupsWithCustomAttribute() throws GitLabApiException { + gitLabApi.getGroupApi().setCustomAttribute(TEST_GROUP, "test_key", "test_value"); + + GroupFilter wrongKeyFilter = new GroupFilter().withCustomAttributeFilter("other_key", "test_value"); + GroupFilter multipleFilter = new GroupFilter().withCustomAttributeFilter("test_key", "test_value").withCustomAttributeFilter("other_key", "test_value"); + GroupFilter matchingFilter = new GroupFilter().withCustomAttributeFilter("test_key", "test_value"); + + assertEquals(1, gitLabApi.getGroupApi().getGroups(matchingFilter).size()); + assertTrue(gitLabApi.getGroupApi().getGroups(wrongKeyFilter).isEmpty()); + assertTrue(gitLabApi.getGroupApi().getGroups(multipleFilter).isEmpty()); + + gitLabApi.getGroupApi().deleteCustomAttribute(TEST_GROUP, "test_key"); + } + @Test public void getOptionalGroup() { Optional optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP); diff --git a/src/test/resources/org/gitlab4j/api/group.json b/src/test/resources/org/gitlab4j/api/group.json index ab0d99d5e..f19ba317c 100644 --- a/src/test/resources/org/gitlab4j/api/group.json +++ b/src/test/resources/org/gitlab4j/api/group.json @@ -22,6 +22,12 @@ "lfs_objects_size" : 123, "job_artifacts_size" : 57 }, + "custom_attributes": [ + { + "key": "flagged", + "value": "YAY" + } + ], "shared_with_groups": [ { "group_id": 104,