PostGpgKeys: Don't attempt to send notification when no keys were added

If no keys were added in the request (i.e. if it only contained keys to
be deleted), it's not necessary to create the "GPG key added" sender.

Also factor out the user to a variable and reuse it.

Change-Id: I9f618bf6bdcc04cc97d4c0e5824afe102619e998
This commit is contained in:
David Pursehouse
2019-04-25 10:45:48 +09:00
parent 66d912defd
commit 5a2159838a

View File

@@ -196,10 +196,11 @@ public class PostGpgKeys implements RestModifyView<AccountResource, Input> {
throws BadRequestException, ResourceConflictException, PGPException, IOException {
try (PublicKeyStore store = storeProvider.get()) {
List<String> addedKeys = new ArrayList<>();
IdentifiedUser user = rsrc.getUser();
for (PGPPublicKeyRing keyRing : keyRings) {
PGPPublicKey key = keyRing.getPublicKey();
// Don't check web of trust; admins can fill in certifications later.
CheckResult result = checkerFactory.create(rsrc.getUser(), store).disableTrust().check(key);
CheckResult result = checkerFactory.create(user, store).disableTrust().check(key);
if (!result.isOk()) {
throw new BadRequestException(
String.format(
@@ -214,7 +215,7 @@ public class PostGpgKeys implements RestModifyView<AccountResource, Input> {
}
CommitBuilder cb = new CommitBuilder();
PersonIdent committer = serverIdent.get();
cb.setAuthor(rsrc.getUser().newCommitterIdent(committer.getWhen(), committer.getTimeZone()));
cb.setAuthor(user.newCommitterIdent(committer.getWhen(), committer.getTimeZone()));
cb.setCommitter(committer);
RefUpdate.Result saveResult = store.save(cb);
@@ -222,14 +223,15 @@ public class PostGpgKeys implements RestModifyView<AccountResource, Input> {
case NEW:
case FAST_FORWARD:
case FORCED:
if (!addedKeys.isEmpty()) {
try {
addKeyFactory.create(rsrc.getUser(), addedKeys).send();
addKeyFactory.create(user, addedKeys).send();
} catch (EmailException e) {
log.error(
"Cannot send GPG key added message to "
+ rsrc.getUser().getAccount().getPreferredEmail(),
"Cannot send GPG key added message to " + user.getAccount().getPreferredEmail(),
e);
}
}
break;
case NO_CHANGE:
break;