Add a 'run' action to reboot checks plugin

Change-Id: I6914da5a34bb687d999f6dda8faeb4e4ddf5d401
diff --git a/gr-checks/gr-checks-reboot.js b/gr-checks/gr-checks-reboot.js
index 7f41f66..d32f4a5 100644
--- a/gr-checks/gr-checks-reboot.js
+++ b/gr-checks/gr-checks-reboot.js
@@ -22,63 +22,93 @@
  */
 
 export class RebootFetcher {
+
   constructor(restApi) {
     this.restApi = restApi;
   }
 
+  async fetchCurrent() {
+    return this.fetch(this.changeNumber, this.patchsetNumber);
+  }
+
   async fetch(changeNumber, patchsetNumber) {
+    this.changeNumber = changeNumber;
+    this.patchsetNumber = patchsetNumber;
     console.log('Issuing REBOOT http call.');
-    const checks = await this.restApi.get(
-        '/changes/' + changeNumber + '/revisions/' + patchsetNumber
-        + '/checks?o=CHECKER');
+    const checks = await this.apiGet('?o=CHECKER');
     console.log('Returning REBOOT response.');
     return {
       responseCode: 'OK',
-      runs: checks.map(convert),
+      runs: checks.map(check => this.convert(check)),
     };
   }
-}
 
-/**
- * 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';
+  async apiGet(suffix) {
+    return this.restApi.get(
+        '/changes/' + this.changeNumber + '/revisions/' + this.patchsetNumber
+        + '/checks' + suffix);
   }
-  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 (check.state === 'SUCCESSFUL') {
-    run.statusDescription = check.message;
-    if (check.url) {
-      run.statusLink = check.url;
+
+  async apiPost(suffix) {
+    return this.restApi.post(
+        '/changes/' + this.changeNumber + '/revisions/' + this.patchsetNumber
+        + '/checks' + suffix);
+  }
+
+  /**
+   * Converts a Checks Plugin CheckInfo object into a Reboot Checks API Run
+   * object.
+   *
+   * TODO(brohlfs): Refine this conversion and add tests.
+   */
+  convert(check) {
+    let status = 'RUNNABLE';
+    if (check.state === 'RUNNING' || check.state === 'SCHEDULED') {
+      status = 'RUNNING';
+    } else if (check.state === 'FAILED' || check.state === 'SUCCESSFUL') {
+      status = 'COMPLETED';
     }
-  } else if (check.state === 'FAILED') {
-    run.results = [{
-      category: 'ERROR',
-      summary: check.message,
-    }];
-    if (check.url) {
-      run.results[0].links = [{
-        url: check.url,
+    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 (check.state === 'SUCCESSFUL') {
+      run.statusDescription = check.message;
+      if (check.url) {
+        run.statusLink = check.url;
+      }
+    } else if (check.state === 'FAILED') {
+      run.results = [{
+        category: 'ERROR',
+        summary: check.message,
+      }];
+      if (check.url) {
+        run.results[0].links = [{
+          url: check.url,
+          primary: true,
+          icon: 'EXTERNAL',
+        }];
+      }
+    }
+    if (status !== 'RUNNING') {
+      run.actions = [{
+        name: "Run",
         primary: true,
-        icon: 'EXTERNAL',
+        callback: () => this.run(check.checker_uuid),
       }];
     }
+    return run;
   }
-  return run;
+
+  run(uuid) {
+    this.apiPost('/' + uuid + '/rerun')
+        .then(() => this.fetchCurrent().then(() => {}))
+        .catch(e => {errorMessage: e.message});
+  }
 }