Add optional version parameter to JS API

If a user provides a version that is different than the current
one, a warning will be printed to the console and the plugin
callback won’t be called.

A string version is used for now since comparing versions (to check
for backward compatibility) is not a goal at this time. Plus a
version may have a tag associated like 0.1-beta or be a date.

Also adds a check for when the optional URL parameter was not
passed to the Plugin object and handles it accordingly.

Feature: Bug 3915
Change-Id: I1bbdda5a822e362db459e451595f67709a78aa07
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 3be232e..76fa91b 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
@@ -40,7 +40,7 @@
     setup(function() {
       element = fixture('basic');
       errorStub = sinon.stub(console, 'error');
-      Gerrit.install(function(p) { plugin = p; },
+      Gerrit.install(function(p) { plugin = p; }, '0.1',
           'http://test.com/plugins/testplugin/static/test.js');
     });
 
@@ -108,5 +108,11 @@
       assert.isTrue(errorStub.calledTwice);
     });
 
+    test('versioning', function() {
+      var callback = sinon.spy();
+      Gerrit.install(callback, '0.0pre-alpha');
+      assert(callback.notCalled);
+    });
+
   });
 </script>
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
index 578f44d..c63221d 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -14,11 +14,19 @@
 (function(window) {
   'use strict';
 
+  var API_VERSION = '0.1';
+
   // GWT JSNI uses $wnd to refer to window.
   // http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
   window.$wnd = window;
 
   function Plugin(opt_url) {
+    if (!opt_url) {
+      console.warn('Plugin not being loaded from /plugins base path.',
+          'Unable to determine name.');
+      return;
+    }
+
     this._url = new URL(opt_url);
     if (this._url.pathname.indexOf('/plugins') !== 0) {
       console.warn('Plugin not being loaded from /plugins base path:',
@@ -68,7 +76,13 @@
     return name;
   };
 
-  Gerrit.install = function(callback, opt_src) {
+  Gerrit.install = function(callback, opt_version, opt_src) {
+    if (opt_version && opt_version !== API_VERSION) {
+      console.warn('Only version ' + API_VERSION +
+          ' is supported in PolyGerrit. ' + opt_version + ' was given.');
+      return;
+    }
+
     // TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it).
     var src = opt_src || (document.currentScript && document.currentScript.src);
     callback(new Plugin(src));