Add cache and cache installation token

This commit is contained in:
Brandon Keepers 2017-03-27 22:13:17 -05:00
parent c0d8f4d112
commit 530da8b31f
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
3 changed files with 21 additions and 4 deletions

View File

@ -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 => {

View File

@ -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) {

View File

@ -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",