Add hook for specifying which labels need to be set on reverts

Bug: Issue 4675
Change-Id: I1df47e58ccb5799a9d9349f377b655baf7d80d61
diff --git a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js
index 1d87533..4f9a09d 100644
--- a/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js
+++ b/polygerrit-ui/app/elements/change/gr-change-actions/gr-change-actions.js
@@ -419,10 +419,22 @@
       });
     },
 
+    // TODO(rmistry): Redo this after
+    // https://bugs.chromium.org/p/gerrit/issues/detail?id=4671 is resolved.
+    _setLabelValuesOnRevert: function(newChangeId) {
+      var labels = this.$.jsAPI.getLabelValuesPostRevert(this.change);
+      if (labels) {
+        var url = '/changes/' + newChangeId + '/revisions/current/review';
+        this.$.restAPI.send(this.actions.revert.method, url, {labels: labels});
+      }
+    },
+
     _handleResponse: function(action, response) {
       return this.$.restAPI.getResponseObject(response).then(function(obj) {
         switch (action.__key) {
           case ChangeActions.REVERT:
+            this._setLabelValuesOnRevert(obj.change_id);
+            // Fall through.
           case RevisionActions.CHERRYPICK:
             page.show(this.changePath(obj._number));
             break;
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
index 4dfcf48..be33ad7 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.js
@@ -21,6 +21,7 @@
     SUBMIT_CHANGE: 'submitchange',
     COMMENT: 'comment',
     REVERT: 'revert',
+    POST_REVERT: 'postrevert',
   };
 
   var Element = {
@@ -160,6 +161,20 @@
       return msg;
     },
 
+    getLabelValuesPostRevert: function(change) {
+      var labels = {};
+      this._getEventCallbacks(EventType.POST_REVERT).forEach(
+          function(callback) {
+            try {
+              labels = callback(change);
+            } catch (err) {
+              console.error(err);
+            }
+          }
+      );
+      return labels;
+    },
+
     _getEventCallbacks: function(type) {
       return this._eventCallbacks[type] || [];
     },
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
index 46a555a..97e0a18 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
@@ -121,6 +121,22 @@
       assert.isTrue(errorStub.calledTwice);
     });
 
+    test('postrevert event', function(done) {
+      function getLabels(c) {
+        return {'Code-Review': 1};
+      }
+      done();
+
+      assert.deepEqual(element.getLabelValuesPostRevert(null), {});
+      assert.equal(errorStub.callCount, 0);
+
+      plugin.on(element.EventType.POST_REVERT, throwErrFn);
+      plugin.on(element.EventType.POST_REVERT, getLabels);
+      assert.deepEqual(element.getLabelValuesPostRevert(null),
+                       {'Code-Review': 1});
+      assert.isTrue(errorStub.calledOnce);
+    });
+
     test('labelchange event', function(done) {
       var testChange = {_number: 42};
       plugin.on(element.EventType.LABEL_CHANGE, throwErrFn);