1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 |
|
19 | require("source-map-support/register");
|
20 | const skill_logging_1 = require("@atomist/skill-logging");
|
21 | const context_1 = require("./context");
|
22 | const log_1 = require("./log");
|
23 | const message_1 = require("./message");
|
24 | const parameter_prompt_1 = require("./parameter_prompt");
|
25 | const payload_1 = require("./payload");
|
26 | const util_1 = require("./util");
|
27 | exports.entryPoint = async (pubSubEvent, context) => {
|
28 | const attributes = Object.assign(Object.assign({}, (pubSubEvent.attributes || {})), { eventId: context.eventId });
|
29 | log_1.debug(`atm:attributes=${JSON.stringify(attributes)}`);
|
30 | const payload = JSON.parse(Buffer.from(pubSubEvent.data, "base64").toString());
|
31 | log_1.info(`Incoming pub/sub message: ${JSON.stringify(payload, util_1.replacer)}`);
|
32 |
|
33 |
|
34 | if (payload_1.isEventIncoming(payload)) {
|
35 | await processEvent(payload, context);
|
36 | }
|
37 | else if (payload_1.isCommandIncoming(payload)) {
|
38 | await processCommand(payload, context);
|
39 | }
|
40 | };
|
41 | async function processEvent(event, ctx, loader = util_1.handlerLoader) {
|
42 | const context = context_1.createContext(event, ctx);
|
43 | try {
|
44 | log_1.debug(`Invoking event handler '${context.name}'`);
|
45 | const result = await (await loader(`events/${context.name}`))(context);
|
46 | await context.message.publish(message_1.prepareStatus(result || { code: 0 }, context));
|
47 | }
|
48 | catch (e) {
|
49 | await context.audit.log(`Error occurred: ${e.stack}`, skill_logging_1.Severity.ERROR);
|
50 | await context.message.publish(message_1.prepareStatus(e, context));
|
51 | }
|
52 | finally {
|
53 | await context.close();
|
54 | }
|
55 | log_1.debug(`Completed event handler '${context.name}'`);
|
56 | }
|
57 | exports.processEvent = processEvent;
|
58 | async function processCommand(event, ctx, loader = util_1.handlerLoader) {
|
59 | const context = context_1.createContext(event, ctx);
|
60 | try {
|
61 | log_1.debug(`Invoking command handler '${context.name}'`);
|
62 | const result = await (await loader(`commands/${context.name}`))(context);
|
63 | await context.message.publish(message_1.prepareStatus(result || { code: 0 }, context));
|
64 | }
|
65 | catch (e) {
|
66 | if (e instanceof parameter_prompt_1.CommandListenerExecutionInterruptError) {
|
67 | await context.message.publish(message_1.prepareStatus({ code: 0 }, context));
|
68 | }
|
69 | else {
|
70 | await context.audit.log(`Error occurred: ${e.stack}`, skill_logging_1.Severity.ERROR);
|
71 | await context.message.publish(message_1.prepareStatus(e, context));
|
72 | }
|
73 | }
|
74 | finally {
|
75 | await context.close();
|
76 | }
|
77 | log_1.debug(`Completed command handler '${context.name}'`);
|
78 | }
|
79 | exports.processCommand = processCommand;
|
80 |
|
\ | No newline at end of file |