-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathworkflow-command.ts
More file actions
198 lines (186 loc) · 7.13 KB
/
Copy pathworkflow-command.ts
File metadata and controls
198 lines (186 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { Command } from "commander";
import type { CliContext } from "./context.ts";
import { pruneEmpty } from "../lib/compact-json.ts";
import { resolveChannelId } from "../slack/channels.ts";
import {
getWorkflowSchema,
listChannelWorkflows,
previewWorkflow,
resolveShortcutUrl,
runWorkflow,
} from "../slack/workflows.ts";
import {
requireBrowserAuth,
submitWorkflow,
validateFieldInputs,
} from "../slack/workflow-submit.ts";
type WorkspaceOption = {
workspace?: string;
};
type RunOptions = WorkspaceOption & {
channel: string;
field?: string[];
};
export function registerWorkflowCommand(input: { program: Command; ctx: CliContext }): void {
const workflowCmd = input.program
.command("workflow")
.description("Discover and interact with Slack workflows");
workflowCmd
.command("list")
.description("List workflows bookmarked or featured in a channel")
.argument("<channel>", "Channel id or name (#channel, channel, C...)")
.option(
"--workspace <url>",
"Workspace selector (full URL or unique substring; required if you have multiple workspaces)",
)
.action(async (...args) => {
const [channel, options] = args as [string, WorkspaceOption];
try {
const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace);
const payload = await input.ctx.withAutoRefresh({
workspaceUrl,
work: async () => {
const { client } = await input.ctx.getClientForWorkspace(workspaceUrl);
const channelId = await resolveChannelId(client, channel);
return await listChannelWorkflows(client, channelId);
},
});
console.log(JSON.stringify(pruneEmpty(payload), null, 2));
} catch (err: unknown) {
console.error(input.ctx.errorMessage(err));
process.exitCode = 1;
}
});
workflowCmd
.command("preview")
.description("Get workflow metadata from a trigger ID (no side effects)")
.argument("<trigger-id>", "Trigger ID (Ft...)")
.option(
"--workspace <url>",
"Workspace selector (full URL or unique substring; required if you have multiple workspaces)",
)
.action(async (...args) => {
const [triggerId, options] = args as [string, WorkspaceOption];
try {
const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace);
const payload = await input.ctx.withAutoRefresh({
workspaceUrl,
work: async () => {
const { client } = await input.ctx.getClientForWorkspace(workspaceUrl);
return await previewWorkflow(client, triggerId);
},
});
console.log(JSON.stringify(pruneEmpty(payload), null, 2));
} catch (err: unknown) {
console.error(input.ctx.errorMessage(err));
process.exitCode = 1;
}
});
workflowCmd
.command("get")
.description("Get workflow definition including form fields and steps (accepts Ft... or Wf...)")
.argument("<id>", "Trigger ID (Ft...) or Workflow ID (Wf...)")
.option(
"--workspace <url>",
"Workspace selector (full URL or unique substring; required if you have multiple workspaces)",
)
.action(async (...args) => {
const [id, options] = args as [string, WorkspaceOption];
try {
const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace);
const payload = await input.ctx.withAutoRefresh({
workspaceUrl,
work: async () => {
const { client } = await input.ctx.getClientForWorkspace(workspaceUrl);
let workflowId = id;
if (id.startsWith("Ft")) {
const preview = await previewWorkflow(client, id);
workflowId = preview.workflow.id;
}
return await getWorkflowSchema(client, workflowId);
},
});
console.log(JSON.stringify(pruneEmpty(payload), null, 2));
} catch (err: unknown) {
console.error(input.ctx.errorMessage(err));
process.exitCode = 1;
}
});
workflowCmd
.command("run")
.description("Trip a workflow trigger (with --field, submits form data)")
.argument("<trigger-id>", "Trigger ID (Ft...)")
.requiredOption("--channel <id-or-name>", "Channel where the workflow is bookmarked")
.option(
"--field <title=value>",
"Form field value (repeatable)",
(v, prev: string[]) => {
prev.push(v);
return prev;
},
[] as string[],
)
.option(
"--workspace <url>",
"Workspace selector (full URL or unique substring; required if you have multiple workspaces)",
)
.action(async (...args) => {
const [triggerId, options] = args as [string, RunOptions];
try {
const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace);
const fieldArgs = options.field ?? [];
if (fieldArgs.length === 0) {
// Trip-only (existing behavior, no WebSocket)
const payload = await input.ctx.withAutoRefresh({
workspaceUrl,
work: async () => {
const { client } = await input.ctx.getClientForWorkspace(workspaceUrl);
const channelId = await resolveChannelId(client, options.channel);
const shortcutUrl = await resolveShortcutUrl(client, { channelId, triggerId });
return await runWorkflow(client, { shortcutUrl, channelId, triggerId });
},
});
console.log(JSON.stringify(pruneEmpty(payload), null, 2));
} else {
// Parse --field args
const fields = new Map<string, string>();
for (const arg of fieldArgs) {
const eqIdx = arg.indexOf("=");
if (eqIdx < 1) {
throw new Error(`Invalid --field format: "${arg}". Expected Title=value`);
}
fields.set(arg.substring(0, eqIdx), arg.substring(eqIdx + 1));
}
const payload = await input.ctx.withAutoRefresh({
workspaceUrl,
work: async () => {
const { client, auth } = await input.ctx.getClientForWorkspace(workspaceUrl);
requireBrowserAuth(auth);
const channelId = await resolveChannelId(client, options.channel);
// Preview → schema → validate before opening WebSocket
const preview = await previewWorkflow(client, triggerId);
const schema = await getWorkflowSchema(client, preview.workflow.id);
const errors = validateFieldInputs(fields, schema);
if (errors.length > 0) {
throw new Error(errors.join("\n"));
}
const shortcutUrl = await resolveShortcutUrl(client, { channelId, triggerId });
return await submitWorkflow({
client,
auth,
shortcutUrl,
channelId,
triggerId,
fields,
schema,
});
},
});
console.log(JSON.stringify(pruneEmpty(payload), null, 2));
}
} catch (err: unknown) {
console.error(input.ctx.errorMessage(err));
process.exitCode = 1;
}
});
}