From 657aa4d57e50e417a335ca641dd42e145cde939d Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Mon, 9 Mar 2015 15:00:14 -0700 Subject: [PATCH] Add "from" search operator to match by owner or comments In the current implementation, searching by owner is somewhat redundant, since creating a new change inserts a new change message, which is picked up by commentby; but we consider this an implementation detail. Change-Id: I2a8bd6c43b2498b4d9bf82de09e09538dc047e10 --- Documentation/user-search.txt | 6 ++++ .../query/change/ChangeQueryBuilder.java | 27 +++++++++++++----- .../change/AbstractQueryChangesTest.java | 28 +++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Documentation/user-search.txt b/Documentation/user-search.txt index be4f8907af..1f95d9560d 100644 --- a/Documentation/user-search.txt +++ b/Documentation/user-search.txt @@ -333,6 +333,12 @@ Changes containing a top-level or inline comment by 'USER'. The special case of `commentby:self` will find changes where the caller has commented. +[[from]] +from:'USER':: ++ +Changes containing a top-level or inline comment by 'USER', or owned by +'USER'. Equivalent to `(owner:USER OR commentby:USER)`. + == Argument Quoting diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java index 5460296e15..4e0cc3ec0a 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java @@ -660,9 +660,12 @@ public class ChangeQueryBuilder extends QueryBuilder { @Operator public Predicate owner(String who) throws QueryParseException, OrmException { - Set m = parseAccount(who); - List p = Lists.newArrayListWithCapacity(m.size()); - for (Account.Id id : m) { + return owner(parseAccount(who)); + } + + private Predicate owner(Set who) { + List p = Lists.newArrayListWithCapacity(who.size()); + for (Account.Id id : who) { p.add(new OwnerPredicate(id)); } return Predicate.or(p); @@ -747,14 +750,24 @@ public class ChangeQueryBuilder extends QueryBuilder { @Operator public Predicate commentby(String who) throws QueryParseException, OrmException { - Set m = parseAccount(who); - List p = Lists.newArrayListWithCapacity(m.size()); - for (Account.Id id : m) { - p.add(new OwnerPredicate(id)); + return commentby(parseAccount(who)); + } + + private Predicate commentby(Set who) { + List p = Lists.newArrayListWithCapacity(who.size()); + for (Account.Id id : who) { + p.add(new CommentByPredicate(id)); } return Predicate.or(p); } + @Operator + public Predicate from(String who) + throws QueryParseException, OrmException { + Set ownerIds = parseAccount(who); + return Predicate.or(owner(ownerIds), commentby(ownerIds)); + } + @Override protected Predicate defaultField(String query) throws QueryParseException { if (query.startsWith("refs/")) { diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java index d03c5e50ed..d64f327139 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java @@ -1127,6 +1127,34 @@ public abstract class AbstractQueryChangesTest { assertThat(query("commentby:" + user2)).isEmpty(); } + @Test + public void byFrom() throws Exception { + TestRepository repo = createProject("repo"); + Change change1 = newChange(repo, null, null, null, null).insert(); + + int user2 = accountManager.authenticate(AuthRequest.forUser("anotheruser")) + .getAccountId().get(); + ChangeInserter ins2 = newChange(repo, null, null, user2, null); + Change change2 = ins2.insert(); + PatchSet ps2 = ins2.getPatchSet(); + + ReviewInput input = new ReviewInput(); + input.message = "toplevel"; + ReviewInput.CommentInput comment = new ReviewInput.CommentInput(); + comment.line = 1; + comment.message = "inline"; + input.comments = ImmutableMap.> of( + Patch.COMMIT_MSG, ImmutableList. of(comment)); + postReview.apply(new RevisionResource(changes.parse(change2.getId()), ps2), + input); + + List results = query("from:" + userId.get()); + assertThat(results).hasSize(2); + assertResultEquals(change2, results.get(0)); + assertResultEquals(change1, results.get(1)); + assertResultEquals(change2, queryOne("from:" + user2)); + } + protected ChangeInserter newChange( TestRepository repo, @Nullable RevCommit commit, @Nullable String key, @Nullable Integer owner,