Schedule/parse from commit instead of ref

To be able to requeue SCC creation from a patchset ref we need to save
the commit and not the ref, to later parse it in the EventParser.

Solves: Jira GER-1715
Change-Id: Ibd974347e5211c68279e17948d9b9647c2cf37eb
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java
index 771816c..34ac69e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParser.java
@@ -119,19 +119,30 @@
   }
 
   /**
-   * Creates SCC events from a ref. ref can be a branch or a patch-set ref.
+   * Creates SCC events for all commits reachable from branch. I.e. create SCCs for already merged
+   * commit that are missing SCCs.
    *
-   * @param repoName
-   * @param ref - schedule event creation for commits reachable from this ref.
+   * @param repoName - the repository containing the commits to create events from.
    * @param targetBranch - used to populate data.gitIdentifier.branch.
    */
-  public void createAndScheduleSccFromRef(String repoName, String ref, String targetBranch) {
-    ObjectId tip = getTipOf(repoName, ref);
+  public void createAndScheduleSccFromBranch(String repoName, String targetBranch) {
+    ObjectId tip = getTipOf(repoName, targetBranch);
     if (tip == null) {
       return;
     }
+    createAndScheduleSccFromCommit(repoName, targetBranch, tip.getName());
+  }
 
-    SourceChangeEventKey scc = SourceChangeEventKey.sccKey(repoName, targetBranch, tip.getName());
+  /**
+   * Creates SCC events for all commits reachable from commit.
+   *
+   * @param repoName - the repository containing the commits to create events from.
+   * @param targetBranch  - used to populate data.gitIdentifier.branch.
+   * @param commit - create events from commits reachable from commit.
+   */
+  public void createAndScheduleSccFromCommit(String repoName, String targetBranch, String commit) {
+
+    SourceChangeEventKey scc = SourceChangeEventKey.sccKey(repoName, targetBranch, commit);
     try {
       createAndScheduleMissingSccs(scc);
     } catch (IOException
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java
index 3c7b422..e477baa 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/parsing/EiffelEventParsingQueue.java
@@ -69,35 +69,45 @@
   }
 
   /**
-   * Schedule SCC creation from a branch ref. This is the special case where the ref that the
-   * commits, that the events that are supposed to be created from, are reachable from is the same
-   * as the target Branch.
+   * Schedules SCC creation for all commits reachable from branchRef.
    *
-   * @param repoName
-   * @param branch
+   * @param repoName - the repository containing the commits to create events from.
+   * @param branchRef  - create events for commits reachable from tip of branchRef.
    */
-  public void scheduleSccCreation(String repoName, String branch) {
-    scheduleSccCreation(repoName, branch, branch);
-  }
-
-  /**
-   * Schedule SCC creation from a ref. ref can be a branch or a patch-set ref.
-   *
-   * @param repoName
-   * @param ref - schedule event creation for commits reachable from this ref.
-   * @param targetBranch - used to populate data.gitIdentifier.branch.
-   */
-  public void scheduleSccCreation(String repoName, String ref, String targetBranch) {
+  public void scheduleSccCreation(String repoName, String branchRef) {
     schedule(
-        new EventParsingWorker(SCC, repoName, targetBranch, null) {
+        new EventParsingWorker(SCC, repoName, branchRef, null) {
 
           @Override
           public void doRun() {
             try {
-              eventParser.createAndScheduleSccFromRef(repoName, ref, targetBranch);
+              eventParser.createAndScheduleSccFromBranch(repoName, branchRef);
             } catch (Exception e) {
               logger.atSevere().withCause(e).log(
-                  "Failed to create SCC for %s:%s:%s.", repoName, ref, targetBranch);
+                  "Failed to create SCC for %s:%s.", repoName, branchRef);
+            }
+          }
+        });
+  }
+
+  /**
+   * Schedule SCC creation for all commits reachable from commit.
+   *
+   * @param repoName - the repository containing the commits to create events from.
+   * @param branchRef  - used to populate data.gitIdentifier.branch.
+   * @param commit - create events from commits reachable from commit.
+   */
+  public void scheduleSccCreation(String repoName, String branchRef, String commit) {
+    schedule(
+        new EventParsingWorker(SCC, repoName, branchRef, commit) {
+
+          @Override
+          public void doRun() {
+            try {
+              eventParser.createAndScheduleSccFromCommit(repoName, branchRef, commit);
+            } catch (Exception e) {
+              logger.atSevere().withCause(e).log(
+                  "Failed to create SCC for %s:%s.", repoName, branchRef);
             }
           }
         });
@@ -126,17 +136,17 @@
         });
   }
 
-  public void scheduleScsCreation(String repoName, String branch) {
+  public void scheduleScsCreation(String repoName, String branchRef) {
     schedule(
-        new EventParsingWorker(SCS, repoName, branch, null) {
+        new EventParsingWorker(SCS, repoName, branchRef, null) {
 
           @Override
           public void doRun() {
             try {
-              eventParser.createAndScheduleMissingScssFromBranch(repoName, branch);
+              eventParser.createAndScheduleMissingScssFromBranch(repoName, branchRef);
             } catch (Exception e) {
               logger.atSevere().withCause(e).log(
-                  "Failed to create SCS for %s:%s", repoName, branch);
+                  "Failed to create SCS for %s:%s", repoName, branchRef);
             }
           }
         });
diff --git a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/rest/CreateSccs.java b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/rest/CreateSccs.java
index 1f5deb6..1f4416e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/rest/CreateSccs.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/eventseiffel/rest/CreateSccs.java
@@ -60,7 +60,7 @@
               .findFirst()
               .map(c -> c.change().getDest().branch());
       if (targetBranch.isPresent()) {
-        queue.scheduleSccCreation(resource.getName(), resource.getRef(), targetBranch.get());
+        queue.scheduleSccCreation(resource.getName(), targetBranch.get(), resource.getRevision());
         return EventCreationResponse.scc(resource, targetBranch.get());
       }
       throw new BadRequestException(