@onlyflows/servicenow-mcp 2.3.0 → 2.4.0-rc.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/CHANGELOG.md +23 -0
- package/README.md +206 -22
- package/dist/atf-result-cache.d.ts +2 -2
- package/dist/call-metrics.d.ts +11 -1
- package/dist/call-metrics.d.ts.map +1 -1
- package/dist/call-metrics.js +2 -0
- package/dist/call-metrics.js.map +1 -1
- package/dist/client.d.ts +10 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +31 -3
- package/dist/client.js.map +1 -1
- package/dist/execution-context.d.ts +3 -3
- package/dist/execution-context.d.ts.map +1 -1
- package/dist/execution-context.js +7 -2
- package/dist/execution-context.js.map +1 -1
- package/dist/field-policy.d.ts +8 -0
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +239 -46
- package/dist/field-policy.js.map +1 -1
- package/dist/hidden-values.d.ts +368 -21
- package/dist/hidden-values.d.ts.map +1 -1
- package/dist/hidden-values.js +1050 -101
- package/dist/hidden-values.js.map +1 -1
- package/dist/http-observability.js +3 -1
- package/dist/http-observability.js.map +1 -1
- package/dist/inert-text.d.ts +27 -0
- package/dist/inert-text.d.ts.map +1 -0
- package/dist/inert-text.js +57 -0
- package/dist/inert-text.js.map +1 -0
- package/dist/policy-card.d.ts +1 -1
- package/dist/policy-card.d.ts.map +1 -1
- package/dist/policy-card.js +3 -1
- package/dist/policy-card.js.map +1 -1
- package/dist/setup-prompts.js +1 -0
- package/dist/setup-prompts.js.map +1 -1
- package/dist/setup.js +6 -0
- package/dist/setup.js.map +1 -1
- package/dist/tool-error.d.ts +39 -3
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +101 -2
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +5 -0
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/aggregate.d.ts +8 -0
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +70 -11
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/atf-contracts.d.ts +9 -9
- package/dist/tools/catalog.d.ts +2 -2
- package/dist/tools/catalog.d.ts.map +1 -1
- package/dist/tools/catalog.js +9 -0
- package/dist/tools/catalog.js.map +1 -1
- package/dist/tools/envelope-text-fit.d.ts +20 -0
- package/dist/tools/envelope-text-fit.d.ts.map +1 -0
- package/dist/tools/envelope-text-fit.js +33 -0
- package/dist/tools/envelope-text-fit.js.map +1 -0
- package/dist/tools/flow-view-mermaid.d.ts.map +1 -1
- package/dist/tools/flow-view-mermaid.js +6 -13
- package/dist/tools/flow-view-mermaid.js.map +1 -1
- package/dist/tools/flow-view-module.js +1 -1
- package/dist/tools/get-module.js +1 -1
- package/dist/tools/history-contracts.d.ts +198 -0
- package/dist/tools/history-contracts.d.ts.map +1 -0
- package/dist/tools/history-contracts.js +317 -0
- package/dist/tools/history-contracts.js.map +1 -0
- package/dist/tools/history-module.d.ts +158 -0
- package/dist/tools/history-module.d.ts.map +1 -0
- package/dist/tools/history-module.js +937 -0
- package/dist/tools/history-module.js.map +1 -0
- package/dist/tools/history-renderers.d.ts +32 -0
- package/dist/tools/history-renderers.d.ts.map +1 -0
- package/dist/tools/history-renderers.js +154 -0
- package/dist/tools/history-renderers.js.map +1 -0
- package/dist/tools/index.d.ts +3 -3
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +31 -14
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/platform-view-contracts.d.ts +4 -4
- package/dist/tools/platform-view-contracts.d.ts.map +1 -1
- package/dist/tools/platform-view-contracts.js +3 -0
- package/dist/tools/platform-view-contracts.js.map +1 -1
- package/dist/tools/platform-view-evaluate.d.ts +7 -2
- package/dist/tools/platform-view-evaluate.d.ts.map +1 -1
- package/dist/tools/platform-view-evaluate.js +44 -130
- package/dist/tools/platform-view-evaluate.js.map +1 -1
- package/dist/tools/platform-view-module.d.ts +12 -3
- package/dist/tools/platform-view-module.d.ts.map +1 -1
- package/dist/tools/platform-view-module.js +61 -16
- package/dist/tools/platform-view-module.js.map +1 -1
- package/dist/tools/platform-view-payload.d.ts +101 -0
- package/dist/tools/platform-view-payload.d.ts.map +1 -0
- package/dist/tools/platform-view-payload.js +302 -0
- package/dist/tools/platform-view-payload.js.map +1 -0
- package/dist/tools/platform-view-update-sets.d.ts +129 -0
- package/dist/tools/platform-view-update-sets.d.ts.map +1 -0
- package/dist/tools/platform-view-update-sets.js +1060 -0
- package/dist/tools/platform-view-update-sets.js.map +1 -0
- package/dist/tools/query-module.js +1 -1
- package/dist/tools/relationships-map.d.ts +59 -0
- package/dist/tools/relationships-map.d.ts.map +1 -0
- package/dist/tools/relationships-map.js +162 -0
- package/dist/tools/relationships-map.js.map +1 -0
- package/dist/tools/relationships-renderer.d.ts +22 -0
- package/dist/tools/relationships-renderer.d.ts.map +1 -0
- package/dist/tools/relationships-renderer.js +90 -0
- package/dist/tools/relationships-renderer.js.map +1 -0
- package/dist/tools/relationships.d.ts +37 -2
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/relationships.js +602 -183
- package/dist/tools/relationships.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +314 -15
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +120 -19
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/syslog.d.ts +1 -1
- package/dist/tools/table-hierarchy.d.ts +17 -0
- package/dist/tools/table-hierarchy.d.ts.map +1 -1
- package/dist/tools/table-hierarchy.js +41 -0
- package/dist/tools/table-hierarchy.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/docs/CLIENT-SETUP.md +2 -2
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +2 -2
- package/docs/PRODUCTION-SECURITY.md +145 -2
- package/docs/RELEASE-VALIDATION.md +2 -2
- package/docs/TESTING-OAUTH.md +1 -1
- package/package.json +2 -2
- package/server.json +2 -2
|
@@ -0,0 +1,1060 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `sn_platform_view`, the update set views: `update_sets`, `update_set`,
|
|
4
|
+
* `changes` (with the masked payload of one change), `batch` and
|
|
5
|
+
* `remote_update_sets`.
|
|
6
|
+
*
|
|
7
|
+
* Reads, each group in its own 1 MiB response scope, at most
|
|
8
|
+
* {@link UPDATE_SET_VIEW_LIMITS}.MAX_SCOPES (6) scopes per call, never cached:
|
|
9
|
+
* - `update_sets`: one page of `sys_update_set`, then one Aggregate API count
|
|
10
|
+
* of `sys_update_xml` grouped by `update_set` for that page (a set with no
|
|
11
|
+
* rows has no group: its count is 0). 2 requests.
|
|
12
|
+
* - `update_set`: the set (by sys_id or exact name), then together: its row
|
|
13
|
+
* counts grouped by `type` and `action`, its children (at most 50 shown),
|
|
14
|
+
* and the retrieved set it came from (`remote_sys_id`). At most 4 requests.
|
|
15
|
+
* - `changes`: the set (local, or retrieved with `remote_update_set`), its
|
|
16
|
+
* descendants one level per read when `include_children` (depth and count
|
|
17
|
+
* capped, cycle-safe), then one page of `sys_update_xml` metadata. With
|
|
18
|
+
* `change` and `payload: true`, the one change's payload, then the
|
|
19
|
+
* hierarchies and field types of the tables it writes (see
|
|
20
|
+
* `platform-view-payload.ts`).
|
|
21
|
+
* - `batch`: the member, its batch root (`base_update_set`, else the
|
|
22
|
+
* member's parents), the tree under the root one level per read (depth at
|
|
23
|
+
* most 10, at most 200 sets, cycle-safe), and the row counts per set (one
|
|
24
|
+
* Aggregate API read per 100 sets).
|
|
25
|
+
* - `remote_update_sets`: one page of `sys_remote_update_set`; with
|
|
26
|
+
* `remote_update_set` and `problems: true`, that set, one page of its
|
|
27
|
+
* preview problems and their counts by type and status.
|
|
28
|
+
*
|
|
29
|
+
* Every read names its fields. `sys_update_xml.payload` is asked for only by
|
|
30
|
+
* the payload read, and only when field policy lets the profile read it;
|
|
31
|
+
* `payload_diff` never. Every field the views filter or order on must be
|
|
32
|
+
* readable, or the call is `denied` before it reads. Every row passes the
|
|
33
|
+
* field access before it is projected.
|
|
34
|
+
*
|
|
35
|
+
* Queries are built from canonical selectors (escaped by the dispatcher) and
|
|
36
|
+
* validated sys_ids only; no instance text ever shapes a query. A 403 on a
|
|
37
|
+
* view's first table is `status: "denied"`, and a missing table
|
|
38
|
+
* `status: "unavailable"`, each with a fixed hint, never an empty success.
|
|
39
|
+
*
|
|
40
|
+
* Text: instance strings are bounded here and rendered by
|
|
41
|
+
* {@link renderUpdateSetViewText} inside an `untrustedBlock`.
|
|
42
|
+
*
|
|
43
|
+
* @module tools/platform-view-update-sets
|
|
44
|
+
*/
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.handleUpdateSetViews = exports.UPDATE_SET_VIEW_WARNINGS = exports.AMBIGUOUS_HINT = exports.REMOTE_NOT_FOUND_HINT = exports.NOT_FOUND_HINT = exports.REMOTE_SETS_UNAVAILABLE_HINT = exports.REMOTE_SETS_DENIED_HINT = exports.UPDATE_SETS_UNAVAILABLE_HINT = exports.UPDATE_SETS_DENIED_HINT = exports.UPDATE_SET_VIEW_DOT_WALKS = exports.UPDATE_SET_VIEWS_FIELD_PLAN = exports.PROBLEM_FIELDS = exports.REMOTE_SET_FIELDS = exports.CHANGE_FIELDS = exports.UPDATE_SET_FIELDS = exports.UPDATE_SET_VIEW_LIMITS = void 0;
|
|
47
|
+
exports.createUpdateSetViewsHandler = createUpdateSetViewsHandler;
|
|
48
|
+
exports.renderUpdateSetViewText = renderUpdateSetViewText;
|
|
49
|
+
const client_js_1 = require("../client.js");
|
|
50
|
+
const credential_tables_js_1 = require("../credential-tables.js");
|
|
51
|
+
const hidden_values_js_1 = require("../hidden-values.js");
|
|
52
|
+
const metadata_cache_js_1 = require("../metadata-cache.js");
|
|
53
|
+
const tool_error_js_1 = require("../tool-error.js");
|
|
54
|
+
const flow_view_module_js_1 = require("./flow-view-module.js");
|
|
55
|
+
const markdown_js_1 = require("./markdown.js");
|
|
56
|
+
const platform_view_payload_js_1 = require("./platform-view-payload.js");
|
|
57
|
+
const platform_view_upgrades_js_1 = require("./platform-view-upgrades.js");
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// Limits, fields and fixed text
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
exports.UPDATE_SET_VIEW_LIMITS = Object.freeze({
|
|
62
|
+
/** Response scopes per call, each under the client's 1 MiB cap. */
|
|
63
|
+
MAX_SCOPES: 6,
|
|
64
|
+
/** Levels of descendants read through `parent`. */
|
|
65
|
+
MAX_DEPTH: 10,
|
|
66
|
+
/** Sets in a batch tree, the root included. */
|
|
67
|
+
MAX_BATCH_NODES: 200,
|
|
68
|
+
/** Sets whose changes one `changes` call lists, the selected one included. */
|
|
69
|
+
MAX_SCOPE_SETS: 100,
|
|
70
|
+
/** Children listed by `update_set`. */
|
|
71
|
+
MAX_CHILDREN: 50,
|
|
72
|
+
/** Sets per Aggregate API read. */
|
|
73
|
+
STATS_CHUNK: 100,
|
|
74
|
+
/** `type`/`action` groups read for one set's counts. */
|
|
75
|
+
MAX_COUNT_GROUPS: 500,
|
|
76
|
+
DESCRIPTION_CHARS: 200,
|
|
77
|
+
PROBLEM_DESCRIPTION_CHARS: 300,
|
|
78
|
+
NAME_CHARS: 200,
|
|
79
|
+
});
|
|
80
|
+
const L = exports.UPDATE_SET_VIEW_LIMITS;
|
|
81
|
+
/** `sys_update_set`, as the list and the detail views read it. */
|
|
82
|
+
exports.UPDATE_SET_FIELDS = Object.freeze([
|
|
83
|
+
"sys_id", "name", "state", "application", "is_default", "parent", "base_update_set",
|
|
84
|
+
"sys_created_by", "sys_updated_on", "completed_on", "description",
|
|
85
|
+
]);
|
|
86
|
+
const UPDATE_SET_DETAIL_FIELDS = Object.freeze([
|
|
87
|
+
...exports.UPDATE_SET_FIELDS, "completed_by", "remote_sys_id", "sys_created_on",
|
|
88
|
+
]);
|
|
89
|
+
/** A set as a node of a tree or a child list. */
|
|
90
|
+
const UPDATE_SET_NODE_FIELDS = Object.freeze(["sys_id", "name", "state", "parent", "base_update_set"]);
|
|
91
|
+
const REMOTE_NODE_FIELDS = Object.freeze(["sys_id", "name", "state", "parent"]);
|
|
92
|
+
/** `sys_update_xml` metadata. Never `payload` or `payload_diff`: the payload read adds `payload` alone. */
|
|
93
|
+
exports.CHANGE_FIELDS = Object.freeze([
|
|
94
|
+
"sys_id", "type", "name", "target_name", "table", "action", "application",
|
|
95
|
+
"sys_updated_by", "sys_updated_on", "replace_on_upgrade", "update_set", "remote_update_set",
|
|
96
|
+
]);
|
|
97
|
+
/** `sys_remote_update_set`. */
|
|
98
|
+
exports.REMOTE_SET_FIELDS = Object.freeze([
|
|
99
|
+
"sys_id", "name", "state", "application_scope", "collisions", "inserted", "updated", "deleted",
|
|
100
|
+
"parent", "remote_base_update_set", "update_source", "commit_date", "sys_created_on",
|
|
101
|
+
]);
|
|
102
|
+
/** `sys_update_preview_problem`. */
|
|
103
|
+
exports.PROBLEM_FIELDS = Object.freeze([
|
|
104
|
+
"sys_id", "type", "status", "missing_item", "missing_item_table", "remote_update", "description",
|
|
105
|
+
"remote_update_set",
|
|
106
|
+
]);
|
|
107
|
+
/**
|
|
108
|
+
* Every field these views may request or filter on, by table: what the
|
|
109
|
+
* module prepares (intersected with what the profile may read). The payload
|
|
110
|
+
* read adds `sys_update_xml.payload`; the payload's type lookup reads the two
|
|
111
|
+
* metadata tables through `loadFieldTypesInPlan`.
|
|
112
|
+
*/
|
|
113
|
+
exports.UPDATE_SET_VIEWS_FIELD_PLAN = Object.freeze({
|
|
114
|
+
sys_update_set: [...UPDATE_SET_DETAIL_FIELDS],
|
|
115
|
+
sys_update_xml: [...exports.CHANGE_FIELDS, "payload"],
|
|
116
|
+
sys_remote_update_set: [...exports.REMOTE_SET_FIELDS],
|
|
117
|
+
sys_update_preview_problem: [...exports.PROBLEM_FIELDS],
|
|
118
|
+
...hidden_values_js_1.PLAN_FIELD_TYPE_FIELDS,
|
|
119
|
+
});
|
|
120
|
+
/** The dot-walks these views make: only the payload's type lookup, one hop into its own table. */
|
|
121
|
+
exports.UPDATE_SET_VIEW_DOT_WALKS = hidden_values_js_1.PLAN_FIELD_TYPE_DOT_WALKS;
|
|
122
|
+
exports.UPDATE_SETS_DENIED_HINT = "Reading update sets was denied for this account.";
|
|
123
|
+
exports.UPDATE_SETS_UNAVAILABLE_HINT = "The update set tables are not available on this instance.";
|
|
124
|
+
exports.REMOTE_SETS_DENIED_HINT = "Reading retrieved update sets was denied for this account.";
|
|
125
|
+
exports.REMOTE_SETS_UNAVAILABLE_HINT = "The retrieved update set tables are not available on this instance.";
|
|
126
|
+
exports.NOT_FOUND_HINT = "No update set matches. Pass its sys_id, or check the name.";
|
|
127
|
+
exports.REMOTE_NOT_FOUND_HINT = "No retrieved update set has this sys_id.";
|
|
128
|
+
exports.AMBIGUOUS_HINT = "More than one update set has this name. Pass update_set as a sys_id.";
|
|
129
|
+
/** Fixed warnings; nothing in them comes from the instance. */
|
|
130
|
+
exports.UPDATE_SET_VIEW_WARNINGS = Object.freeze({
|
|
131
|
+
counts_unavailable: "Row counts were not readable; update_count is null.",
|
|
132
|
+
counts_truncated: `More than ${L.MAX_COUNT_GROUPS} type and action groups; the counts cover the first ones.`,
|
|
133
|
+
children_unreadable: "Field policy hides the parent field, so child sets were not read.",
|
|
134
|
+
children_unavailable: "Child sets could not be read.",
|
|
135
|
+
remote_unavailable: "The retrieved set this one came from could not be read.",
|
|
136
|
+
sets_capped: `The scope has more than ${L.MAX_SCOPE_SETS} sets or is deeper than ${L.MAX_DEPTH} levels; changes of the first sets only.`,
|
|
137
|
+
depth_capped: `The batch is deeper than ${L.MAX_DEPTH} levels; deeper sets were not read.`,
|
|
138
|
+
nodes_capped: `The batch has more than ${L.MAX_BATCH_NODES} sets; the rest were not read.`,
|
|
139
|
+
cycles: "Some sets point back to a set already in the tree; each is shown once.",
|
|
140
|
+
root_unresolved: `The batch root was not found within ${L.MAX_DEPTH} parents; the tree starts at the highest set found.`,
|
|
141
|
+
problems_denied: "Reading preview problems was denied for this account.",
|
|
142
|
+
problem_counts_unavailable: "Preview problem counts were not readable.",
|
|
143
|
+
});
|
|
144
|
+
const UPDATE_SET_VIEWS = Object.freeze(["update_sets", "update_set", "changes", "batch", "remote_update_sets"]);
|
|
145
|
+
/** Raised inside a view for a fixed `denied` or `unavailable` result. */
|
|
146
|
+
class ViewStop {
|
|
147
|
+
status;
|
|
148
|
+
hint;
|
|
149
|
+
constructor(status, hint) {
|
|
150
|
+
this.status = status;
|
|
151
|
+
this.hint = hint;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function isCancellation(ctx, error) {
|
|
155
|
+
return ctx.signal?.aborted === true || (0, client_js_1.isServiceNowRequestCancellation)(error);
|
|
156
|
+
}
|
|
157
|
+
/** Run `operation` in a new response scope. The views use at most MAX_SCOPES by construction. */
|
|
158
|
+
async function scoped(ctx, operation) {
|
|
159
|
+
if (!ctx.scopes.take())
|
|
160
|
+
throw (0, tool_error_js_1.createToolError)("internal", "do_not_retry");
|
|
161
|
+
return (0, platform_view_upgrades_js_1.inOwnScope)(ctx.signal, operation);
|
|
162
|
+
}
|
|
163
|
+
/** A first read of a view's own table: a 403 is `denied`, a missing table `unavailable`. */
|
|
164
|
+
async function firstRead(ctx, remote, operation) {
|
|
165
|
+
try {
|
|
166
|
+
return await scoped(ctx, operation);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
if ((0, platform_view_upgrades_js_1.errorCategory)(error) === "authorization") {
|
|
170
|
+
throw new ViewStop("denied", remote ? exports.REMOTE_SETS_DENIED_HINT : exports.UPDATE_SETS_DENIED_HINT);
|
|
171
|
+
}
|
|
172
|
+
if ((0, platform_view_upgrades_js_1.isMissingTable)(error)) {
|
|
173
|
+
throw new ViewStop("unavailable", remote ? exports.REMOTE_SETS_UNAVAILABLE_HINT : exports.UPDATE_SETS_UNAVAILABLE_HINT);
|
|
174
|
+
}
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function requireReadable(ctx, table, fields) {
|
|
179
|
+
if ((0, platform_view_upgrades_js_1.firstUnreadable)(ctx.access, table, fields) !== undefined)
|
|
180
|
+
throw new ViewStop("denied", platform_view_upgrades_js_1.FIELD_POLICY_HINT);
|
|
181
|
+
}
|
|
182
|
+
async function readRows(ctx, table, fields, query, limit, offset = 0) {
|
|
183
|
+
const asked = (0, platform_view_upgrades_js_1.readableOf)(ctx.access, table, fields);
|
|
184
|
+
return (0, platform_view_upgrades_js_1.rowsOf)(await ctx.ops.get(`${platform_view_upgrades_js_1.TABLE_PATH}${table}`, (0, platform_view_upgrades_js_1.listParams)({ fields: asked, query, limit, offset }))).map((row) => (0, platform_view_upgrades_js_1.readableRow)(ctx.access, table, row, asked));
|
|
185
|
+
}
|
|
186
|
+
const lowerId = (value) => {
|
|
187
|
+
const id = (0, platform_view_upgrades_js_1.scalar)(value)?.trim().toLowerCase();
|
|
188
|
+
return id !== undefined && platform_view_upgrades_js_1.SYS_ID.test(id) ? id : undefined;
|
|
189
|
+
};
|
|
190
|
+
function optionalText(key, value, max) {
|
|
191
|
+
const clean = (0, platform_view_upgrades_js_1.cleanText)(value, max);
|
|
192
|
+
return clean === undefined ? {} : { [key]: clean };
|
|
193
|
+
}
|
|
194
|
+
function optionalId(key, value) {
|
|
195
|
+
if (value === undefined)
|
|
196
|
+
return {};
|
|
197
|
+
return { [key]: lowerId(value) ?? null };
|
|
198
|
+
}
|
|
199
|
+
function optionalCount(key, value) {
|
|
200
|
+
const count = (0, platform_view_upgrades_js_1.wholeNumber)(value);
|
|
201
|
+
return count === undefined ? {} : { [key]: count };
|
|
202
|
+
}
|
|
203
|
+
function optionalFlag(key, value) {
|
|
204
|
+
const bool = (0, platform_view_upgrades_js_1.flag)(value);
|
|
205
|
+
return bool === undefined ? {} : { [key]: bool };
|
|
206
|
+
}
|
|
207
|
+
/** Aggregate API groups: each group's fields by name and its count. */
|
|
208
|
+
function statGroups(response) {
|
|
209
|
+
const result = response?.result;
|
|
210
|
+
const groups = Array.isArray(result) ? result : typeof result === "object" && result !== null ? [result] : [];
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const entry of groups) {
|
|
213
|
+
const group = (0, platform_view_upgrades_js_1.asRecord)(entry);
|
|
214
|
+
const fields = new Map();
|
|
215
|
+
for (const field of Array.isArray(group.groupby_fields) ? group.groupby_fields : []) {
|
|
216
|
+
const record = (0, platform_view_upgrades_js_1.asRecord)(field);
|
|
217
|
+
const name = (0, platform_view_upgrades_js_1.scalar)(record.field);
|
|
218
|
+
if (name !== undefined)
|
|
219
|
+
fields.set(name, (0, platform_view_upgrades_js_1.scalar)(record.value) ?? "");
|
|
220
|
+
}
|
|
221
|
+
const count = (0, platform_view_upgrades_js_1.wholeNumber)((0, platform_view_upgrades_js_1.asRecord)(group.stats).count);
|
|
222
|
+
if (count !== undefined && count >= 0)
|
|
223
|
+
out.push({ fields, count });
|
|
224
|
+
}
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Rows per set, from Aggregate API reads grouped by `field`, one read per
|
|
229
|
+
* {@link UPDATE_SET_VIEW_LIMITS}.STATS_CHUNK sets. A set with no rows has no
|
|
230
|
+
* group: its count is 0. Undefined when a read failed or the field is hidden.
|
|
231
|
+
*/
|
|
232
|
+
async function countRows(ctx, field, ids) {
|
|
233
|
+
if (ids.length === 0)
|
|
234
|
+
return new Map();
|
|
235
|
+
if (!ctx.access.canRead("sys_update_xml", field))
|
|
236
|
+
return undefined;
|
|
237
|
+
const chunks = [];
|
|
238
|
+
for (let start = 0; start < ids.length; start += L.STATS_CHUNK)
|
|
239
|
+
chunks.push(ids.slice(start, start + L.STATS_CHUNK));
|
|
240
|
+
try {
|
|
241
|
+
const responses = await scoped(ctx, () => Promise.all(chunks.map((chunk) => ctx.ops.get(`${platform_view_upgrades_js_1.STATS_PATH}sys_update_xml`, {
|
|
242
|
+
sysparm_query: `${field}IN${chunk.join(",")}`,
|
|
243
|
+
sysparm_group_by: field,
|
|
244
|
+
sysparm_count: "true",
|
|
245
|
+
}))));
|
|
246
|
+
const counts = new Map(ids.map((id) => [id, 0]));
|
|
247
|
+
for (const response of responses) {
|
|
248
|
+
for (const group of statGroups(response)) {
|
|
249
|
+
const id = lowerId(group.fields.get(field));
|
|
250
|
+
if (id !== undefined && counts.has(id))
|
|
251
|
+
counts.set(id, group.count);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return counts;
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
if (isCancellation(ctx, error))
|
|
258
|
+
throw error;
|
|
259
|
+
return undefined;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
// Projections
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
function projectSet(row) {
|
|
266
|
+
const id = lowerId(row.sys_id);
|
|
267
|
+
if (id === undefined)
|
|
268
|
+
return undefined;
|
|
269
|
+
return {
|
|
270
|
+
sys_id: id,
|
|
271
|
+
...optionalText("name", row.name, L.NAME_CHARS),
|
|
272
|
+
...optionalText("state", row.state, 40),
|
|
273
|
+
...("application" in row ? { application: (0, platform_view_upgrades_js_1.cleanText)(row.application, 40) ?? null } : {}),
|
|
274
|
+
...optionalFlag("is_default", row.is_default),
|
|
275
|
+
...optionalId("parent", row.parent),
|
|
276
|
+
...optionalId("base_update_set", row.base_update_set),
|
|
277
|
+
...optionalText("created_by", row.sys_created_by, 100),
|
|
278
|
+
...optionalText("created_on", row.sys_created_on, 40),
|
|
279
|
+
...optionalText("updated_on", row.sys_updated_on, 40),
|
|
280
|
+
...optionalText("completed_on", row.completed_on, 40),
|
|
281
|
+
...optionalText("completed_by", row.completed_by, 100),
|
|
282
|
+
...optionalId("remote_sys_id", row.remote_sys_id),
|
|
283
|
+
...optionalText("description", row.description, L.DESCRIPTION_CHARS),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function projectNode(row) {
|
|
287
|
+
const id = lowerId(row.sys_id);
|
|
288
|
+
if (id === undefined)
|
|
289
|
+
return undefined;
|
|
290
|
+
return { sys_id: id, ...optionalText("name", row.name, L.NAME_CHARS), ...optionalText("state", row.state, 40) };
|
|
291
|
+
}
|
|
292
|
+
function projectChange(row) {
|
|
293
|
+
const id = lowerId(row.sys_id);
|
|
294
|
+
if (id === undefined)
|
|
295
|
+
return undefined;
|
|
296
|
+
return {
|
|
297
|
+
sys_id: id,
|
|
298
|
+
...optionalText("type", row.type, 80),
|
|
299
|
+
...optionalText("name", row.name, L.NAME_CHARS),
|
|
300
|
+
...optionalText("target_name", row.target_name, L.NAME_CHARS),
|
|
301
|
+
...optionalText("table", row.table, 80),
|
|
302
|
+
...optionalText("action", row.action, 40),
|
|
303
|
+
...("application" in row ? { application: (0, platform_view_upgrades_js_1.cleanText)(row.application, 40) ?? null } : {}),
|
|
304
|
+
...optionalText("updated_by", row.sys_updated_by, 100),
|
|
305
|
+
...optionalText("updated_on", row.sys_updated_on, 40),
|
|
306
|
+
...optionalFlag("replace_on_upgrade", row.replace_on_upgrade),
|
|
307
|
+
...optionalId("update_set", row.update_set),
|
|
308
|
+
...optionalId("remote_update_set", row.remote_update_set),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function projectRemoteSet(row) {
|
|
312
|
+
const id = lowerId(row.sys_id);
|
|
313
|
+
if (id === undefined)
|
|
314
|
+
return undefined;
|
|
315
|
+
return {
|
|
316
|
+
sys_id: id,
|
|
317
|
+
...optionalText("name", row.name, L.NAME_CHARS),
|
|
318
|
+
...optionalText("state", row.state, 40),
|
|
319
|
+
...optionalText("application_scope", row.application_scope, 100),
|
|
320
|
+
...optionalCount("collisions", row.collisions),
|
|
321
|
+
...optionalCount("inserted", row.inserted),
|
|
322
|
+
...optionalCount("updated", row.updated),
|
|
323
|
+
...optionalCount("deleted", row.deleted),
|
|
324
|
+
...optionalId("parent", row.parent),
|
|
325
|
+
...optionalId("remote_base_update_set", row.remote_base_update_set),
|
|
326
|
+
...optionalId("update_source", row.update_source),
|
|
327
|
+
...optionalText("commit_date", row.commit_date, 40),
|
|
328
|
+
...optionalText("created_on", row.sys_created_on, 40),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function projectProblem(row) {
|
|
332
|
+
const id = lowerId(row.sys_id);
|
|
333
|
+
if (id === undefined)
|
|
334
|
+
return undefined;
|
|
335
|
+
return {
|
|
336
|
+
sys_id: id,
|
|
337
|
+
...optionalText("type", row.type, 40),
|
|
338
|
+
...optionalText("status", row.status, 40),
|
|
339
|
+
...optionalText("missing_item", row.missing_item, L.NAME_CHARS),
|
|
340
|
+
...optionalText("missing_item_table", row.missing_item_table, 80),
|
|
341
|
+
...optionalId("remote_update", row.remote_update),
|
|
342
|
+
...optionalText("description", row.description, L.PROBLEM_DESCRIPTION_CHARS),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/** A local set by sys_id or exact name, in its own scope (the view's first read). */
|
|
346
|
+
async function findLocalSet(ctx, selector, fields) {
|
|
347
|
+
const byId = platform_view_upgrades_js_1.SYS_ID.test(selector);
|
|
348
|
+
requireReadable(ctx, "sys_update_set", byId ? ["sys_id"] : ["sys_id", "name"]);
|
|
349
|
+
const rows = await firstRead(ctx, false, () => readRows(ctx, "sys_update_set", fields, byId ? `sys_id=${selector}` : `name=${selector}^ORDERBYsys_id`, byId ? 1 : 2));
|
|
350
|
+
return found(rows);
|
|
351
|
+
}
|
|
352
|
+
/** A retrieved set by sys_id, in its own scope (the view's first read). */
|
|
353
|
+
async function findRemoteSet(ctx, id, fields) {
|
|
354
|
+
requireReadable(ctx, "sys_remote_update_set", ["sys_id"]);
|
|
355
|
+
return found(await firstRead(ctx, true, () => readRows(ctx, "sys_remote_update_set", fields, `sys_id=${id}`, 1)));
|
|
356
|
+
}
|
|
357
|
+
function found(rows) {
|
|
358
|
+
const valid = rows.filter((row) => lowerId(row.sys_id) !== undefined);
|
|
359
|
+
if (valid.length === 0)
|
|
360
|
+
return { kind: "not_found" };
|
|
361
|
+
if (valid.length > 1) {
|
|
362
|
+
return {
|
|
363
|
+
kind: "ambiguous",
|
|
364
|
+
candidates: valid.flatMap((row) => {
|
|
365
|
+
const node = projectNode(row);
|
|
366
|
+
return node === undefined ? [] : [node];
|
|
367
|
+
}),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
return { kind: "found", row: valid[0], id: lowerId(valid[0].sys_id) };
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* The children of `parents`, in sys_id order: one read per
|
|
374
|
+
* {@link UPDATE_SET_VIEW_LIMITS}.STATS_CHUNK parents (so a level's query stays
|
|
375
|
+
* well under URL limits), read together and merged as one read would return
|
|
376
|
+
* them. Each read asks for at most `limit` rows.
|
|
377
|
+
*/
|
|
378
|
+
async function childrenOf(ctx, table, parents, fields, limit) {
|
|
379
|
+
const chunks = [];
|
|
380
|
+
for (let start = 0; start < parents.length; start += L.STATS_CHUNK)
|
|
381
|
+
chunks.push(parents.slice(start, start + L.STATS_CHUNK));
|
|
382
|
+
const reads = await Promise.all(chunks.map((chunk) => readRows(ctx, table, fields, `parentIN${chunk.join(",")}^ORDERBYsys_id`, limit)));
|
|
383
|
+
if (reads.length === 1)
|
|
384
|
+
return reads[0];
|
|
385
|
+
const key = (row) => lowerId(row.sys_id) ?? "";
|
|
386
|
+
return reads.flat().sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0));
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The descendants of `root` through `parent`, one read per level (per 100
|
|
390
|
+
* sets of a level), in the caller's scope. Stops at `maxDepth` levels (reading one row past the last
|
|
391
|
+
* level to know whether it was the last) and at `maxNodes` sets; a set seen
|
|
392
|
+
* twice is a cycle, counted once and not followed.
|
|
393
|
+
*/
|
|
394
|
+
async function descend(ctx, table, root, fields, maxNodes) {
|
|
395
|
+
const nodes = new Map([[root.id, { row: root.row, depth: 0 }]]);
|
|
396
|
+
let frontier = [root.id];
|
|
397
|
+
let depthCapped = false;
|
|
398
|
+
let nodesCapped = false;
|
|
399
|
+
let cycles = 0;
|
|
400
|
+
for (let depth = 1; frontier.length > 0 && !nodesCapped; depth += 1) {
|
|
401
|
+
if (depth > L.MAX_DEPTH) {
|
|
402
|
+
// One row is enough to know whether the tree goes deeper.
|
|
403
|
+
const deeper = await childrenOf(ctx, table, frontier, ["sys_id"], 1);
|
|
404
|
+
depthCapped = deeper.length > 0;
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
const level = await childrenOf(ctx, table, frontier, fields, maxNodes + 1);
|
|
408
|
+
const next = [];
|
|
409
|
+
for (const row of level) {
|
|
410
|
+
const id = lowerId(row.sys_id);
|
|
411
|
+
if (id === undefined)
|
|
412
|
+
continue;
|
|
413
|
+
if (nodes.has(id)) {
|
|
414
|
+
cycles += 1;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (nodes.size >= maxNodes) {
|
|
418
|
+
nodesCapped = true;
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
nodes.set(id, { row, depth });
|
|
422
|
+
next.push(id);
|
|
423
|
+
}
|
|
424
|
+
frontier = next;
|
|
425
|
+
}
|
|
426
|
+
return { nodes, depthCapped, nodesCapped, cycles };
|
|
427
|
+
}
|
|
428
|
+
// ---------------------------------------------------------------------------
|
|
429
|
+
// update_sets
|
|
430
|
+
// ---------------------------------------------------------------------------
|
|
431
|
+
async function viewUpdateSets(ctx) {
|
|
432
|
+
const { args, access } = ctx;
|
|
433
|
+
if (args.application !== undefined && args.application !== "global" && !platform_view_upgrades_js_1.SYS_ID.test(args.application.toLowerCase())) {
|
|
434
|
+
// A scope is matched by its sys_id (or "global"); its name would need a read outside this view's plan.
|
|
435
|
+
throw (0, tool_error_js_1.createToolError)("conflict", "retry_after_correction");
|
|
436
|
+
}
|
|
437
|
+
const clauses = [];
|
|
438
|
+
const filterFields = ["sys_id"];
|
|
439
|
+
if (args.state !== undefined) {
|
|
440
|
+
clauses.push(`state=${args.state}`);
|
|
441
|
+
filterFields.push("state");
|
|
442
|
+
}
|
|
443
|
+
if (args.name_contains !== undefined) {
|
|
444
|
+
clauses.push(`nameLIKE${args.name_contains}`);
|
|
445
|
+
filterFields.push("name");
|
|
446
|
+
}
|
|
447
|
+
if (args.application !== undefined) {
|
|
448
|
+
clauses.push(`application=${args.application.toLowerCase()}`);
|
|
449
|
+
filterFields.push("application");
|
|
450
|
+
}
|
|
451
|
+
if (args.updated_since !== undefined) {
|
|
452
|
+
clauses.push(`sys_updated_on>=${args.updated_since}`);
|
|
453
|
+
filterFields.push("sys_updated_on");
|
|
454
|
+
}
|
|
455
|
+
if (args.batch_root !== undefined) {
|
|
456
|
+
// The root and every set whose batch root it is.
|
|
457
|
+
clauses.push(`base_update_set=${args.batch_root}^ORsys_id=${args.batch_root}`);
|
|
458
|
+
filterFields.push("base_update_set");
|
|
459
|
+
}
|
|
460
|
+
requireReadable(ctx, "sys_update_set", filterFields);
|
|
461
|
+
const order = access.canRead("sys_update_set", "sys_updated_on") ? "ORDERBYDESCsys_updated_on^ORDERBYsys_id" : "ORDERBYsys_id";
|
|
462
|
+
const query = [...clauses, order].join("^");
|
|
463
|
+
const rows = await firstRead(ctx, false, () => readRows(ctx, "sys_update_set", exports.UPDATE_SET_FIELDS, query, args.limit + 1, args.offset));
|
|
464
|
+
const { page, hasMore } = (0, platform_view_upgrades_js_1.pageOf)(rows, args.limit);
|
|
465
|
+
const sets = page.flatMap((row) => {
|
|
466
|
+
const set = projectSet(row);
|
|
467
|
+
return set === undefined ? [] : [set];
|
|
468
|
+
});
|
|
469
|
+
const counts = await countRows(ctx, "update_set", sets.map((set) => String(set.sys_id)));
|
|
470
|
+
if (counts === undefined && sets.length > 0)
|
|
471
|
+
ctx.warnings.add("counts_unavailable");
|
|
472
|
+
const results = sets.map((set) => ({ ...set, update_count: counts?.get(String(set.sys_id)) ?? null }));
|
|
473
|
+
return result(ctx, "update_sets", {
|
|
474
|
+
summary: {
|
|
475
|
+
filters: {
|
|
476
|
+
...(args.state === undefined ? {} : { state: args.state }),
|
|
477
|
+
...(args.name_contains === undefined ? {} : { name_contains: args.name_contains }),
|
|
478
|
+
...(args.application === undefined ? {} : { application: args.application.toLowerCase() }),
|
|
479
|
+
...(args.updated_since === undefined ? {} : { updated_since: args.updated_since }),
|
|
480
|
+
...(args.batch_root === undefined ? {} : { batch_root: args.batch_root }),
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
results,
|
|
484
|
+
record_count: results.length,
|
|
485
|
+
has_more: hasMore,
|
|
486
|
+
...(hasMore ? { next_offset: args.offset + page.length } : {}),
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
// ---------------------------------------------------------------------------
|
|
490
|
+
// update_set
|
|
491
|
+
// ---------------------------------------------------------------------------
|
|
492
|
+
async function viewUpdateSet(ctx) {
|
|
493
|
+
const selector = requiredSelector(ctx.args.update_set);
|
|
494
|
+
const set = await findLocalSet(ctx, selector, UPDATE_SET_DETAIL_FIELDS);
|
|
495
|
+
if (set.kind !== "found")
|
|
496
|
+
return notFound(ctx, "update_set", set);
|
|
497
|
+
const detail = projectSet(set.row);
|
|
498
|
+
// Counts, children and the origin, together: none depends on another.
|
|
499
|
+
const countsRead = async () => {
|
|
500
|
+
if ((0, platform_view_upgrades_js_1.firstUnreadable)(ctx.access, "sys_update_xml", ["update_set"]) !== undefined)
|
|
501
|
+
return undefined;
|
|
502
|
+
const fields = ["type", "action"].filter((field) => ctx.access.canRead("sys_update_xml", field));
|
|
503
|
+
try {
|
|
504
|
+
const response = await scoped(ctx, () => ctx.ops.get(`${platform_view_upgrades_js_1.STATS_PATH}sys_update_xml`, {
|
|
505
|
+
sysparm_query: `update_set=${set.id}`,
|
|
506
|
+
sysparm_count: "true",
|
|
507
|
+
...(fields.length > 0 ? { sysparm_group_by: fields.join(",") } : {}),
|
|
508
|
+
sysparm_limit: String(L.MAX_COUNT_GROUPS + 1),
|
|
509
|
+
}));
|
|
510
|
+
return { groups: statGroups(response), fields };
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
if (isCancellation(ctx, error))
|
|
514
|
+
throw error;
|
|
515
|
+
return undefined;
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
const childrenRead = async () => {
|
|
519
|
+
if (!ctx.access.canRead("sys_update_set", "parent")) {
|
|
520
|
+
ctx.warnings.add("children_unreadable");
|
|
521
|
+
return undefined;
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
return await scoped(ctx, () => readRows(ctx, "sys_update_set", UPDATE_SET_NODE_FIELDS, `parent=${set.id}^ORDERBYsys_id`, L.MAX_CHILDREN + 1));
|
|
525
|
+
}
|
|
526
|
+
catch (error) {
|
|
527
|
+
if (isCancellation(ctx, error))
|
|
528
|
+
throw error;
|
|
529
|
+
ctx.warnings.add("children_unavailable");
|
|
530
|
+
return undefined;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const remoteId = lowerId(set.row.remote_sys_id);
|
|
534
|
+
const originRead = async () => {
|
|
535
|
+
if (remoteId === undefined || !ctx.access.canRead("sys_remote_update_set", "sys_id"))
|
|
536
|
+
return undefined;
|
|
537
|
+
try {
|
|
538
|
+
const rows = await scoped(ctx, () => readRows(ctx, "sys_remote_update_set", exports.REMOTE_SET_FIELDS, `sys_id=${remoteId}`, 1));
|
|
539
|
+
return rows[0] === undefined ? null : projectRemoteSet(rows[0]) ?? null;
|
|
540
|
+
}
|
|
541
|
+
catch (error) {
|
|
542
|
+
if (isCancellation(ctx, error))
|
|
543
|
+
throw error;
|
|
544
|
+
ctx.warnings.add("remote_unavailable");
|
|
545
|
+
return undefined;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const [counts, children, origin] = await Promise.all([countsRead(), childrenRead(), originRead()]);
|
|
549
|
+
let countSummary = null;
|
|
550
|
+
if (counts === undefined)
|
|
551
|
+
ctx.warnings.add("counts_unavailable");
|
|
552
|
+
else {
|
|
553
|
+
const byType = new Map();
|
|
554
|
+
const byAction = new Map();
|
|
555
|
+
let total = 0;
|
|
556
|
+
for (const group of counts.groups.slice(0, L.MAX_COUNT_GROUPS)) {
|
|
557
|
+
total += group.count;
|
|
558
|
+
const type = (0, platform_view_upgrades_js_1.cleanText)(group.fields.get("type"), 80) ?? "";
|
|
559
|
+
const action = (0, platform_view_upgrades_js_1.cleanText)(group.fields.get("action"), 40) ?? "";
|
|
560
|
+
if (counts.fields.includes("type"))
|
|
561
|
+
byType.set(type, (byType.get(type) ?? 0) + group.count);
|
|
562
|
+
if (counts.fields.includes("action"))
|
|
563
|
+
byAction.set(action, (byAction.get(action) ?? 0) + group.count);
|
|
564
|
+
}
|
|
565
|
+
if (counts.groups.length > L.MAX_COUNT_GROUPS)
|
|
566
|
+
ctx.warnings.add("counts_truncated");
|
|
567
|
+
const listed = (map, key) => [...map].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1)).map(([value, count]) => ({ [key]: value, count }));
|
|
568
|
+
countSummary = {
|
|
569
|
+
// A set with no rows has no group: 0.
|
|
570
|
+
total,
|
|
571
|
+
...(counts.fields.includes("type") ? { by_type: listed(byType, "type") } : {}),
|
|
572
|
+
...(counts.fields.includes("action") ? { by_action: listed(byAction, "action") } : {}),
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
const childNodes = (children ?? []).slice(0, L.MAX_CHILDREN).flatMap((row) => {
|
|
576
|
+
const node = projectNode(row);
|
|
577
|
+
return node === undefined ? [] : [node];
|
|
578
|
+
});
|
|
579
|
+
const base = lowerId(set.row.base_update_set);
|
|
580
|
+
return result(ctx, "update_set", {
|
|
581
|
+
summary: {
|
|
582
|
+
set: detail,
|
|
583
|
+
counts: countSummary,
|
|
584
|
+
batch: {
|
|
585
|
+
root: base ?? (childNodes.length > 0 ? set.id : null),
|
|
586
|
+
parent: lowerId(set.row.parent) ?? null,
|
|
587
|
+
...(children === undefined ? { children: null } : { children: childNodes }),
|
|
588
|
+
...(children !== undefined && children.length > L.MAX_CHILDREN ? { children_more: true } : {}),
|
|
589
|
+
},
|
|
590
|
+
remote_origin: origin ?? null,
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
// ---------------------------------------------------------------------------
|
|
595
|
+
// changes
|
|
596
|
+
// ---------------------------------------------------------------------------
|
|
597
|
+
async function viewChanges(ctx) {
|
|
598
|
+
const { args, access } = ctx;
|
|
599
|
+
const given = [args.update_set, args.remote_update_set].filter((value) => value !== undefined);
|
|
600
|
+
if (given.length !== 1)
|
|
601
|
+
throw (0, tool_error_js_1.createToolError)("conflict", "retry_after_correction");
|
|
602
|
+
// A payload is one named change's.
|
|
603
|
+
if (args.payload === true && args.change === undefined)
|
|
604
|
+
throw (0, tool_error_js_1.createToolError)("conflict", "retry_after_correction");
|
|
605
|
+
const remote = args.remote_update_set !== undefined;
|
|
606
|
+
const setTable = remote ? "sys_remote_update_set" : "sys_update_set";
|
|
607
|
+
const setField = remote ? "remote_update_set" : "update_set";
|
|
608
|
+
// Every field the row read filters or orders on.
|
|
609
|
+
const filterFields = ["sys_id", setField];
|
|
610
|
+
if (args.type !== undefined)
|
|
611
|
+
filterFields.push("type");
|
|
612
|
+
if (args.action !== undefined)
|
|
613
|
+
filterFields.push("action");
|
|
614
|
+
requireReadable(ctx, "sys_update_xml", filterFields);
|
|
615
|
+
// 1. The set and, when asked, its descendants: one scope.
|
|
616
|
+
const children = args.include_children === true;
|
|
617
|
+
const selector = remote ? args.remote_update_set : args.update_set;
|
|
618
|
+
const byId = platform_view_upgrades_js_1.SYS_ID.test(selector);
|
|
619
|
+
requireReadable(ctx, setTable, [...(byId ? ["sys_id"] : ["sys_id", "name"]), ...(children ? ["parent"] : [])]);
|
|
620
|
+
const fields = remote ? REMOTE_NODE_FIELDS : UPDATE_SET_NODE_FIELDS;
|
|
621
|
+
const resolved = await firstRead(ctx, remote, async () => {
|
|
622
|
+
const set = found(await readRows(ctx, setTable, fields, byId ? `sys_id=${selector}` : `name=${selector}^ORDERBYsys_id`, byId ? 1 : 2));
|
|
623
|
+
if (set.kind !== "found")
|
|
624
|
+
return set;
|
|
625
|
+
const tree = children
|
|
626
|
+
? await descend(ctx, setTable, set, fields, L.MAX_SCOPE_SETS)
|
|
627
|
+
: { nodes: new Map([[set.id, { row: set.row, depth: 0 }]]), depthCapped: false, nodesCapped: false, cycles: 0 };
|
|
628
|
+
return { root: set.id, tree };
|
|
629
|
+
});
|
|
630
|
+
if ("kind" in resolved)
|
|
631
|
+
return notFound(ctx, "changes", resolved, remote);
|
|
632
|
+
const { root: rootId, tree: scopeTree } = resolved;
|
|
633
|
+
if (scopeTree.depthCapped || scopeTree.nodesCapped)
|
|
634
|
+
ctx.warnings.add("sets_capped");
|
|
635
|
+
const ids = [...scopeTree.nodes.keys()];
|
|
636
|
+
// 2. One page of change metadata.
|
|
637
|
+
const clauses = [`${setField}IN${ids.join(",")}`];
|
|
638
|
+
if (args.change !== undefined)
|
|
639
|
+
clauses.push(`sys_id=${args.change}`);
|
|
640
|
+
if (args.type !== undefined)
|
|
641
|
+
clauses.push(`type=${args.type}`);
|
|
642
|
+
if (args.action !== undefined)
|
|
643
|
+
clauses.push(`action=${args.action}`);
|
|
644
|
+
const order = access.canRead("sys_update_xml", "sys_updated_on") ? "ORDERBYDESCsys_updated_on^ORDERBYsys_id" : "ORDERBYsys_id";
|
|
645
|
+
const metadata = exports.CHANGE_FIELDS.filter((field) => field !== (remote ? "update_set" : "remote_update_set"));
|
|
646
|
+
let rows;
|
|
647
|
+
try {
|
|
648
|
+
rows = await scoped(ctx, () => readRows(ctx, "sys_update_xml", metadata, [...clauses, order].join("^"), args.limit + 1, args.offset));
|
|
649
|
+
}
|
|
650
|
+
catch (error) {
|
|
651
|
+
if ((0, platform_view_upgrades_js_1.errorCategory)(error) === "authorization")
|
|
652
|
+
throw new ViewStop("denied", exports.UPDATE_SETS_DENIED_HINT);
|
|
653
|
+
throw error;
|
|
654
|
+
}
|
|
655
|
+
const { page, hasMore } = (0, platform_view_upgrades_js_1.pageOf)(rows, args.limit);
|
|
656
|
+
const results = page.flatMap((row) => {
|
|
657
|
+
const change = projectChange(row);
|
|
658
|
+
return change === undefined ? [] : [change];
|
|
659
|
+
});
|
|
660
|
+
// 3. The payload of the one named change.
|
|
661
|
+
let payload;
|
|
662
|
+
if (args.payload === true) {
|
|
663
|
+
const row = page.find((candidate) => lowerId(candidate.sys_id) === args.change);
|
|
664
|
+
payload = row === undefined ? (0, platform_view_payload_js_1.payloadRefusal)("withheld", "not_found") : await readPayload(ctx, setField, row);
|
|
665
|
+
}
|
|
666
|
+
return result(ctx, "changes", {
|
|
667
|
+
summary: {
|
|
668
|
+
scope: {
|
|
669
|
+
source: remote ? "remote" : "local",
|
|
670
|
+
root: rootId,
|
|
671
|
+
sets: ids.length,
|
|
672
|
+
...(children ? { include_children: true } : {}),
|
|
673
|
+
},
|
|
674
|
+
filters: {
|
|
675
|
+
...(args.change === undefined ? {} : { change: args.change }),
|
|
676
|
+
...(args.type === undefined ? {} : { type: args.type }),
|
|
677
|
+
...(args.action === undefined ? {} : { action: args.action }),
|
|
678
|
+
},
|
|
679
|
+
...(payload === undefined ? {} : { payload }),
|
|
680
|
+
},
|
|
681
|
+
results,
|
|
682
|
+
record_count: results.length,
|
|
683
|
+
has_more: hasMore,
|
|
684
|
+
...(hasMore ? { next_offset: args.offset + page.length } : {}),
|
|
685
|
+
...(args.change !== undefined && results.length === 0 ? { hint: platform_view_payload_js_1.PAYLOAD_HINTS.not_found } : {}),
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Read and mask one change's payload (R6). Its metadata is already read: a
|
|
690
|
+
* row on a credential table is refused before its payload is read.
|
|
691
|
+
*/
|
|
692
|
+
async function readPayload(ctx, setField, row) {
|
|
693
|
+
if (!ctx.access.canRead("sys_update_xml", "payload"))
|
|
694
|
+
return (0, platform_view_payload_js_1.payloadRefusal)("not_readable", "field_policy");
|
|
695
|
+
const tables = (0, platform_view_payload_js_1.rowTables)({ table: (0, platform_view_upgrades_js_1.scalar)(row.table), name: (0, platform_view_upgrades_js_1.scalar)(row.name) });
|
|
696
|
+
if (tables.some((table) => (0, credential_tables_js_1.isCredentialTable)(table))) {
|
|
697
|
+
// Checked again, on every table the payload writes and their hierarchies, once it is read.
|
|
698
|
+
return (0, platform_view_payload_js_1.payloadRefusal)("refused", "credential_table");
|
|
699
|
+
}
|
|
700
|
+
const id = lowerId(row.sys_id);
|
|
701
|
+
const set = lowerId(row[setField]);
|
|
702
|
+
let raw;
|
|
703
|
+
try {
|
|
704
|
+
const read = await scoped(ctx, () => ctx.ops.get(`${platform_view_upgrades_js_1.TABLE_PATH}sys_update_xml`, (0, platform_view_upgrades_js_1.listParams)({
|
|
705
|
+
fields: ["sys_id", "payload"],
|
|
706
|
+
query: `sys_id=${id}${set === undefined ? "" : `^${setField}=${set}`}`,
|
|
707
|
+
limit: 1,
|
|
708
|
+
})));
|
|
709
|
+
raw = (0, platform_view_upgrades_js_1.readableRow)(ctx.access, "sys_update_xml", (0, platform_view_upgrades_js_1.rowsOf)(read)[0] ?? {}, ["sys_id", "payload"]).payload;
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
if ((0, client_js_1.isServiceNowResponseTooLarge)(error) || (0, platform_view_upgrades_js_1.errorCategory)(error) === "too_large")
|
|
713
|
+
return (0, platform_view_payload_js_1.payloadRefusal)("withheld", "too_large");
|
|
714
|
+
throw error;
|
|
715
|
+
}
|
|
716
|
+
const loaderOptions = {
|
|
717
|
+
readChains: (0, platform_view_upgrades_js_1.firstUnreadable)(ctx.access, "sys_db_object", hidden_values_js_1.PLAN_FIELD_TYPE_FIELDS.sys_db_object) === undefined,
|
|
718
|
+
readTypes: (0, platform_view_upgrades_js_1.firstUnreadable)(ctx.access, "sys_dictionary", hidden_values_js_1.PLAN_FIELD_TYPE_FIELDS.sys_dictionary) === undefined,
|
|
719
|
+
filterDictionaryRow: (entry) => (0, platform_view_upgrades_js_1.readableRow)(ctx.access, "sys_dictionary", entry, hidden_values_js_1.PLAN_FIELD_TYPE_FIELDS.sys_dictionary),
|
|
720
|
+
signal: ctx.signal,
|
|
721
|
+
};
|
|
722
|
+
return (0, platform_view_payload_js_1.preparePayload)((0, platform_view_upgrades_js_1.scalar)(raw), tables, (written) => scoped(ctx, () => (0, hidden_values_js_1.loadFieldTypesInPlan)(ctx.ops, written, loaderOptions)), (0, platform_view_payload_js_1.payloadCharsFor)(ctx.args.max_response_bytes), (0, platform_view_payload_js_1.payloadBytesFor)(ctx.args.max_response_bytes));
|
|
723
|
+
}
|
|
724
|
+
// ---------------------------------------------------------------------------
|
|
725
|
+
// batch
|
|
726
|
+
// ---------------------------------------------------------------------------
|
|
727
|
+
async function viewBatch(ctx) {
|
|
728
|
+
const { args } = ctx;
|
|
729
|
+
const selector = requiredSelector(args.update_set);
|
|
730
|
+
requireReadable(ctx, "sys_update_set", ["sys_id", "parent"]);
|
|
731
|
+
// 1. The member, then its root: one scope.
|
|
732
|
+
const resolved = await firstRead(ctx, false, async () => {
|
|
733
|
+
const byId = platform_view_upgrades_js_1.SYS_ID.test(selector);
|
|
734
|
+
if (!byId)
|
|
735
|
+
requireReadable(ctx, "sys_update_set", ["name"]);
|
|
736
|
+
const member = found(await readRows(ctx, "sys_update_set", UPDATE_SET_NODE_FIELDS, byId ? `sys_id=${selector}` : `name=${selector}^ORDERBYsys_id`, byId ? 1 : 2));
|
|
737
|
+
if (member.kind !== "found")
|
|
738
|
+
return member;
|
|
739
|
+
// The root: the member's batch root, or else the top of its parents.
|
|
740
|
+
const base = lowerId(member.row.base_update_set);
|
|
741
|
+
if (base !== undefined && base !== member.id) {
|
|
742
|
+
const rows = await readRows(ctx, "sys_update_set", UPDATE_SET_NODE_FIELDS, `sys_id=${base}`, 1);
|
|
743
|
+
const root = rows[0] === undefined ? undefined : lowerId(rows[0].sys_id);
|
|
744
|
+
if (root !== undefined)
|
|
745
|
+
return { member: member.id, root: { id: root, row: rows[0] }, rootFound: true };
|
|
746
|
+
}
|
|
747
|
+
let current = { id: member.id, row: member.row };
|
|
748
|
+
const seen = new Set([member.id]);
|
|
749
|
+
for (let step = 0; step < L.MAX_DEPTH; step += 1) {
|
|
750
|
+
const parent = lowerId(current.row.parent);
|
|
751
|
+
if (parent === undefined)
|
|
752
|
+
return { member: member.id, root: current, rootFound: true };
|
|
753
|
+
if (seen.has(parent)) {
|
|
754
|
+
ctx.warnings.add("cycles");
|
|
755
|
+
return { member: member.id, root: current, rootFound: true };
|
|
756
|
+
}
|
|
757
|
+
seen.add(parent);
|
|
758
|
+
const rows = await readRows(ctx, "sys_update_set", UPDATE_SET_NODE_FIELDS, `sys_id=${parent}`, 1);
|
|
759
|
+
if (rows[0] === undefined || lowerId(rows[0].sys_id) === undefined) {
|
|
760
|
+
return { member: member.id, root: current, rootFound: true };
|
|
761
|
+
}
|
|
762
|
+
current = { id: parent, row: rows[0] };
|
|
763
|
+
}
|
|
764
|
+
return { member: member.id, root: current, rootFound: false };
|
|
765
|
+
});
|
|
766
|
+
if ("kind" in resolved)
|
|
767
|
+
return notFound(ctx, "batch", resolved);
|
|
768
|
+
if (!resolved.rootFound)
|
|
769
|
+
ctx.warnings.add("root_unresolved");
|
|
770
|
+
// 2. The tree under the root: one scope.
|
|
771
|
+
const { root, member } = resolved;
|
|
772
|
+
const tree = await scoped(ctx, () => descend(ctx, "sys_update_set", root, UPDATE_SET_NODE_FIELDS, L.MAX_BATCH_NODES));
|
|
773
|
+
if (tree.depthCapped)
|
|
774
|
+
ctx.warnings.add("depth_capped");
|
|
775
|
+
if (tree.nodesCapped)
|
|
776
|
+
ctx.warnings.add("nodes_capped");
|
|
777
|
+
if (tree.cycles > 0)
|
|
778
|
+
ctx.warnings.add("cycles");
|
|
779
|
+
// 3. Row counts per set.
|
|
780
|
+
const ids = [...tree.nodes.keys()];
|
|
781
|
+
const counts = await countRows(ctx, "update_set", ids);
|
|
782
|
+
if (counts === undefined)
|
|
783
|
+
ctx.warnings.add("counts_unavailable");
|
|
784
|
+
const childCount = new Map();
|
|
785
|
+
for (const { row } of tree.nodes.values()) {
|
|
786
|
+
const parent = lowerId(row.parent);
|
|
787
|
+
if (parent !== undefined)
|
|
788
|
+
childCount.set(parent, (childCount.get(parent) ?? 0) + 1);
|
|
789
|
+
}
|
|
790
|
+
const nodes = [...tree.nodes].map(([id, { row, depth }]) => ({
|
|
791
|
+
...projectNode(row),
|
|
792
|
+
parent: id === root.id ? null : lowerId(row.parent) ?? null,
|
|
793
|
+
depth,
|
|
794
|
+
children: childCount.get(id) ?? 0,
|
|
795
|
+
update_count: counts?.get(id) ?? null,
|
|
796
|
+
}));
|
|
797
|
+
const states = {};
|
|
798
|
+
for (const node of nodes)
|
|
799
|
+
states[node.state ?? ""] = (states[node.state ?? ""] ?? 0) + 1;
|
|
800
|
+
const page = nodes.slice(args.offset, args.offset + args.limit);
|
|
801
|
+
const hasMore = args.offset + page.length < nodes.length;
|
|
802
|
+
return result(ctx, "batch", {
|
|
803
|
+
summary: {
|
|
804
|
+
root: projectNode(root.row),
|
|
805
|
+
member,
|
|
806
|
+
sets: nodes.length,
|
|
807
|
+
depth: nodes.reduce((max, node) => Math.max(max, node.depth), 0),
|
|
808
|
+
depth_capped: tree.depthCapped,
|
|
809
|
+
sets_capped: tree.nodesCapped,
|
|
810
|
+
cycles: tree.cycles,
|
|
811
|
+
states,
|
|
812
|
+
update_count: counts === undefined ? null : nodes.reduce((sum, node) => sum + (node.update_count ?? 0), 0),
|
|
813
|
+
},
|
|
814
|
+
results: page,
|
|
815
|
+
record_count: page.length,
|
|
816
|
+
has_more: hasMore,
|
|
817
|
+
...(hasMore ? { next_offset: args.offset + page.length } : {}),
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
// ---------------------------------------------------------------------------
|
|
821
|
+
// remote_update_sets
|
|
822
|
+
// ---------------------------------------------------------------------------
|
|
823
|
+
async function viewRemoteUpdateSets(ctx) {
|
|
824
|
+
const { args, access } = ctx;
|
|
825
|
+
// Problems belong to one retrieved set.
|
|
826
|
+
if (args.problems === true && args.remote_update_set === undefined) {
|
|
827
|
+
throw (0, tool_error_js_1.createToolError)("conflict", "retry_after_correction");
|
|
828
|
+
}
|
|
829
|
+
if (args.problems === true)
|
|
830
|
+
return viewProblems(ctx, args.remote_update_set);
|
|
831
|
+
const clauses = [];
|
|
832
|
+
const filterFields = ["sys_id"];
|
|
833
|
+
if (args.state !== undefined) {
|
|
834
|
+
clauses.push(`state=${args.state}`);
|
|
835
|
+
filterFields.push("state");
|
|
836
|
+
}
|
|
837
|
+
if (args.remote_update_set !== undefined)
|
|
838
|
+
clauses.push(`sys_id=${args.remote_update_set}`);
|
|
839
|
+
requireReadable(ctx, "sys_remote_update_set", filterFields);
|
|
840
|
+
const order = access.canRead("sys_remote_update_set", "sys_created_on") ? "ORDERBYDESCsys_created_on^ORDERBYsys_id" : "ORDERBYsys_id";
|
|
841
|
+
const rows = await firstRead(ctx, true, () => readRows(ctx, "sys_remote_update_set", exports.REMOTE_SET_FIELDS, [...clauses, order].join("^"), args.limit + 1, args.offset));
|
|
842
|
+
const { page, hasMore } = (0, platform_view_upgrades_js_1.pageOf)(rows, args.limit);
|
|
843
|
+
const results = page.flatMap((row) => {
|
|
844
|
+
const set = projectRemoteSet(row);
|
|
845
|
+
return set === undefined ? [] : [set];
|
|
846
|
+
});
|
|
847
|
+
return result(ctx, "remote_update_sets", {
|
|
848
|
+
summary: { filters: { ...(args.state === undefined ? {} : { state: args.state }) } },
|
|
849
|
+
results,
|
|
850
|
+
record_count: results.length,
|
|
851
|
+
has_more: hasMore,
|
|
852
|
+
...(hasMore ? { next_offset: args.offset + page.length } : {}),
|
|
853
|
+
...(args.remote_update_set !== undefined && results.length === 0 ? { hint: exports.REMOTE_NOT_FOUND_HINT } : {}),
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
async function viewProblems(ctx, id) {
|
|
857
|
+
const { args, access } = ctx;
|
|
858
|
+
const set = await findRemoteSet(ctx, id, exports.REMOTE_SET_FIELDS);
|
|
859
|
+
if (set.kind !== "found")
|
|
860
|
+
return notFound(ctx, "remote_update_sets", set, true);
|
|
861
|
+
if ((0, platform_view_upgrades_js_1.firstUnreadable)(access, "sys_update_preview_problem", ["sys_id", "remote_update_set"]) !== undefined) {
|
|
862
|
+
throw new ViewStop("denied", platform_view_upgrades_js_1.FIELD_POLICY_HINT);
|
|
863
|
+
}
|
|
864
|
+
const groupFields = ["type", "status"].filter((field) => access.canRead("sys_update_preview_problem", field));
|
|
865
|
+
let rows;
|
|
866
|
+
let groups;
|
|
867
|
+
try {
|
|
868
|
+
[rows, groups] = await Promise.all([
|
|
869
|
+
scoped(ctx, () => readRows(ctx, "sys_update_preview_problem", exports.PROBLEM_FIELDS, `remote_update_set=${set.id}^ORDERBYsys_id`, args.limit + 1, args.offset)),
|
|
870
|
+
scoped(ctx, () => ctx.ops.get(`${platform_view_upgrades_js_1.STATS_PATH}sys_update_preview_problem`, {
|
|
871
|
+
sysparm_query: `remote_update_set=${set.id}`,
|
|
872
|
+
sysparm_count: "true",
|
|
873
|
+
...(groupFields.length > 0 ? { sysparm_group_by: groupFields.join(",") } : {}),
|
|
874
|
+
})).then(statGroups, (error) => {
|
|
875
|
+
if (isCancellation(ctx, error))
|
|
876
|
+
throw error;
|
|
877
|
+
ctx.warnings.add("problem_counts_unavailable");
|
|
878
|
+
return undefined;
|
|
879
|
+
}),
|
|
880
|
+
]);
|
|
881
|
+
}
|
|
882
|
+
catch (error) {
|
|
883
|
+
if ((0, platform_view_upgrades_js_1.errorCategory)(error) !== "authorization")
|
|
884
|
+
throw error;
|
|
885
|
+
ctx.warnings.add("problems_denied");
|
|
886
|
+
rows = undefined;
|
|
887
|
+
}
|
|
888
|
+
const remoteSet = projectRemoteSet(set.row);
|
|
889
|
+
if (rows === undefined) {
|
|
890
|
+
return result(ctx, "remote_update_sets", { summary: { remote_set: remoteSet, problems: "denied" } });
|
|
891
|
+
}
|
|
892
|
+
const { page, hasMore } = (0, platform_view_upgrades_js_1.pageOf)(rows, args.limit);
|
|
893
|
+
const results = page.flatMap((row) => {
|
|
894
|
+
const problem = projectProblem(row);
|
|
895
|
+
return problem === undefined ? [] : [problem];
|
|
896
|
+
});
|
|
897
|
+
const tally = (field) => {
|
|
898
|
+
if (groups === undefined || !groupFields.includes(field))
|
|
899
|
+
return undefined;
|
|
900
|
+
const out = {};
|
|
901
|
+
for (const group of groups) {
|
|
902
|
+
const key = (0, platform_view_upgrades_js_1.cleanText)(group.fields.get(field), 40) ?? "";
|
|
903
|
+
out[key] = (out[key] ?? 0) + group.count;
|
|
904
|
+
}
|
|
905
|
+
return out;
|
|
906
|
+
};
|
|
907
|
+
const byStatus = tally("status");
|
|
908
|
+
const byType = tally("type");
|
|
909
|
+
return result(ctx, "remote_update_sets", {
|
|
910
|
+
summary: {
|
|
911
|
+
remote_set: remoteSet,
|
|
912
|
+
problems: "read",
|
|
913
|
+
...(groups === undefined
|
|
914
|
+
? {}
|
|
915
|
+
: {
|
|
916
|
+
problem_counts: {
|
|
917
|
+
// No problems has no group: 0.
|
|
918
|
+
total: groups.reduce((sum, group) => sum + group.count, 0),
|
|
919
|
+
...(byStatus === undefined ? {} : { by_status: byStatus }),
|
|
920
|
+
...(byType === undefined ? {} : { by_type: byType }),
|
|
921
|
+
},
|
|
922
|
+
}),
|
|
923
|
+
},
|
|
924
|
+
results,
|
|
925
|
+
record_count: results.length,
|
|
926
|
+
has_more: hasMore,
|
|
927
|
+
...(hasMore ? { next_offset: args.offset + page.length } : {}),
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
// ---------------------------------------------------------------------------
|
|
931
|
+
// Results and the handler
|
|
932
|
+
// ---------------------------------------------------------------------------
|
|
933
|
+
function requiredSelector(value) {
|
|
934
|
+
if (value === undefined)
|
|
935
|
+
throw (0, tool_error_js_1.createToolError)("conflict", "retry_after_correction");
|
|
936
|
+
return value;
|
|
937
|
+
}
|
|
938
|
+
function notFound(ctx, view, set, remote = false) {
|
|
939
|
+
return result(ctx, view, {
|
|
940
|
+
summary: { set: null, ...(set.kind === "ambiguous" ? { candidates: set.candidates } : {}) },
|
|
941
|
+
results: [],
|
|
942
|
+
hint: set.kind === "ambiguous" ? exports.AMBIGUOUS_HINT : remote ? exports.REMOTE_NOT_FOUND_HINT : exports.NOT_FOUND_HINT,
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
function result(ctx, view, data) {
|
|
946
|
+
const warnings = [...ctx.warnings].map((key) => exports.UPDATE_SET_VIEW_WARNINGS[key]);
|
|
947
|
+
return (0, platform_view_upgrades_js_1.viewResult)(ctx.args, {
|
|
948
|
+
view,
|
|
949
|
+
status: "ok",
|
|
950
|
+
...data,
|
|
951
|
+
summary: { ...data.summary, upstream_scopes: ctx.scopes.opened },
|
|
952
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Build the handler of the five update set views. `handleUpdateSetViews` is
|
|
957
|
+
* the one the module registers; a test builds its own with a fixed field
|
|
958
|
+
* access.
|
|
959
|
+
*/
|
|
960
|
+
function createUpdateSetViewsHandler(defaults = {}) {
|
|
961
|
+
return async (args, services, fieldAccess) => {
|
|
962
|
+
if (!UPDATE_SET_VIEWS.includes(args.view))
|
|
963
|
+
throw (0, tool_error_js_1.createToolError)("internal", "do_not_retry");
|
|
964
|
+
const ctx = {
|
|
965
|
+
args,
|
|
966
|
+
access: (0, platform_view_upgrades_js_1.memoizedFieldAccess)(fieldAccess ?? defaults.fieldAccess ?? platform_view_upgrades_js_1.liveFieldAccess),
|
|
967
|
+
ops: (0, metadata_cache_js_1.uncachedOperations)(services.serviceNow),
|
|
968
|
+
signal: services.context?.signal,
|
|
969
|
+
scopes: new flow_view_module_js_1.FlowViewScopeBudget(L.MAX_SCOPES),
|
|
970
|
+
warnings: new Set(),
|
|
971
|
+
};
|
|
972
|
+
const view = args.view;
|
|
973
|
+
try {
|
|
974
|
+
switch (view) {
|
|
975
|
+
case "update_sets":
|
|
976
|
+
return await viewUpdateSets(ctx);
|
|
977
|
+
case "update_set":
|
|
978
|
+
return await viewUpdateSet(ctx);
|
|
979
|
+
case "changes":
|
|
980
|
+
return await viewChanges(ctx);
|
|
981
|
+
case "batch":
|
|
982
|
+
return await viewBatch(ctx);
|
|
983
|
+
case "remote_update_sets":
|
|
984
|
+
return await viewRemoteUpdateSets(ctx);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
catch (error) {
|
|
988
|
+
if (error instanceof ViewStop)
|
|
989
|
+
return (0, platform_view_upgrades_js_1.viewResult)(args, { view, status: error.status, hint: error.hint });
|
|
990
|
+
throw error;
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
/** The handler the dispatcher registers for the five update set views. */
|
|
995
|
+
exports.handleUpdateSetViews = createUpdateSetViewsHandler();
|
|
996
|
+
// ---------------------------------------------------------------------------
|
|
997
|
+
// Text
|
|
998
|
+
// ---------------------------------------------------------------------------
|
|
999
|
+
function shown(value) {
|
|
1000
|
+
return (0, platform_view_upgrades_js_1.scalar)(value) ?? "-";
|
|
1001
|
+
}
|
|
1002
|
+
const TITLES = Object.freeze({
|
|
1003
|
+
update_sets: "Update sets",
|
|
1004
|
+
update_set: "Update set",
|
|
1005
|
+
changes: "Update set changes",
|
|
1006
|
+
batch: "Update set batch",
|
|
1007
|
+
remote_update_sets: "Retrieved update sets",
|
|
1008
|
+
});
|
|
1009
|
+
/** One line per row, `key: value` pairs joined; every value is instance text. */
|
|
1010
|
+
function lines(rows) {
|
|
1011
|
+
return rows
|
|
1012
|
+
.map((row) => Object.entries(row)
|
|
1013
|
+
.map(([key, value]) => `${key}: ${typeof value === "object" && value !== null ? JSON.stringify(value) : shown(value)}`)
|
|
1014
|
+
.join(" | "))
|
|
1015
|
+
.join("\n");
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* The readable text of an update set view's result. Counts, statuses and the
|
|
1019
|
+
* fixed hints, warnings and notes are plain text; every value read from the
|
|
1020
|
+
* instance (names, descriptions, types, payload text) sits inside an
|
|
1021
|
+
* `untrustedBlock`.
|
|
1022
|
+
*/
|
|
1023
|
+
function renderUpdateSetViewText(data) {
|
|
1024
|
+
const record = (0, platform_view_upgrades_js_1.asRecord)(data);
|
|
1025
|
+
const view = record.view;
|
|
1026
|
+
if (!UPDATE_SET_VIEWS.includes(view))
|
|
1027
|
+
return undefined;
|
|
1028
|
+
const title = TITLES[view];
|
|
1029
|
+
if (record.status !== "ok") {
|
|
1030
|
+
return (0, markdown_js_1.renderMarkdown)((0, markdown_js_1.headline)(title, { status: "fail", detail: (0, platform_view_upgrades_js_1.scalar)(record.hint) ?? "not available" }));
|
|
1031
|
+
}
|
|
1032
|
+
const summary = (0, platform_view_upgrades_js_1.asRecord)(record.summary);
|
|
1033
|
+
const rows = Array.isArray(record.results) ? record.results.map(platform_view_upgrades_js_1.asRecord) : [];
|
|
1034
|
+
const blocks = [
|
|
1035
|
+
(0, markdown_js_1.headline)(title, { status: "pass", detail: `${rows.length} row(s)${record.has_more === true ? ", more available" : ""}` }),
|
|
1036
|
+
];
|
|
1037
|
+
if (typeof record.hint === "string")
|
|
1038
|
+
blocks.push((0, markdown_js_1.text)(record.hint));
|
|
1039
|
+
const { payload, ...rest } = summary;
|
|
1040
|
+
const details = Object.entries(rest).filter(([key, value]) => key !== "upstream_scopes" && value !== undefined);
|
|
1041
|
+
if (details.length > 0)
|
|
1042
|
+
blocks.push((0, markdown_js_1.untrustedBlock)("Summary", lines([Object.fromEntries(details)])));
|
|
1043
|
+
if (rows.length > 0)
|
|
1044
|
+
blocks.push((0, markdown_js_1.untrustedBlock)("Rows", lines(rows), 20_000));
|
|
1045
|
+
const outcome = (0, platform_view_upgrades_js_1.asRecord)(payload);
|
|
1046
|
+
if (outcome.status === "shown") {
|
|
1047
|
+
blocks.push((0, markdown_js_1.text)((0, platform_view_upgrades_js_1.scalar)(outcome.note)));
|
|
1048
|
+
blocks.push((0, markdown_js_1.untrustedBlock)("Payload (masked)", shown(outcome.text), 210_000));
|
|
1049
|
+
}
|
|
1050
|
+
else if (typeof outcome.status === "string") {
|
|
1051
|
+
blocks.push((0, markdown_js_1.text)(`Payload ${outcome.status}${typeof outcome.hint === "string" ? `: ${outcome.hint}` : "."}`));
|
|
1052
|
+
}
|
|
1053
|
+
if (record.has_more === true)
|
|
1054
|
+
blocks.push((0, markdown_js_1.text)(`More rows: next_offset ${shown(record.next_offset)}.`));
|
|
1055
|
+
const warnings = Array.isArray(record.warnings) ? record.warnings : [];
|
|
1056
|
+
if (warnings.length > 0)
|
|
1057
|
+
blocks.push((0, markdown_js_1.bulletList)(warnings.map((warning) => String((0, platform_view_upgrades_js_1.scalar)(warning) ?? ""))));
|
|
1058
|
+
return (0, markdown_js_1.renderMarkdown)(...blocks);
|
|
1059
|
+
}
|
|
1060
|
+
//# sourceMappingURL=platform-view-update-sets.js.map
|