repl.it
@amasad/

heroku-bot

Nodejs

A bot that responds to a slack slash command with a diff of commits going out to heroku

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
  • scratch
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// yikes! @amasad pls fix
process.env.NODE_PATH = "/home/runner/heroku-bot/node_modules";
require("module").Module._initPaths();


// Create a .env and add your heroku token, app, and your github repo
const Heroku = require('heroku-client');
const http = require('http');
const token = process.env.HEROKU_TOKEN;
const repo = process.env.REPO_URL;
const heroku = new Heroku({ token });
const app = process.env.APP;

// Get a diff url with commmits between the latest deploy and github master
async function commits() {
  let releases = await heroku.get(`/apps/${app}/releases`);
  const latest = findLatest(releases);
  const frm = latest.description.split(/\s/)[1];
  const toc = 'master';

  return (
    `${repo}/compare/${frm}...${toc}`
  );
}

// Traceback all rollbacks to get to the actual commit
function findLatest(releases) {
  let latest = releases[releases.length - 1];

  top: while (true) {
    if (latest.description.match(/rollback/i)) { 
      const version = latest.description.split(/\s/)[2];
      const num = parseInt(version.slice(1));
      for (const rel of releases) {        
        if (rel.version === num) {
          latest = rel;
          continue top;
        }
      }
    }

    // Actions to be ignored
    if (latest.description.match(/^Update/i) ||
      latest.description.match(/config vars/) ||
      latest.description.match(/^Detach/) ||
      latest.description.match(/^Attach/) ||
      latest.description.match(/completed provisioning/)) {
      latest = releases[releases.indexOf(latest) - 1];
      continue;
    }
    break;
  }

  return latest;
}

http.createServer((req, res) => {
  commits().then((url) => {
    res.setHeader('Content-Type', 'application/json')
    res.end(JSON.stringify({
      "response_type": "in_channel",
      "text": url,
    }))
  });
}).listen(3000)
node v10.16.0