zenhub-automations/index.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-12-29 08:46:46 +00:00
const { Toolkit } = require("actions-toolkit"),
{ join } = require("path");
const tools = new Toolkit();
2019-07-10 14:01:40 +00:00
2020-11-30 10:25:29 +00:00
const handlers = {
2020-12-29 08:46:46 +00:00
create: "create.js",
issues: "issues.js",
ready_for_review: "create.js",
pull_request: "pull_request.js",
};
2020-11-30 10:25:29 +00:00
2020-12-29 08:46:46 +00:00
Toolkit.run(async (tools) => {
2020-11-30 10:25:29 +00:00
const handlerRef = tools.context.event;
tools.log.info(`Trying to load handler: "${handlerRef}"...`);
try {
2020-11-30 10:25:29 +00:00
const file = handlers[handlerRef];
if (!file) {
2020-12-29 08:46:46 +00:00
tools.log.info(`Could not find handler for "${handlerRef}"`);
return;
2020-11-30 10:25:29 +00:00
}
var eventModule = require(`./lib/handlers/${file}`);
} catch (e) {
2020-12-29 08:46:46 +00:00
console.log(e);
return tools.exit.success(
"Failed to load module for event. No action necessary."
);
}
2020-12-29 08:46:46 +00:00
const moduleAction =
eventModule[tools.context.payload.action] ||
eventModule[tools.context.payload.ref_type];
console.log(tools.context.payload);
if (!moduleAction) {
2020-12-29 08:46:46 +00:00
return tools.exit.success(
"Failed to find sub handler. No action necessary."
);
}
try {
await moduleAction(tools);
} catch (e) {
return tools.exit.failure(`Failed to run event handler: ${e}`);
}
2020-12-29 08:46:46 +00:00
tools.exit.success("Executed event handler.");
});