コード例 #1
0
 private PrologEnvironment getPrologEnvironment(CurrentUser user) throws RuleEvalException {
   ProjectState projectState = control.getProjectControl().getProjectState();
   PrologEnvironment env;
   try {
     if (rule == null) {
       env = projectState.newPrologEnvironment();
     } else {
       env = projectState.newPrologEnvironment("stdin", new StringReader(rule));
     }
   } catch (CompileException err) {
     String msg;
     if (rule == null && control.getProjectControl().isOwner()) {
       msg = String.format("Cannot load rules.pl for %s: %s", getProjectName(), err.getMessage());
     } else if (rule != null) {
       msg = err.getMessage();
     } else {
       msg = String.format("Cannot load rules.pl for %s", getProjectName());
     }
     throw new RuleEvalException(msg, err);
   }
   env.set(StoredValues.REVIEW_DB, cd.db());
   env.set(StoredValues.CHANGE_DATA, cd);
   env.set(StoredValues.CHANGE_CONTROL, control);
   if (user != null) {
     env.set(StoredValues.CURRENT_USER, user);
   }
   return env;
 }
コード例 #2
0
 private List<SubmitRecord> cannotSubmitDraft() {
   try {
     if (!control.isDraftVisible(cd.db(), cd)) {
       return createRuleError("Patch set " + patchSet.getId() + " not found");
     }
     initPatchSet();
     if (patchSet.isDraft()) {
       return createRuleError("Cannot submit draft patch sets");
     } else {
       return createRuleError("Cannot submit draft changes");
     }
   } catch (OrmException err) {
     String msg = "Cannot check visibility of patch set " + patchSet.getId();
     log.error(msg, err);
     return createRuleError(msg);
   }
 }
コード例 #3
0
  /**
   * Evaluate the submit type rules to get the submit type.
   *
   * @return record from the evaluated rules.
   */
  public SubmitTypeRecord getSubmitType() {
    try {
      initPatchSet();
    } catch (OrmException e) {
      return typeError("Error looking up patch set " + control.getChange().currentPatchSetId());
    }

    try {
      if (control.getChange().getStatus() == Change.Status.DRAFT
          && !control.isDraftVisible(cd.db(), cd)) {
        return SubmitTypeRecord.error("Patch set " + patchSet.getId() + " not found");
      }
      if (patchSet.isDraft() && !control.isDraftVisible(cd.db(), cd)) {
        return SubmitTypeRecord.error("Patch set " + patchSet.getId() + " not found");
      }
    } catch (OrmException err) {
      String msg = "Cannot read patch set " + patchSet.getId();
      log.error(msg, err);
      return SubmitTypeRecord.error(msg);
    }

    List<Term> results;
    try {
      results =
          evaluateImpl(
              "locate_submit_type",
              "get_submit_type",
              "locate_submit_type_filter",
              "filter_submit_type_results",
              // Do not include current user in submit type evaluation. This is used
              // for mergeability checks, which are stored persistently and so must
              // have a consistent view of the submit type.
              null);
    } catch (RuleEvalException e) {
      return typeError(e.getMessage(), e);
    }

    if (results.isEmpty()) {
      // Should never occur for a well written rule
      return typeError(
          "Submit rule '"
              + getSubmitRuleName()
              + "' for change "
              + cd.getId()
              + " of "
              + getProjectName()
              + " has no solution.");
    }

    Term typeTerm = results.get(0);
    if (!(typeTerm instanceof SymbolTerm)) {
      return typeError(
          "Submit rule '"
              + getSubmitRuleName()
              + "' for change "
              + cd.getId()
              + " of "
              + getProjectName()
              + " did not return a symbol.");
    }

    String typeName = ((SymbolTerm) typeTerm).name();
    try {
      return SubmitTypeRecord.OK(SubmitType.valueOf(typeName.toUpperCase()));
    } catch (IllegalArgumentException e) {
      return typeError(
          "Submit type rule "
              + getSubmitRule()
              + " for change "
              + cd.getId()
              + " of "
              + getProjectName()
              + " output invalid result: "
              + typeName);
    }
  }