Add plugin name to event properties

Thereby, rules in the common actions.config can be made to run only
for certain Its plugins on sites that have more than one Its plugins
installed.

Change-Id: Ib14f2d4ba8d7d7b4c0498f436a26bdb2c2803c0e
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
index 0f6f937..9c751ec 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractor.java
@@ -18,6 +18,7 @@
 import java.util.Set;
 
 import com.google.common.collect.Sets;
+import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.server.data.ApprovalAttribute;
@@ -42,14 +43,17 @@
   private IssueExtractor issueExtractor;
   private Property.Factory propertyFactory;
   private PropertyAttributeExtractor propertyAttributeExtractor;
+  private final String pluginName;
 
   @Inject
   PropertyExtractor(IssueExtractor issueExtractor,
       Property.Factory propertyFactory,
-      PropertyAttributeExtractor propertyAttributeExtractor) {
+      PropertyAttributeExtractor propertyAttributeExtractor,
+      @PluginName String pluginName) {
     this.issueExtractor = issueExtractor;
     this.propertyFactory = propertyFactory;
     this.propertyAttributeExtractor = propertyAttributeExtractor;
+    this.pluginName = pluginName;
   }
 
   /**
@@ -226,6 +230,8 @@
         Set<Property> properties = Sets.newHashSet();
         Property property = propertyFactory.create("issue", issue);
         properties.add(property);
+        property = propertyFactory.create("its-name", pluginName);
+        properties.add(property);
         for (String occurrence: associations.get(issue)) {
           property = propertyFactory.create("association", occurrence);
           properties.add(property);
diff --git a/src/main/resources/Documentation/config-rulebase-common.md b/src/main/resources/Documentation/config-rulebase-common.md
index 60e1185..ab25dd3 100644
--- a/src/main/resources/Documentation/config-rulebase-common.md
+++ b/src/main/resources/Documentation/config-rulebase-common.md
@@ -130,6 +130,28 @@
 : How the issue of property `issue` got associated to this event.
   See [Property: `association`][property-association].
 
+`its-name`
+:   Name of this plugin (i.e.: `@PLUGIN@`). This property can be used to
+    make a rule in the rulebase match only for certain ITS plugins, if more
+    than one is installed.
+
+    For example
+
+    ```
+    [rule "someRuleForBugzillaOnly"]
+      its-name = its-bugzilla
+      approval-Code-Review = -2
+      action = add-comment Heya Bugzilla users, the change had a -2 Code-Review approval.
+    [rule "someRuleForJiraOnly"]
+      its-name = its-jira
+      approval-Code-Review = -2
+      action = add-comment Dear JIRA users, the change had a -2 Code-Review approval.
+    ```
+
+    would report the “Heya Bugzilla...” text only through its-bugzilla for
+    changes that had a -2 Code-Review and have an association through
+    its-bugzilla. And for changes that had a -2 Code-Review and have an
+    association through its-jira, its-jira would report “Dear Jira users, ...”.
 
 The further properties are listed in the event's
 corresponding subsection below:
diff --git a/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java b/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
index 7657f24..b08317d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/hooks/util/PropertyExtractorTest.java
@@ -17,6 +17,7 @@
 
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.server.config.FactoryModule;
@@ -377,6 +378,10 @@
     PropertyExtractor propertyExtractor = injector.getInstance(
         PropertyExtractor.class);
 
+    Property propertyItsName = createMock(Property.class);
+    expect(propertyFactory.create("its-name", "ItsTestName"))
+        .andReturn(propertyItsName).anyTimes();
+
     Property propertyEvent = createMock(Property.class);
     expect(propertyFactory.create("event", "com.google.gerrit.server.events." +
         className)).andReturn(propertyEvent);
@@ -423,6 +428,7 @@
 
     Set<Set<Property>> expected = Sets.newHashSet();
     Set<Property> properties = Sets.newHashSet();
+    properties.add(propertyItsName);
     properties.add(propertyEvent);
     properties.add(propertyEventType);
     properties.add(propertyAssociationAnywhere);
@@ -432,6 +438,7 @@
     expected.add(properties);
 
     properties = Sets.newHashSet();
+    properties.add(propertyItsName);
     properties.add(propertyEvent);
     properties.add(propertyEventType);
     properties.add(propertyAssociationAnywhere);
@@ -450,6 +457,9 @@
   private class TestModule extends FactoryModule {
     @Override
     protected void configure() {
+      bind(String.class).annotatedWith(PluginName.class)
+          .toInstance("ItsTestName");
+
       issueExtractor = createMock(IssueExtractor.class);
       bind(IssueExtractor.class).toInstance(issueExtractor);