UNPKG

3.58 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2020 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18// tslint:disable-next-line:no-import-side-effect
19require("source-map-support/register");
20const skill_logging_1 = require("@atomist/skill-logging");
21const context_1 = require("./context");
22const log_1 = require("./log");
23const message_1 = require("./message");
24const parameter_prompt_1 = require("./parameter_prompt");
25const payload_1 = require("./payload");
26const util_1 = require("./util");
27exports.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 // debug(`Incoming message context: ${JSON.stringify(context, replacer)}`);
33 // debug(`Incoming mskill.tsessage: ${JSON.stringify(pubSubEvent, replacer)}`);
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};
41async 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}
57exports.processEvent = processEvent;
58async 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}
79exports.processCommand = processCommand;
80//# sourceMappingURL=function.js.map
\No newline at end of file