Search in sources :

Example 16 with QueryParseException

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

the class SetLabel method updateLabel.

/**
 * Updates the given label.
 *
 * @param config the project config
 * @param labelType the label type that should be updated
 * @param input the input that describes the label update
 * @return whether the label type was modified
 * @throws BadRequestException if there was invalid data in the input
 * @throws ResourceConflictException if the update cannot be applied due to a conflict
 */
public boolean updateLabel(ProjectConfig config, LabelType labelType, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
    boolean dirty = false;
    LabelType.Builder labelTypeBuilder = labelType.toBuilder();
    if (input.name != null) {
        String newName = input.name.trim();
        if (newName.isEmpty()) {
            throw new BadRequestException("name cannot be empty");
        }
        if (!newName.equals(labelType.getName())) {
            if (config.getLabelSections().containsKey(newName)) {
                throw new ResourceConflictException(String.format("name %s already in use", newName));
            }
            for (String labelName : config.getLabelSections().keySet()) {
                if (labelName.equalsIgnoreCase(newName)) {
                    throw new ResourceConflictException(String.format("name %s conflicts with existing label %s", newName, labelName));
                }
            }
            try {
                LabelType.checkName(newName);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException("invalid name: " + input.name, e);
            }
            labelTypeBuilder.setName(newName);
            dirty = true;
        }
    }
    if (input.description != null) {
        String description = Strings.emptyToNull(input.description.trim());
        labelTypeBuilder.setDescription(Optional.ofNullable(description));
        dirty = true;
    }
    if (input.function != null) {
        if (input.function.trim().isEmpty()) {
            throw new BadRequestException("function cannot be empty");
        }
        labelTypeBuilder.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
        dirty = true;
    }
    if (input.values != null) {
        if (input.values.isEmpty()) {
            throw new BadRequestException("values cannot be empty");
        }
        labelTypeBuilder.setValues(LabelDefinitionInputParser.parseValues(input.values));
        dirty = true;
    }
    if (input.defaultValue != null) {
        labelTypeBuilder.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelTypeBuilder, input.defaultValue));
        dirty = true;
    }
    if (input.branches != null) {
        labelTypeBuilder.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
        dirty = true;
    }
    if (input.canOverride != null) {
        labelTypeBuilder.setCanOverride(input.canOverride);
        dirty = true;
    }
    input.copyCondition = Strings.emptyToNull(input.copyCondition);
    if (input.copyCondition != null) {
        try {
            approvalQueryBuilder.parse(input.copyCondition);
        } catch (QueryParseException e) {
            throw new BadRequestException("unable to parse copy condition. got: " + input.copyCondition + ". " + e.getMessage(), e);
        }
        labelTypeBuilder.setCopyCondition(input.copyCondition);
        dirty = true;
        if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
            throw new BadRequestException("can't set and unset copyCondition in the same request");
        }
    }
    if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
        labelTypeBuilder.setCopyCondition(null);
        dirty = true;
    }
    if (input.copyAnyScore != null) {
        labelTypeBuilder.setCopyAnyScore(input.copyAnyScore);
        dirty = true;
    }
    if (input.copyMinScore != null) {
        labelTypeBuilder.setCopyMinScore(input.copyMinScore);
        dirty = true;
    }
    if (input.copyMaxScore != null) {
        labelTypeBuilder.setCopyMaxScore(input.copyMaxScore);
        dirty = true;
    }
    if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
        labelTypeBuilder.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
        dirty = true;
    }
    if (input.copyAllScoresIfNoChange != null) {
        labelTypeBuilder.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
        dirty = true;
    }
    if (input.copyAllScoresIfNoCodeChange != null) {
        labelTypeBuilder.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
        dirty = true;
    }
    if (input.copyAllScoresOnTrivialRebase != null) {
        labelTypeBuilder.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
        dirty = true;
    }
    if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
        labelTypeBuilder.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
        dirty = true;
    }
    if (input.copyValues != null) {
        labelTypeBuilder.setCopyValues(input.copyValues);
        dirty = true;
    }
    if (input.allowPostSubmit != null) {
        labelTypeBuilder.setAllowPostSubmit(input.allowPostSubmit);
        dirty = true;
    }
    if (input.ignoreSelfApproval != null) {
        labelTypeBuilder.setIgnoreSelfApproval(input.ignoreSelfApproval);
        dirty = true;
    }
    config.getLabelSections().remove(labelType.getName());
    config.upsertLabelType(labelTypeBuilder.build());
    return dirty;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 17 with QueryParseException

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

