private BatchMetaDataUpdate saveToBatch( ChangeControl ctl, ChangeUpdate callerUpdate, LabelNormalizer.Result normalized, Timestamp timestamp) throws IOException { Table<Account.Id, String, Optional<Short>> byUser = HashBasedTable.create(); for (PatchSetApproval psa : normalized.updated()) { byUser.put(psa.getAccountId(), psa.getLabel(), Optional.of(psa.getValue())); } for (PatchSetApproval psa : normalized.deleted()) { byUser.put(psa.getAccountId(), psa.getLabel(), Optional.<Short>absent()); } BatchMetaDataUpdate batch = callerUpdate.openUpdate(); for (Account.Id accountId : byUser.rowKeySet()) { if (!accountId.equals(callerUpdate.getUser().getAccountId())) { ChangeUpdate update = updateFactory.create(ctl.forUser(identifiedUserFactory.create(accountId)), timestamp); update.setSubject("Finalize approvals at submit"); putApprovals(update, byUser.row(accountId)); CommitBuilder commit = new CommitBuilder(); commit.setCommitter(new PersonIdent(serverIdent, timestamp)); batch.write(update, commit); } } putApprovals(callerUpdate, byUser.row(callerUpdate.getUser().getAccountId())); return batch; }
private BatchMetaDataUpdate approve( ChangeControl control, PatchSet.Id psId, IdentifiedUser user, ChangeUpdate update, Timestamp timestamp) throws OrmException { Map<PatchSetApproval.Key, PatchSetApproval> byKey = Maps.newHashMap(); for (PatchSetApproval psa : approvalsUtil.byPatchSet(db, control, psId)) { if (!byKey.containsKey(psa.getKey())) { byKey.put(psa.getKey(), psa); } } PatchSetApproval submit = new PatchSetApproval( new PatchSetApproval.Key(psId, user.getAccountId(), LabelId.SUBMIT), (short) 1, TimeUtil.nowTs()); byKey.put(submit.getKey(), submit); submit.setValue((short) 1); submit.setGranted(timestamp); // Flatten out existing approvals for this patch set based upon the current // permissions. Once the change is closed the approvals are not updated at // presentation view time, except for zero votes used to indicate a reviewer // was added. So we need to make sure votes are accurate now. This way if // permissions get modified in the future, historical records stay accurate. LabelNormalizer.Result normalized = labelNormalizer.normalize(control, byKey.values()); // TODO(dborowitz): Don't use a label in notedb; just check when status // change happened. update.putApproval(submit.getLabel(), submit.getValue()); logDebug("Adding submit label " + submit); db.patchSetApprovals().upsert(normalized.getNormalized()); db.patchSetApprovals().delete(normalized.deleted()); try { return saveToBatch(control, update, normalized, timestamp); } catch (IOException e) { throw new OrmException(e); } }