com.google.gerrit.entities.SubmitRequirementExpressionResult Maven / Gradle / Ivy
// Copyright (C) 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.entities;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import java.util.Optional;
/** Result of evaluating a submit requirement expression on a given Change. */
@AutoValue
public abstract class SubmitRequirementExpressionResult {
  /** Submit requirement expression for which this result is evaluated. */
  public abstract SubmitRequirementExpression expression();
  /** Status of evaluation. */
  public abstract Status status();
  /**
   * Optional error message. Populated if the evaluator fails to evaluate the expression for a
   * certain change.
   */
  public abstract Optional errorMessage();
  /**
   * List leaf predicates that are fulfilled, for example the expression
   *
   * label:Code-Review=+2 and branch:refs/heads/master
   *
   * 
has two leaf predicates:
   *
   * 
   *   - label:Code-Review=+2
   *   
 - branch:refs/heads/master
   * 
 
   *
   * This method will return the leaf predicates that were fulfilled, for example if only the first
   * predicate was fulfilled, the returned list will be equal to ["label:Code-Review=+2"].
   */
  public abstract ImmutableList passingAtoms();
  /**
   * List of leaf predicates that are not fulfilled. See {@link #passingAtoms()} for more details.
   */
  public abstract ImmutableList failingAtoms();
  /**
   * Map of leaf predicates to their explanations.
   *
   * This is used to provide more information about complex atoms, which may otherwise be opaque
   * and hard to debug.
   *
   * 
This will only be populated/implemented for some atoms.
   */
  public abstract Optional> atomExplanations();
  public static SubmitRequirementExpressionResult create(
      SubmitRequirementExpression expression, PredicateResult predicateResult) {
    return create(
        expression,
        predicateResult.status() ? Status.PASS : Status.FAIL,
        predicateResult.getPassingAtoms(),
        predicateResult.getFailingAtoms(),
        Optional.of(predicateResult.getAtomExplanations()));
  }
  public static SubmitRequirementExpressionResult create(
      SubmitRequirementExpression expression,
      Status status,
      ImmutableList passingAtoms,
      ImmutableList failingAtoms) {
    return create(expression, status, passingAtoms, failingAtoms, Optional.empty());
  }
  public static SubmitRequirementExpressionResult create(
      SubmitRequirementExpression expression,
      Status status,
      ImmutableList passingAtoms,
      ImmutableList failingAtoms,
      Optional> atomExplanations) {
    return create(
        expression, status, passingAtoms, failingAtoms, atomExplanations, Optional.empty());
  }
  public static SubmitRequirementExpressionResult create(
      SubmitRequirementExpression expression,
      Status status,
      ImmutableList passingAtoms,
      ImmutableList failingAtoms,
      Optional> atomExplanations,
      Optional errorMessage) {
    return new AutoValue_SubmitRequirementExpressionResult(
        expression, status, errorMessage, passingAtoms, failingAtoms, atomExplanations);
  }
  public static SubmitRequirementExpressionResult error(
      SubmitRequirementExpression expression, String errorMessage) {
    return new AutoValue_SubmitRequirementExpressionResult(
        expression,
        Status.ERROR,
        Optional.of(errorMessage),
        ImmutableList.of(),
        ImmutableList.of(),
        Optional.empty());
  }
  public static SubmitRequirementExpressionResult notEvaluated(SubmitRequirementExpression expr) {
    return SubmitRequirementExpressionResult.create(
        expr, Status.NOT_EVALUATED, ImmutableList.of(), ImmutableList.of());
  }
  public static TypeAdapter typeAdapter(Gson gson) {
    return new AutoValue_SubmitRequirementExpressionResult.GsonTypeAdapter(gson);
  }
  public abstract Builder toBuilder();
  @AutoValue.Builder
  public abstract static class Builder {
    public abstract Builder expression(SubmitRequirementExpression expression);
    public abstract Builder status(Status status);
    public abstract Builder errorMessage(Optional errorMessage);
    public abstract Builder passingAtoms(ImmutableList passingAtoms);
    public abstract Builder failingAtoms(ImmutableList failingAtoms);
    public abstract Builder atomExplanations(
        Optional> atomExplanations);
    public abstract SubmitRequirementExpressionResult build();
  }
  public enum Status {
    /** Submit requirement expression is fulfilled for a given change. */
    PASS,
    /** Submit requirement expression is failing for a given change. */
    FAIL,
    /** Submit requirement expression contains invalid syntax and is not parsable. */
    ERROR,
    /** Submit requirement expression was not evaluated. */
    NOT_EVALUATED
  }
  /**
   * Entity detailing the result of evaluating a predicate.
   *
   * Example - branch:refs/heads/foo and has:unresolved
   *
   * 
The above predicate is an "And" predicate having two child predicates:
   *
   * 
   *   - branch:refs/heads/foo
   *   
 - has:unresolved
   * 
 
   *
   * Each child predicate as well as the parent contains the result of its evaluation.
   */
  @AutoValue
  public abstract static class PredicateResult {
    abstract ImmutableList childPredicateResults();
    /** We only set this field for leaf predicates. */
    public abstract String predicateString();
    /** true if the predicate is passing for a given change. */
    abstract boolean status();
    /**
     * An explanation of the predicate result.
     *
     * This is used to provide more information about complex atoms, which may otherwise be
     * opaque and hard to debug.
     *
     * 
This will be empty for most predicate results and all non-leaf predicates.
     */
    public abstract String explanation();
    /** Returns a list of leaf predicate results whose {@link PredicateResult#status()} is true. */
    ImmutableList getPassingAtoms() {
      return getAtoms(/* status= */ true).stream()
          .map(PredicateResult::predicateString)
          .collect(ImmutableList.toImmutableList());
    }
    /** Returns a list of leaf predicate results whose {@link PredicateResult#status()} is false. */
    ImmutableList getFailingAtoms() {
      return getAtoms(/* status= */ false).stream()
          .map(PredicateResult::predicateString)
          .collect(ImmutableList.toImmutableList());
    }
    ImmutableMap getAtomExplanations() {
      return getAtoms().stream()
          .collect(
              ImmutableMap.toImmutableMap(
                  PredicateResult::predicateString, PredicateResult::explanation, (a, b) -> a));
    }
    /** Returns the list of leaf {@link PredicateResult}. */
    private ImmutableList getAtoms() {
      ImmutableList.Builder atomsList = ImmutableList.builder();
      getAtomsRecursively(atomsList);
      return atomsList.build();
    }
    /**
     * Returns the list of leaf {@link PredicateResult} whose {@link #status()} is equal to the
     * {@code status} parameter.
     */
    private ImmutableList getAtoms(boolean status) {
      ImmutableList.Builder atomsList = ImmutableList.builder();
      getAtomsRecursively(atomsList, status);
      return atomsList.build();
    }
    private void getAtomsRecursively(ImmutableList.Builder list) {
      if (!predicateString().isEmpty()) {
        list.add(this);
        return;
      }
      childPredicateResults().forEach(c -> c.getAtomsRecursively(list));
    }
    private void getAtomsRecursively(ImmutableList.Builder list, boolean status) {
      if (!predicateString().isEmpty() && status() == status) {
        list.add(this);
        return;
      }
      childPredicateResults().forEach(c -> c.getAtomsRecursively(list, status));
    }
    public static Builder builder() {
      return new AutoValue_SubmitRequirementExpressionResult_PredicateResult.Builder();
    }
    @AutoValue.Builder
    public abstract static class Builder {
      protected abstract ImmutableList.Builder childPredicateResultsBuilder();
      public abstract Builder predicateString(String value);
      public abstract Builder status(boolean value);
      public abstract Builder explanation(String value);
      @CanIgnoreReturnValue
      public Builder addChildPredicateResult(PredicateResult result) {
        childPredicateResultsBuilder().add(result);
        return this;
      }
      public abstract PredicateResult build();
    }
  }
}