Propagator: Reuse change notes more

Avoid repeated index lookups to create a change notes object and pass
the object we've already created to other methods that need it.

Change-Id: Iff07a33e3648718e9ab241751551e56edefdc2cb
diff --git a/src/main/java/com/googlesource/gerrit/plugins/depends/on/ChangeMessageStore.java b/src/main/java/com/googlesource/gerrit/plugins/depends/on/ChangeMessageStore.java
index 67f5040..c7fa832 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/depends/on/ChangeMessageStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/depends/on/ChangeMessageStore.java
@@ -158,6 +158,12 @@
   /** Create a comment on the change with a DependsOn for the deps. */
   public void store(PatchSet.Id patchSetId, Set<DependsOn> deps, String message)
       throws InvalidChangeOperationException, StorageException {
+    store(changeNotesFactory.createCheckedUsingIndexLookup(patchSetId.changeId()), deps, message);
+  }
+
+  /** Create a comment on the change with a DependsOn for the deps. */
+  public void store(ChangeNotes changeNotes, Set<DependsOn> deps, String message)
+      throws InvalidChangeOperationException, StorageException {
     StringBuilder comment = new StringBuilder();
     if (message != null) {
       comment.append(message + "\n\n");
@@ -165,10 +171,8 @@
     comment.append(Comment.getMessages(deps));
     ReviewInput review = new ReviewInput();
     review.message = Strings.emptyToNull(comment.toString());
-    ChangeNotes changeNotes =
-        changeNotesFactory.createCheckedUsingIndexLookup(patchSetId.changeId());
     ChangeResource changeResource = changeResourceFactory.create(changeNotes, currentUser);
-    PatchSet patchSet = changeNotes.load().getPatchSets().get(patchSetId);
+    PatchSet patchSet = changeNotes.getCurrentPatchSet();
     try {
       retryHelper
           .changeUpdate(
diff --git a/src/main/java/com/googlesource/gerrit/plugins/depends/on/CoreListener.java b/src/main/java/com/googlesource/gerrit/plugins/depends/on/CoreListener.java
index 9b66a07..74cfd25 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/depends/on/CoreListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/depends/on/CoreListener.java
@@ -50,10 +50,9 @@
           Optional<Id> sourceId = Change.Id.tryParse(change.cherryPickOfChange.toString());
           Optional<Id> destId = Change.Id.tryParse(Integer.toString(change.number));
           if (sourceId.isPresent() && destId.isPresent()) {
-            Change sourceChange =
-                changeNotesFactory.createCheckedUsingIndexLookup(sourceId.get()).getChange();
-            Change destChange =
-                changeNotesFactory.createCheckedUsingIndexLookup(destId.get()).getChange();
+            ChangeNotes sourceChange =
+                changeNotesFactory.createCheckedUsingIndexLookup(sourceId.get());
+            ChangeNotes destChange = changeNotesFactory.createCheckedUsingIndexLookup(destId.get());
             propagator.propagateFromSourceToDestination(sourceChange, destChange);
           }
         } catch (InvalidChangeOperationException | NoSuchChangeException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/depends/on/Propagator.java b/src/main/java/com/googlesource/gerrit/plugins/depends/on/Propagator.java
index 5047e8b..a6a241c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/depends/on/Propagator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/depends/on/Propagator.java
@@ -36,19 +36,19 @@
     this.changeNotesFactory = changeNotesFactory;
   }
 
-  public void propagateFromSourceToDestination(Change srcChange, Change destChange)
+  public void propagateFromSourceToDestination(ChangeNotes srcChange, ChangeNotes destChange)
       throws InvalidChangeOperationException, NoSuchChangeException {
 
-    Set<DependsOn> deps = changeMessageStore.load(srcChange.getId());
+    Set<DependsOn> deps = changeMessageStore.load(srcChange);
     if (!deps.isEmpty()) {
       Set<DependsOn> keyDeps = new HashSet<DependsOn>(deps.size());
       for (DependsOn dep : deps) {
         keyDeps.add(DependsOn.create(loadChangeKey(dep)));
       }
       changeMessageStore.store(
-          destChange.currentPatchSetId(),
+          destChange,
           keyDeps,
-          "Dependencies propagated from " + srcChange.currentPatchSetId());
+          "Dependencies propagated from " + srcChange.getChange().currentPatchSetId());
     }
   }