From 530da8b31fc058e7510e868a9996e91c85accf6b Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 27 Mar 2017 22:13:17 -0500 Subject: [PATCH 1/3] Add cache and cache installation token --- index.js | 8 +++++++- lib/robot.js | 15 ++++++++++++--- package.json | 2 ++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 548d5907..d4528493 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,15 @@ +const cacheManager = require('cache-manager'); const createWebhook = require('github-webhook-handler'); const createIntegration = require('github-integration'); const createRobot = require('./lib/robot'); const createServer = require('./lib/server'); module.exports = options => { + const cache = cacheManager.caching({ + store: 'memory', + ttl: 60 * 60 // 1 hour + }); + const webhook = createWebhook({path: '/', secret: options.secret}); const integration = createIntegration({ id: options.id, @@ -11,7 +17,7 @@ module.exports = options => { debug: process.env.LOG_LEVEL === 'trace' }); const server = createServer(webhook); - const robot = createRobot(integration, webhook); + const robot = createRobot(integration, webhook, cache); // Show trace for any unhandled rejections process.on('unhandledRejection', reason => { diff --git a/lib/robot.js b/lib/robot.js index aaeb1b8a..88e489d4 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -1,5 +1,6 @@ const bunyan = require('bunyan'); const bunyanFormat = require('bunyan-format'); +const GitHubApi = require('github'); const Context = require('./context'); const logger = bunyan.createLogger({ @@ -9,9 +10,10 @@ const logger = bunyan.createLogger({ }); class Robot { - constructor(integration, webhook) { + constructor(integration, webhook, cache) { this.integration = integration; this.webhook = webhook; + this.cache = cache; } on(event, callback) { @@ -24,8 +26,15 @@ class Robot { }); } - auth(id) { - return this.integration.asInstallation(id); + async auth(id) { + const token = await this.cache.wrap(`integration:${id}:token`, () => { + this.log.info(`creating token for installation ${id}`); + return this.integration.createToken(id); + }, {ttl: 60 * 60}); + + const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'}); + github.authenticate({type: 'token', token: token.token}); + return github; } log(...args) { diff --git a/package.json b/package.json index 39eedb0c..bf1a7be9 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,10 @@ "dependencies": { "bunyan": "^1.8.5", "bunyan-format": "^0.2.1", + "cache-manager": "^2.4.0", "commander": "^2.9.0", "dotenv": "~4.0.0", + "github": "^8.1.0", "github-integration": "^1.0.0", "github-webhook-handler": "^0.6.0", "pkg-conf": "^2.0.0", From 97c7180d870095aba4d6aa545e05e6c43e78ffe0 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 27 Mar 2017 22:20:52 -0500 Subject: [PATCH 2/3] Fix travis version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 76632d7d..f00bf4ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: node_js node_js: - - "6" + - "7.7.1" notifications: disabled: true env: From 8291867879b8c72e4b945f26f8a2375b2f9d2a6e Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 27 Mar 2017 22:22:59 -0500 Subject: [PATCH 3/3] Remove env variables --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index f00bf4ec..effdb13a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,3 @@ node_js: - "7.7.1" notifications: disabled: true -env: - - PRIVATE_KEY=ssssshhh - - INTEGRATION_ID=1 - - WEBHOOK_SECRET=test