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
This commit is contained in:
Dave Borowitz 2015-03-09 15:00:14 -07:00
parent e6e14b970b
commit 657aa4d57e
3 changed files with 54 additions and 7 deletions

View File

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

View File

@ -660,9 +660,12 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
@Operator
public Predicate<ChangeData> owner(String who) throws QueryParseException,
OrmException {
Set<Account.Id> m = parseAccount(who);
List<OwnerPredicate> p = Lists.newArrayListWithCapacity(m.size());
for (Account.Id id : m) {
return owner(parseAccount(who));
}
private Predicate<ChangeData> owner(Set<Account.Id> who) {
List<OwnerPredicate> 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<ChangeData> {
@Operator
public Predicate<ChangeData> commentby(String who)
throws QueryParseException, OrmException {
Set<Account.Id> m = parseAccount(who);
List<OwnerPredicate> p = Lists.newArrayListWithCapacity(m.size());
for (Account.Id id : m) {
p.add(new OwnerPredicate(id));
return commentby(parseAccount(who));
}
private Predicate<ChangeData> commentby(Set<Account.Id> who) {
List<CommentByPredicate> p = Lists.newArrayListWithCapacity(who.size());
for (Account.Id id : who) {
p.add(new CommentByPredicate(id));
}
return Predicate.or(p);
}
@Operator
public Predicate<ChangeData> from(String who)
throws QueryParseException, OrmException {
Set<Account.Id> ownerIds = parseAccount(who);
return Predicate.or(owner(ownerIds), commentby(ownerIds));
}
@Override
protected Predicate<ChangeData> defaultField(String query) throws QueryParseException {
if (query.startsWith("refs/")) {

View File

@ -1127,6 +1127,34 @@ public abstract class AbstractQueryChangesTest {
assertThat(query("commentby:" + user2)).isEmpty();
}
@Test
public void byFrom() throws Exception {
TestRepository<InMemoryRepository> 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.<String, List<ReviewInput.CommentInput>> of(
Patch.COMMIT_MSG, ImmutableList.<ReviewInput.CommentInput> of(comment));
postReview.apply(new RevisionResource(changes.parse(change2.getId()), ps2),
input);
List<ChangeInfo> 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<InMemoryRepository> repo,
@Nullable RevCommit commit, @Nullable String key, @Nullable Integer owner,