From 88222e46cb402fbedc8dd7d6d2ecf1341a5904f9 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Tue, 28 Aug 2018 16:30:17 -0400 Subject: [PATCH] Reshuffles the routing around in Peril, blocks the runtime from having access to installation webhooks --- source/plugins/installationLifeCycle.ts | 40 +++++++++++-------------- source/routing/router.ts | 17 +++++++---- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/source/plugins/installationLifeCycle.ts b/source/plugins/installationLifeCycle.ts index e3459596..421f5943 100644 --- a/source/plugins/installationLifeCycle.ts +++ b/source/plugins/installationLifeCycle.ts @@ -5,30 +5,24 @@ import { RootObject as InstallationCreated } from "../github/events/types/integr import logger from "../logger" export const installationLifeCycle = (event: string, req: express.Request, res: express.Response, ___: any) => { - if (event !== "installation") { - return false - } - - const request = req.body as InstallationCreated - const action = request.action - const installation = request.installation + if (event === "installation") { + const request = req.body as InstallationCreated + const action = request.action + const installation = request.installation - // Create a db entry for any new installation - if (action === "created") { - logger.info("") - logger.info(`## Creating new installation for ${request.installation.account.login}`) - createInstallation(installation, req, res) - return true - } + // Create a db entry for any new installation + if (action === "created") { + logger.info("") + logger.info(`## Creating new installation for ${request.installation.account.login}`) + createInstallation(installation, req, res) + } - // Delete any integrations that have uninstalled Peril :wave: - if (action === "deleted") { - logger.info("") - logger.info(`## Deleting installation ${installation.id}`) - const db = getDB() - db.deleteInstallation(installation.id) - return true + // Delete any integrations that have uninstalled Peril :wave: + if (action === "deleted") { + logger.info("") + logger.info(`## Deleting installation ${installation.id}`) + const db = getDB() + db.deleteInstallation(installation.id) + } } - - return false } diff --git a/source/routing/router.ts b/source/routing/router.ts index 41f5a3af..1ce25b86 100644 --- a/source/routing/router.ts +++ b/source/routing/router.ts @@ -12,6 +12,14 @@ export const githubRouter = (req: Request, res: Response, next: NextFunction) => const event = req.header("X-GitHub-Event") winston.log("router", `Received ${event}:`) + // Creating / Removing installations from the DB + installationLifeCycle(event, req, res, next) + + // There are some webhook events that shouldn't be passed through to users/plugins + if (webhookSkipListForPeril.includes(event)) { + return + } + githubEventPluginHandler(event, req, res, next) // The Peril/Danger runner @@ -29,14 +37,13 @@ export const githubEventPluginHandler = (event: string, req: Request, res: Respo return } - // Creating / Removing installations from the DB - if (installationLifeCycle(event, req, res, next)) { - return - } - // Allow a dev mode recordWebhook(event, req, res, next) // Updating an install when the JSON changes installationSettingsUpdater(event, req, res, next) } + +// Installation addition/removal isn't too useful, and knowing when the repos +// have changed isn't of much value to peril considering how the JSON file is set up +export const webhookSkipListForPeril = ["integration_installation", "installation"]