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 | const cloneDeep = require("lodash.clonedeep");
|
19 | const map = require("lodash.map");
|
20 | const set = require("lodash.set");
|
21 | /**
|
22 | * Different strategies to ask for parameters in chat or web
|
23 | */
|
24 | var ParameterStyle;
|
25 | (function (ParameterStyle) {
|
26 |
|
27 | ParameterStyle["Dialog"] = "dialog";
|
28 |
|
29 | ParameterStyle["Threaded"] = "threaded";
|
30 | |
31 |
|
32 |
|
33 |
|
34 | ParameterStyle["Unthreaded"] = "unthreaded";
|
35 | |
36 |
|
37 |
|
38 |
|
39 | ParameterStyle["DialogAction"] = "dialog_action";
|
40 | })(ParameterStyle = exports.ParameterStyle || (exports.ParameterStyle = {}));
|
41 | exports.AtomistContinuationMimeType = "application/x-atomist-continuation+json";
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function commandRequestParameterPromptFactory(messageClient, payload) {
|
47 | return async (parameters, options = {}) => {
|
48 | var _a, _b, _c;
|
49 | const existingParameters = payload.parameters;
|
50 | const newParameters = cloneDeep(parameters);
|
51 |
|
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 |
|
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 |
|
68 | if (!requiredMissing) {
|
69 | return params;
|
70 | }
|
71 |
|
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 | }
|
100 | exports.commandRequestParameterPromptFactory = commandRequestParameterPromptFactory;
|
101 | class CommandListenerExecutionInterruptError extends Error {
|
102 | constructor(message) {
|
103 | super(message);
|
104 | this.message = message;
|
105 | }
|
106 | }
|
107 | exports.CommandListenerExecutionInterruptError = CommandListenerExecutionInterruptError;
|
108 |
|
\ | No newline at end of file |