Merge branch 'stable-3.5' into stable-3.9

* stable-3.5:
  DependencyResolver: Resolve using ChangeNotes input
  Propagator: Reuse change notes more

Change-Id: Iedd48ddd8a4577762acc3807ca40f4ba40bd3252
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..6633132 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
@@ -134,10 +134,9 @@
 
   /** If needed, create a comment on the change with a DependsOn for the dependencies. */
   @Override
-  public boolean resolveDependencies(PatchSet.Id patchSetId, Set<Set<BranchNameKey>> deliverables)
+  public boolean resolveDependencies(ChangeNotes changeNotes, Set<Set<BranchNameKey>> deliverables)
       throws InvalidChangeOperationException, StorageException {
-    Change.Id cid = patchSetId.changeId();
-    Set<DependsOn> deps = load(cid);
+    Set<DependsOn> deps = load(changeNotes);
     if (Resolver.isResolved(deps)) {
       return false;
     }
@@ -146,17 +145,26 @@
       return false; // Nothing resolved this pass
     }
     // ToDo: add info about the resolved depends-on (deliverable, branch, and ChangeId?)
-    store(patchSetId, resolved, "Auto-updating resolved Depends-on");
+    store(changeNotes, resolved, "Auto-updating resolved Depends-on");
     return true;
   }
 
+  /** If needed, create a comment on the change with a DependsOn for the dependencies. */
+  @Deprecated
+  @Override
+  public boolean resolveDependencies(PatchSet.Id patchSetId, Set<Set<BranchNameKey>> deliverables)
+      throws InvalidChangeOperationException, StorageException {
+    return resolveDependencies(
+        changeNotesFactory.createCheckedUsingIndexLookup(patchSetId.changeId()), deliverables);
+  }
+
   @Override
   public boolean hasUnresolvedDependsOn(Change.Id changeId) {
     return !Resolver.isResolved(load(changeId));
   }
 
   /** Create a comment on the change with a DependsOn for the deps. */
-  public void store(PatchSet.Id patchSetId, Set<DependsOn> deps, String message)
+  public void store(ChangeNotes changeNotes, Set<DependsOn> deps, String message)
       throws InvalidChangeOperationException, StorageException {
     StringBuilder comment = new StringBuilder();
     if (message != null) {
@@ -165,10 +173,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/Module.java b/src/main/java/com/googlesource/gerrit/plugins/depends/on/Module.java
index 2b40b88..0ac50e2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/depends/on/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/depends/on/Module.java
@@ -26,6 +26,7 @@
 import com.google.gerrit.server.DynamicOptions.DynamicBean;
 import com.google.gerrit.server.change.ChangePluginDefinedInfoFactory;
 import com.google.gerrit.server.events.EventListener;
+import com.google.gerrit.server.notedb.ChangeNotes;
 import com.google.gerrit.server.project.InvalidChangeOperationException;
 import com.google.gerrit.server.project.NoSuchChangeException;
 import com.google.gerrit.server.query.change.ChangeQueryBuilder.ChangeHasOperandFactory;
@@ -79,6 +80,14 @@
     }
 
     @Override
+    public boolean resolveDependencies(
+        ChangeNotes changeNotes, Set<Set<BranchNameKey>> deliverables)
+        throws InvalidChangeOperationException {
+      return changeMessageStore.resolveDependencies(changeNotes, deliverables);
+    }
+
+    @Deprecated
+    @Override
     public boolean resolveDependencies(PatchSet.Id patchSetId, Set<Set<BranchNameKey>> deliverables)
         throws InvalidChangeOperationException, StorageException, NoSuchChangeException {
       return changeMessageStore.resolveDependencies(patchSetId, deliverables);
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());
     }
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/depends/on/extensions/DependencyResolver.java b/src/main/java/com/googlesource/gerrit/plugins/depends/on/extensions/DependencyResolver.java
index bf92899..3cde2eb 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/depends/on/extensions/DependencyResolver.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/depends/on/extensions/DependencyResolver.java
@@ -19,12 +19,17 @@
 import com.google.gerrit.entities.PatchSet;
 import com.google.gerrit.exceptions.StorageException;
 import com.google.gerrit.server.DynamicOptions.DynamicBean;
+import com.google.gerrit.server.notedb.ChangeNotes;
 import com.google.gerrit.server.project.InvalidChangeOperationException;
 import java.util.Set;
 
 public interface DependencyResolver extends DynamicBean {
-  public boolean resolveDependencies(PatchSet.Id patchSetId, Set<Set<BranchNameKey>> deliverables)
+  boolean resolveDependencies(ChangeNotes changeNotes, Set<Set<BranchNameKey>> deliverables)
       throws InvalidChangeOperationException, StorageException;
 
-  public boolean hasUnresolvedDependsOn(Change.Id changeId) throws StorageException;
+  @Deprecated
+  boolean resolveDependencies(PatchSet.Id patchSetId, Set<Set<BranchNameKey>> deliverables)
+      throws InvalidChangeOperationException, StorageException;
+
+  boolean hasUnresolvedDependsOn(Change.Id changeId) throws StorageException;
 }