@jacebenson/jsn 5.5.1-dev.159.1 → 5.5.1-dev.160.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.1-dev.159.1",
3
+ "version": "5.5.1-dev.160.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,11 +10,29 @@ import { alignUpdateSetCapture } from '../context.js';
10
10
  import { EXCLUDED_TRIGGER_TYPES, SUPPORTED_TRIGGER_TYPES, readTriggerCatalog, readTriggerInputDefinitions } from '../flow-triggers.js';
11
11
  import { testFlow } from '../flow-test.js';
12
12
  import { releaseOwnFlowSafeEdit } from '../flow-safe-edit.js';
13
- import { errUsage } from '../errors.js';
13
+ import { AppError, CodeAPI, CodeForbidden, errUsage } from '../errors.js';
14
14
  import { cancelled, pickDesignerRecord, guidedFlowCreate, guidedFlowEdit, guidedFlowTestRecord, hasDefinitionData, confirmDesignerMutation } from '../flow-designer-interactive.js';
15
15
 
16
16
  const pickFlow = (argv, app, query = '') => pickDesignerRecord(argv, app, argv.action ? 'sys_hub_action_type_definition' : 'sys_hub_flow', argv.action ? 'action' : 'flow', query);
17
17
 
18
+ // Rows the provider cascades when the sys_hub_flow record is deleted. Counted
19
+ // before and after so the command reports what went with the flow.
20
+ const FLOW_OWNED_TABLES = [
21
+ { table: 'sys_hub_flow_version', label: 'version', query: id => `flow=${id}` },
22
+ { table: 'sys_hub_action_instance_v2', label: 'action instance', query: id => `flow=${id}` },
23
+ { table: 'sys_hub_flow_logic_instance_v2', label: 'logic block', query: id => `flow=${id}` },
24
+ { table: 'sys_hub_flow_stage', label: 'stage', query: id => `flow=${id}` },
25
+ // Flow variables hang off `model`, not `flow`.
26
+ { table: 'sys_hub_flow_variable', label: 'flow variable', query: id => `model=${id}` },
27
+ ];
28
+
29
+ function describeOwned(counts) {
30
+ const parts = Object.entries(counts)
31
+ .filter(([, count]) => count)
32
+ .map(([table, count]) => `${count} ${FLOW_OWNED_TABLES.find(entry => entry.table === table)?.label ?? table}${count === 1 ? '' : 's'}`);
33
+ return parts.length ? parts.join(', ') : 'nothing';
34
+ }
35
+
18
36
  const FLOW_COMMANDS = [
19
37
  { name: 'schemas', description: 'Read live trigger catalog and input schemas' },
20
38
  { name: 'catalog-variables [identifier]', description: 'List a catalog item\'s variables for a service_catalog trigger' },
@@ -28,7 +46,7 @@ const FLOW_COMMANDS = [
28
46
  { name: 'update [identifier]', description: 'Update a draft, or a published flow whose definition reports write access (alias: edit)' },
29
47
  { name: 'doctor', description: 'Check whether this instance can publish flows at all' },
30
48
  { name: 'unlock [identifier]', description: 'Release this user\'s stale safe-edit session (orphaned lease) after a failed update' },
31
- { name: 'delete [identifier]', description: 'Delete a flow (not implemented: use the Flow Designer UI)' },
49
+ { name: 'delete [identifier]', description: 'Delete a flow and the versions, actions, logic and stages it owns' },
32
50
  ];
33
51
 
34
52
  function flowInspectionAdapter(app) {
@@ -453,12 +471,53 @@ export function flowsCmd(wrap) {
453
471
  })
454
472
  .command({
455
473
  command: 'delete [identifier]',
456
- describe: 'Delete a flow (not implemented: use the Flow Designer UI)',
474
+ describe: 'Delete a flow and everything that belongs to it',
457
475
  handler: wrap(async (argv, app) => {
476
+ app.requireInstance();
458
477
  const identifier = await pickFlow(argv, app);
459
478
  if (identifier === undefined) return;
460
- throw errUsage('Flow deletion requires the Flow Designer GraphQL API - not yet implemented.\n'
461
- + 'Use the ServiceNow web UI to delete flows.');
479
+ const sysID = unwrapSysId(await resolveRecord(app.sdk, {
480
+ table: 'sys_hub_flow', identifier, matchField: 'name', resource: 'Flow',
481
+ }));
482
+ const [flow] = await app.sdk.list('sys_hub_flow', new URLSearchParams({
483
+ sysparm_query: `sys_id=${sysID}`,
484
+ sysparm_display_value: 'all',
485
+ sysparm_fields: 'sys_id,name,status,active,sys_scope',
486
+ sysparm_limit: '1',
487
+ }));
488
+ if (!flow) throw errUsage(`Flow ${identifier} (${sysID}) was not found`);
489
+ const name = getStringField(flow, 'name');
490
+ const active = getStringField(flow, 'active') === 'true';
491
+ const counted = async () => {
492
+ const counts = {};
493
+ for (const { table, query } of FLOW_OWNED_TABLES) {
494
+ counts[table] = await app.sdk.aggregateCount(table, query(sysID)).catch(() => null);
495
+ }
496
+ return counts;
497
+ };
498
+ const before = await counted();
499
+ const scopeName = getStringField(flow, 'sys_scope');
500
+ await confirmDesignerMutation(argv, app, `Delete flow "${name}" (${sysID})${active ? ', which is ACTIVE' : ''} and ${describeOwned(before)}? This cannot be undone.`);
501
+ try {
502
+ await app.sdk.delete('sys_hub_flow', sysID);
503
+ } catch (error) {
504
+ // The provider only deletes flows in the session's application scope.
505
+ if (/\b403\b|forbidden/i.test(error.message || '')) {
506
+ throw new AppError(CodeForbidden, `Flow "${name}" is in application scope ${scopeName ? `"${scopeName}"` : 'another scope'}; the provider refused the delete with HTTP 403. Switch to that scope first (jsn scopes), then retry. No delete was applied.`);
507
+ }
508
+ throw error;
509
+ }
510
+ const [stillThere] = await app.sdk.list('sys_hub_flow', new URLSearchParams({
511
+ sysparm_query: `sys_id=${sysID}`, sysparm_fields: 'sys_id', sysparm_limit: '1',
512
+ }));
513
+ if (stillThere) throw new AppError(CodeAPI, `Flow ${sysID} is still present after the delete request; nothing is reported as removed`);
514
+ const remaining = await counted();
515
+ const orphans = Object.entries(remaining).filter(([, count]) => count);
516
+ const removedText = describeOwned(before);
517
+ return app.ok({ flow: { id: sysID, name, active }, removed: before, remaining }, {
518
+ summary: `Deleted flow "${name}" (${sysID})${removedText === 'nothing' ? '' : ` with ${removedText}`}${orphans.length ? `; ${orphans.map(([table, count]) => `${count} ${table} row(s) remain`).join(', ')}` : ''}`,
519
+ breadcrumbs: [{ action: 'list', cmd: 'jsn flows list', description: 'List the remaining flows' }],
520
+ });
462
521
  }),
463
522
  })
464
523
  .epilogue('Run "jsn flows <command> --help" for details. In a terminal you can omit the target and pick\n'