@sanity/workflows-cli 0.0.0-pr-744-20261109125055
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1848 -0
- package/LICENSE +21 -0
- package/README.md +798 -0
- package/bin/run.js +8 -0
- package/dist/command-ids.d.ts +10 -0
- package/dist/command-ids.js +10 -0
- package/dist/commands/workflows/abort.d.ts +29 -0
- package/dist/commands/workflows/abort.js +50 -0
- package/dist/commands/workflows/blueprints/generate.d.ts +12 -0
- package/dist/commands/workflows/blueprints/generate.js +46 -0
- package/dist/commands/workflows/codemod/packages.d.ts +13 -0
- package/dist/commands/workflows/codemod/packages.js +41 -0
- package/dist/commands/workflows/definitions/delete.d.ts +44 -0
- package/dist/commands/workflows/definitions/delete.js +74 -0
- package/dist/commands/workflows/definitions/diff.d.ts +23 -0
- package/dist/commands/workflows/definitions/diff.js +55 -0
- package/dist/commands/workflows/definitions/list.d.ts +51 -0
- package/dist/commands/workflows/definitions/list.js +139 -0
- package/dist/commands/workflows/definitions/show.d.ts +65 -0
- package/dist/commands/workflows/definitions/show.js +129 -0
- package/dist/commands/workflows/definitions/validate.d.ts +14 -0
- package/dist/commands/workflows/definitions/validate.js +50 -0
- package/dist/commands/workflows/deploy.d.ts +117 -0
- package/dist/commands/workflows/deploy.js +266 -0
- package/dist/commands/workflows/diagnose.d.ts +52 -0
- package/dist/commands/workflows/diagnose.js +195 -0
- package/dist/commands/workflows/fire-action.d.ts +53 -0
- package/dist/commands/workflows/fire-action.js +182 -0
- package/dist/commands/workflows/list.d.ts +78 -0
- package/dist/commands/workflows/list.js +248 -0
- package/dist/commands/workflows/nuke.d.ts +73 -0
- package/dist/commands/workflows/nuke.js +176 -0
- package/dist/commands/workflows/reset-activity.d.ts +32 -0
- package/dist/commands/workflows/reset-activity.js +60 -0
- package/dist/commands/workflows/show.d.ts +29 -0
- package/dist/commands/workflows/show.js +156 -0
- package/dist/commands/workflows/start.d.ts +57 -0
- package/dist/commands/workflows/start.js +196 -0
- package/dist/commands/workflows/tail.d.ts +23 -0
- package/dist/commands/workflows/tail.js +83 -0
- package/dist/commands/workflows/whoami.d.ts +15 -0
- package/dist/commands/workflows/whoami.js +53 -0
- package/dist/help.d.ts +13 -0
- package/dist/help.js +50 -0
- package/dist/hooks/finally/telemetry.d.ts +3 -0
- package/dist/hooks/finally/telemetry.js +9 -0
- package/dist/hooks/prerun/telemetry.d.ts +28 -0
- package/dist/hooks/prerun/telemetry.js +37 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/base-command.d.ts +14 -0
- package/dist/lib/base-command.js +34 -0
- package/dist/lib/blueprint-emit.d.ts +56 -0
- package/dist/lib/blueprint-emit.js +233 -0
- package/dist/lib/blueprint-needs.d.ts +5 -0
- package/dist/lib/blueprint-needs.js +205 -0
- package/dist/lib/blueprint-stacks.d.ts +6 -0
- package/dist/lib/blueprint-stacks.js +88 -0
- package/dist/lib/cause-detail.d.ts +9 -0
- package/dist/lib/cause-detail.js +112 -0
- package/dist/lib/client.d.ts +24 -0
- package/dist/lib/client.js +40 -0
- package/dist/lib/codemod/manifest-rewrite.d.ts +14 -0
- package/dist/lib/codemod/manifest-rewrite.js +97 -0
- package/dist/lib/codemod/package-map.d.ts +22 -0
- package/dist/lib/codemod/package-map.js +72 -0
- package/dist/lib/codemod/report.d.ts +5 -0
- package/dist/lib/codemod/report.js +43 -0
- package/dist/lib/codemod/run.d.ts +23 -0
- package/dist/lib/codemod/run.js +103 -0
- package/dist/lib/codemod/source-rewrite.d.ts +18 -0
- package/dist/lib/codemod/source-rewrite.js +155 -0
- package/dist/lib/command-context.d.ts +20 -0
- package/dist/lib/command-context.js +24 -0
- package/dist/lib/command-telemetry.d.ts +54 -0
- package/dist/lib/command-telemetry.js +82 -0
- package/dist/lib/context.d.ts +155 -0
- package/dist/lib/context.js +268 -0
- package/dist/lib/default-partition.d.ts +23 -0
- package/dist/lib/default-partition.js +48 -0
- package/dist/lib/definition-files.d.ts +11 -0
- package/dist/lib/definition-files.js +58 -0
- package/dist/lib/definitions.d.ts +63 -0
- package/dist/lib/definitions.js +84 -0
- package/dist/lib/diff.d.ts +12 -0
- package/dist/lib/diff.js +93 -0
- package/dist/lib/env.d.ts +7 -0
- package/dist/lib/env.js +8 -0
- package/dist/lib/fail.d.ts +29 -0
- package/dist/lib/fail.js +46 -0
- package/dist/lib/flags.d.ts +49 -0
- package/dist/lib/flags.js +117 -0
- package/dist/lib/follow-up-command.d.ts +18 -0
- package/dist/lib/follow-up-command.js +12 -0
- package/dist/lib/history-rows.d.ts +3 -0
- package/dist/lib/history-rows.js +44 -0
- package/dist/lib/load-config.d.ts +46 -0
- package/dist/lib/load-config.js +110 -0
- package/dist/lib/nuke.d.ts +124 -0
- package/dist/lib/nuke.js +206 -0
- package/dist/lib/operation-args.d.ts +42 -0
- package/dist/lib/operation-args.js +21 -0
- package/dist/lib/ops-report.d.ts +47 -0
- package/dist/lib/ops-report.js +31 -0
- package/dist/lib/params.d.ts +3 -0
- package/dist/lib/params.js +21 -0
- package/dist/lib/pick-dataset.d.ts +10 -0
- package/dist/lib/pick-dataset.js +30 -0
- package/dist/lib/prompt.d.ts +35 -0
- package/dist/lib/prompt.js +11 -0
- package/dist/lib/read-fanout.d.ts +45 -0
- package/dist/lib/read-fanout.js +44 -0
- package/dist/lib/select-deployment.d.ts +31 -0
- package/dist/lib/select-deployment.js +91 -0
- package/dist/lib/share-definitions.d.ts +101 -0
- package/dist/lib/share-definitions.js +188 -0
- package/dist/lib/telemetry-setup.d.ts +81 -0
- package/dist/lib/telemetry-setup.js +111 -0
- package/dist/lib/telemetry.d.ts +78 -0
- package/dist/lib/telemetry.js +90 -0
- package/dist/lib/ui.d.ts +91 -0
- package/dist/lib/ui.js +116 -0
- package/dist/standalone-argv.d.ts +14 -0
- package/dist/standalone-argv.js +57 -0
- package/oclif.manifest.json +1731 -0
- package/package.json +121 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { formatTimeAgo } from '@sanity/cli-core/dates';
|
|
3
|
+
import { WORKFLOW_INSTANCE_TYPE, assignmentPrefilter, assertReadableModel, documentPrefilter, inFlightFilter, instanceWatchesDocument, instanceAssignmentStateCounts, partitionScopeFilter, terminalState, } from '@sanity/workflows';
|
|
4
|
+
import logSymbols from 'log-symbols';
|
|
5
|
+
import { WorkflowCommand } from '../../lib/base-command.js';
|
|
6
|
+
import { resolveReadTargets } from '../../lib/context.js';
|
|
7
|
+
import { failOnThrow } from '../../lib/fail.js';
|
|
8
|
+
import { jsonFlags, readFlags, resourceFlags } from '../../lib/flags.js';
|
|
9
|
+
import { logJsonListing, runReadAcrossTargets } from '../../lib/read-fanout.js';
|
|
10
|
+
import { logClippedTable, searchedScope } from '../../lib/ui.js';
|
|
11
|
+
const DOCUMENT_LIST_PROJECTION = `{
|
|
12
|
+
_type,
|
|
13
|
+
_id,
|
|
14
|
+
workflowResource,
|
|
15
|
+
modelVersion,
|
|
16
|
+
minReaderModel,
|
|
17
|
+
definition,
|
|
18
|
+
tag,
|
|
19
|
+
currentStage,
|
|
20
|
+
fields,
|
|
21
|
+
stages,
|
|
22
|
+
ancestors,
|
|
23
|
+
subworkflows,
|
|
24
|
+
completedAt,
|
|
25
|
+
abortedAt,
|
|
26
|
+
lastChangedAt
|
|
27
|
+
}`;
|
|
28
|
+
export function buildListQuery(flags) {
|
|
29
|
+
const filters = [`_type == "${WORKFLOW_INSTANCE_TYPE}"`];
|
|
30
|
+
const params = {};
|
|
31
|
+
if (!flags['include-completed'])
|
|
32
|
+
filters.push(inFlightFilter());
|
|
33
|
+
if (flags.definition) {
|
|
34
|
+
filters.push('definition == $definition');
|
|
35
|
+
params['definition'] = flags.definition;
|
|
36
|
+
}
|
|
37
|
+
if (flags.failed)
|
|
38
|
+
filters.push('count(stages[].activities[status == "failed"]) > 0');
|
|
39
|
+
if (flags.partition) {
|
|
40
|
+
filters.push(partitionScopeFilter());
|
|
41
|
+
params['tag'] = flags.partition;
|
|
42
|
+
}
|
|
43
|
+
const assignment = assignmentFilter(flags);
|
|
44
|
+
if (assignment !== undefined) {
|
|
45
|
+
const armParams = {};
|
|
46
|
+
filters.push(assignmentPrefilter(assignment, armParams));
|
|
47
|
+
Object.assign(params, armParams);
|
|
48
|
+
}
|
|
49
|
+
if (flags.document) {
|
|
50
|
+
const armParams = {};
|
|
51
|
+
filters.push(`(${documentPrefilter([flags.document], armParams)})`);
|
|
52
|
+
Object.assign(params, armParams);
|
|
53
|
+
params['limit'] = flags.limit + 1;
|
|
54
|
+
return {
|
|
55
|
+
groq: `*[${filters.join(' && ')}] | order(lastChangedAt desc)[0...$limit]${DOCUMENT_LIST_PROJECTION}`,
|
|
56
|
+
params,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
params['limit'] = flags.limit + 1;
|
|
60
|
+
const groq = `*[${filters.join(' && ')}] | order(lastChangedAt desc) [0...$limit]{
|
|
61
|
+
_id,
|
|
62
|
+
definition,
|
|
63
|
+
tag,
|
|
64
|
+
currentStage,
|
|
65
|
+
completedAt,
|
|
66
|
+
abortedAt,
|
|
67
|
+
lastChangedAt,
|
|
68
|
+
modelVersion,
|
|
69
|
+
minReaderModel
|
|
70
|
+
${assignment === undefined ? '' : ', stages'}
|
|
71
|
+
}`;
|
|
72
|
+
return { groq, params };
|
|
73
|
+
}
|
|
74
|
+
function assignmentFilter(flags) {
|
|
75
|
+
const userId = flags['assignment-user'];
|
|
76
|
+
const roles = flags['assignment-role'];
|
|
77
|
+
const rawStates = flags['assignment-state'];
|
|
78
|
+
if (userId === undefined) {
|
|
79
|
+
if (roles !== undefined || rawStates !== undefined) {
|
|
80
|
+
throw new Error('--assignment-role and --assignment-state require --assignment-user');
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const states = rawStates;
|
|
85
|
+
return {
|
|
86
|
+
userId,
|
|
87
|
+
...(roles === undefined ? {} : { roles }),
|
|
88
|
+
...(states === undefined ? {} : { states }),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function instanceRow(r, identity) {
|
|
92
|
+
return {
|
|
93
|
+
_id: r._id,
|
|
94
|
+
definition: r.definition,
|
|
95
|
+
partition: r.tag,
|
|
96
|
+
currentStage: r.currentStage,
|
|
97
|
+
status: terminalState({
|
|
98
|
+
...(r.completedAt != null ? { completedAt: r.completedAt } : {}),
|
|
99
|
+
...(r.abortedAt != null ? { abortedAt: r.abortedAt } : {}),
|
|
100
|
+
}),
|
|
101
|
+
lastChangedAt: r.lastChangedAt,
|
|
102
|
+
...(identity === undefined ? {} : { assignment: assignmentCounts(r, identity) }),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function assignmentCounts(row, identity) {
|
|
106
|
+
return instanceAssignmentStateCounts({ currentStage: row.currentStage, stages: row.stages ?? [] }, identity);
|
|
107
|
+
}
|
|
108
|
+
function assignmentIdentityFor(flags) {
|
|
109
|
+
const assignment = assignmentFilter(flags);
|
|
110
|
+
return assignment === undefined
|
|
111
|
+
? undefined
|
|
112
|
+
: { userId: assignment.userId, roles: assignment.roles ?? [] };
|
|
113
|
+
}
|
|
114
|
+
async function logTargetInstances(args) {
|
|
115
|
+
const fetched = await fetchRows(args);
|
|
116
|
+
if (fetched.rows.length === 0) {
|
|
117
|
+
const message = fetched.hasMore
|
|
118
|
+
? `no matches in the first ${args.limit} candidates ${args.scope} — raise --limit to search further`
|
|
119
|
+
: `no instances match ${args.scope}`;
|
|
120
|
+
args.log(`${logSymbols.info} ${message}`);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
logClippedTable({
|
|
124
|
+
rows: fetched.rows,
|
|
125
|
+
limit: args.limit,
|
|
126
|
+
moreAvailable: fetched.hasMore,
|
|
127
|
+
headers: [
|
|
128
|
+
'instance',
|
|
129
|
+
'workflow',
|
|
130
|
+
'partition',
|
|
131
|
+
'stage',
|
|
132
|
+
'status',
|
|
133
|
+
'updated',
|
|
134
|
+
...(args.identity === undefined ? [] : ['assignment u/r/h']),
|
|
135
|
+
],
|
|
136
|
+
toCells: (candidate) => {
|
|
137
|
+
const row = instanceRow(candidate, args.identity);
|
|
138
|
+
return [
|
|
139
|
+
row._id,
|
|
140
|
+
row.definition,
|
|
141
|
+
row.partition,
|
|
142
|
+
row.currentStage,
|
|
143
|
+
row.status,
|
|
144
|
+
formatTimeAgo(new Date(row.lastChangedAt)),
|
|
145
|
+
...(row.assignment === undefined
|
|
146
|
+
? []
|
|
147
|
+
: [`${row.assignment.unrouted}/${row.assignment.routed}/${row.assignment.held}`]),
|
|
148
|
+
];
|
|
149
|
+
},
|
|
150
|
+
log: args.log,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
export default class List extends WorkflowCommand {
|
|
154
|
+
static description = 'List workflow instances (in-flight by default) across the resources sanity.workflow.ts declares, or in the one resource --project-id/--dataset or --media-library-id names.';
|
|
155
|
+
static examples = [
|
|
156
|
+
'<%= config.bin %> workflows list',
|
|
157
|
+
'<%= config.bin %> workflows list --include-completed',
|
|
158
|
+
'<%= config.bin %> workflows list --definition product-launch',
|
|
159
|
+
'<%= config.bin %> workflows list --document dataset:proj:ds:article-1',
|
|
160
|
+
'<%= config.bin %> workflows list --assignment-user gAda --assignment-role legal --assignment-state routed',
|
|
161
|
+
'<%= config.bin %> workflows list --partition prod',
|
|
162
|
+
'<%= config.bin %> workflows list --json',
|
|
163
|
+
];
|
|
164
|
+
static flags = {
|
|
165
|
+
...readFlags,
|
|
166
|
+
...resourceFlags,
|
|
167
|
+
'include-completed': Flags.boolean({
|
|
168
|
+
description: 'Include completed/aborted instances (default: in-flight only).',
|
|
169
|
+
default: false,
|
|
170
|
+
}),
|
|
171
|
+
failed: Flags.boolean({
|
|
172
|
+
description: 'Only instances with at least one failed activity.',
|
|
173
|
+
default: false,
|
|
174
|
+
}),
|
|
175
|
+
definition: Flags.string({
|
|
176
|
+
description: "Only instances of this workflow definition (its `name`; the instance's `definition` field).",
|
|
177
|
+
}),
|
|
178
|
+
document: Flags.string({
|
|
179
|
+
description: 'Only instances that reference this document (resource-qualified GDR URI, ' +
|
|
180
|
+
'e.g. "dataset:proj:ds:article-1").',
|
|
181
|
+
}),
|
|
182
|
+
'assignment-user': Flags.string({
|
|
183
|
+
description: 'Account-global user id used for viewer-scoped assignment filtering and counts.',
|
|
184
|
+
}),
|
|
185
|
+
'assignment-role': Flags.string({
|
|
186
|
+
description: 'A literal project role held by --assignment-user. Repeat for multiple roles.',
|
|
187
|
+
multiple: true,
|
|
188
|
+
}),
|
|
189
|
+
'assignment-state': Flags.string({
|
|
190
|
+
description: 'Assignment state to include: unrouted, routed (offered through a supplied role), or held. Repeat for multiple states.',
|
|
191
|
+
multiple: true,
|
|
192
|
+
options: ['unrouted', 'routed', 'held'],
|
|
193
|
+
}),
|
|
194
|
+
limit: Flags.integer({
|
|
195
|
+
description: 'Maximum rows to return.',
|
|
196
|
+
default: 50,
|
|
197
|
+
}),
|
|
198
|
+
...jsonFlags,
|
|
199
|
+
};
|
|
200
|
+
async run() {
|
|
201
|
+
const { flags } = await this.parse(List);
|
|
202
|
+
const { targets, partition } = await resolveReadTargets(flags);
|
|
203
|
+
const { groq, params } = failOnThrow('Invalid list filter:', () => buildListQuery({ ...flags, partition }));
|
|
204
|
+
const document = flags.document;
|
|
205
|
+
const identity = assignmentIdentityFor(flags);
|
|
206
|
+
if (flags.json) {
|
|
207
|
+
await logJsonListing({
|
|
208
|
+
targets,
|
|
209
|
+
key: 'instances',
|
|
210
|
+
limit: flags.limit,
|
|
211
|
+
log: (line) => this.log(line),
|
|
212
|
+
fetch: ({ client }) => fetchRows({ client, groq, params, document, limit: flags.limit }),
|
|
213
|
+
toRow: (row) => instanceRow(row, identity),
|
|
214
|
+
});
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const failures = await runReadAcrossTargets({
|
|
218
|
+
targets,
|
|
219
|
+
log: (line) => this.log(line),
|
|
220
|
+
run: ({ client, resource }) => logTargetInstances({
|
|
221
|
+
client,
|
|
222
|
+
groq,
|
|
223
|
+
params,
|
|
224
|
+
document,
|
|
225
|
+
limit: flags.limit,
|
|
226
|
+
identity,
|
|
227
|
+
scope: searchedScope({ partition, resources: [resource], deployment: flags.deployment }),
|
|
228
|
+
log: (line) => this.log(line),
|
|
229
|
+
}),
|
|
230
|
+
});
|
|
231
|
+
if (failures.length > 0) {
|
|
232
|
+
this.exit(1);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
export async function fetchRows({ client, groq, params, document, limit, }) {
|
|
237
|
+
if (document === undefined) {
|
|
238
|
+
const rows = await client.fetch(groq, params, { tag: 'list' });
|
|
239
|
+
return { rows: rows.map(assertReadableModel), hasMore: rows.length > limit };
|
|
240
|
+
}
|
|
241
|
+
const candidates = await client.fetch(groq, params, { tag: 'list' });
|
|
242
|
+
return {
|
|
243
|
+
rows: candidates
|
|
244
|
+
.map(assertReadableModel)
|
|
245
|
+
.filter((instance) => instanceWatchesDocument(instance, document)),
|
|
246
|
+
hasMore: candidates.length > limit,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type WorkflowConfig, type WorkflowDeployment, type WorkflowInstance, type WorkflowResource } from '@sanity/workflows';
|
|
2
|
+
import { WorkflowCommand } from '../../lib/base-command.ts';
|
|
3
|
+
import { type FollowUpContext } from '../../lib/follow-up-command.ts';
|
|
4
|
+
export default class Nuke extends WorkflowCommand {
|
|
5
|
+
static summary: string;
|
|
6
|
+
static description: string;
|
|
7
|
+
static examples: string[];
|
|
8
|
+
static flags: {
|
|
9
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
instance: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
force: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
14
|
+
};
|
|
15
|
+
run(): Promise<void>;
|
|
16
|
+
/** Partition mode: delete every engine-owned document for the selected deployment. */
|
|
17
|
+
private nukePartition;
|
|
18
|
+
/**
|
|
19
|
+
* Instance mode: delete ONE terminal instance plus its guard docs. Refuses an
|
|
20
|
+
* in-flight instance — abort it first, so pending effects are cancelled and
|
|
21
|
+
* children condemned; this command only removes what is already settled. The
|
|
22
|
+
* guard sweep runs regardless of how it terminated: a normally completed
|
|
23
|
+
* instance still holds its terminal-stage guards.
|
|
24
|
+
*
|
|
25
|
+
* Deliberately does NOT run {@link refuseOverlappingNuke}, which partition mode
|
|
26
|
+
* must. That refusal exists because guard ids embed only the partition, so a
|
|
27
|
+
* partition sweep cannot tell two same-partition deployments' guards apart in
|
|
28
|
+
* a shared resource. The instance sweep matches `sourceInstanceId` (and a `<type>.<instanceId>.` id
|
|
29
|
+
* prefix), both of which name one instance — so same-partition siblings cannot
|
|
30
|
+
* collide and adding the gate back would refuse valid deletes.
|
|
31
|
+
*/
|
|
32
|
+
private deleteInstance;
|
|
33
|
+
}
|
|
34
|
+
export declare function assertNukeFlags(flags: {
|
|
35
|
+
instance?: string | undefined;
|
|
36
|
+
deployment?: string | undefined;
|
|
37
|
+
partition?: string | undefined;
|
|
38
|
+
}): void;
|
|
39
|
+
/**
|
|
40
|
+
* The configured deployment that owns an instance — matched by the partition
|
|
41
|
+
* stored in the instance's `tag` field AND the resource that held it. Config
|
|
42
|
+
* forbids two deployments sharing partition + workflow resource, so this is
|
|
43
|
+
* unambiguous. Needed for the alias-bound resources a single-instance guard
|
|
44
|
+
* sweep must also cover.
|
|
45
|
+
*
|
|
46
|
+
* Both halves of the match are named on failure: a partition that IS configured
|
|
47
|
+
* but on another resource is the likelier miss of the two, and naming only the
|
|
48
|
+
* partition makes that read as though the partition were unknown.
|
|
49
|
+
*/
|
|
50
|
+
export declare function deploymentForInstanceScope(config: WorkflowConfig, located: {
|
|
51
|
+
partition: string | undefined;
|
|
52
|
+
resource: WorkflowResource;
|
|
53
|
+
}): WorkflowDeployment;
|
|
54
|
+
/**
|
|
55
|
+
* Refuse anything that is not an engine instance document. The locate reads a
|
|
56
|
+
* projection by raw id, so nothing upstream has proved the id names an instance
|
|
57
|
+
* — and engine documents can share a dataset with the content they govern, where
|
|
58
|
+
* a bare id would otherwise let a delete reach a content document. The plan
|
|
59
|
+
* promises "content is never touched"; this is what makes that true.
|
|
60
|
+
*/
|
|
61
|
+
export declare function assertEngineInstanceOrFail(document: {
|
|
62
|
+
_type: string;
|
|
63
|
+
}, instanceId: string): void;
|
|
64
|
+
/**
|
|
65
|
+
* Refuse to delete an in-flight instance — abort it first, so its pending
|
|
66
|
+
* effects are cancelled and children condemned. This command only removes what
|
|
67
|
+
* is already settled ({@link terminalState} is not `'in-flight'`); it performs
|
|
68
|
+
* no engine mutation of its own.
|
|
69
|
+
*/
|
|
70
|
+
export declare function assertInstanceTerminalOrFail(instance: Pick<WorkflowInstance, 'completedAt' | 'abortedAt'>, { instanceId, followUp }: {
|
|
71
|
+
instanceId: string;
|
|
72
|
+
followUp: FollowUpContext;
|
|
73
|
+
}): void;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { input } from '@sanity/cli-core/ux';
|
|
3
|
+
import { WORKFLOW_INSTANCE_TYPE, resourceGdr, terminalState, } from '@sanity/workflows';
|
|
4
|
+
import logSymbols from 'log-symbols';
|
|
5
|
+
import { WorkflowCommand } from '../../lib/base-command.js';
|
|
6
|
+
import { clientFor, resolveApiHost, resolveTokenOrFail } from '../../lib/client.js';
|
|
7
|
+
import { dedupeResources, resolveInstanceStamps } from '../../lib/context.js';
|
|
8
|
+
import { fail } from '../../lib/fail.js';
|
|
9
|
+
import { configFlags, partitionFlag } from '../../lib/flags.js';
|
|
10
|
+
import { followUpCommand } from '../../lib/follow-up-command.js';
|
|
11
|
+
import { loadWorkflowConfig } from '../../lib/load-config.js';
|
|
12
|
+
import { confirmationMatches, executeNuke, formatInstanceNukeSummary, formatNukePreview, formatNukeSummary, involvedTargets, planCounts, refuseOverlappingNuke, renderNukePlan, resolveInstanceNukePlan, resolveNukePlan, sweptResources, } from '../../lib/nuke.js';
|
|
13
|
+
import { runWriteVerb } from '../../lib/ops-report.js';
|
|
14
|
+
import { canPromptOnStderr, confirmOnStderr } from '../../lib/prompt.js';
|
|
15
|
+
import { selectDeployment } from '../../lib/select-deployment.js';
|
|
16
|
+
import { resourceLabel } from '../../lib/ui.js';
|
|
17
|
+
function planApiHost() {
|
|
18
|
+
return resolveApiHost() ?? 'https://api.sanity.io (production default)';
|
|
19
|
+
}
|
|
20
|
+
export default class Nuke extends WorkflowCommand {
|
|
21
|
+
static summary = 'Delete engine-owned documents — a whole deployment partition, or a single instance.';
|
|
22
|
+
static description = 'The reset for a dataset holding engine documents the versioned upgrade framework cannot yet ' +
|
|
23
|
+
"migrate: deletes the partition's instances, definitions, and guards (across every alias-bound " +
|
|
24
|
+
'resource). --instance <id> instead deletes one terminal instance plus its guards. Content ' +
|
|
25
|
+
'documents are never touched. Prints a dry-run plan, then confirms (--force skips the prompt; ' +
|
|
26
|
+
'the plan still prints).';
|
|
27
|
+
static examples = [
|
|
28
|
+
'<%= config.bin %> workflows nuke --deployment plugin-dev',
|
|
29
|
+
'<%= config.bin %> workflows nuke --partition plugin-dev --force',
|
|
30
|
+
'<%= config.bin %> workflows nuke --instance plugin-dev.wf-instance.abc123',
|
|
31
|
+
];
|
|
32
|
+
static flags = {
|
|
33
|
+
...configFlags,
|
|
34
|
+
deployment: Flags.string({
|
|
35
|
+
description: 'The deployment name to reset. With --instance, narrows which deployment the ' +
|
|
36
|
+
'instance is looked up in when its partition spans several.',
|
|
37
|
+
}),
|
|
38
|
+
partition: partitionFlag({
|
|
39
|
+
description: 'The deployment partition to reset (while it names exactly one deployment). Not valid ' +
|
|
40
|
+
'with --instance, which reads its partition from the stored instance.',
|
|
41
|
+
}),
|
|
42
|
+
instance: Flags.string({
|
|
43
|
+
description: 'Delete a single terminal instance by id, plus its guard docs, instead of a partition.',
|
|
44
|
+
}),
|
|
45
|
+
force: Flags.boolean({
|
|
46
|
+
description: 'Skip the confirmation prompt (for scripts/CI). The plan still prints.',
|
|
47
|
+
default: false,
|
|
48
|
+
}),
|
|
49
|
+
};
|
|
50
|
+
async run() {
|
|
51
|
+
const { flags } = await this.parse(Nuke);
|
|
52
|
+
assertNukeFlags(flags);
|
|
53
|
+
if (flags.instance !== undefined) {
|
|
54
|
+
await this.deleteInstance(flags.instance, flags);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await this.nukePartition(flags);
|
|
58
|
+
}
|
|
59
|
+
async nukePartition(flags) {
|
|
60
|
+
const config = await loadWorkflowConfig(flags.path);
|
|
61
|
+
const deployment = await selectDeployment(config, {
|
|
62
|
+
name: flags.deployment,
|
|
63
|
+
partition: flags.partition,
|
|
64
|
+
});
|
|
65
|
+
refuseOverlappingNuke(config.deployments, deployment);
|
|
66
|
+
const targets = buildTargets(deployment, await resolveTokenOrFail());
|
|
67
|
+
const plan = await resolveNukePlan({ partition: deployment.partition, targets });
|
|
68
|
+
renderNukePlan(plan, { apiHost: planApiHost() }).forEach((line) => this.log(line));
|
|
69
|
+
const counts = planCounts(plan);
|
|
70
|
+
if (counts.total === 0) {
|
|
71
|
+
this.log(`${logSymbols.info} Nothing to delete in partition "${deployment.partition}".`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.log(formatNukePreview(deployment.partition, counts));
|
|
75
|
+
if (!flags.force)
|
|
76
|
+
await confirmOrFail(involvedTargets(plan));
|
|
77
|
+
await runWriteVerb({
|
|
78
|
+
startLabel: 'Nuking…',
|
|
79
|
+
failLabel: 'Nuke failed',
|
|
80
|
+
failHeadline: 'workflow nuke error:',
|
|
81
|
+
run: () => executeNuke(plan),
|
|
82
|
+
report: () => ({ changed: true, message: formatNukeSummary(deployment.partition, counts) }),
|
|
83
|
+
log: (line) => this.log(line),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async deleteInstance(instanceId, flags) {
|
|
87
|
+
const config = await loadWorkflowConfig(flags.path);
|
|
88
|
+
const { resource, stamps } = await resolveInstanceStamps(flags, instanceId);
|
|
89
|
+
assertEngineInstanceOrFail(stamps, instanceId);
|
|
90
|
+
assertInstanceTerminalOrFail(stamps, {
|
|
91
|
+
instanceId,
|
|
92
|
+
followUp: { bin: this.config.bin, selection: { deployment: flags.deployment } },
|
|
93
|
+
});
|
|
94
|
+
const deployment = deploymentForInstanceScope(config, { partition: stamps.tag, resource });
|
|
95
|
+
const targets = buildTargets(deployment, await resolveTokenOrFail());
|
|
96
|
+
const plan = await resolveInstanceNukePlan({
|
|
97
|
+
instance: { id: instanceId, resource },
|
|
98
|
+
partition: deployment.partition,
|
|
99
|
+
targets,
|
|
100
|
+
});
|
|
101
|
+
renderNukePlan(plan, {
|
|
102
|
+
apiHost: planApiHost(),
|
|
103
|
+
heading: `nuke — instance "${instanceId}"`,
|
|
104
|
+
}).forEach((line) => this.log(line));
|
|
105
|
+
const counts = planCounts(plan);
|
|
106
|
+
if (!flags.force)
|
|
107
|
+
await confirmInstanceDeleteOrFail(instanceId);
|
|
108
|
+
await runWriteVerb({
|
|
109
|
+
startLabel: `Deleting ${instanceId}…`,
|
|
110
|
+
failLabel: 'Delete failed',
|
|
111
|
+
failHeadline: 'workflow nuke error:',
|
|
112
|
+
run: () => executeNuke(plan),
|
|
113
|
+
report: () => ({ changed: true, message: formatInstanceNukeSummary(instanceId, counts) }),
|
|
114
|
+
log: (line) => this.log(line),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
export function assertNukeFlags(flags) {
|
|
119
|
+
if (flags.instance !== undefined) {
|
|
120
|
+
if (flags.partition !== undefined) {
|
|
121
|
+
fail('--instance cannot be combined with --partition.', 'The partition is read from the stored instance; pass --deployment to disambiguate if needed.');
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const selectors = [flags.deployment, flags.partition].filter((value) => value !== undefined);
|
|
126
|
+
if (selectors.length !== 1) {
|
|
127
|
+
fail('Pass exactly one of --deployment or --partition (or --instance <id> to delete one instance).');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
export function deploymentForInstanceScope(config, located) {
|
|
131
|
+
const gdr = resourceGdr(located.resource);
|
|
132
|
+
const deployment = config.deployments.find((candidate) => candidate.partition === located.partition && resourceGdr(candidate.workflowResource) === gdr);
|
|
133
|
+
if (deployment === undefined) {
|
|
134
|
+
fail(`No deployment in this workflow config has partition "${located.partition}" on ` +
|
|
135
|
+
`${resourceLabel(located.resource)}.`, 'A single-instance delete resolves the deployment to find the alias-bound resources its ' +
|
|
136
|
+
'guards can live in. Add that deployment to the config, or pass --deployment to name ' +
|
|
137
|
+
'the one that should own it.');
|
|
138
|
+
}
|
|
139
|
+
return deployment;
|
|
140
|
+
}
|
|
141
|
+
export function assertEngineInstanceOrFail(document, instanceId) {
|
|
142
|
+
if (document._type !== WORKFLOW_INSTANCE_TYPE) {
|
|
143
|
+
fail(`${instanceId} is not a workflow instance (its _type is "${document._type}").`, `Only ${WORKFLOW_INSTANCE_TYPE} documents can be deleted this way.`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export function assertInstanceTerminalOrFail(instance, { instanceId, followUp }) {
|
|
147
|
+
if (terminalState(instance) === 'in-flight') {
|
|
148
|
+
fail('Instance is in-flight — abort it first, then delete.', `Run: ${followUpCommand(followUp, 'abort', instanceId)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function buildTargets(deployment, token) {
|
|
152
|
+
const engineGdr = resourceGdr(deployment.workflowResource);
|
|
153
|
+
return dedupeResources(sweptResources(deployment)).map((resource) => ({
|
|
154
|
+
resource,
|
|
155
|
+
client: clientFor(resource, token),
|
|
156
|
+
holdsEngineDocs: resourceGdr(resource) === engineGdr,
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
function assertPromptableOrFail(action) {
|
|
160
|
+
if (!canPromptOnStderr()) {
|
|
161
|
+
fail(`Refusing to ${action} without confirmation in a non-interactive shell.`, 'Re-run with --force to skip the prompt.');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async function confirmOrFail(targets) {
|
|
165
|
+
assertPromptableOrFail('nuke');
|
|
166
|
+
const answer = await input({ message: `Type every target to confirm deletion (space-separated):\n ${targets.join(' ')}\n` }, { output: process.stderr });
|
|
167
|
+
if (!confirmationMatches(answer, targets)) {
|
|
168
|
+
fail('Confirmation did not match — nothing was deleted.', `Expected: ${targets.join(' ')}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function confirmInstanceDeleteOrFail(instanceId) {
|
|
172
|
+
assertPromptableOrFail('delete');
|
|
173
|
+
if (!(await confirmOnStderr(`Delete instance ${instanceId}?`))) {
|
|
174
|
+
fail('Cancelled — nothing was deleted.');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ResetActivityTarget, type WorkflowInstance } from '@sanity/workflows';
|
|
2
|
+
import { WorkflowCommand } from '../../lib/base-command.ts';
|
|
3
|
+
import { type WriteOutcome, type WriteReport } from '../../lib/ops-report.ts';
|
|
4
|
+
export default class ResetActivity extends WorkflowCommand {
|
|
5
|
+
static description: string;
|
|
6
|
+
static examples: string[];
|
|
7
|
+
static args: {
|
|
8
|
+
instanceId: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
9
|
+
activity: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
10
|
+
};
|
|
11
|
+
static flags: {
|
|
12
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
|
+
skip: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
};
|
|
17
|
+
run(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** {@link WriteOutcome} with the terminal stamp the no-op copy reads. */
|
|
20
|
+
interface ResetActivityOutcome extends WriteOutcome {
|
|
21
|
+
instance: Pick<WorkflowInstance, 'currentStage' | 'completedAt'>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Turn a `resetActivity` result into the spinner message. Pure so the
|
|
25
|
+
* skipped / re-run / already-at-target / terminal / cascaded permutations can
|
|
26
|
+
* be asserted without driving a real reset.
|
|
27
|
+
*/
|
|
28
|
+
export declare function resetActivityReport(result: ResetActivityOutcome, { activity, to }: {
|
|
29
|
+
activity: string;
|
|
30
|
+
to: ResetActivityTarget;
|
|
31
|
+
}): WriteReport;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
import { Args, Flags } from '@oclif/core';
|
|
3
|
+
import { workflow } from '@sanity/workflows';
|
|
4
|
+
import { WorkflowCommand } from '../../lib/base-command.js';
|
|
5
|
+
import { resolveInstanceContext } from '../../lib/context.js';
|
|
6
|
+
import { instanceFlags } from '../../lib/flags.js';
|
|
7
|
+
import { buildOperationArgs } from '../../lib/operation-args.js';
|
|
8
|
+
import { cascadeTail, runWriteVerb, } from '../../lib/ops-report.js';
|
|
9
|
+
export default class ResetActivity extends WorkflowCommand {
|
|
10
|
+
static description = 'Reset a failed activity on an in-flight instance — back to active to re-run it, or --skip to bypass it (mark it skipped) so a gated exit transition can fire.';
|
|
11
|
+
static examples = [
|
|
12
|
+
'<%= config.bin %> workflows reset-activity wf-instance.abc123 legal-review',
|
|
13
|
+
'<%= config.bin %> workflows reset-activity wf-instance.abc123 legal-review --skip',
|
|
14
|
+
];
|
|
15
|
+
static args = {
|
|
16
|
+
instanceId: Args.string({ required: true, description: 'Workflow instance id.' }),
|
|
17
|
+
activity: Args.string({ required: true, description: 'Activity name within the current stage.' }),
|
|
18
|
+
};
|
|
19
|
+
static flags = {
|
|
20
|
+
...instanceFlags,
|
|
21
|
+
skip: Flags.boolean({
|
|
22
|
+
default: false,
|
|
23
|
+
description: 'Bypass the activity (mark it skipped) instead of re-running it (back to active).',
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
async run() {
|
|
27
|
+
const { args, flags } = await this.parse(ResetActivity);
|
|
28
|
+
const { client, scope } = await resolveInstanceContext(flags, args.instanceId);
|
|
29
|
+
const to = flags.skip ? 'skipped' : 'active';
|
|
30
|
+
await runWriteVerb({
|
|
31
|
+
startLabel: `Resetting ${args.activity} on ${args.instanceId}…`,
|
|
32
|
+
failLabel: 'Reset rejected',
|
|
33
|
+
failHeadline: 'reset-activity error:',
|
|
34
|
+
run: () => workflow.resetActivity({
|
|
35
|
+
client,
|
|
36
|
+
...buildOperationArgs({ scope, instanceId: args.instanceId, reason: undefined }),
|
|
37
|
+
activity: args.activity,
|
|
38
|
+
to,
|
|
39
|
+
}),
|
|
40
|
+
report: (result) => resetActivityReport(result, { activity: args.activity, to }),
|
|
41
|
+
log: (line) => this.log(line),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export function resetActivityReport(result, { activity, to }) {
|
|
46
|
+
const name = styleText('bold', activity);
|
|
47
|
+
if (!result.changed) {
|
|
48
|
+
const { completedAt } = result.instance;
|
|
49
|
+
const message = completedAt !== undefined
|
|
50
|
+
? `reset-activity changed nothing — instance is terminal (since ${completedAt})`
|
|
51
|
+
: `reset-activity changed nothing — ${name} is already ${to}`;
|
|
52
|
+
return { changed: false, message };
|
|
53
|
+
}
|
|
54
|
+
const stage = styleText('bold', result.instance.currentStage);
|
|
55
|
+
const verb = to === 'skipped' ? 'Skipped' : 'Reset';
|
|
56
|
+
return {
|
|
57
|
+
changed: true,
|
|
58
|
+
message: `${verb} ${name} — now at ${stage}${cascadeTail(result.cascaded)}`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type WorkflowInstance } from '@sanity/workflows';
|
|
2
|
+
import { WorkflowCommand } from '../../lib/base-command.ts';
|
|
3
|
+
import { type InstanceDiagnostic } from '../../lib/cause-detail.ts';
|
|
4
|
+
export default class Show extends WorkflowCommand {
|
|
5
|
+
static description: string;
|
|
6
|
+
static examples: string[];
|
|
7
|
+
static args: {
|
|
8
|
+
instanceId: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
9
|
+
};
|
|
10
|
+
static flags: {
|
|
11
|
+
path: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
'media-library-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
|
+
deployment: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
16
|
+
partition: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
17
|
+
json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
18
|
+
include: import("@oclif/core/interfaces").OptionFlag<string[], import("@oclif/core/interfaces").CustomOptions>;
|
|
19
|
+
};
|
|
20
|
+
run(): Promise<void>;
|
|
21
|
+
private diagnostic;
|
|
22
|
+
}
|
|
23
|
+
/** The summary block at the top of `show` (and reused by `diagnose` /
|
|
24
|
+
* `fire-action`): a bold workflow + id title, then aligned detail rows. */
|
|
25
|
+
export declare function instanceHeader(instance: WorkflowInstance): string;
|
|
26
|
+
export declare function describeInstance(instance: WorkflowInstance, options: {
|
|
27
|
+
includeHistory: boolean;
|
|
28
|
+
diagnostic?: InstanceDiagnostic | undefined;
|
|
29
|
+
}): string[];
|