Search in sources :

Example 1 with ScriptException

use of org.projectnessie.cel.tools.ScriptException in project nessie by projectnessie.

the class TreeApiImpl method filterReferences.

/**
 * Applies different filters to the {@link Stream} of references on the filter.
 *
 * @param references The references that different filters will be applied to
 * @param filter The filter to filter by
 * @return A potentially filtered {@link Stream} of commits based on the filter
 */
private Stream<Reference> filterReferences(Stream<Reference> references, String filter) {
    if (Strings.isNullOrEmpty(filter)) {
        return references;
    }
    final Script script;
    try {
        script = SCRIPT_HOST.buildScript(filter).withContainer(CONTAINER).withDeclarations(REFERENCES_DECLARATIONS).withTypes(REFERENCES_TYPES).build();
    } catch (ScriptException e) {
        throw new IllegalArgumentException(e);
    }
    return references.filter(reference -> {
        try {
            ReferenceMetadata refMeta = reference.getMetadata();
            if (refMeta == null) {
                refMeta = CELUtil.EMPTY_REFERENCE_METADATA;
            }
            CommitMeta commit = refMeta.getCommitMetaOfHEAD();
            if (commit == null) {
                commit = CELUtil.EMPTY_COMMIT_META;
            }
            return script.execute(Boolean.class, ImmutableMap.of(VAR_REF, reference, VAR_REF_TYPE, reference.getType().name(), VAR_COMMIT, commit, VAR_REF_META, refMeta));
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Script(org.projectnessie.cel.tools.Script) ScriptException(org.projectnessie.cel.tools.ScriptException) CommitMeta(org.projectnessie.model.CommitMeta) ReferenceMetadata(org.projectnessie.model.ReferenceMetadata) ImmutableReferenceMetadata(org.projectnessie.model.ImmutableReferenceMetadata)

Example 2 with ScriptException

use of org.projectnessie.cel.tools.ScriptException in project nessie by projectnessie.

the class TreeApiImpl method filterCommitLog.

/**
 * Applies different filters to the {@link Stream} of commits based on the filter.
 *
 * @param logEntries The commit log that different filters will be applied to
 * @param filter The filter to filter by
 * @return A potentially filtered {@link Stream} of commits based on the filter
 */
private Stream<LogEntry> filterCommitLog(Stream<LogEntry> logEntries, String filter) {
    if (Strings.isNullOrEmpty(filter)) {
        return logEntries;
    }
    final Script script;
    try {
        script = SCRIPT_HOST.buildScript(filter).withContainer(CONTAINER).withDeclarations(COMMIT_LOG_DECLARATIONS).withTypes(COMMIT_LOG_TYPES).build();
    } catch (ScriptException e) {
        throw new IllegalArgumentException(e);
    }
    return logEntries.filter(logEntry -> {
        try {
            List<Operation> operations = logEntry.getOperations();
            if (operations == null) {
                operations = Collections.emptyList();
            }
            // ContentKey has some @JsonIgnore attributes, which would otherwise not be accessible.
            List<Object> operationsForCel = operations.stream().map(CELUtil::forCel).collect(Collectors.toList());
            return script.execute(Boolean.class, ImmutableMap.of(VAR_COMMIT, logEntry.getCommitMeta(), VAR_OPERATIONS, operationsForCel));
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Script(org.projectnessie.cel.tools.Script) ScriptException(org.projectnessie.cel.tools.ScriptException) Operation(org.projectnessie.model.Operation)

Aggregations

Script (org.projectnessie.cel.tools.Script)2 ScriptException (org.projectnessie.cel.tools.ScriptException)2 CommitMeta (org.projectnessie.model.CommitMeta)1 ImmutableReferenceMetadata (org.projectnessie.model.ImmutableReferenceMetadata)1 Operation (org.projectnessie.model.Operation)1 ReferenceMetadata (org.projectnessie.model.ReferenceMetadata)1