Skip to content

Commit fcecb0b

Browse files
Jenson3210Jente Sondervorst
and
Jente Sondervorst
authored
Enables group api to search by custom attribute and to return custom_attributes (gitlab4j#1099)
* Fixes gitlab4j#1098 --------- Co-authored-by: Jente Sondervorst <[email protected]>
1 parent ed0ebb6 commit fcecb0b

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

src/main/java/org/gitlab4j/api/models/Group.java

+14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void setJobArtifactsSize(Long jobArtifactsSize) {
6666
private List<Project> sharedProjects;
6767
private Date createdAt;
6868
private List<SharedGroup> sharedWithGroups;
69+
private List<CustomAttribute> customAttributes;
6970
private String runnersToken;
7071
private Boolean preventSharingGroupsOutsideHierarchy;
7172
private Boolean preventForkingOutsideGroup;
@@ -228,6 +229,14 @@ public void setDefaultBranchProtection(DefaultBranchProtectionLevel defaultBranc
228229
this.defaultBranchProtection = defaultBranchProtection;
229230
}
230231

232+
public List<CustomAttribute> getCustomAttributes() {
233+
return customAttributes;
234+
}
235+
236+
public void setCustomAttributes(List<CustomAttribute> customAttributes) {
237+
this.customAttributes = customAttributes;
238+
}
239+
231240
public Group withPath(String path) {
232241
this.path = path;
233242
return this;
@@ -303,6 +312,11 @@ public Group withDefaultBranchProtection(DefaultBranchProtectionLevel defaultBra
303312
return this;
304313
}
305314

315+
public Group withCustomAttributes(List<CustomAttribute> customAttributes) {
316+
this.customAttributes = customAttributes;
317+
return this;
318+
}
319+
306320
@Override
307321
public String toString() {
308322
return (JacksonJson.toJsonString(this));

src/main/java/org/gitlab4j/api/models/GroupFilter.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.gitlab4j.api.utils.JacksonJson;
77

88
import java.io.Serializable;
9+
import java.util.ArrayList;
910
import java.util.List;
1011

1112
/**
@@ -24,6 +25,7 @@ public class GroupFilter implements Serializable {
2425
private Boolean owned;
2526
private AccessLevel accessLevel;
2627
private Boolean topLevelOnly;
28+
private List<CustomAttribute> customAttributesFilter = new ArrayList<>();
2729

2830
/**
2931
* Do not include the provided groups IDs.
@@ -111,6 +113,18 @@ public GroupFilter withCustomAttributes(Boolean withCustomAttributes) {
111113
return (this);
112114
}
113115

116+
/**
117+
* Results must have custom attribute (admins only). Can be chained to combine multiple attribute checks.
118+
*
119+
* @param key the assets returned must have the specified custom attribute key
120+
* @param value the assets returned must have the specified value for the custom attribute key
121+
* @return the reference to this GroupFilter instance
122+
*/
123+
public GroupFilter withCustomAttributeFilter(String key, String value) {
124+
this.customAttributesFilter.add(new CustomAttribute().withKey(key).withValue(value));
125+
return (this);
126+
}
127+
114128
/**
115129
* Limit by groups explicitly owned by the current user
116130
*
@@ -150,18 +164,20 @@ public GroupFilter withTopLevelOnly(Boolean topLevelOnly) {
150164
* @return a GitLabApiForm instance holding the query parameters for this GroupFilter instance
151165
*/
152166
public GitLabApiForm getQueryParams() {
153-
return (new GitLabApiForm()
154-
.withParam("skip_groups", skipGroups)
155-
.withParam("all_available", allAvailable)
156-
.withParam("search", search)
157-
.withParam("order_by", orderBy)
158-
.withParam("sort", sort)
159-
.withParam("statistics", statistics)
160-
.withParam("with_custom_attributes", withCustomAttributes)
161-
.withParam("owned", owned)
162-
.withParam("min_access_level", accessLevel)
163-
.withParam("top_level_only", topLevelOnly)
164-
);
167+
GitLabApiForm form = new GitLabApiForm().withParam("skip_groups", skipGroups)
168+
.withParam("all_available", allAvailable)
169+
.withParam("search", search)
170+
.withParam("order_by", orderBy)
171+
.withParam("sort", sort)
172+
.withParam("statistics", statistics)
173+
.withParam("with_custom_attributes", withCustomAttributes)
174+
.withParam("owned", owned)
175+
.withParam("min_access_level", accessLevel)
176+
.withParam("top_level_only", topLevelOnly);
177+
for (CustomAttribute customAttribute : customAttributesFilter) {
178+
form.withParam(String.format("custom_attributes[%s]", customAttribute.getKey()), customAttribute.getValue());
179+
}
180+
return form;
165181
}
166182

167183
@Override

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

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.gitlab4j.api.models.AccessLevel;
2222
import org.gitlab4j.api.models.AccessRequest;
2323
import org.gitlab4j.api.models.Group;
24+
import org.gitlab4j.api.models.GroupFilter;
2425
import org.gitlab4j.api.models.GroupParams;
2526
import org.gitlab4j.api.models.Member;
2627
import org.gitlab4j.api.models.User;
@@ -193,6 +194,21 @@ public void getGroup() throws GitLabApiException {
193194
assertNotNull(group);
194195
}
195196

197+
@Test
198+
public void getGroupsWithCustomAttribute() throws GitLabApiException {
199+
gitLabApi.getGroupApi().setCustomAttribute(TEST_GROUP, "test_key", "test_value");
200+
201+
GroupFilter wrongKeyFilter = new GroupFilter().withCustomAttributeFilter("other_key", "test_value");
202+
GroupFilter multipleFilter = new GroupFilter().withCustomAttributeFilter("test_key", "test_value").withCustomAttributeFilter("other_key", "test_value");
203+
GroupFilter matchingFilter = new GroupFilter().withCustomAttributeFilter("test_key", "test_value");
204+
205+
assertEquals(1, gitLabApi.getGroupApi().getGroups(matchingFilter).size());
206+
assertTrue(gitLabApi.getGroupApi().getGroups(wrongKeyFilter).isEmpty());
207+
assertTrue(gitLabApi.getGroupApi().getGroups(multipleFilter).isEmpty());
208+
209+
gitLabApi.getGroupApi().deleteCustomAttribute(TEST_GROUP, "test_key");
210+
}
211+
196212
@Test
197213
public void getOptionalGroup() {
198214
Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);

src/test/resources/org/gitlab4j/api/group.json

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"lfs_objects_size" : 123,
2323
"job_artifacts_size" : 57
2424
},
25+
"custom_attributes": [
26+
{
27+
"key": "flagged",
28+
"value": "YAY"
29+
}
30+
],
2531
"shared_with_groups": [
2632
{
2733
"group_id": 104,

0 commit comments

Comments
 (0)