Avoid unclear exception when no bot user exists.
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
index 78d6a81..f300fb1 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
@@ -79,9 +79,7 @@
* @throws OrmException
*/
public boolean hasDependentReview(final int number) throws IOException, NoSuchChangeException, OrmException {
- final Set<Account.Id> ids = byEmailCache.get(config.getBotEmail());
- final IdentifiedUser bot = factory.create(ids.iterator().next());
- final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), bot);
+ final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), getBotUser());
final ChangeData changeData = changeDataFactory.create(db.get(), new Change.Id(number));
final RevisionResource r = new RevisionResource(collection.parse(ctl), changeData.currentPatchSet());
final RelatedInfo related = getRelated.apply(r);
@@ -135,12 +133,19 @@
public void mergeReview(ChangeInfo info) throws RestApiException, NoSuchChangeException, OrmException, IOException {
final SubmitInput input = new SubmitInput();
input.waitForMerge = true;
- final Set<Account.Id> ids = byEmailCache.get(config.getBotEmail());
- final IdentifiedUser bot = factory.create(ids.iterator().next());
- final ChangeControl ctl = changeFactory.controlFor(new Change.Id(info._number), bot);
+ final ChangeControl ctl = changeFactory.controlFor(new Change.Id(info._number), getBotUser());
final ChangeData changeData = changeDataFactory.create(db.get(), new Change.Id(info._number));
final RevisionResource r = new RevisionResource(collection.parse(ctl), changeData.currentPatchSet());
submitter.apply(r, input);
}
+
+ public IdentifiedUser getBotUser() {
+ final Set<Account.Id> ids = byEmailCache.get(config.getBotEmail());
+ if (ids.isEmpty()) {
+ throw new RuntimeException("No user found with email: " + config.getBotEmail());
+ }
+ final IdentifiedUser bot = factory.create(ids.iterator().next());
+ return bot;
+ }
}
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java b/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
index 18e0b89..23ffd2a 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
@@ -56,14 +56,14 @@
@Inject
Provider<PostReview> reviewer;
+ @Inject
+ private AtomicityHelper atomicityHelper;
+
public void commentOnReview(final int number, final String commentTemplate) throws RestApiException, OrmException, IOException, NoSuchChangeException {
final ReviewInput message = new ReviewInput();
message.message = getCommentFromFile(commentTemplate);
- final Set<Account.Id> ids = byEmailCache.get(config.getBotEmail());
- final IdentifiedUser bot = factory.create(ids.iterator().next());
- final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), bot);
+ final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), atomicityHelper.getBotUser());
final ChangeData changeData = changeDataFactory.create(db.get(), new Change.Id(number));
-
final RevisionResource r = new RevisionResource(collection.parse(ctl), changeData.currentPatchSet());
reviewer.get().apply(r, message);
}
@@ -84,11 +84,8 @@
final ReviewInput message = new ReviewInput();
message.message = getCommentFromFile(commentTemplate);
message.label("Code-Review", -2);
- final Set<Account.Id> ids = byEmailCache.get(config.getBotEmail());
- final IdentifiedUser bot = factory.create(ids.iterator().next());
- final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), bot);
+ final ChangeControl ctl = changeFactory.controlFor(new Change.Id(number), atomicityHelper.getBotUser());
final ChangeData changeData = changeDataFactory.create(db.get(), new Change.Id(number));
-
final RevisionResource r = new RevisionResource(collection.parse(ctl), changeData.currentPatchSet());
reviewer.get().apply(r, message);
}