Add support for new reboot ChecksApi

The plan is to switch the Checks Plugin to the new standard API and its
UI at some point. This will be done via an experiment flag.

This change adds basic support for this flag and converting/providing
its data to the new plugin API. At the moment the flag is turned off
for everyone, so this is currently for developing/debugging only.

This was tested by using the Dev Helper and adding a few lines of code
to the ChecksApi such that the provider was called. It was manually
verified that the provider has converted and returned data. Unit tests
will be added in later changes.

Change-Id: Ib6b9f66f6bf34471db7b167495acccf7fac4d216
diff --git a/gr-checks/gr-checks-reboot.js b/gr-checks/gr-checks-reboot.js
new file mode 100644
index 0000000..8d67778
--- /dev/null
+++ b/gr-checks/gr-checks-reboot.js
@@ -0,0 +1,79 @@
+/**
+ * @license
+ * Copyright (C) 2020 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.
+ */
+
+/**
+ * Heads up! Everything in this file is still in flux. The new reboot checks API
+ * is still in development. So everything in this file can change. And it is
+ * expected that the amount of comments and tests is limited for the time being.
+ */
+
+export class RebootFetcher {
+  constructor(restApi) {
+    this.restApi = restApi;
+  }
+
+  async fetch(changeNumber, patchsetNumber) {
+    console.log('Issuing REBOOT http call.');
+    const checks = await this.restApi.get(
+        '/changes/' + changeNumber + '/revisions/' + patchsetNumber
+        + '/checks?o=CHECKER');
+    console.log('Returning REBOOT response.');
+    return {
+      responseCode: 'OK',
+      runs: checks.map(convert),
+    };
+  }
+}
+
+/**
+ * Converts a Checks Plugin CheckInfo object into a Reboot Checks API Run
+ * object.
+ *
+ * TODO(brohlfs): Refine this conversion and add tests.
+ */
+function convert(check) {
+  let status = 'RUNNABLE';
+  if (check.state === 'RUNNING' || check.state === 'SCHEDULED') {
+    status = 'RUNNING';
+  } else if (check.state === 'FAILED' || check.state === 'SUCCESSFUL') {
+    status = 'COMPLETED';
+  }
+  const run = {
+    checkName: check.checker_name,
+    checkDescription: check.checker_description,
+    externalId: check.checker_uuid,
+    status,
+  };
+  if (check.started) run.startedTimestamp = new Date(check.started);
+  if (check.finished) run.finishedTimestamp = new Date(check.finished);
+  if (status === 'RUNNING') {
+    run.statusDescription = check.message;
+  } else if (status === 'COMPLETED') {
+    run.results = [{
+      category: check.state === 'FAILED' ? 'ERROR' : 'INFO',
+      summary: check.message,
+    }];
+    if (check.url) {
+      run.results[0].links = [{
+        url: check.url,
+        primary: true,
+        icon: 'EXTERNAL',
+      }];
+    }
+  }
+  return run;
+}
diff --git a/gr-checks/gr-checks.js b/gr-checks/gr-checks.js
index b8c2920..e9e9c28 100644
--- a/gr-checks/gr-checks.js
+++ b/gr-checks/gr-checks.js
@@ -21,8 +21,9 @@
 import './gr-checks-change-list-item-cell-view.js';
 import './gr-checks-item.js';
 import './gr-checks-status.js';
+import {RebootFetcher} from './gr-checks-reboot.js';
 
-Gerrit.install(plugin => {
+function installChecksLegacy(plugin) {
   const getChecks = (change, revision) => {
     return plugin.restApi().get(
         '/changes/' + change + '/revisions/' + revision + '/checks?o=CHECKER');
@@ -54,4 +55,25 @@
         view['isConfigured'] = repository => Promise.resolve(true);
         view['getChecks'] = getChecks;
       });
-});
\ No newline at end of file
+}
+
+function installChecksReboot(plugin) {
+  const checksApi = plugin.checks();
+  const fetcher = new RebootFetcher(plugin.restApi());
+  checksApi.register({
+    fetch: (changeNumber, patchsetNumber) => fetcher.fetch(changeNumber,
+        patchsetNumber)
+  });
+}
+
+Gerrit.install(plugin => {
+  const experiments = window.ENABLED_EXPERIMENTS || [];
+  if (experiments.includes("UiFeature__ci_reboot_checks")) {
+    // Until end of 2020 this is only interesting for developing purposes. So
+    // no real user is affected for the time being.
+    console.log('Installing checks REBOOT plugin.');
+    installChecksReboot(plugin);
+  } else {
+    installChecksLegacy(plugin);
+  }
+});