the class Index method get.

/**
 * Get a single document from the index.
 *
 * @param key document key.
 * @param opts query options. Options that do not make sense in the context of a single document,
 *     such as start, will be ignored.
 * @return a single document if present.
 */
default Optional<V> get(K key, QueryOptions opts) {
    opts = opts.withStart(0).withLimit(2);
    ImmutableList<V> results;
    try {
        results = getSource(keyPredicate(key), opts).read().toList();
    } catch (QueryParseException e) {
        throw new StorageException("Unexpected QueryParseException during get()", e);
    }
    if (results.size() > 1) {
        throw new StorageException("Multiple results found in index for key " + key + ": " + results);
    }
    return results.stream().findFirst();
}
Also used : StorageException(com.google.gerrit.exceptions.StorageException) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 18 with QueryParseException

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

the class Index method getRaw.

/**
 * Get a single raw document from the index.
 *
 * @param key document key.
 * @param opts query options. Options that do not make sense in the context of a single document,
 *     such as start, will be ignored.
 * @return an abstraction of a raw index document to retrieve fields from.
 */
default Optional<FieldBundle> getRaw(K key, QueryOptions opts) {
    opts = opts.withStart(0).withLimit(2);
    ImmutableList<FieldBundle> results;
    try {
        results = getSource(keyPredicate(key), opts).readRaw().toList();
    } catch (QueryParseException e) {
        throw new StorageException("Unexpected QueryParseException during get()", e);
    }
    if (results.size() > 1) {
        throw new StorageException("Multiple results found in index for key " + key + ": " + results);
    }
    return results.stream().findFirst();
}
Also used : FieldBundle(com.google.gerrit.index.query.FieldBundle) StorageException(com.google.gerrit.exceptions.StorageException) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 19 with QueryParseException

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

the class ProjectWatch method add.

private boolean add(Watchers matching, Account.Id accountId, ProjectWatchKey key, Set<NotifyConfig.NotifyType> watchedTypes, NotifyConfig.NotifyType type) {
    logger.atFine().log("Checking project watch %s of account %s", key, accountId);
    IdentifiedUser user = args.identifiedUserFactory.create(accountId);
    try {
        if (filterMatch(user, key.filter())) {
            // Otherwise, still return true to stop notifications for this user.
            if (watchedTypes.contains(type)) {
                matching.bcc.accounts.add(accountId);
            }
            logger.atFine().log("Added account %s as watcher", accountId);
            return true;
        }
        logger.atFine().log("The filter did not match for account %s; skip notification", accountId);
    } catch (QueryParseException e) {
        // Ignore broken filter expressions.
        logger.atInfo().log("Account %s has invalid filter in project watch %s: %s", accountId, key, e.getMessage());
    }
    return false;
}
Also used : IdentifiedUser(com.google.gerrit.server.IdentifiedUser) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 20 with QueryParseException

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

the class CreateLabel method createLabel.

/**
 * Creates a new label.
 *
 * @param config the project config
 * @param label the name of the new label
 * @param input the input that describes the new label
 * @return the created label type
 * @throws BadRequestException if there was invalid data in the input
 * @throws ResourceConflictException if the label cannot be created due to a conflict
 */
