Search in sources :

Example 16 with QueryParseException

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

the class ChangeQueryBuilder method query.

@Operator
public Predicate<ChangeData> query(String name) throws QueryParseException {
    try (Repository git = args.repoManager.openRepository(args.allUsersName)) {
        VersionedAccountQueries q = VersionedAccountQueries.forUser(self());
        q.load(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: " + name, e);
    }
    throw new QueryParseException("Unknown named query: " + name);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) 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.server.query.QueryParseException)

Example 17 with QueryParseException

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

the class ChangeQueryBuilder method destination.

@Operator
public Predicate<ChangeData> destination(String name) throws QueryParseException {
    try (Repository git = args.repoManager.openRepository(args.allUsersName)) {
        VersionedAccountDestinations d = VersionedAccountDestinations.forUser(self());
        d.load(git);
        Set<Branch.NameKey> destinations = d.getDestinationList().getDestinations(name);
        if (destinations != null) {
            return new DestinationPredicate(destinations, name);
        }
    } 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: " + name, e);
    }
    throw new QueryParseException("Unknown named destination: " + name);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) 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.server.query.QueryParseException)

Example 18 with QueryParseException

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

the class ChangeQueryBuilder method reviewerByState.

public Predicate<ChangeData> reviewerByState(String who, ReviewerStateInternal state, boolean forDefaultField) throws QueryParseException, OrmException {
    Predicate<ChangeData> reviewerByEmailPredicate = null;
    if (args.index.getSchema().hasField(ChangeField.REVIEWER_BY_EMAIL)) {
        Address address = Address.tryParse(who);
        if (address != null) {
            reviewerByEmailPredicate = ReviewerByEmailPredicate.forState(args, 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(args, id, state)).collect(toList()));
        }
    } catch (QueryParseException e) {
        // 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) ChangeControl(com.google.gerrit.server.project.ChangeControl) OrmException(com.google.gwtorm.server.OrmException) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Config(org.eclipse.jgit.lib.Config) ChangeTriplet(com.google.gerrit.server.change.ChangeTriplet) CapabilityControl(com.google.gerrit.server.account.CapabilityControl) VersionedAccountQueries(com.google.gerrit.server.account.VersionedAccountQueries) Map(java.util.Map) CHANGE_ID_PATTERN(com.google.gerrit.reviewdb.client.Change.CHANGE_ID_PATTERN) ProvisionException(com.google.inject.ProvisionException) SubmitDryRun(com.google.gerrit.server.git.strategy.SubmitDryRun) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Schema(com.google.gerrit.server.index.Schema) Collection(java.util.Collection) ListChildProjects(com.google.gerrit.server.project.ListChildProjects) Set(java.util.Set) Collectors(java.util.stream.Collectors) Predicate(com.google.gerrit.server.query.Predicate) Objects(java.util.Objects) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) List(java.util.List) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) RefNames(com.google.gerrit.reviewdb.client.RefNames) ChangeData.asChanges(com.google.gerrit.server.query.change.ChangeData.asChanges) Optional(java.util.Optional) Providers(com.google.inject.util.Providers) Pattern(java.util.regex.Pattern) NotesMigration(com.google.gerrit.server.notedb.NotesMigration) GroupReference(com.google.gerrit.common.data.GroupReference) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeIndexRewriter(com.google.gerrit.server.index.change.ChangeIndexRewriter) AccountCache(com.google.gerrit.server.account.AccountCache) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Change(com.google.gerrit.reviewdb.client.Change) GroupBackends(com.google.gerrit.server.account.GroupBackends) CommentsUtil(com.google.gerrit.server.CommentsUtil) Function(java.util.function.Function) Enums(com.google.common.base.Enums) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) HashSet(java.util.HashSet) NotSignedInException(com.google.gerrit.common.errors.NotSignedInException) IndexConfig(com.google.gerrit.server.index.IndexConfig) Lists(com.google.common.collect.Lists) Account(com.google.gerrit.reviewdb.client.Account) SchemaUtil(com.google.gerrit.server.index.SchemaUtil) CurrentUser(com.google.gerrit.server.CurrentUser) AccountResolver(com.google.gerrit.server.account.AccountResolver) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) PatchListCache(com.google.gerrit.server.patch.PatchListCache) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) VersionedAccountDestinations(com.google.gerrit.server.account.VersionedAccountDestinations) Ints(com.google.common.primitives.Ints) ChangeField(com.google.gerrit.server.index.change.ChangeField) QueryParseException(com.google.gerrit.server.query.QueryParseException) Collectors.toList(java.util.stream.Collectors.toList) Address(com.google.gerrit.server.mail.Address) LimitPredicate(com.google.gerrit.server.query.LimitPredicate) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ListMembers(com.google.gerrit.server.group.ListMembers) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TrackingFooters(com.google.gerrit.server.config.TrackingFooters) FieldDef(com.google.gerrit.server.index.FieldDef) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) VisibleForTesting(com.google.common.annotations.VisibleForTesting) QueryBuilder(com.google.gerrit.server.query.QueryBuilder) ChangeIndex(com.google.gerrit.server.index.change.ChangeIndex) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) StarredChangesUtil(com.google.gerrit.server.StarredChangesUtil) Address(com.google.gerrit.server.mail.Address) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 19 with QueryParseException

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

the class ChangeQueryBuilder method label.

@Operator
public Predicate<ChangeData> label(String name) throws QueryParseException, OrmException {
    Set<Account.Id> accounts = null;
    AccountGroup.UUID group = null;
    // Parse for:
    // label:CodeReview=1,user=jsmith or
    // label:CodeReview=1,jsmith or
    // label:CodeReview=1,group=android_approvers or
    // label:CodeReview=1,android_approvers
    // user/groups without a label will first attempt to match user
    // Special case: votes by owners can be tracked with ",owner":
    // label:Code-Review+2,owner
    // label:Code-Review+2,user=owner
    String[] splitReviewer = name.split(",", 2);
    // remove all but the vote piece, e.g.'CodeReview=1'
    name = splitReviewer[0];
    if (splitReviewer.length == 2) {
        // process the user/group piece
        PredicateArgs lblArgs = new PredicateArgs(splitReviewer[1]);
        for (Map.Entry<String, String> pair : lblArgs.keyValue.entrySet()) {
            if (pair.getKey().equalsIgnoreCase(ARG_ID_USER)) {
                if (pair.getValue().equals(ARG_ID_OWNER)) {
                    accounts = Collections.singleton(OWNER_ACCOUNT_ID);
                } else {
                    accounts = parseAccount(pair.getValue());
                }
            } else if (pair.getKey().equalsIgnoreCase(ARG_ID_GROUP)) {
                group = parseGroup(pair.getValue()).getUUID();
            } else {
                throw new QueryParseException("Invalid argument identifier '" + pair.getKey() + "'");
            }
        }
        for (String value : lblArgs.positional) {
            if (accounts != null || group != null) {
                throw new QueryParseException("more than one user/group specified (" + value + ")");
            }
            try {
                if (value.equals(ARG_ID_OWNER)) {
                    accounts = Collections.singleton(OWNER_ACCOUNT_ID);
                } else {
                    accounts = parseAccount(value);
                }
            } catch (QueryParseException qpex) {
                // (accounts get precedence)
                try {
                    group = parseGroup(value).getUUID();
                } catch (QueryParseException e) {
                    throw error("Neither user nor group " + value + " found", e);
                }
            }
        }
    }
    // expand a group predicate into multiple user predicates
    if (group != null) {
        Set<Account.Id> allMembers = args.listMembers.get().setRecursive(true).apply(group).stream().map(a -> new Account.Id(a._accountId)).collect(toSet());
        int maxLimit = args.indexConfig.maxLimit();
        if (allMembers.size() > maxLimit) {
            // limit the number of query terms otherwise Gerrit will barf
            accounts = ImmutableSet.copyOf(Iterables.limit(allMembers, maxLimit));
        } else {
            accounts = allMembers;
        }
    }
    // If the vote piece looks like Code-Review=NEED with a valid non-numeric
    // submit record status, interpret as a submit record query.
    int eq = name.indexOf('=');
    if (args.getSchema().hasField(ChangeField.SUBMIT_RECORD) && eq > 0) {
        String statusName = name.substring(eq + 1).toUpperCase();
        if (!isInt(statusName)) {
            SubmitRecord.Label.Status status = Enums.getIfPresent(SubmitRecord.Label.Status.class, statusName).orNull();
            if (status == null) {
                throw error("Invalid label status " + statusName + " in " + name);
            }
            return SubmitRecordPredicate.create(name.substring(0, eq), status, accounts);
        }
    }
    return new LabelPredicate(args, name, accounts, group);
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) GroupBackend(com.google.gerrit.server.account.GroupBackend) ChangeControl(com.google.gerrit.server.project.ChangeControl) OrmException(com.google.gwtorm.server.OrmException) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Config(org.eclipse.jgit.lib.Config) ChangeTriplet(com.google.gerrit.server.change.ChangeTriplet) CapabilityControl(com.google.gerrit.server.account.CapabilityControl) VersionedAccountQueries(com.google.gerrit.server.account.VersionedAccountQueries) Map(java.util.Map) CHANGE_ID_PATTERN(com.google.gerrit.reviewdb.client.Change.CHANGE_ID_PATTERN) ProvisionException(com.google.inject.ProvisionException) SubmitDryRun(com.google.gerrit.server.git.strategy.SubmitDryRun) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Schema(com.google.gerrit.server.index.Schema) Collection(java.util.Collection) ListChildProjects(com.google.gerrit.server.project.ListChildProjects) Set(java.util.Set) Collectors(java.util.stream.Collectors) Predicate(com.google.gerrit.server.query.Predicate) Objects(java.util.Objects) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) List(java.util.List) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) RefNames(com.google.gerrit.reviewdb.client.RefNames) ChangeData.asChanges(com.google.gerrit.server.query.change.ChangeData.asChanges) Optional(java.util.Optional) Providers(com.google.inject.util.Providers) Pattern(java.util.regex.Pattern) NotesMigration(com.google.gerrit.server.notedb.NotesMigration) GroupReference(com.google.gerrit.common.data.GroupReference) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeIndexRewriter(com.google.gerrit.server.index.change.ChangeIndexRewriter) AccountCache(com.google.gerrit.server.account.AccountCache) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Change(com.google.gerrit.reviewdb.client.Change) GroupBackends(com.google.gerrit.server.account.GroupBackends) CommentsUtil(com.google.gerrit.server.CommentsUtil) Function(java.util.function.Function) Enums(com.google.common.base.Enums) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) HashSet(java.util.HashSet) NotSignedInException(com.google.gerrit.common.errors.NotSignedInException) IndexConfig(com.google.gerrit.server.index.IndexConfig) Lists(com.google.common.collect.Lists) Account(com.google.gerrit.reviewdb.client.Account) SchemaUtil(com.google.gerrit.server.index.SchemaUtil) CurrentUser(com.google.gerrit.server.CurrentUser) AccountResolver(com.google.gerrit.server.account.AccountResolver) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) PatchListCache(com.google.gerrit.server.patch.PatchListCache) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) VersionedAccountDestinations(com.google.gerrit.server.account.VersionedAccountDestinations) Ints(com.google.common.primitives.Ints) ChangeField(com.google.gerrit.server.index.change.ChangeField) QueryParseException(com.google.gerrit.server.query.QueryParseException) Collectors.toList(java.util.stream.Collectors.toList) Address(com.google.gerrit.server.mail.Address) LimitPredicate(com.google.gerrit.server.query.LimitPredicate) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ListMembers(com.google.gerrit.server.group.ListMembers) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) TrackingFooters(com.google.gerrit.server.config.TrackingFooters) FieldDef(com.google.gerrit.server.index.FieldDef) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) VisibleForTesting(com.google.common.annotations.VisibleForTesting) QueryBuilder(com.google.gerrit.server.query.QueryBuilder) ChangeIndex(com.google.gerrit.server.index.change.ChangeIndex) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) StarredChangesUtil(com.google.gerrit.server.StarredChangesUtil) QueryParseException(com.google.gerrit.server.query.QueryParseException) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Aggregations

QueryParseException (com.google.gerrit.server.query.QueryParseException)19 OrmException (com.google.gwtorm.server.OrmException)7 Account (com.google.gerrit.reviewdb.client.Account)6 Predicate (com.google.gerrit.server.query.Predicate)6 IOException (java.io.IOException)5 Repository (org.eclipse.jgit.lib.Repository)5 Change (com.google.gerrit.reviewdb.client.Change)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)4 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)3 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)3 VersionedAccountDestinations (com.google.gerrit.server.account.VersionedAccountDestinations)3 VersionedAccountQueries (com.google.gerrit.server.account.VersionedAccountQueries)3 ChangeData (com.google.gerrit.server.query.change.ChangeData)3 Map (java.util.Map)3 Collectors.toList (java.util.stream.Collectors.toList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2