ListGroups: Add support for filtering groups by regex

Change-Id: I325937caaef973d8d213c9f03a1ac9caa0a5a764
This commit is contained in:
David Pursehouse 2017-07-12 20:12:26 +09:00 committed by Paladox none
parent 180bc34538
commit 969e674491
5 changed files with 100 additions and 0 deletions

View File

@ -224,6 +224,54 @@ error out.)
}
----
Regex(r)::
Limit the results to those groups that match the specified regex.
+
Boundary matchers '^' and '$' are implicit. For example: the regex 'test.*' will
match any groups that start with 'test' and regex '.*test' will match any
group that end with 'test'.
+
The match is case sensitive.
+
List all groups that match regex `test.*group`:
+
.Request
----
GET /groups/?r=test.*group HTTP/1.0
----
+
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json; charset=UTF-8
)]}'
{
"test/some-group": {
"url": "#/admin/groups/uuid-59b92f35489e62c80d1ab1bf0c2d17843038df8b",
"options": {},
"description": "Gerrit Site Administrators",
"group_id": 1,
"owner": "Administrators",
"owner_id": "59b92f35489e62c80d1ab1bf0c2d17843038df8b",
"created_on": "2013-02-01 09:59:32.126000000",
"id": "59b92f35489e62c80d1ab1bf0c2d17843038df8b"
}
"test/some-other-group": {
"url": "#/admin/groups/uuid-99b92f35489e62c80d1ab1bf0c2d17843038df8b",
"options": {},
"description": "Gerrit Site Administrators",
"group_id": 1,
"owner": "Administrators",
"owner_id": "99b92f35489e62c80d1ab1bf0c2d17843038df8b",
"created_on": "2014-02-01 09:59:32.126000000",
"id": "99b92f35489e62c80d1ab1bf0c2d17843038df8b"
}
}
----
Substring(m)::
Limit the results to those groups that match the specified substring.
+

View File

@ -502,6 +502,7 @@ public class GroupsIT extends AbstractDaemonTest {
assertThat(groups).containsKey("Administrators");
assertThat(groups).hasSize(1);
assertBadRequest(gApi.groups().list().withSuggest("adm").withSubstring("foo"));
assertBadRequest(gApi.groups().list().withSuggest("adm").withRegex("foo.*"));
assertBadRequest(gApi.groups().list().withSuggest("adm").withUser("user"));
assertBadRequest(gApi.groups().list().withSuggest("adm").withOwned(true));
assertBadRequest(gApi.groups().list().withSuggest("adm").withVisibleToAll(true));
@ -529,6 +530,22 @@ public class GroupsIT extends AbstractDaemonTest {
assertThat(groups).isEmpty();
}
@Test
public void withRegex() throws Exception {
Map<String, GroupInfo> groups = gApi.groups().list().withRegex("Admin.*").getAsMap();
assertThat(groups).containsKey("Administrators");
assertThat(groups).hasSize(1);
groups = gApi.groups().list().withRegex("admin.*").getAsMap();
assertThat(groups).isEmpty();
groups = gApi.groups().list().withRegex(".*istrators").getAsMap();
assertThat(groups).containsKey("Administrators");
assertThat(groups).hasSize(1);
assertBadRequest(gApi.groups().list().withRegex(".*istrators").withSubstring("s"));
}
@Test
public void allGroupInfoFieldsSetCorrectly() throws Exception {
AccountGroup adminGroup = getFromCache("Administrators");

View File

@ -79,6 +79,7 @@ public interface Groups {
private int start;
private String substring;
private String suggest;
private String regex;
public List<GroupInfo> get() throws RestApiException {
Map<String, GroupInfo> map = getAsMap();
@ -149,6 +150,11 @@ public interface Groups {
return this;
}
public ListRequest withRegex(String regex) {
this.regex = regex;
return this;
}
public ListRequest withSuggest(String suggest) {
this.suggest = suggest;
return this;
@ -190,6 +196,10 @@ public interface Groups {
return substring;
}
public String getRegex() {
return regex;
}
public String getSuggest() {
return suggest;
}

View File

@ -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);

View File

@ -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;