From 0fc7ccad52dad46a776f6f6377feaa3993cc37b5 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 30 Nov 2020 11:25:29 +0100 Subject: [PATCH] Use map of handlers --- index.js | 16 ++++++++++++++-- lib/handlers/pull_request.js | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c617575..0991eeb 100644 --- a/index.js +++ b/index.js @@ -3,13 +3,25 @@ const { Toolkit } = require('actions-toolkit'), const tools = new Toolkit(); +const handlers = { + create: 'create.js', + ready_for_review: 'create.js', + pull_request: 'pull_request.js' +} + Toolkit.run(async tools => { - const handlerRef = `${tools.context.event}.js`; + const handlerRef = tools.context.event; tools.log.info(`Trying to load handler: "${handlerRef}"...`); try { - var eventModule = require(`./lib/handlers/${handlerRef}`); + const file = handlers[handlerRef]; + if (!file) { + tools.log.info(`Could not find handler for "${handlerRef}"`); + return + } + + var eventModule = require(`./lib/handlers/${file}`); } catch (e) { console.log(e) return tools.exit.success('Failed to load module for event. No action necessary.'); diff --git a/lib/handlers/pull_request.js b/lib/handlers/pull_request.js index 7836f10..e68a2af 100644 --- a/lib/handlers/pull_request.js +++ b/lib/handlers/pull_request.js @@ -4,7 +4,7 @@ const zh = require('../zh-client'), INPUT_REVIEW_COLUMN } = process.env; -exports.opened = async function handleOpenedPR (tools) { +async function handleOpenedPR (tools) { tools.log.info('Handling opened PR...'); const failures = [], @@ -32,3 +32,6 @@ exports.opened = async function handleOpenedPR (tools) { throw new Error(`Failed to execute some actions: ${failures.map(x => x.message || x).join(', ')}`); } }; + +exports.opened = handleOpenedPR; +exports.ready_for_review = handleOpenedPR;