Search in sources :

Example 21 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.

the class QueryBuilder method fullTextQuery.

private Query fullTextQuery(IndexPredicate<V> p) throws QueryParseException {
    String value = p.getValue();
    if (value == null) {
        throw new QueryParseException("Full-text search over empty string not supported");
    }
    Query query = queryBuilder.createPhraseQuery(p.getField().getName(), value);
    if (query == null) {
        throw new QueryParseException("Cannot create full-text query with value: " + value);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) LegacyNumericRangeQuery(org.apache.lucene.search.LegacyNumericRangeQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 22 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method destination.

@Operator
public Predicate<ChangeData> destination(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 destination: " + 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();
        }
        VersionedAccountDestinations d = VersionedAccountDestinations.forUser(account);
        d.load(args.allUsersName, git);
        Set<BranchNameKey> destinations = d.getDestinationList().getDestinations(name);
        if (destinations != null && !destinations.isEmpty()) {
            return new DestinationPredicate(destinations, value);
        }
    } catch (RepositoryNotFoundException e) {
        throw new QueryParseException("Unknown named destination (no " + args.allUsersName + " repo): " + name, e);
    } catch (IOException | ConfigInvalidException e) {
        throw new QueryParseException("Error parsing named destination: " + value, e);
    }
    throw new QueryParseException("Unknown named destination: " + 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) VersionedAccountDestinations(com.google.gerrit.server.account.VersionedAccountDestinations) QueryParseException(com.google.gerrit.index.query.QueryParseException) Repository(org.eclipse.jgit.lib.Repository) BranchNameKey(com.google.gerrit.entities.BranchNameKey)

Example 23 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method reviewerByState.

public Predicate<ChangeData> reviewerByState(String who, ReviewerStateInternal state, boolean forDefaultField) throws QueryParseException, IOException, ConfigInvalidException {
    Predicate<ChangeData> reviewerByEmailPredicate = null;
    Address address = Address.tryParse(who);
    if (address != null) {
        reviewerByEmailPredicate = ReviewerByEmailPredicate.forState(address, state);
    }
    Predicate<ChangeData> reviewerPredicate = null;
    try {
        Set<Account.Id> accounts = parseAccount(who);
        if (!forDefaultField || accounts.size() <= MAX_ACCOUNTS_PER_DEFAULT_FIELD) {
            reviewerPredicate = Predicate.or(accounts.stream().map(id -> ReviewerPredicate.forState(id, state)).collect(toList()));
        } else {
            logger.atFine().log("Skipping reviewer predicate for %s in default field query" + " because the number of matched accounts (%d) exceeds the limit of %d", who, accounts.size(), MAX_ACCOUNTS_PER_DEFAULT_FIELD);
        }
    } catch (QueryParseException e) {
        logger.atFine().log("Parsing %s as account failed: %s", who, e.getMessage());
        // Propagate this exception only if we can't use 'who' to query by email
        if (reviewerByEmailPredicate == null) {
            throw e;
        }
    }
    if (reviewerPredicate != null && reviewerByEmailPredicate != null) {
        return Predicate.or(reviewerPredicate, reviewerByEmailPredicate);
    } else if (reviewerPredicate != null) {
        return reviewerPredicate;
    } else if (reviewerByEmailPredicate != null) {
        return reviewerByEmailPredicate;
    } else {
        return Predicate.any();
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) GroupBackend(com.google.gerrit.server.account.GroupBackend) Arrays(java.util.Arrays) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) OperatorAliasConfig(com.google.gerrit.server.config.OperatorAliasConfig) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Config(org.eclipse.jgit.lib.Config) ChangeTriplet(com.google.gerrit.server.change.ChangeTriplet) VersionedAccountQueries(com.google.gerrit.server.account.VersionedAccountQueries) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) ProvisionException(com.google.inject.ProvisionException) Splitter(com.google.common.base.Splitter) Collectors.toSet(java.util.stream.Collectors.toSet) FieldDef(com.google.gerrit.index.FieldDef) Schema(com.google.gerrit.index.Schema) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Collection(java.util.Collection) LimitPredicate(com.google.gerrit.index.query.LimitPredicate) Account(com.google.gerrit.entities.Account) Set(java.util.Set) SubmitRecord(com.google.gerrit.entities.SubmitRecord) BranchNameKey(com.google.gerrit.entities.BranchNameKey) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) List(java.util.List) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) CHANGE_ID_PATTERN(com.google.gerrit.entities.Change.CHANGE_ID_PATTERN) ChangeData.asChanges(com.google.gerrit.server.query.change.ChangeData.asChanges) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) SubmitDryRun(com.google.gerrit.server.submit.SubmitDryRun) Optional(java.util.Optional) Providers(com.google.inject.util.Providers) Pattern(java.util.regex.Pattern) SchemaUtil(com.google.gerrit.index.SchemaUtil) ValOp(com.google.gerrit.server.query.change.PredicateArgs.ValOp) FluentLogger(com.google.common.flogger.FluentLogger) QueryRequiresAuthException(com.google.gerrit.index.query.QueryRequiresAuthException) ChangeIndexRewriter(com.google.gerrit.server.index.change.ChangeIndexRewriter) AccountCache(com.google.gerrit.server.account.AccountCache) Iterables(com.google.common.collect.Iterables) IndexConfig(com.google.gerrit.index.IndexConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) MergeabilityComputationBehavior(com.google.gerrit.server.change.MergeabilityComputationBehavior) GroupBackends(com.google.gerrit.server.account.GroupBackends) HasOperandAliasConfig(com.google.gerrit.server.config.HasOperandAliasConfig) CommentsUtil(com.google.gerrit.server.CommentsUtil) Function(java.util.function.Function) Enums(com.google.common.base.Enums) GroupMembers(com.google.gerrit.server.account.GroupMembers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) AccountResolver.isSelf(com.google.gerrit.server.account.AccountResolver.isSelf) GroupDescription(com.google.gerrit.entities.GroupDescription) QueryParseException(com.google.gerrit.index.query.QueryParseException) UnresolvableAccountException(com.google.gerrit.server.account.AccountResolver.UnresolvableAccountException) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Address(com.google.gerrit.entities.Address) Predicate(com.google.gerrit.index.query.Predicate) QueryBuilder(com.google.gerrit.index.query.QueryBuilder) AccountGroup(com.google.gerrit.entities.AccountGroup) CurrentUser(com.google.gerrit.server.CurrentUser) AccountResolver(com.google.gerrit.server.account.AccountResolver) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) SubmitRule(com.google.gerrit.server.rules.SubmitRule) NotSignedInException(com.google.gerrit.exceptions.NotSignedInException) ExperimentFeatures(com.google.gerrit.server.experiments.ExperimentFeatures) PatchListCache(com.google.gerrit.server.patch.PatchListCache) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException) VersionedAccountDestinations(com.google.gerrit.server.account.VersionedAccountDestinations) Ints(com.google.common.primitives.Ints) ChildProjects(com.google.gerrit.server.project.ChildProjects) ChangeField(com.google.gerrit.server.index.change.ChangeField) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Project(com.google.gerrit.entities.Project) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AccountState(com.google.gerrit.server.account.AccountState) ChangeIndex(com.google.gerrit.server.index.change.ChangeIndex) Collections(java.util.Collections) GERRIT_BACKEND_REQUEST_FEATURE_COMPUTE_FROM_ALL_USERS_REPOSITORY(com.google.gerrit.server.experiments.ExperimentFeaturesConstants.GERRIT_BACKEND_REQUEST_FEATURE_COMPUTE_FROM_ALL_USERS_REPOSITORY) Repository(org.eclipse.jgit.lib.Repository) StarredChangesUtil(com.google.gerrit.server.StarredChangesUtil) Address(com.google.gerrit.entities.Address) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 24 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method visibleto.

