UNPKG

4.91 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 });
18const cloneDeep = require("lodash.clonedeep");
19const map = require("lodash.map");
20const set = require("lodash.set");
21/**
22 * Different strategies to ask for parameters in chat or web
23 */
24var ParameterStyle;
25(function (ParameterStyle) {
26 /** Parameter questions will be prompted in a dialog */
27 ParameterStyle["Dialog"] = "dialog";
28 /** Parameter questions will be prompted in a thread */
29 ParameterStyle["Threaded"] = "threaded";
30 /**
31 * Parameter questions will be prompted in the channel where the
32 * command is being run
33 */
34 ParameterStyle["Unthreaded"] = "unthreaded";
35 /**
36 * Parameter questions will be prompted in a dialog if the command
37 * is triggered from a button or menu
38 */
39 ParameterStyle["DialogAction"] = "dialog_action";
40})(ParameterStyle = exports.ParameterStyle || (exports.ParameterStyle = {}));
41exports.AtomistContinuationMimeType = "application/x-atomist-continuation+json";
42/**
43 * Default ParameterPromptFactory that uses the WebSocket connection to send parameter prompts to the backend.
44 * @param ctx
45 */
46function commandRequestParameterPromptFactory(messageClient, payload) {
47 return async (parameters, options = {}) => {
48 var _a, _b, _c;
49 const existingParameters = payload.parameters;
50 const newParameters = cloneDeep(parameters);
51 // Find out if - and if - which parameters are actually missing
52 let requiredMissing = false;
53 const params = {};
54 for (const parameter in parameters) {
55 const existingParameter = existingParameters.find(p => p.name === parameter);
56 if (!existingParameter) {
57 // If required isn't defined it means the parameter is required
58 if (newParameters[parameter].required || newParameters[parameter].required === undefined) {
59 requiredMissing = true;
60 }
61 }
62 else {
63 params[parameter] = existingParameter.value;
64 delete newParameters[parameter];
65 }
66 }
67 // If no parameters are missing we can return the already collected parameters
68 if (!requiredMissing) {
69 return params;
70 }
71 // Set up the thread_ts for this response message
72 let threadTs;
73 if (options.thread === true && !!payload.source) {
74 threadTs = (_c = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.source) === null || _a === void 0 ? void 0 : _a.slack) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.ts;
75 }
76 else if (typeof options.thread === "string") {
77 threadTs = options.thread;
78 }
79 const destination = cloneDeep(payload.source);
80 set(destination, "slack.thread_ts", threadTs);
81 // Create a continuation message using the existing HandlerResponse and mixing in parameters
82 // and parameter_specs
83 const response = {
84 api_version: "1",
85 correlation_id: payload.correlation_id,
86 team: payload.team,
87 command: payload.command,
88 source: payload.source,
89 destinations: [destination],
90 parameters: payload.parameters,
91 auto_submit: options.autoSubmit ? options.autoSubmit : undefined,
92 question: options.parameterStyle ? options.parameterStyle.toString() : undefined,
93 parameter_specs: map(newParameters, (v, k) => (Object.assign(Object.assign({}, v), { name: k, required: v.required !== undefined ? v.required : true, pattern: v.pattern ? v.pattern.source : undefined }))),
94 content_type: exports.AtomistContinuationMimeType,
95 };
96 await messageClient.respond(response);
97 throw new CommandListenerExecutionInterruptError("Prompting for parameters");
98 };
99}
100exports.commandRequestParameterPromptFactory = commandRequestParameterPromptFactory;
101class CommandListenerExecutionInterruptError extends Error {
102 constructor(message) {
103 super(message);
104 this.message = message;
105 }
106}
107exports.CommandListenerExecutionInterruptError = CommandListenerExecutionInterruptError;
108//# sourceMappingURL=parameter_prompt.js.map
\No newline at end of file