Files
  • index.js
  • package-lock.json
  • package.json
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
// 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];
  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;
        }
      }
    }

    if (latest.description.match(/^Update/i) || latest.description.match(/config vars/)) {
      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(8080)
node v9.7.1 linux/amd64