Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Apr 13, 2024
2 parents 264884a + fcecb0b commit d34694c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void setJobArtifactsSize(Long jobArtifactsSize) {
private List<Project> sharedProjects;
private Date createdAt;
private List<SharedGroup> sharedWithGroups;
private List<CustomAttribute> customAttributes;
private String runnersToken;
private Boolean preventSharingGroupsOutsideHierarchy;
private Boolean preventForkingOutsideGroup;
Expand Down Expand Up @@ -228,6 +229,14 @@ public void setDefaultBranchProtection(DefaultBranchProtectionLevel defaultBranc
this.defaultBranchProtection = defaultBranchProtection;
}

public List<CustomAttribute> getCustomAttributes() {
return customAttributes;
}

public void setCustomAttributes(List<CustomAttribute> customAttributes) {
this.customAttributes = customAttributes;
}

public Group withPath(String path) {
this.path = path;
return this;
Expand Down Expand Up @@ -303,6 +312,11 @@ public Group withDefaultBranchProtection(DefaultBranchProtectionLevel defaultBra
return this;
}

public Group withCustomAttributes(List<CustomAttribute> customAttributes) {
this.customAttributes = customAttributes;
return this;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/org/gitlab4j/api/models/GroupFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.gitlab4j.api.utils.JacksonJson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

/**
* Do not include the provided groups IDs.
Expand Down Expand Up @@ -103,6 +105,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
*
Expand Down Expand Up @@ -142,18 +156,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
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGroupApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/org/gitlab4j/api/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"lfs_objects_size" : 123,
"job_artifacts_size" : 57
},
"custom_attributes": [
{
"key": "flagged",
"value": "YAY"
}
],
"shared_with_groups": [
{
"group_id": 104,
Expand Down

0 comments on commit d34694c

Please sign in to comment.