diff --git a/src/adapters/github/index.ts b/src/adapters/github/index.ts index c421c02..7e63d6e 100644 --- a/src/adapters/github/index.ts +++ b/src/adapters/github/index.ts @@ -1,4 +1,5 @@ import * as cacheManager from 'cache-manager' +import * as jwt from 'jsonwebtoken' import {Context} from '../../context' import {GitHubAPI} from '../../github' import {logger} from '../../logger' @@ -17,12 +18,24 @@ function isUnauthenticatedEvent (context) { export class GitHubAdapter { public log: LoggerWithTarget - public jwt: () => string + public id: number + public cert: string - constructor({jwt}) { + constructor({id, cert}) { + this.id = id + this.cert = cert this.log = wrapLogger(logger, logger) - this.jwt = jwt + } + public jwt() { + const payload = { + exp: Math.floor(Date.now() / 1000) + 60, // JWT expiration time + iat: Math.floor(Date.now() / 1000), // Issued at time + iss: this.id // GitHub App ID + } + + // Sign with RSA SHA256 + return jwt.sign(payload, this.cert, {algorithm: 'RS256'}) } public async createContext (event) { diff --git a/src/github-app.ts b/src/github-app.ts deleted file mode 100644 index bada1a0..0000000 --- a/src/github-app.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as jwt from 'jsonwebtoken' - -export const createApp = (options: AppOptions) => { - return () => { - const payload = { - exp: Math.floor(Date.now() / 1000) + 60, // JWT expiration time - iat: Math.floor(Date.now() / 1000), // Issued at time - iss: options.id // GitHub App ID - } - - // Sign with RSA SHA256 - return jwt.sign(payload, options.cert, {algorithm: 'RS256'}) - } -} - -export interface AppOptions { - id: number - cert: string -} diff --git a/src/github/logging.ts b/src/github/logging.ts index 9248721..0248804 100644 --- a/src/github/logging.ts +++ b/src/github/logging.ts @@ -14,7 +14,7 @@ export function addLogging (client: GitHubAPI, logger: Logger) { }) client.hook.after('request', (result, options) => { const {method, url, headers, ...params} = options - const msg = `GitHub request: ${method} ${url} - ${result.headers.status}` + const msg = `GitHub request: ${method} ${url} - ${result.meta.status}` logger.debug({params}, msg) }) } diff --git a/src/index.ts b/src/index.ts index 1bbc3d0..5552104 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import * as express from 'express' import {GitHubAdapter} from './adapters/github' import {Application, WebhookEvent} from './application' import {Context} from './context' -import {createApp} from './github-app' import {logger} from './logger' import {resolve} from './resolver' import {createServer} from './server' @@ -25,7 +24,6 @@ export class Probot { private options: Options private apps: Application[] - private app: () => string private adapter: GitHubAdapter constructor(options: Options) { @@ -35,12 +33,11 @@ export class Probot { this.logger = logger this.apps = [] this.webhook = new Webhooks({path: options.webhookPath, secret: options.secret}) - this.app = createApp({ id: options.id, cert: options.cert }) this.server = createServer({logger}) this.server.use(this.webhook.middleware) - this.adapter = new GitHubAdapter({jwt: this.app}) + this.adapter = new GitHubAdapter({ id: options.id, cert: options.cert }) // Log all received webhooks this.webhook.on('*', (event: any) => { diff --git a/test/plugins/helper.js b/test/plugins/helper.js index 48d5895..a722ee1 100644 --- a/test/plugins/helper.js +++ b/test/plugins/helper.js @@ -5,7 +5,8 @@ const {GitHubAdapter} = require('../../src/adapters/github') module.exports = { createApp (plugin = () => {}) { - const adapter = new GitHubAdapter({jwt: jest.fn().mockReturnValue('test')}) + const adapter = new GitHubAdapter({}) + adapter.jwt = jest.fn().mockReturnValue('test') const app = new Application({adapter}) plugin(app) return app