ListGroups: Add support for filtering groups by regex
Change-Id: I325937caaef973d8d213c9f03a1ac9caa0a5a764
This commit is contained in:
committed by
Paladox none
parent
180bc34538
commit
969e674491
@@ -143,6 +143,7 @@ class GroupsImpl implements Groups {
|
||||
list.setLimit(req.getLimit());
|
||||
list.setStart(req.getStart());
|
||||
list.setMatchSubstring(req.getSubstring());
|
||||
list.setMatchRegex(req.getRegex());
|
||||
list.setSuggest(req.getSuggest());
|
||||
try {
|
||||
return list.apply(tlr);
|
||||
|
||||
@@ -53,6 +53,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.regex.Pattern;
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
/** List groups visible to the calling user. */
|
||||
@@ -77,6 +78,7 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
private int limit;
|
||||
private int start;
|
||||
private String matchSubstring;
|
||||
private String matchRegex;
|
||||
private String suggest;
|
||||
|
||||
@Option(
|
||||
@@ -170,6 +172,16 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
this.matchSubstring = matchSubstring;
|
||||
}
|
||||
|
||||
@Option(
|
||||
name = "--regex",
|
||||
aliases = {"-r"},
|
||||
metaVar = "REGEX",
|
||||
usage = "match group regex"
|
||||
)
|
||||
public void setMatchRegex(String matchRegex) {
|
||||
this.matchRegex = matchRegex;
|
||||
}
|
||||
|
||||
@Option(
|
||||
name = "--suggest",
|
||||
aliases = {"-s"},
|
||||
@@ -237,6 +249,10 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
return suggestGroups();
|
||||
}
|
||||
|
||||
if (!Strings.isNullOrEmpty(matchSubstring) && !Strings.isNullOrEmpty(matchRegex)) {
|
||||
throw new BadRequestException("Specify one of m/r");
|
||||
}
|
||||
|
||||
if (owned) {
|
||||
return getGroupsOwnedBy(user != null ? userFactory.create(user) : identifiedUser.get());
|
||||
}
|
||||
@@ -328,6 +344,9 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
||||
return true;
|
||||
}
|
||||
if (!Strings.isNullOrEmpty(matchRegex)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -356,6 +375,7 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
|
||||
private List<AccountGroup> filterGroups(Collection<AccountGroup> groups) {
|
||||
List<AccountGroup> filteredGroups = new ArrayList<>(groups.size());
|
||||
Pattern pattern = Strings.isNullOrEmpty(matchRegex) ? null : Pattern.compile(matchRegex);
|
||||
for (AccountGroup group : groups) {
|
||||
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
||||
if (!group
|
||||
@@ -364,6 +384,10 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
||||
.contains(matchSubstring.toLowerCase(Locale.US))) {
|
||||
continue;
|
||||
}
|
||||
} else if (pattern != null) {
|
||||
if (!pattern.matcher(group.getName()).matches()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (visibleToAll && !group.isVisibleToAll()) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user