Unverified Commit fcecb0b2 authored by Jente Sondervorst's avatar Jente Sondervorst Committed by GitHub
Browse files

Enables group api to search by custom attribute and to return custom_attributes (#1099)



* Fixes #1098

---------

Co-authored-by: default avatarJente Sondervorst <jente.sondervorst@colruytgroup.com>
parent ed0ebb6d
...@@ -66,6 +66,7 @@ public class Group extends AbstractGroup<Group> { ...@@ -66,6 +66,7 @@ public class Group extends AbstractGroup<Group> {
private List<Project> sharedProjects; private List<Project> sharedProjects;
private Date createdAt; private Date createdAt;
private List<SharedGroup> sharedWithGroups; private List<SharedGroup> sharedWithGroups;
private List<CustomAttribute> customAttributes;
private String runnersToken; private String runnersToken;
private Boolean preventSharingGroupsOutsideHierarchy; private Boolean preventSharingGroupsOutsideHierarchy;
private Boolean preventForkingOutsideGroup; private Boolean preventForkingOutsideGroup;
...@@ -228,6 +229,14 @@ public class Group extends AbstractGroup<Group> { ...@@ -228,6 +229,14 @@ public class Group extends AbstractGroup<Group> {
this.defaultBranchProtection = defaultBranchProtection; this.defaultBranchProtection = defaultBranchProtection;
} }
public List<CustomAttribute> getCustomAttributes() {
return customAttributes;
}
public void setCustomAttributes(List<CustomAttribute> customAttributes) {
this.customAttributes = customAttributes;
}
public Group withPath(String path) { public Group withPath(String path) {
this.path = path; this.path = path;
return this; return this;
...@@ -303,6 +312,11 @@ public class Group extends AbstractGroup<Group> { ...@@ -303,6 +312,11 @@ public class Group extends AbstractGroup<Group> {
return this; return this;
} }
public Group withCustomAttributes(List<CustomAttribute> customAttributes) {
this.customAttributes = customAttributes;
return this;
}
@Override @Override
public String toString() { public String toString() {
return (JacksonJson.toJsonString(this)); return (JacksonJson.toJsonString(this));
......
...@@ -6,6 +6,7 @@ import org.gitlab4j.api.GitLabApiForm; ...@@ -6,6 +6,7 @@ import org.gitlab4j.api.GitLabApiForm;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
...@@ -24,6 +25,7 @@ public class GroupFilter implements Serializable { ...@@ -24,6 +25,7 @@ public class GroupFilter implements Serializable {
private Boolean owned; private Boolean owned;
private AccessLevel accessLevel; private AccessLevel accessLevel;
private Boolean topLevelOnly; private Boolean topLevelOnly;
private List<CustomAttribute> customAttributesFilter = new ArrayList<>();
/** /**
* Do not include the provided groups IDs. * Do not include the provided groups IDs.
...@@ -111,6 +113,18 @@ public class GroupFilter implements Serializable { ...@@ -111,6 +113,18 @@ public class GroupFilter implements Serializable {
return (this); 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 * Limit by groups explicitly owned by the current user
* *
...@@ -150,18 +164,20 @@ public class GroupFilter implements Serializable { ...@@ -150,18 +164,20 @@ public class GroupFilter implements Serializable {
* @return a GitLabApiForm instance holding the query parameters for this GroupFilter instance * @return a GitLabApiForm instance holding the query parameters for this GroupFilter instance
*/ */
public GitLabApiForm getQueryParams() { public GitLabApiForm getQueryParams() {
return (new GitLabApiForm() GitLabApiForm form = new GitLabApiForm().withParam("skip_groups", skipGroups)
.withParam("skip_groups", skipGroups) .withParam("all_available", allAvailable)
.withParam("all_available", allAvailable) .withParam("search", search)
.withParam("search", search) .withParam("order_by", orderBy)
.withParam("order_by", orderBy) .withParam("sort", sort)
.withParam("sort", sort) .withParam("statistics", statistics)
.withParam("statistics", statistics) .withParam("with_custom_attributes", withCustomAttributes)
.withParam("with_custom_attributes", withCustomAttributes) .withParam("owned", owned)
.withParam("owned", owned) .withParam("min_access_level", accessLevel)
.withParam("min_access_level", accessLevel) .withParam("top_level_only", topLevelOnly);
.withParam("top_level_only", topLevelOnly) for (CustomAttribute customAttribute : customAttributesFilter) {
); form.withParam(String.format("custom_attributes[%s]", customAttribute.getKey()), customAttribute.getValue());
}
return form;
} }
@Override @Override
......
...@@ -21,6 +21,7 @@ import javax.ws.rs.core.Response; ...@@ -21,6 +21,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest; import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupFilter;
import org.gitlab4j.api.models.GroupParams; import org.gitlab4j.api.models.GroupParams;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
...@@ -193,6 +194,21 @@ public class TestGroupApi extends AbstractIntegrationTest { ...@@ -193,6 +194,21 @@ public class TestGroupApi extends AbstractIntegrationTest {
assertNotNull(group); 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 @Test
public void getOptionalGroup() { public void getOptionalGroup() {
Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP); Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
......
...@@ -22,6 +22,12 @@ ...@@ -22,6 +22,12 @@
"lfs_objects_size" : 123, "lfs_objects_size" : 123,
"job_artifacts_size" : 57 "job_artifacts_size" : 57
}, },
"custom_attributes": [
{
"key": "flagged",
"value": "YAY"
}
],
"shared_with_groups": [ "shared_with_groups": [
{ {
"group_id": 104, "group_id": 104,
......
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