ListGroups: Add support for filtering groups by regex
Change-Id: I325937caaef973d8d213c9f03a1ac9caa0a5a764
This commit is contained in:
parent
180bc34538
commit
969e674491
@ -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)::
|
Substring(m)::
|
||||||
Limit the results to those groups that match the specified substring.
|
Limit the results to those groups that match the specified substring.
|
||||||
+
|
+
|
||||||
|
@ -502,6 +502,7 @@ public class GroupsIT extends AbstractDaemonTest {
|
|||||||
assertThat(groups).containsKey("Administrators");
|
assertThat(groups).containsKey("Administrators");
|
||||||
assertThat(groups).hasSize(1);
|
assertThat(groups).hasSize(1);
|
||||||
assertBadRequest(gApi.groups().list().withSuggest("adm").withSubstring("foo"));
|
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").withUser("user"));
|
||||||
assertBadRequest(gApi.groups().list().withSuggest("adm").withOwned(true));
|
assertBadRequest(gApi.groups().list().withSuggest("adm").withOwned(true));
|
||||||
assertBadRequest(gApi.groups().list().withSuggest("adm").withVisibleToAll(true));
|
assertBadRequest(gApi.groups().list().withSuggest("adm").withVisibleToAll(true));
|
||||||
@ -529,6 +530,22 @@ public class GroupsIT extends AbstractDaemonTest {
|
|||||||
assertThat(groups).isEmpty();
|
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
|
@Test
|
||||||
public void allGroupInfoFieldsSetCorrectly() throws Exception {
|
public void allGroupInfoFieldsSetCorrectly() throws Exception {
|
||||||
AccountGroup adminGroup = getFromCache("Administrators");
|
AccountGroup adminGroup = getFromCache("Administrators");
|
||||||
|
@ -79,6 +79,7 @@ public interface Groups {
|
|||||||
private int start;
|
private int start;
|
||||||
private String substring;
|
private String substring;
|
||||||
private String suggest;
|
private String suggest;
|
||||||
|
private String regex;
|
||||||
|
|
||||||
public List<GroupInfo> get() throws RestApiException {
|
public List<GroupInfo> get() throws RestApiException {
|
||||||
Map<String, GroupInfo> map = getAsMap();
|
Map<String, GroupInfo> map = getAsMap();
|
||||||
@ -149,6 +150,11 @@ public interface Groups {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ListRequest withRegex(String regex) {
|
||||||
|
this.regex = regex;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public ListRequest withSuggest(String suggest) {
|
public ListRequest withSuggest(String suggest) {
|
||||||
this.suggest = suggest;
|
this.suggest = suggest;
|
||||||
return this;
|
return this;
|
||||||
@ -190,6 +196,10 @@ public interface Groups {
|
|||||||
return substring;
|
return substring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRegex() {
|
||||||
|
return regex;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSuggest() {
|
public String getSuggest() {
|
||||||
return suggest;
|
return suggest;
|
||||||
}
|
}
|
||||||
|
@ -143,6 +143,7 @@ class GroupsImpl implements Groups {
|
|||||||
list.setLimit(req.getLimit());
|
list.setLimit(req.getLimit());
|
||||||
list.setStart(req.getStart());
|
list.setStart(req.getStart());
|
||||||
list.setMatchSubstring(req.getSubstring());
|
list.setMatchSubstring(req.getSubstring());
|
||||||
|
list.setMatchRegex(req.getRegex());
|
||||||
list.setSuggest(req.getSuggest());
|
list.setSuggest(req.getSuggest());
|
||||||
try {
|
try {
|
||||||
return list.apply(tlr);
|
return list.apply(tlr);
|
||||||
|
@ -53,6 +53,7 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import org.kohsuke.args4j.Option;
|
import org.kohsuke.args4j.Option;
|
||||||
|
|
||||||
/** List groups visible to the calling user. */
|
/** List groups visible to the calling user. */
|
||||||
@ -77,6 +78,7 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
private int limit;
|
private int limit;
|
||||||
private int start;
|
private int start;
|
||||||
private String matchSubstring;
|
private String matchSubstring;
|
||||||
|
private String matchRegex;
|
||||||
private String suggest;
|
private String suggest;
|
||||||
|
|
||||||
@Option(
|
@Option(
|
||||||
@ -170,6 +172,16 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
this.matchSubstring = matchSubstring;
|
this.matchSubstring = matchSubstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Option(
|
||||||
|
name = "--regex",
|
||||||
|
aliases = {"-r"},
|
||||||
|
metaVar = "REGEX",
|
||||||
|
usage = "match group regex"
|
||||||
|
)
|
||||||
|
public void setMatchRegex(String matchRegex) {
|
||||||
|
this.matchRegex = matchRegex;
|
||||||
|
}
|
||||||
|
|
||||||
@Option(
|
@Option(
|
||||||
name = "--suggest",
|
name = "--suggest",
|
||||||
aliases = {"-s"},
|
aliases = {"-s"},
|
||||||
@ -237,6 +249,10 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
return suggestGroups();
|
return suggestGroups();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Strings.isNullOrEmpty(matchSubstring) && !Strings.isNullOrEmpty(matchRegex)) {
|
||||||
|
throw new BadRequestException("Specify one of m/r");
|
||||||
|
}
|
||||||
|
|
||||||
if (owned) {
|
if (owned) {
|
||||||
return getGroupsOwnedBy(user != null ? userFactory.create(user) : identifiedUser.get());
|
return getGroupsOwnedBy(user != null ? userFactory.create(user) : identifiedUser.get());
|
||||||
}
|
}
|
||||||
@ -328,6 +344,9 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (!Strings.isNullOrEmpty(matchRegex)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,6 +375,7 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
|
|
||||||
private List<AccountGroup> filterGroups(Collection<AccountGroup> groups) {
|
private List<AccountGroup> filterGroups(Collection<AccountGroup> groups) {
|
||||||
List<AccountGroup> filteredGroups = new ArrayList<>(groups.size());
|
List<AccountGroup> filteredGroups = new ArrayList<>(groups.size());
|
||||||
|
Pattern pattern = Strings.isNullOrEmpty(matchRegex) ? null : Pattern.compile(matchRegex);
|
||||||
for (AccountGroup group : groups) {
|
for (AccountGroup group : groups) {
|
||||||
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
if (!Strings.isNullOrEmpty(matchSubstring)) {
|
||||||
if (!group
|
if (!group
|
||||||
@ -364,6 +384,10 @@ public class ListGroups implements RestReadView<TopLevelResource> {
|
|||||||
.contains(matchSubstring.toLowerCase(Locale.US))) {
|
.contains(matchSubstring.toLowerCase(Locale.US))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} else if (pattern != null) {
|
||||||
|
if (!pattern.matcher(group.getName()).matches()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (visibleToAll && !group.isVisibleToAll()) {
|
if (visibleToAll && !group.isVisibleToAll()) {
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
Reference in New Issue
Block a user