void add()

in src/main/java/com/googlesource/gerrit/plugins/importer/AddApprovalsStep.java [81:127]


  void add(GerritApi api)
      throws OrmException, NoSuchChangeException, IOException, NoSuchAccountException,
          RestApiException, ConfigInvalidException {
    if (resume) {
      db.patchSetApprovals().delete(db.patchSetApprovals().byChange(change.getId()));
    }

    List<PatchSetApproval> approvals = new ArrayList<>();
    for (Entry<String, LabelInfo> e : changeInfo.labels.entrySet()) {
      String labelName = e.getKey();
      LabelInfo label = e.getValue();
      if (label.all != null) {
        for (ApprovalInfo a : label.all) {
          Account.Id user = accountUtil.resolveUser(api, a);
          ChangeData cd = changeDataFactory.create(db, change);
          LabelType labelType = cd.getLabelTypes().byLabel(labelName);
          if (labelType == null) {
            log.warn(
                String.format(
                    "Label '%s' not found in target system."
                        + " This label was referenced by an approval provided from '%s'"
                        + " for change '%s'."
                        + " This approval will be skipped. In order to import this"
                        + " approval configure the missing label and resume the import"
                        + " with the force option.",
                    labelName, a.username, changeInfo.id));
            continue;
          }
          short shortValue = a.value != null ? a.value.shortValue() : 0;
          approvals.add(
              new PatchSetApproval(
                  new PatchSetApproval.Key(
                      change.currentPatchSetId(), user, labelType.getLabelId()),
                  shortValue,
                  MoreObjects.firstNonNull(a.date, TimeUtil.nowTs())));
          ChangeUpdate update = updateFactory.create(cd.notes(), genericUserFactory.create(user));
          if (shortValue != 0) {
            update.putApproval(labelName, shortValue);
          } else {
            update.removeApproval(labelName);
          }
          update.commit();
        }
      }
    }
    db.patchSetApprovals().insert(approvals);
  }