UNPKG

3.65 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 flat_1 = require("flat");
19const merge = require("lodash.merge");
20const forOwn = require("lodash.forown");
21/**
22 * Create a slack button that invokes a command handler.
23 */
24function buttonForCommand(buttonSpec, command, parameters = {}) {
25 const params = mergeParameters(command, parameters);
26 const id = command.toLocaleLowerCase();
27 const action = chatButtonFrom(buttonSpec, { id });
28 action.command = {
29 id,
30 name: command,
31 parameters: params,
32 };
33 return action;
34}
35exports.buttonForCommand = buttonForCommand;
36/**
37 * Create a Slack menu that invokes a command handler.
38 */
39function menuForCommand(selectSpec, command, parameterName, parameters = {}) {
40 const params = mergeParameters(command, parameters);
41 const id = command.toLocaleLowerCase();
42 const action = chatMenuFrom(selectSpec, { id, parameterName });
43 action.command = {
44 id,
45 name: command,
46 parameters: params,
47 parameterName,
48 };
49 return action;
50}
51exports.menuForCommand = menuForCommand;
52/**
53 * Merge the provided parameters into any parameters provided as
54 * command object instance variables.
55 */
56function mergeParameters(command, parameters) {
57 // Reuse parameters defined on the instance
58 if (typeof command !== "string" && typeof command !== "function") {
59 const newParameters = merge(command, parameters);
60 return flat_1.flatten(newParameters);
61 }
62 return flat_1.flatten(parameters);
63}
64exports.mergeParameters = mergeParameters;
65function chatButtonFrom(action, command) {
66 if (!command.id) {
67 throw new Error(`Please provide a valid non-empty command id`);
68 }
69 const button = {
70 text: action.text,
71 type: "button",
72 name: `automation-command::${command.id}`,
73 };
74 forOwn(action, (v, k) => {
75 button[k] = v;
76 });
77 return button;
78}
79function chatMenuFrom(action, command) {
80 if (!command.id) {
81 throw new Error("SelectableIdentifiableInstruction must have id set");
82 }
83 if (!command.parameterName) {
84 throw new Error("SelectableIdentifiableInstruction must have parameterName set");
85 }
86 const select = {
87 text: action.text,
88 type: "select",
89 name: `automation-command::${command.id}`,
90 };
91 if (typeof action.options === "string") {
92 select.data_source = action.options; // eslint-disable-line @typescript-eslint/camelcase
93 }
94 else if (action.options.length > 0) {
95 const first = action.options[0];
96 if (first.value) {
97 // then it's normal options
98 select.options = action.options;
99 }
100 else {
101 // then it's option groups
102 select.option_groups = action.options; // eslint-disable-line @typescript-eslint/camelcase
103 }
104 }
105 forOwn(action, (v, k) => {
106 if (k !== "options") {
107 select[k] = v;
108 }
109 });
110 return select;
111}
112//# sourceMappingURL=button.js.map
\No newline at end of file