Implement change-deleted hook

Bug: Issue 9711
Change-Id: I8bda9a8951e54add030398a66a12713d041ec2df
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/ChangeDeleted.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/ChangeDeleted.java
new file mode 100644
index 0000000..85a4938
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/ChangeDeleted.java
@@ -0,0 +1,48 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.hooks;
+
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.extensions.events.ChangeDeletedListener;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+@Singleton
+public class ChangeDeleted implements ChangeDeletedListener {
+  private final AsynchronousHook hook;
+  private final HookFactory hookFactory;
+
+  @Inject
+  ChangeDeleted(HookFactory hookFactory) {
+    this.hook = hookFactory.createAsync("changeDeletedHook", "change-deleted");
+    this.hookFactory = hookFactory;
+  }
+
+  @Override
+  public void onChangeDeleted(ChangeDeletedListener.Event event) {
+    HookArgs args = hookFactory.createArgs();
+
+    ChangeInfo c = event.getChange();
+    args.add("--change", c.id);
+    args.addUrl(c);
+    args.add("--change-owner", c.owner);
+    args.add("--project", c.project);
+    args.add("--branch", c.branch);
+    args.add("--topic", c.topic);
+    args.add("--deleter", event.getWho());
+
+    hook.submit(c.project, args);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/Module.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/Module.java
index bb2ee4c..8243791 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/Module.java
@@ -16,6 +16,7 @@
 
 import com.google.gerrit.extensions.events.AgreementSignupListener;
 import com.google.gerrit.extensions.events.ChangeAbandonedListener;
+import com.google.gerrit.extensions.events.ChangeDeletedListener;
 import com.google.gerrit.extensions.events.ChangeMergedListener;
 import com.google.gerrit.extensions.events.ChangeRestoredListener;
 import com.google.gerrit.extensions.events.CommentAddedListener;
@@ -46,6 +47,7 @@
 
     DynamicSet.bind(binder(), AgreementSignupListener.class).to(AgreementSignup.class);
     DynamicSet.bind(binder(), ChangeAbandonedListener.class).to(ChangeAbandoned.class);
+    DynamicSet.bind(binder(), ChangeDeletedListener.class).to(ChangeDeleted.class);
     DynamicSet.bind(binder(), ChangeMergedListener.class).to(ChangeMerged.class);
     DynamicSet.bind(binder(), ChangeRestoredListener.class).to(ChangeRestored.class);
     DynamicSet.bind(binder(), CommentAddedListener.class).to(CommentAdded.class);
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 7a1cbd2..1e5502a 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -39,6 +39,9 @@
 hooks.changeAbandonedHook
 :	Filename for the change abandoned hook. If not set, defaults to `change-abandoned`.
 
+hooks.changeDeletedHook
+:	Filename for the change deleted hook. If not set, defaults to `change-deleted`.
+
 hooks.changeMergedHook
 :	Filename for the change merged hook. If not set, defaults to `change-merged`.
 
diff --git a/src/main/resources/Documentation/hooks.md b/src/main/resources/Documentation/hooks.md
index 4fd192d..c8b6c81 100644
--- a/src/main/resources/Documentation/hooks.md
+++ b/src/main/resources/Documentation/hooks.md
@@ -93,6 +93,14 @@
   change-abandoned --change <change id> --change-url <change url> --change-owner <change owner> --project <project name> --branch <branch> --topic <topic> --abandoner <abandoner> --commit <sha1> --reason <reason>
 ```
 
+### change-deleted
+
+Called whenever a change has been deleted.
+
+```
+  change-deleted --change <change id> --change-url <change url> --change-owner <change owner> --project <project name> --branch <branch> --topic <topic> --deleter <deleter>
+```
+
 ### change-restored
 
 Called whenever a change has been restored.