Search in sources :

Example 1 with VersionedAccountQueries

use of com.google.gerrit.server.account.VersionedAccountQueries in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method query.

@Operator
public Predicate<ChangeData> query(String value) throws QueryParseException {
    // [name=]<name>[,user=<user>] || [user=<user>,][name=]<name>
    PredicateArgs inputArgs = new PredicateArgs(value);
    String name = null;
    Account.Id account = null;
    try (Repository git = args.repoManager.openRepository(args.allUsersName)) {
        // [name=]<name>
        if (inputArgs.keyValue.containsKey(ARG_ID_NAME)) {
            name = inputArgs.keyValue.get(ARG_ID_NAME).value();
        } else if (inputArgs.positional.size() == 1) {
            name = Iterables.getOnlyElement(inputArgs.positional);
        } else if (inputArgs.positional.size() > 1) {
            throw new QueryParseException("Error parsing named query: " + value);
        }
        // [,user=<user>]
        if (inputArgs.keyValue.containsKey(ARG_ID_USER)) {
            Set<Account.Id> accounts = parseAccount(inputArgs.keyValue.get(ARG_ID_USER).value());
            if (accounts != null && accounts.size() > 1) {
                throw error(String.format("\"%s\" resolves to multiple accounts", inputArgs.keyValue.get(ARG_ID_USER)));
            }
            account = (accounts == null ? self() : Iterables.getOnlyElement(accounts));
        } else {
            account = self();
        }
        VersionedAccountQueries q = VersionedAccountQueries.forUser(account);
        q.load(args.allUsersName, git);
        String query = q.getQueryList().getQuery(name);
        if (query != null) {
            return parse(query);
        }
    } catch (RepositoryNotFoundException e) {
        throw new QueryParseException("Unknown named query (no " + args.allUsersName + " repo): " + name, e);
    } catch (IOException | ConfigInvalidException e) {
        throw new QueryParseException("Error parsing named query: " + value, e);
    }
    throw new QueryParseException("Unknown named query: " + name);
}
Also used : Account(com.google.gerrit.entities.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) VersionedAccountQueries(com.google.gerrit.server.account.VersionedAccountQueries) QueryParseException(com.google.gerrit.index.query.QueryParseException) Repository(org.eclipse.jgit.lib.Repository)

Example 2 with VersionedAccountQueries

use of com.google.gerrit.server.account.VersionedAccountQueries in project gerrit by GerritCodeReview.

the class AbstractQueryChangesTest method userQuery.

@GerritConfig(name = "accounts.visibility", value = "NONE")
@Test
public void userQuery() throws Exception {
    TestRepository<Repo> repo = createProject("repo");
    Change change1 = insert(repo, newChange(repo));
    Change change2 = insert(repo, newChangeForBranch(repo, "stable"));
    Account.Id anotherUserId = accountManager.authenticate(authRequestFactory.createForUser("anotheruser")).getAccountId();
    String queryListText = "query1\tproject:repo\n" + "query2\tproject:repo status:open\n" + "query3\tproject:repo branch:stable\n" + "query4\tproject:repo branch:other";
    String anotherQueryListText = "query5\tproject:repo\n" + "query6\tproject:repo status:merged\n" + "query7\tproject:repo branch:stable\n" + "query8\tproject:repo branch:other";
    try (TestRepository<Repo> allUsers = new TestRepository<>(repoManager.openRepository(allUsersName));
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsersName);
        MetaDataUpdate anotherMd = metaDataUpdateFactory.create(allUsersName)) {
        VersionedAccountQueries queries = VersionedAccountQueries.forUser(userId);
        queries.load(md);
        queries.setQueryList(queryListText);
        queries.commit(md);
        VersionedAccountQueries anotherQueries = VersionedAccountQueries.forUser(anotherUserId);
        anotherQueries.load(anotherMd);
        anotherQueries.setQueryList(anotherQueryListText);
        anotherQueries.commit(anotherMd);
    }
    assertThatQueryException("query:foo").hasMessageThat().isEqualTo("Unknown named query: foo");
    assertThatQueryException("query:query1,user=" + anotherUserId).hasMessageThat().isEqualTo("Unknown named query: query1");
    assertThatQueryException("query:query1,user=test").hasMessageThat().isEqualTo("Account 'test' not found");
    requestContext.setContext(newRequestContext(anotherUserId));
    // account 1000000 is not visible to 'anotheruser' as they are not an admin
    assertThatQueryException("query:query1,user=" + userId).hasMessageThat().isEqualTo("Account '1000000' not found");
    requestContext.setContext(newRequestContext(userId));
    assertQuery("query:query1", change2, change1);
    assertQuery("query:query2", change2, change1);
    assertQuery("query:name=query5,user=" + anotherUserId, change2, change1);
    assertQuery("query:user=" + anotherUserId + ",name=query6");
    gApi.changes().id(change1.getChangeId()).current().review(ReviewInput.approve());
    gApi.changes().id(change1.getChangeId()).current().submit();
    assertQuery("query:query2", change2);
    assertQuery("query:query3", change2);
    assertQuery("query:query4");
    assertQuery("query:query6,user=" + anotherUserId, change1);
    assertQuery("query:user=" + anotherUserId + ",query7", change2);
    assertQuery("query:query8,user=" + anotherUserId);
}
Also used : Account(com.google.gerrit.entities.Account) TestRepository(org.eclipse.jgit.junit.TestRepository) Repo(com.google.gerrit.testing.InMemoryRepositoryManager.Repo) Change(com.google.gerrit.entities.Change) VersionedAccountQueries(com.google.gerrit.server.account.VersionedAccountQueries) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test)

Aggregations

Account (com.google.gerrit.entities.Account)2 VersionedAccountQueries (com.google.gerrit.server.account.VersionedAccountQueries)2 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)1 Change (com.google.gerrit.entities.Change)1 QueryParseException (com.google.gerrit.index.query.QueryParseException)1 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)1 Repo (com.google.gerrit.testing.InMemoryRepositoryManager.Repo)1 IOException (java.io.IOException)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1 TestRepository (org.eclipse.jgit.junit.TestRepository)1 Repository (org.eclipse.jgit.lib.Repository)1 Test (org.junit.Test)1