Handle WIP and private state change events

Extract changer, change, and issues based on the commit message.

Change-Id: I76cf7440f77e9a1646fe60f533a61356217644e1
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
index a004f6f..552065e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/its/ItsConfig.java
@@ -29,8 +29,10 @@
 import com.google.gerrit.server.events.ChangeRestoredEvent;
 import com.google.gerrit.server.events.CommentAddedEvent;
 import com.google.gerrit.server.events.PatchSetCreatedEvent;
+import com.google.gerrit.server.events.PrivateStateChangedEvent;
 import com.google.gerrit.server.events.RefEvent;
 import com.google.gerrit.server.events.RefUpdatedEvent;
+import com.google.gerrit.server.events.WorkInProgressStateChangedEvent;
 import com.google.gerrit.server.project.NoSuchProjectException;
 import com.google.gerrit.server.project.ProjectCache;
 import com.google.gerrit.server.project.ProjectState;
@@ -82,6 +84,8 @@
         || event instanceof ChangeMergedEvent
         || event instanceof ChangeAbandonedEvent
         || event instanceof ChangeRestoredEvent
+        || event instanceof PrivateStateChangedEvent
+        || event instanceof WorkInProgressStateChangedEvent
         || event instanceof RefUpdatedEvent) {
       return isEnabled(event.getProjectNameKey(), event.getRefName());
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/util/IssueExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/util/IssueExtractor.java
index fd37249..cb577c5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/util/IssueExtractor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/util/IssueExtractor.java
@@ -126,7 +126,26 @@
   public Map<String, Set<String>> getIssueIds(String projectName, String commitId) {
     Map<String, Set<String>> ret = Maps.newHashMap();
     String commitMessage = commitMessageFetcher.fetchGuarded(projectName, commitId);
+    addIssueIdsFromCommitMessage(ret, commitMessage);
+    return ret;
+  }
 
+  /**
+   * Gets issues from a commit message.
+   *
+   * @param commitMessage The commit message string.
+   * @return A mapping, whose keys are issue ids and whose values is a set of places where the issue
+   *     occurs. Each issue occurs at least in "somewhere". Issues from the first line get tagged
+   *     with an occurrence "subject". Issues in the last block get tagged with "footer". Issues
+   *     occurring between "subject" and "footer" get tagged with "body".
+   */
+  public Map<String, Set<String>> getIssueIdsFromCommitMessage(String commitMessage) {
+    Map<String, Set<String>> ret = Maps.newHashMap();
+    addIssueIdsFromCommitMessage(ret, commitMessage);
+    return ret;
+  }
+
+  private void addIssueIdsFromCommitMessage(Map<String, Set<String>> ret, String commitMessage) {
     addIssuesOccurrence(commitMessage, "somewhere", ret);
 
     String[] lines = commitMessage.split("\n");
@@ -190,7 +209,6 @@
         addIssuesOccurrence(footer, "footer", ret);
       }
     }
-    return ret;
   }
 
   /**
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/base/util/PropertyExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/its/base/util/PropertyExtractor.java
index a59de25..e165baf 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/its/base/util/PropertyExtractor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/its/base/util/PropertyExtractor.java
@@ -28,8 +28,10 @@
 import com.google.gerrit.server.events.CommentAddedEvent;
 import com.google.gerrit.server.events.PatchSetCreatedEvent;
 import com.google.gerrit.server.events.PatchSetEvent;
+import com.google.gerrit.server.events.PrivateStateChangedEvent;
 import com.google.gerrit.server.events.RefEvent;
 import com.google.gerrit.server.events.RefUpdatedEvent;
+import com.google.gerrit.server.events.WorkInProgressStateChangedEvent;
 import com.google.inject.Inject;
 import com.googlesource.gerrit.plugins.its.base.workflow.RefEventProperties;
 import java.util.Collections;
@@ -132,6 +134,28 @@
     return extractMapFrom(event, common);
   }
 
+  private Map<String, Set<String>> extractFrom(
+      WorkInProgressStateChangedEvent event, Map<String, String> common) {
+    common.putAll(propertyAttributeExtractor.extractFrom(event.changer.get(), "changer"));
+    return extractFrom((ChangeEvent) event, common);
+  }
+
+  private Map<String, Set<String>> extractFrom(
+      PrivateStateChangedEvent event, Map<String, String> common) {
+    common.putAll(propertyAttributeExtractor.extractFrom(event.changer.get(), "changer"));
+    return extractFrom((ChangeEvent) event, common);
+  }
+
+  private Map<String, Set<String>> extractFrom(ChangeEvent event, Map<String, String> common) {
+    common.put("event-type", event.type);
+    ChangeAttribute change = event.change.get();
+    common.putAll(propertyAttributeExtractor.extractFrom(change));
+    common.put("refName", event.refName);
+
+    // Got no patch set information, extract from commit message.
+    return issueExtractor.getIssueIdsFromCommitMessage(change.commitMessage);
+  }
+
   /**
    * A set of properties extracted from an event.
    *
@@ -197,6 +221,10 @@
       associations = extractFrom((PatchSetCreatedEvent) event, common);
     } else if (event instanceof RefUpdatedEvent) {
       associations = extractFrom((RefUpdatedEvent) event, common);
+    } else if (event instanceof PrivateStateChangedEvent) {
+      associations = extractFrom((PrivateStateChangedEvent) event, common);
+    } else if (event instanceof WorkInProgressStateChangedEvent) {
+      associations = extractFrom((WorkInProgressStateChangedEvent) event, common);
     }
 
     Set<Map<String, String>> ret = new HashSet<>();
diff --git a/src/main/resources/Documentation/config-rulebase-common.md b/src/main/resources/Documentation/config-rulebase-common.md
index 4d970b6..17baf77 100644
--- a/src/main/resources/Documentation/config-rulebase-common.md
+++ b/src/main/resources/Documentation/config-rulebase-common.md
@@ -235,6 +235,8 @@
 * [CommentAddedEvent](#commentaddedevent)
 * [PatchSetCreatedEvent](#patchSetcreatedevent)
 * [RefUpdatedEvent](#refupdatedevent)
+* [WorkInProgressStateChangedEvent](#workinprogressstatechangedevent)
+* [PrivateStateChangedEvent](#privatestatechangedevent)
 * [Common properties for events on a change](#common-properties-for-events-on-a-change)
 * [Common properties for events on a patch set](#common-properties-for-events-on-a-patch-set)
 
@@ -441,6 +443,40 @@
 `submitterUsername`
 : username of the user that updated the ref.
 
+### WorkInProgressStateChangedEvent
+
+`event`
+:   `com.google.gerrit.server.events.WorkInProgressStateChangedEvent`
+
+`event-type`
+:   `wip-state-changed`
+
+`changerEmail`
+:   email address of the user that changed the WIP state
+
+`submitterName`
+:   name of the user that changed the WIP state
+
+`changerUsername`
+:   username of the user that changed the WIP state
+
+### PrivateStateChangedEvent
+
+`event`
+:   `com.google.gerrit.server.events.PrivateStateChangedEvent`
+
+`event-type`
+:   `private-state-changed`
+
+`changerEmail`
+:   email address of the user that changed the private state
+
+`submitterName`
+:   name of the user that changed the private state
+
+`changerUsername`
+:   username of the user that changed the private state
+
 ### Common properties for events on a change
 
 `branch`