Use map of handlers

This commit is contained in:
Andrea Maria Piana 2020-11-30 11:25:29 +01:00
parent c4e3526bef
commit 0fc7ccad52
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 18 additions and 3 deletions

View File

@ -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.');

View File

@ -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;