public LabelType createLabel(ProjectConfig config, String label, LabelDefinitionInput input) throws BadRequestException, ResourceConflictException {
    if (config.getLabelSections().containsKey(label)) {
        throw new ResourceConflictException(String.format("label %s already exists", label));
    }
    for (String labelName : config.getLabelSections().keySet()) {
        if (labelName.equalsIgnoreCase(label)) {
            throw new ResourceConflictException(String.format("label %s conflicts with existing label %s", label, labelName));
        }
    }
    if (input.values == null || input.values.isEmpty()) {
        throw new BadRequestException("values are required");
    }
    try {
        LabelType.checkName(label);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("invalid name: " + label, e);
    }
    List<LabelValue> values = LabelDefinitionInputParser.parseValues(input.values);
    LabelType.Builder labelType = LabelType.builder(LabelType.checkName(label), values);
    if (input.description != null) {
        String description = Strings.emptyToNull(input.description.trim());
        labelType.setDescription(Optional.ofNullable(description));
    }
    if (input.function != null && !input.function.trim().isEmpty()) {
        labelType.setFunction(LabelDefinitionInputParser.parseFunction(input.function));
    } else {
        labelType.setFunction(LabelFunction.MAX_WITH_BLOCK);
    }
    if (input.defaultValue != null) {
        labelType.setDefaultValue(LabelDefinitionInputParser.parseDefaultValue(labelType, input.defaultValue));
    }
    if (input.branches != null) {
        labelType.setRefPatterns(LabelDefinitionInputParser.parseBranches(input.branches));
    }
    if (input.canOverride != null) {
        labelType.setCanOverride(input.canOverride);
    }
    if (input.copyAnyScore != null) {
        labelType.setCopyAnyScore(input.copyAnyScore);
    }
    if (input.copyCondition != null) {
        try {
            approvalQueryBuilder.parse(input.copyCondition);
        } catch (QueryParseException e) {
            throw new BadRequestException("unable to parse copy condition. got: " + input.copyCondition + ". " + e.getMessage(), e);
        }
        if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
            throw new BadRequestException("can't set and unset copyCondition in the same request");
        }
        labelType.setCopyCondition(Strings.emptyToNull(input.copyCondition));
    }
    if (Boolean.TRUE.equals(input.unsetCopyCondition)) {
        labelType.setCopyCondition(null);
    }
    if (input.copyMinScore != null) {
        labelType.setCopyMinScore(input.copyMinScore);
    }
    if (input.copyMaxScore != null) {
        labelType.setCopyMaxScore(input.copyMaxScore);
    }
    if (input.copyAllScoresIfListOfFilesDidNotChange != null) {
        labelType.setCopyAllScoresIfListOfFilesDidNotChange(input.copyAllScoresIfListOfFilesDidNotChange);
    }
    if (input.copyAllScoresIfNoChange != null) {
        labelType.setCopyAllScoresIfNoChange(input.copyAllScoresIfNoChange);
    }
    if (input.copyAllScoresIfNoCodeChange != null) {
        labelType.setCopyAllScoresIfNoCodeChange(input.copyAllScoresIfNoCodeChange);
    }
    if (input.copyAllScoresOnTrivialRebase != null) {
        labelType.setCopyAllScoresOnTrivialRebase(input.copyAllScoresOnTrivialRebase);
    }
    if (input.copyAllScoresOnMergeFirstParentUpdate != null) {
        labelType.setCopyAllScoresOnMergeFirstParentUpdate(input.copyAllScoresOnMergeFirstParentUpdate);
    }
    if (input.copyValues != null) {
        labelType.setCopyValues(input.copyValues);
    }
    if (input.allowPostSubmit != null) {
        labelType.setAllowPostSubmit(input.allowPostSubmit);
    }
    if (input.ignoreSelfApproval != null) {
        labelType.setIgnoreSelfApproval(input.ignoreSelfApproval);
    }
    LabelType lt = labelType.build();
    config.upsertLabelType(lt);
    return lt;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IdString(com.google.gerrit.extensions.restapi.IdString) QueryParseException(com.google.gerrit.index.query.QueryParseException)

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