@Operator
public Predicate<ChangeData> visibleto(String who) throws QueryParseException, IOException, ConfigInvalidException {
    if (isSelf(who)) {
        return isVisible();
    }
    Set<Account.Id> accounts = null;
    try {
        accounts = parseAccount(who);
    } catch (QueryParseException e) {
        if (e instanceof QueryRequiresAuthException) {
            throw e;
        }
    }
    if (accounts != null) {
        if (accounts.size() == 1) {
            return visibleto(args.userFactory.create(Iterables.getOnlyElement(accounts)));
        } else if (accounts.size() > 1) {
            throw error(String.format("\"%s\" resolves to multiple accounts", who));
        }
    }
    // If its not an account, maybe its a group?
    Collection<GroupReference> suggestions = args.groupBackend.suggest(who, null);
    if (!suggestions.isEmpty()) {
        HashSet<AccountGroup.UUID> ids = new HashSet<>();
        for (GroupReference ref : suggestions) {
            ids.add(ref.getUUID());
        }
        return visibleto(new GroupBackedUser(ids));
    }
    throw error("No user or group matches \"" + who + "\".");
}
Also used : QueryRequiresAuthException(com.google.gerrit.index.query.QueryRequiresAuthException) GroupReference(com.google.gerrit.entities.GroupReference) QueryParseException(com.google.gerrit.index.query.QueryParseException) HashSet(java.util.HashSet)

Example 25 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method conflicts.

@Operator
public Predicate<ChangeData> conflicts(String value) throws QueryParseException {
    if (!args.conflictsPredicateEnabled) {
        throw new QueryParseException("'conflicts:' operator is not supported by server");
    }
    List<Change> changes = parseChange(value);
    List<Predicate<ChangeData>> or = new ArrayList<>(changes.size());
    for (Change c : changes) {
        or.add(ConflictsPredicate.create(args, value, c));
    }
    return Predicate.or(or);
}
Also used : ArrayList(java.util.ArrayList) Change(com.google.gerrit.entities.Change) QueryParseException(com.google.gerrit.index.query.QueryParseException) LimitPredicate(com.google.gerrit.index.query.LimitPredicate) Predicate(com.google.gerrit.index.query.Predicate)

Aggregations

QueryParseException (com.google.gerrit.index.query.QueryParseException)30 Account (com.google.gerrit.entities.Account)7 StorageException (com.google.gerrit.exceptions.StorageException)6 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 ArrayList (java.util.ArrayList)6 Predicate (com.google.gerrit.index.query.Predicate)5 AccountState (com.google.gerrit.server.account.AccountState)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 IOException (java.io.IOException)5 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)5 Repository (org.eclipse.jgit.lib.Repository)4 FluentLogger (com.google.common.flogger.FluentLogger)3 GroupReference (com.google.gerrit.entities.GroupReference)3 Project (com.google.gerrit.entities.Project)3 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)3 LimitPredicate (com.google.gerrit.index.query.LimitPredicate)3 Inject (com.google.inject.Inject)3 Provider (com.google.inject.Provider)3 Collections (java.util.Collections)3 List (java.util.List)3