@lincolnminto/zendesk-mcp-server 2.25.0 → 2.27.0
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/README.md +40 -2
- package/dist/index.js +2242 -20
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
3
|
-
import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { chmodSync, constants, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { createServer } from "node:http";
|
|
5
5
|
import { homedir, release } from "node:os";
|
|
6
6
|
import open from "open";
|
|
7
|
-
import { basename, dirname, extname, isAbsolute, join } from "node:path";
|
|
7
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
8
8
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
9
9
|
import { parseArgs } from "node:util";
|
|
10
10
|
import * as z from "zod/v4";
|
|
@@ -20,7 +20,8 @@ import remarkParse from "remark-parse";
|
|
|
20
20
|
import remarkRehype from "remark-rehype";
|
|
21
21
|
import remarkStringify from "remark-stringify";
|
|
22
22
|
import { unified } from "unified";
|
|
23
|
-
import { readFile, stat } from "node:fs/promises";
|
|
23
|
+
import { lstat, mkdir, open as open$1, readFile, rename, stat } from "node:fs/promises";
|
|
24
|
+
import { createInterface } from "node:readline";
|
|
24
25
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
25
26
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
26
27
|
//#region src/auth/api-token.ts
|
|
@@ -502,6 +503,7 @@ const createAuthRequiredError = (authorizeUrl) => Object.assign(/* @__PURE__ */
|
|
|
502
503
|
name: "AuthRequiredError",
|
|
503
504
|
authorizeUrl
|
|
504
505
|
});
|
|
506
|
+
const isAuthRequiredError = (err) => err instanceof Error && err.name === "AuthRequiredError" && typeof err.authorizeUrl === "string";
|
|
505
507
|
const expiryFrom = (expiresIn) => typeof expiresIn === "number" ? Date.now() + expiresIn * 1e3 : void 0;
|
|
506
508
|
const createTokenStore = (config, logger = silentLogger) => {
|
|
507
509
|
const readOnly = config.readOnly ?? false;
|
|
@@ -530,7 +532,14 @@ const createTokenStore = (config, logger = silentLogger) => {
|
|
|
530
532
|
persist(token);
|
|
531
533
|
};
|
|
532
534
|
const needsRefresh = (t) => typeof t.expiresAt === "number" ? Date.now() >= t.expiresAt - EXPIRY_SKEW_MS : t.refreshToken !== void 0 && !probedUnknownExpiry;
|
|
533
|
-
const
|
|
535
|
+
const adoptRotatedToken = (current) => {
|
|
536
|
+
const loaded = loadUsableToken();
|
|
537
|
+
if (!loaded?.refreshToken || loaded.refreshToken === current.refreshToken) return void 0;
|
|
538
|
+
token = loaded;
|
|
539
|
+
probedUnknownExpiry = false;
|
|
540
|
+
return loaded;
|
|
541
|
+
};
|
|
542
|
+
const tryRefresh = async (current, { dropOnFailure = true, retryOnRotation = true } = {}) => {
|
|
534
543
|
if (!current.refreshToken) return void 0;
|
|
535
544
|
try {
|
|
536
545
|
const result = await refreshAccessToken({
|
|
@@ -552,13 +561,28 @@ const createTokenStore = (config, logger = silentLogger) => {
|
|
|
552
561
|
return token.accessToken;
|
|
553
562
|
} catch (err) {
|
|
554
563
|
logger.warn("oauth_token_refresh_failed", { error: err instanceof Error ? err.message : String(err) });
|
|
555
|
-
|
|
556
|
-
token = void 0;
|
|
557
|
-
clearToken(tokenPath, logger);
|
|
558
|
-
}
|
|
559
|
-
return;
|
|
564
|
+
return recoverRefresh(current, dropOnFailure, retryOnRotation);
|
|
560
565
|
}
|
|
561
566
|
};
|
|
567
|
+
const recoverRefresh = async (current, dropOnFailure, retryOnRotation) => {
|
|
568
|
+
const rotated = adoptRotatedToken(current);
|
|
569
|
+
if (rotated) {
|
|
570
|
+
if (!needsRefresh(rotated)) return rotated.accessToken;
|
|
571
|
+
return retryOnRotation ? tryRefresh(rotated, {
|
|
572
|
+
dropOnFailure: false,
|
|
573
|
+
retryOnRotation: false
|
|
574
|
+
}) : void 0;
|
|
575
|
+
}
|
|
576
|
+
if (dropOnFailure) {
|
|
577
|
+
token = void 0;
|
|
578
|
+
clearToken(tokenPath, logger);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
const refreshLatest = (current, options = {}) => {
|
|
582
|
+
const adopted = adoptRotatedToken(current);
|
|
583
|
+
if (adopted && !needsRefresh(adopted)) return Promise.resolve(adopted.accessToken);
|
|
584
|
+
return tryRefresh(adopted ?? current, options);
|
|
585
|
+
};
|
|
562
586
|
const beginAuth = () => {
|
|
563
587
|
logger.info("oauth_auth_start");
|
|
564
588
|
return startBrowserAuth({
|
|
@@ -598,7 +622,7 @@ const createTokenStore = (config, logger = silentLogger) => {
|
|
|
598
622
|
const refreshIfPossible = async () => {
|
|
599
623
|
const current = token;
|
|
600
624
|
if (!current?.refreshToken) return void 0;
|
|
601
|
-
if (refreshing === void 0) refreshing =
|
|
625
|
+
if (refreshing === void 0) refreshing = refreshLatest(current).finally(() => {
|
|
602
626
|
refreshing = void 0;
|
|
603
627
|
});
|
|
604
628
|
return refreshing;
|
|
@@ -631,7 +655,7 @@ const createTokenStore = (config, logger = silentLogger) => {
|
|
|
631
655
|
logger.info("oauth_token_invalidated");
|
|
632
656
|
};
|
|
633
657
|
const scheduledRefresh = setInterval(() => {
|
|
634
|
-
if (token?.refreshToken && !refreshing) refreshing =
|
|
658
|
+
if (token?.refreshToken && !refreshing) refreshing = refreshLatest(token, { dropOnFailure: false }).finally(() => {
|
|
635
659
|
refreshing = void 0;
|
|
636
660
|
});
|
|
637
661
|
}, SCHEDULED_REFRESH_MS);
|
|
@@ -660,9 +684,11 @@ const LogLevel = z.enum([
|
|
|
660
684
|
const Namespace = z.enum([
|
|
661
685
|
"tickets",
|
|
662
686
|
"help_center",
|
|
663
|
-
"users"
|
|
687
|
+
"users",
|
|
688
|
+
"admin"
|
|
664
689
|
]);
|
|
665
690
|
const Transport = z.enum(["stdio", "http"]);
|
|
691
|
+
const NETWORK_PATH$3 = /^[\\/]{2}/;
|
|
666
692
|
const ConfigSchema = z.object({
|
|
667
693
|
subdomain: z.string().min(1, "ZENDESK_SUBDOMAIN is required"),
|
|
668
694
|
oauthClientId: z.string().min(1),
|
|
@@ -741,7 +767,14 @@ const ConfigSchema = z.object({
|
|
|
741
767
|
* because they send no Origin header.
|
|
742
768
|
*/
|
|
743
769
|
corsOrigins: z.array(z.string().url().transform((value) => new URL(value).origin).refine((origin) => origin !== "null", { message: "CORS origin must be an http(s) URL with a host" })).default([]),
|
|
744
|
-
callbackPort: z.number().int().min(1).max(65535).optional()
|
|
770
|
+
callbackPort: z.number().int().min(1).max(65535).optional(),
|
|
771
|
+
/**
|
|
772
|
+
* Where `export_account_config` writes its snapshots; the admin namespace
|
|
773
|
+
* exists only when this is set. It is the operator's choice, never the
|
|
774
|
+
* model's: the tool takes no path, so a prompt cannot steer where files land.
|
|
775
|
+
* stdio only (refused at boot over HTTP, see loadConfig).
|
|
776
|
+
*/
|
|
777
|
+
exportDir: z.string().refine((dir) => isAbsolute(dir) && !NETWORK_PATH$3.test(dir), { message: "The export directory must be an absolute local path (--export-dir / ZENDESK_EXPORT_DIR), not a relative path or a network share." }).optional()
|
|
745
778
|
});
|
|
746
779
|
const DIGITS_ONLY = /^\d+$/;
|
|
747
780
|
const parsePort = (raw, label) => {
|
|
@@ -775,6 +808,7 @@ const CLI_OPTIONS = {
|
|
|
775
808
|
multiple: true
|
|
776
809
|
},
|
|
777
810
|
"callback-port": { type: "string" },
|
|
811
|
+
"export-dir": { type: "string" },
|
|
778
812
|
"read-only": { type: "boolean" },
|
|
779
813
|
"no-topology": { type: "boolean" },
|
|
780
814
|
"no-promoted-articles": { type: "boolean" },
|
|
@@ -790,7 +824,8 @@ const FIELD_BY_FLAG = /* @__PURE__ */ new Map([
|
|
|
790
824
|
["transport", "transport"],
|
|
791
825
|
["host", "host"],
|
|
792
826
|
["public-url", "publicUrl"],
|
|
793
|
-
["cors-origin", "corsOrigins"]
|
|
827
|
+
["cors-origin", "corsOrigins"],
|
|
828
|
+
["export-dir", "exportDir"]
|
|
794
829
|
]);
|
|
795
830
|
const STANDALONE_EFFECTS = /* @__PURE__ */ new Map([
|
|
796
831
|
["read-only", { readOnly: true }],
|
|
@@ -830,6 +865,17 @@ const resolveTransportSettings = (cli) => {
|
|
|
830
865
|
corsOrigins: [...cli.corsOrigins ?? [], ...corsFromEnv]
|
|
831
866
|
};
|
|
832
867
|
};
|
|
868
|
+
const exportAskedFor = (cli) => {
|
|
869
|
+
if (cli.namespaces?.includes("admin")) return "The admin namespace";
|
|
870
|
+
if (cli.tools?.includes("export_account_config")) return "export_account_config";
|
|
871
|
+
};
|
|
872
|
+
const resolveExportDir = (cli, transport) => {
|
|
873
|
+
const exportDir = cli.exportDir ?? requireNonEmptyEnv("ZENDESK_EXPORT_DIR");
|
|
874
|
+
const asked = exportAskedFor(cli);
|
|
875
|
+
if (transport === "http" && (exportDir !== void 0 || asked)) throw new Error("The account export (--export-dir / ZENDESK_EXPORT_DIR, the admin namespace) is stdio only: over HTTP the files would be written on the server host. Leave it out in HTTP mode.");
|
|
876
|
+
if (asked && exportDir === void 0) throw new Error(`${asked} needs an export directory: set --export-dir (or ZENDESK_EXPORT_DIR) to an absolute local path.`);
|
|
877
|
+
return exportDir;
|
|
878
|
+
};
|
|
833
879
|
const loadConfig = (argv = process.argv.slice(2)) => {
|
|
834
880
|
const cli = parseCliArgs(argv);
|
|
835
881
|
const subdomain = cli.subdomain ?? requireNonEmptyEnv("ZENDESK_SUBDOMAIN") ?? "";
|
|
@@ -841,6 +887,7 @@ const loadConfig = (argv = process.argv.slice(2)) => {
|
|
|
841
887
|
const hcResourceScheme = cli.hcResourceScheme ?? requireNonEmptyEnv("HC_RESOURCE_SCHEME");
|
|
842
888
|
const transportSettings = resolveTransportSettings(cli);
|
|
843
889
|
if (transportSettings.transport === "http" && zendeskEmail && zendeskApiToken) throw new Error("API token authentication (ZENDESK_EMAIL + ZENDESK_API_TOKEN) is not supported in HTTP mode. HTTP mode requires per-user OAuth 2.1 PKCE - unset these variables and configure your MCP client to perform the OAuth flow against Zendesk.");
|
|
890
|
+
const exportDir = resolveExportDir(cli, transportSettings.transport);
|
|
844
891
|
return ConfigSchema.parse({
|
|
845
892
|
subdomain,
|
|
846
893
|
oauthClientId,
|
|
@@ -856,6 +903,7 @@ const loadConfig = (argv = process.argv.slice(2)) => {
|
|
|
856
903
|
hcResourceScheme,
|
|
857
904
|
dev: cli.dev ?? false,
|
|
858
905
|
callbackPort,
|
|
906
|
+
exportDir,
|
|
859
907
|
...transportSettings
|
|
860
908
|
});
|
|
861
909
|
};
|
|
@@ -970,6 +1018,7 @@ const createZendeskNetworkError = (method, target, attempts, cause) => Object.as
|
|
|
970
1018
|
target,
|
|
971
1019
|
attempts
|
|
972
1020
|
});
|
|
1021
|
+
const isZendeskNetworkError = (err) => err instanceof Error && err.name === "ZendeskNetworkError" && typeof err.target === "string";
|
|
973
1022
|
/** Exponential backoff with equal jitter: half the window fixed, half random. */
|
|
974
1023
|
const computeBackoffMs = (attempt, random) => {
|
|
975
1024
|
const window = Math.min(BASE_DELAY_MS * 2 ** (attempt - 1), MAX_DELAY_MS);
|
|
@@ -1172,11 +1221,11 @@ const HEADING_LEVELS = /* @__PURE__ */ new Set([
|
|
|
1172
1221
|
"h2",
|
|
1173
1222
|
"h3"
|
|
1174
1223
|
]);
|
|
1175
|
-
const WHITESPACE_RUN = /\s+/;
|
|
1224
|
+
const WHITESPACE_RUN$1 = /\s+/;
|
|
1176
1225
|
const countWords = (text) => {
|
|
1177
1226
|
const trimmed = text.trim();
|
|
1178
1227
|
if (!trimmed) return 0;
|
|
1179
|
-
return trimmed.split(WHITESPACE_RUN).length;
|
|
1228
|
+
return trimmed.split(WHITESPACE_RUN$1).length;
|
|
1180
1229
|
};
|
|
1181
1230
|
const textOf = (html) => {
|
|
1182
1231
|
if (!html) return "";
|
|
@@ -1893,6 +1942,1989 @@ const groupByNamespace = (tools) => {
|
|
|
1893
1942
|
return grouped;
|
|
1894
1943
|
};
|
|
1895
1944
|
//#endregion
|
|
1945
|
+
//#region src/export/ndjson.ts
|
|
1946
|
+
const READ_FLAGS = constants.O_RDONLY | constants.O_NOFOLLOW;
|
|
1947
|
+
const WRITE_FLAGS = constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW;
|
|
1948
|
+
const APPEND_FLAGS = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW;
|
|
1949
|
+
const TRUNCATE_FLAGS = constants.O_WRONLY | constants.O_NOFOLLOW;
|
|
1950
|
+
const SEPARATORS = /[/\\]/;
|
|
1951
|
+
const validateResumeName = (name) => {
|
|
1952
|
+
if (!name || name.includes("..") || SEPARATORS.test(name) || name === ".") throw new Error("Resume must be a snapshot folder name without separators or ..");
|
|
1953
|
+
return name;
|
|
1954
|
+
};
|
|
1955
|
+
/** Reject symlinks in every existing path component, including exportDir. */
|
|
1956
|
+
const safeDirectory = async (path, create) => {
|
|
1957
|
+
const parent = resolve(path, "..");
|
|
1958
|
+
if (parent !== path) await safeDirectory(parent, create);
|
|
1959
|
+
try {
|
|
1960
|
+
const info = await lstat(path);
|
|
1961
|
+
if (!info.isDirectory() || info.isSymbolicLink()) throw new Error("Snapshot directories must be real local directories");
|
|
1962
|
+
} catch (err) {
|
|
1963
|
+
if (!(err instanceof Error && "code" in err && err.code === "ENOENT") || !create) throw err;
|
|
1964
|
+
await mkdir(path);
|
|
1965
|
+
}
|
|
1966
|
+
};
|
|
1967
|
+
const snapshotFile = (snapshot, name) => {
|
|
1968
|
+
const target = resolve(snapshot, name);
|
|
1969
|
+
const rel = relative(snapshot, target);
|
|
1970
|
+
if (!rel || rel.startsWith("..") || isAbsolute(rel)) throw new Error("File must stay inside the snapshot");
|
|
1971
|
+
return target;
|
|
1972
|
+
};
|
|
1973
|
+
const readManifest = async (snapshot) => {
|
|
1974
|
+
const handle = await open$1(join(snapshot, "manifest.json"), READ_FLAGS);
|
|
1975
|
+
try {
|
|
1976
|
+
return JSON.parse(await handle.readFile("utf8"));
|
|
1977
|
+
} finally {
|
|
1978
|
+
await handle.close();
|
|
1979
|
+
}
|
|
1980
|
+
};
|
|
1981
|
+
/** Rename commits the position and all file byte offsets together. */
|
|
1982
|
+
const writeManifest$1 = async (snapshot, manifest) => {
|
|
1983
|
+
const temp = join(snapshot, "manifest.json.tmp");
|
|
1984
|
+
const handle = await open$1(temp, WRITE_FLAGS, 384);
|
|
1985
|
+
try {
|
|
1986
|
+
await handle.writeFile(`${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
1987
|
+
await handle.sync();
|
|
1988
|
+
} finally {
|
|
1989
|
+
await handle.close();
|
|
1990
|
+
}
|
|
1991
|
+
await rename(temp, join(snapshot, "manifest.json"));
|
|
1992
|
+
};
|
|
1993
|
+
const appendPage = async (snapshot, file, rows, previous = {
|
|
1994
|
+
lines: 0,
|
|
1995
|
+
bytes: 0
|
|
1996
|
+
}) => {
|
|
1997
|
+
const handle = await open$1(snapshotFile(snapshot, file), APPEND_FLAGS, 384);
|
|
1998
|
+
const data = rows.map((row) => `${JSON.stringify(row)}\n`).join("");
|
|
1999
|
+
try {
|
|
2000
|
+
await handle.writeFile(data, "utf8");
|
|
2001
|
+
await handle.sync();
|
|
2002
|
+
} finally {
|
|
2003
|
+
await handle.close();
|
|
2004
|
+
}
|
|
2005
|
+
return {
|
|
2006
|
+
lines: previous.lines + rows.length,
|
|
2007
|
+
bytes: previous.bytes + Buffer.byteLength(data)
|
|
2008
|
+
};
|
|
2009
|
+
};
|
|
2010
|
+
const truncateFiles = async (snapshot, files) => {
|
|
2011
|
+
for (const [name, stats] of Object.entries(files)) {
|
|
2012
|
+
if (!Number.isSafeInteger(stats.bytes) || stats.bytes < 0 || !Number.isSafeInteger(stats.lines) || stats.lines < 0) throw new Error("Invalid file checkpoint");
|
|
2013
|
+
const target = snapshotFile(snapshot, name);
|
|
2014
|
+
try {
|
|
2015
|
+
await lstat(target);
|
|
2016
|
+
} catch (err) {
|
|
2017
|
+
if (stats.bytes === 0 && err instanceof Error && "code" in err && err.code === "ENOENT") continue;
|
|
2018
|
+
throw err;
|
|
2019
|
+
}
|
|
2020
|
+
const handle = await open$1(target, TRUNCATE_FLAGS);
|
|
2021
|
+
try {
|
|
2022
|
+
if ((await handle.stat()).size < stats.bytes) throw new Error("Snapshot file is shorter than its checkpoint");
|
|
2023
|
+
await handle.truncate(stats.bytes);
|
|
2024
|
+
} finally {
|
|
2025
|
+
await handle.close();
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
};
|
|
2029
|
+
const writeJsonFile = async (path, value) => {
|
|
2030
|
+
const handle = await open$1(path, WRITE_FLAGS, 384);
|
|
2031
|
+
try {
|
|
2032
|
+
await handle.writeFile(`${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
2033
|
+
await handle.sync();
|
|
2034
|
+
} finally {
|
|
2035
|
+
await handle.close();
|
|
2036
|
+
}
|
|
2037
|
+
};
|
|
2038
|
+
const readFileStats = async (snapshot, file) => {
|
|
2039
|
+
const handle = await open$1(snapshotFile(snapshot, file), READ_FLAGS);
|
|
2040
|
+
try {
|
|
2041
|
+
const data = await handle.readFile();
|
|
2042
|
+
return {
|
|
2043
|
+
lines: data.reduce((count, byte) => count + Number(byte === 10), 0),
|
|
2044
|
+
bytes: data.length
|
|
2045
|
+
};
|
|
2046
|
+
} finally {
|
|
2047
|
+
await handle.close();
|
|
2048
|
+
}
|
|
2049
|
+
};
|
|
2050
|
+
//#endregion
|
|
2051
|
+
//#region src/export/account-config.ts
|
|
2052
|
+
const RESOURCE_KEYS = [
|
|
2053
|
+
"account_settings",
|
|
2054
|
+
"brands",
|
|
2055
|
+
"locales",
|
|
2056
|
+
"support_addresses",
|
|
2057
|
+
"schedules",
|
|
2058
|
+
"groups",
|
|
2059
|
+
"group_memberships",
|
|
2060
|
+
"custom_roles",
|
|
2061
|
+
"ticket_forms",
|
|
2062
|
+
"ticket_fields",
|
|
2063
|
+
"custom_statuses",
|
|
2064
|
+
"user_fields",
|
|
2065
|
+
"organization_fields",
|
|
2066
|
+
"triggers",
|
|
2067
|
+
"trigger_categories",
|
|
2068
|
+
"automations",
|
|
2069
|
+
"macros",
|
|
2070
|
+
"views",
|
|
2071
|
+
"sla_policies",
|
|
2072
|
+
"group_sla_policies",
|
|
2073
|
+
"webhooks",
|
|
2074
|
+
"targets",
|
|
2075
|
+
"dynamic_content",
|
|
2076
|
+
"satisfaction_reasons",
|
|
2077
|
+
"sharing_agreements",
|
|
2078
|
+
"app_installations",
|
|
2079
|
+
"routing_attributes",
|
|
2080
|
+
"custom_objects"
|
|
2081
|
+
];
|
|
2082
|
+
/**
|
|
2083
|
+
* The account-configuration catalog, in export order: what an admin sets up in
|
|
2084
|
+
* Admin Center, not the data that flows through it (tickets, users, articles).
|
|
2085
|
+
* The keys are this tool's own names, so a renamed endpoint changes one row here
|
|
2086
|
+
* and nothing a caller depends on.
|
|
2087
|
+
*/
|
|
2088
|
+
const CONFIG_RESOURCES = {
|
|
2089
|
+
account_settings: {
|
|
2090
|
+
path: "/account/settings",
|
|
2091
|
+
responseKey: "settings",
|
|
2092
|
+
single: true
|
|
2093
|
+
},
|
|
2094
|
+
brands: {
|
|
2095
|
+
path: "/brands",
|
|
2096
|
+
responseKey: "brands"
|
|
2097
|
+
},
|
|
2098
|
+
locales: {
|
|
2099
|
+
path: "/locales",
|
|
2100
|
+
responseKey: "locales"
|
|
2101
|
+
},
|
|
2102
|
+
support_addresses: {
|
|
2103
|
+
path: "/recipient_addresses",
|
|
2104
|
+
responseKey: "recipient_addresses"
|
|
2105
|
+
},
|
|
2106
|
+
schedules: {
|
|
2107
|
+
path: "/business_hours/schedules",
|
|
2108
|
+
responseKey: "schedules",
|
|
2109
|
+
nested: {
|
|
2110
|
+
attachAs: "holidays",
|
|
2111
|
+
idField: "id",
|
|
2112
|
+
path: (id) => `/business_hours/schedules/${id}/holidays`,
|
|
2113
|
+
responseKey: "holidays"
|
|
2114
|
+
}
|
|
2115
|
+
},
|
|
2116
|
+
groups: {
|
|
2117
|
+
path: "/groups",
|
|
2118
|
+
responseKey: "groups"
|
|
2119
|
+
},
|
|
2120
|
+
group_memberships: {
|
|
2121
|
+
path: "/group_memberships",
|
|
2122
|
+
responseKey: "group_memberships"
|
|
2123
|
+
},
|
|
2124
|
+
custom_roles: {
|
|
2125
|
+
path: "/custom_roles",
|
|
2126
|
+
responseKey: "custom_roles"
|
|
2127
|
+
},
|
|
2128
|
+
ticket_forms: {
|
|
2129
|
+
path: "/ticket_forms",
|
|
2130
|
+
responseKey: "ticket_forms"
|
|
2131
|
+
},
|
|
2132
|
+
ticket_fields: {
|
|
2133
|
+
path: "/ticket_fields",
|
|
2134
|
+
responseKey: "ticket_fields"
|
|
2135
|
+
},
|
|
2136
|
+
custom_statuses: {
|
|
2137
|
+
path: "/custom_statuses",
|
|
2138
|
+
responseKey: "custom_statuses"
|
|
2139
|
+
},
|
|
2140
|
+
user_fields: {
|
|
2141
|
+
path: "/user_fields",
|
|
2142
|
+
responseKey: "user_fields"
|
|
2143
|
+
},
|
|
2144
|
+
organization_fields: {
|
|
2145
|
+
path: "/organization_fields",
|
|
2146
|
+
responseKey: "organization_fields"
|
|
2147
|
+
},
|
|
2148
|
+
triggers: {
|
|
2149
|
+
path: "/triggers",
|
|
2150
|
+
responseKey: "triggers"
|
|
2151
|
+
},
|
|
2152
|
+
trigger_categories: {
|
|
2153
|
+
path: "/trigger_categories",
|
|
2154
|
+
responseKey: "trigger_categories"
|
|
2155
|
+
},
|
|
2156
|
+
automations: {
|
|
2157
|
+
path: "/automations",
|
|
2158
|
+
responseKey: "automations"
|
|
2159
|
+
},
|
|
2160
|
+
macros: {
|
|
2161
|
+
path: "/macros",
|
|
2162
|
+
responseKey: "macros",
|
|
2163
|
+
params: { include: "usage_7d,usage_30d" }
|
|
2164
|
+
},
|
|
2165
|
+
views: {
|
|
2166
|
+
path: "/views",
|
|
2167
|
+
responseKey: "views"
|
|
2168
|
+
},
|
|
2169
|
+
sla_policies: {
|
|
2170
|
+
path: "/slas/policies",
|
|
2171
|
+
responseKey: "sla_policies"
|
|
2172
|
+
},
|
|
2173
|
+
group_sla_policies: {
|
|
2174
|
+
path: "/group_slas/policies",
|
|
2175
|
+
responseKey: "group_sla_policies"
|
|
2176
|
+
},
|
|
2177
|
+
webhooks: {
|
|
2178
|
+
path: "/webhooks",
|
|
2179
|
+
responseKey: "webhooks"
|
|
2180
|
+
},
|
|
2181
|
+
targets: {
|
|
2182
|
+
path: "/targets",
|
|
2183
|
+
responseKey: "targets"
|
|
2184
|
+
},
|
|
2185
|
+
dynamic_content: {
|
|
2186
|
+
path: "/dynamic_content/items",
|
|
2187
|
+
responseKey: "items"
|
|
2188
|
+
},
|
|
2189
|
+
satisfaction_reasons: {
|
|
2190
|
+
path: "/satisfaction_reasons",
|
|
2191
|
+
responseKey: "reasons"
|
|
2192
|
+
},
|
|
2193
|
+
sharing_agreements: {
|
|
2194
|
+
path: "/sharing_agreements",
|
|
2195
|
+
responseKey: "sharing_agreements"
|
|
2196
|
+
},
|
|
2197
|
+
app_installations: {
|
|
2198
|
+
path: "/apps/installations",
|
|
2199
|
+
responseKey: "installations"
|
|
2200
|
+
},
|
|
2201
|
+
routing_attributes: {
|
|
2202
|
+
path: "/routing/attributes",
|
|
2203
|
+
responseKey: "attributes",
|
|
2204
|
+
nested: {
|
|
2205
|
+
attachAs: "values",
|
|
2206
|
+
idField: "id",
|
|
2207
|
+
path: (id) => `/routing/attributes/${id}/values`,
|
|
2208
|
+
responseKey: "attribute_values"
|
|
2209
|
+
}
|
|
2210
|
+
},
|
|
2211
|
+
custom_objects: {
|
|
2212
|
+
path: "/custom_objects",
|
|
2213
|
+
responseKey: "custom_objects",
|
|
2214
|
+
nested: {
|
|
2215
|
+
attachAs: "fields",
|
|
2216
|
+
idField: "key",
|
|
2217
|
+
path: (key) => `/custom_objects/${key}/fields`,
|
|
2218
|
+
responseKey: "custom_object_fields"
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
};
|
|
2222
|
+
const CURSOR_FIRST_PAGE = { "page[size]": "100" };
|
|
2223
|
+
/**
|
|
2224
|
+
* The request for the next page, or `undefined` on the last one. Zendesk pages a
|
|
2225
|
+
* configuration list either by offset (`next_page`) or by cursor (`meta.has_more`
|
|
2226
|
+
* + `links.next`); whichever the endpoint answered with is followed. The link
|
|
2227
|
+
* comes from the response, so it is followed only on this account's API: any
|
|
2228
|
+
* other target would receive the bearer token.
|
|
2229
|
+
*/
|
|
2230
|
+
const nextRequest = (body, subdomain) => {
|
|
2231
|
+
const meta = body["meta"];
|
|
2232
|
+
const links = body["links"];
|
|
2233
|
+
const link = meta?.has_more === true && typeof links?.next === "string" ? links.next : body["next_page"];
|
|
2234
|
+
if (typeof link !== "string") return void 0;
|
|
2235
|
+
const url = new URL(link);
|
|
2236
|
+
const api = new URL(getBaseUrl(subdomain));
|
|
2237
|
+
if (url.origin !== api.origin || !url.pathname.startsWith(`${api.pathname}/`)) throw new Error(`Refusing to follow a pagination link outside this account API: ${url.origin}${url.pathname}`);
|
|
2238
|
+
return {
|
|
2239
|
+
path: url.pathname.slice(api.pathname.length),
|
|
2240
|
+
params: Object.fromEntries(url.searchParams)
|
|
2241
|
+
};
|
|
2242
|
+
};
|
|
2243
|
+
const PAGE_FRAME_KEYS = /* @__PURE__ */ new Set([
|
|
2244
|
+
"meta",
|
|
2245
|
+
"links",
|
|
2246
|
+
"next_page",
|
|
2247
|
+
"previous_page",
|
|
2248
|
+
"count"
|
|
2249
|
+
]);
|
|
2250
|
+
/**
|
|
2251
|
+
* The resource carried by one response: its documented key, or else the one list
|
|
2252
|
+
* the response holds, so an endpoint whose key differs from the catalog still
|
|
2253
|
+
* exports instead of failing on a name.
|
|
2254
|
+
*/
|
|
2255
|
+
const extractItems = (body, responseKey) => {
|
|
2256
|
+
if (responseKey in body) return body[responseKey];
|
|
2257
|
+
const lists = Object.entries(body).filter(([key, value]) => !PAGE_FRAME_KEYS.has(key) && Array.isArray(value));
|
|
2258
|
+
const [only] = lists;
|
|
2259
|
+
if (lists.length === 1 && only) return only[1];
|
|
2260
|
+
throw new Error(`Unexpected response shape: no "${responseKey}" list (keys: ${Object.keys(body).join(", ")})`);
|
|
2261
|
+
};
|
|
2262
|
+
/** `<subdomain>-<UTC timestamp>`, safe as a folder name on every OS. */
|
|
2263
|
+
const snapshotDirName = (subdomain, at) => `${subdomain.replace(/[^a-z0-9-]/gi, "_")}-${at.toISOString().replace(/[:.]/g, "-")}`;
|
|
2264
|
+
const listOf = (body, responseKey) => {
|
|
2265
|
+
const items = extractItems(body, responseKey);
|
|
2266
|
+
if (!Array.isArray(items)) throw new Error(`Unexpected response shape: "${responseKey}" is not a list`);
|
|
2267
|
+
return items;
|
|
2268
|
+
};
|
|
2269
|
+
const firstPage = async (subdomain, token, path, request = zendeskGet, params = {}) => {
|
|
2270
|
+
try {
|
|
2271
|
+
return await request(subdomain, token, path, {
|
|
2272
|
+
...CURSOR_FIRST_PAGE,
|
|
2273
|
+
...params
|
|
2274
|
+
});
|
|
2275
|
+
} catch (err) {
|
|
2276
|
+
if (!(err instanceof ZendeskApiError && err.status === 400)) throw err;
|
|
2277
|
+
return request(subdomain, token, path, params);
|
|
2278
|
+
}
|
|
2279
|
+
};
|
|
2280
|
+
const fetchList = async (subdomain, token, path, responseKey, request = zendeskGet, params = {}) => {
|
|
2281
|
+
const items = [];
|
|
2282
|
+
let body = await firstPage(subdomain, token, path, request, params);
|
|
2283
|
+
for (let pages = 1;; pages += 1) {
|
|
2284
|
+
items.push(...listOf(body, responseKey));
|
|
2285
|
+
const next = nextRequest(body, subdomain);
|
|
2286
|
+
if (next === void 0) return {
|
|
2287
|
+
items,
|
|
2288
|
+
truncated: false
|
|
2289
|
+
};
|
|
2290
|
+
if (pages >= 200) return {
|
|
2291
|
+
items,
|
|
2292
|
+
truncated: true
|
|
2293
|
+
};
|
|
2294
|
+
body = await request(subdomain, token, next.path, {
|
|
2295
|
+
...next.params,
|
|
2296
|
+
...params
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2299
|
+
};
|
|
2300
|
+
const attachNested = async (subdomain, token, parents, nested, request = zendeskGet) => {
|
|
2301
|
+
const result = {
|
|
2302
|
+
items: [],
|
|
2303
|
+
truncated: false,
|
|
2304
|
+
missing: 0
|
|
2305
|
+
};
|
|
2306
|
+
for (const parent of parents) {
|
|
2307
|
+
const id = parent[nested.idField];
|
|
2308
|
+
if (typeof id !== "string" && typeof id !== "number") {
|
|
2309
|
+
result.items.push(parent);
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2312
|
+
try {
|
|
2313
|
+
const child = await fetchList(subdomain, token, nested.path(encodeURIComponent(String(id))), nested.responseKey, request);
|
|
2314
|
+
result.truncated ||= child.truncated;
|
|
2315
|
+
result.items.push({
|
|
2316
|
+
...parent,
|
|
2317
|
+
[nested.attachAs]: child.items
|
|
2318
|
+
});
|
|
2319
|
+
} catch (err) {
|
|
2320
|
+
if (!(err instanceof Error) || err instanceof ZendeskApiError && err.status === 401) throw err;
|
|
2321
|
+
result.missing += 1;
|
|
2322
|
+
result.missingReason ??= `${nested.attachAs} of ${nested.idField} ${String(id)}: ${err.message}`;
|
|
2323
|
+
result.items.push(parent);
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
return result;
|
|
2327
|
+
};
|
|
2328
|
+
const fetchResource = async (subdomain, token, spec, request = zendeskGet) => {
|
|
2329
|
+
if (spec.single) {
|
|
2330
|
+
const body = await request(subdomain, token, spec.path);
|
|
2331
|
+
return {
|
|
2332
|
+
data: extractItems(body, spec.responseKey),
|
|
2333
|
+
items: 1,
|
|
2334
|
+
truncated: false,
|
|
2335
|
+
nestedMissing: 0
|
|
2336
|
+
};
|
|
2337
|
+
}
|
|
2338
|
+
const list = await fetchList(subdomain, token, spec.path, spec.responseKey, request, spec.params);
|
|
2339
|
+
const result = spec.nested ? await attachNested(subdomain, token, list.items, spec.nested, request) : {
|
|
2340
|
+
items: list.items,
|
|
2341
|
+
truncated: false,
|
|
2342
|
+
missing: 0
|
|
2343
|
+
};
|
|
2344
|
+
return {
|
|
2345
|
+
data: result.items,
|
|
2346
|
+
items: result.items.length,
|
|
2347
|
+
truncated: list.truncated || result.truncated,
|
|
2348
|
+
nestedMissing: result.missing,
|
|
2349
|
+
nestedReason: result.missingReason
|
|
2350
|
+
};
|
|
2351
|
+
};
|
|
2352
|
+
const zendeskErrorCode = (body) => {
|
|
2353
|
+
try {
|
|
2354
|
+
const parsed = JSON.parse(body);
|
|
2355
|
+
return typeof parsed.error === "string" ? parsed.error : void 0;
|
|
2356
|
+
} catch {
|
|
2357
|
+
return;
|
|
2358
|
+
}
|
|
2359
|
+
};
|
|
2360
|
+
const skipReason = (err) => {
|
|
2361
|
+
const code = zendeskErrorCode(err.body);
|
|
2362
|
+
const status = `${err.status}${code ? ` ${code}` : ""}`;
|
|
2363
|
+
return err.status === 403 ? `No access (${status}): the signed-in account cannot read it, or the plan lacks it.` : `Not available on this account (${status}): the feature is off or not on this plan, or the endpoint does not exist.`;
|
|
2364
|
+
};
|
|
2365
|
+
const tokenStillWorks = async (subdomain, token, request = zendeskGet) => {
|
|
2366
|
+
try {
|
|
2367
|
+
await request(subdomain, token, "/users/me");
|
|
2368
|
+
return true;
|
|
2369
|
+
} catch (err) {
|
|
2370
|
+
return !(err instanceof ZendeskApiError && err.status === 401);
|
|
2371
|
+
}
|
|
2372
|
+
};
|
|
2373
|
+
/**
|
|
2374
|
+
* Export one resource. A resource the account cannot read (403) or that is not
|
|
2375
|
+
* available on it (404) is skipped, and any other failure recorded, so one gap
|
|
2376
|
+
* never costs the rest of the export. A 401 is rethrown only when the token
|
|
2377
|
+
* itself is rejected, so the server's backstop can recover it.
|
|
2378
|
+
*/
|
|
2379
|
+
const exportResource = async (ctx, snapshot, resource, request = zendeskGet) => {
|
|
2380
|
+
const spec = CONFIG_RESOURCES[resource];
|
|
2381
|
+
const endpoint = `/api/v2${spec.path}`;
|
|
2382
|
+
const token = await ctx.getToken();
|
|
2383
|
+
try {
|
|
2384
|
+
const fetched = await fetchResource(ctx.subdomain, token, spec, request);
|
|
2385
|
+
const file = `${resource}.json`;
|
|
2386
|
+
await writeJsonFile(join(snapshot, file), fetched.data);
|
|
2387
|
+
return {
|
|
2388
|
+
resource,
|
|
2389
|
+
endpoint,
|
|
2390
|
+
status: "exported",
|
|
2391
|
+
items: fetched.items,
|
|
2392
|
+
file,
|
|
2393
|
+
truncated: fetched.truncated,
|
|
2394
|
+
...fetched.nestedMissing > 0 && {
|
|
2395
|
+
nested_missing: fetched.nestedMissing,
|
|
2396
|
+
nested_missing_reason: fetched.nestedReason ?? ""
|
|
2397
|
+
}
|
|
2398
|
+
};
|
|
2399
|
+
} catch (err) {
|
|
2400
|
+
if (!(err instanceof Error)) throw err;
|
|
2401
|
+
if (err instanceof ZendeskApiError && err.status === 401) {
|
|
2402
|
+
if (!await tokenStillWorks(ctx.subdomain, token, request)) throw err;
|
|
2403
|
+
return {
|
|
2404
|
+
resource,
|
|
2405
|
+
endpoint,
|
|
2406
|
+
status: "failed",
|
|
2407
|
+
reason: "Unauthorized (401) on this endpoint, while /users/me does not reject the token."
|
|
2408
|
+
};
|
|
2409
|
+
}
|
|
2410
|
+
if (err instanceof ZendeskApiError && (err.status === 403 || err.status === 404)) return {
|
|
2411
|
+
resource,
|
|
2412
|
+
endpoint,
|
|
2413
|
+
status: "skipped",
|
|
2414
|
+
reason: skipReason(err)
|
|
2415
|
+
};
|
|
2416
|
+
return {
|
|
2417
|
+
resource,
|
|
2418
|
+
endpoint,
|
|
2419
|
+
status: "failed",
|
|
2420
|
+
reason: err.message
|
|
2421
|
+
};
|
|
2422
|
+
}
|
|
2423
|
+
};
|
|
2424
|
+
const REASON_CELL_MAX = 160;
|
|
2425
|
+
const WHITESPACE_RUN = /\s+/g;
|
|
2426
|
+
const PIPE = /\|/g;
|
|
2427
|
+
const reasonCell = (reason) => {
|
|
2428
|
+
const line = reason.replace(WHITESPACE_RUN, " ").replace(PIPE, "/").trim();
|
|
2429
|
+
return line.length > REASON_CELL_MAX ? `${line.slice(0, 159)}…` : line;
|
|
2430
|
+
};
|
|
2431
|
+
const statusCell = (entry) => {
|
|
2432
|
+
if (entry.status === "exported") {
|
|
2433
|
+
const notes = [...entry.truncated ? ["truncated"] : [], ...entry.nested_missing ? [`${entry.nested_missing} nested lists missing`] : []];
|
|
2434
|
+
return notes.length > 0 ? `exported (${notes.join(", ")})` : "exported";
|
|
2435
|
+
}
|
|
2436
|
+
return `${entry.status}: ${reasonCell(entry.reason)}`;
|
|
2437
|
+
};
|
|
2438
|
+
const formatSummary = (subdomain, snapshot, entries) => {
|
|
2439
|
+
const count = (status) => entries.filter((e) => e.status === status).length;
|
|
2440
|
+
const rows = entries.map((e) => `| ${e.resource} | ${statusCell(e)} | ${e.status === "exported" ? e.items : "-"} |`);
|
|
2441
|
+
return [
|
|
2442
|
+
`Exported the ${subdomain} account configuration to ${snapshot}`,
|
|
2443
|
+
`${count("exported")} exported, ${count("skipped")} skipped, ${count("failed")} failed. manifest.json has the per-resource details; each <resource>.json holds the raw Zendesk objects.`,
|
|
2444
|
+
"",
|
|
2445
|
+
"| Resource | Status | Items |",
|
|
2446
|
+
"|---|---|---|",
|
|
2447
|
+
...rows
|
|
2448
|
+
].join("\n");
|
|
2449
|
+
};
|
|
2450
|
+
const writeManifest = (snapshot, manifest) => writeManifest$1(snapshot, manifest);
|
|
2451
|
+
/** Export configuration into an already reserved directory. */
|
|
2452
|
+
const runConfigExport = async (ctx, snapshot, selected = RESOURCE_KEYS, request = zendeskGet, exportedAt = /* @__PURE__ */ new Date(), onResource, priorEntries = []) => {
|
|
2453
|
+
const manifest = {
|
|
2454
|
+
subdomain: ctx.subdomain,
|
|
2455
|
+
exported_at: exportedAt.toISOString()
|
|
2456
|
+
};
|
|
2457
|
+
const entries = [...priorEntries];
|
|
2458
|
+
try {
|
|
2459
|
+
for (const resource of selected) {
|
|
2460
|
+
const entry = await exportResource(ctx, snapshot, resource, request);
|
|
2461
|
+
entries.push(entry);
|
|
2462
|
+
await writeManifest(snapshot, {
|
|
2463
|
+
...manifest,
|
|
2464
|
+
resources: entries
|
|
2465
|
+
});
|
|
2466
|
+
await onResource?.(entry);
|
|
2467
|
+
}
|
|
2468
|
+
} catch (err) {
|
|
2469
|
+
const aborted = err instanceof Error ? err.message : String(err);
|
|
2470
|
+
await writeManifest(snapshot, {
|
|
2471
|
+
...manifest,
|
|
2472
|
+
aborted,
|
|
2473
|
+
resources: entries
|
|
2474
|
+
});
|
|
2475
|
+
throw err;
|
|
2476
|
+
}
|
|
2477
|
+
await writeManifest(snapshot, {
|
|
2478
|
+
...manifest,
|
|
2479
|
+
resources: entries
|
|
2480
|
+
});
|
|
2481
|
+
return entries;
|
|
2482
|
+
};
|
|
2483
|
+
//#endregion
|
|
2484
|
+
//#region src/export/goldens-snapshot.ts
|
|
2485
|
+
const pick = (row, keys) => Object.fromEntries(keys.filter((key) => key in row).map((key) => [key, row[key]]));
|
|
2486
|
+
const TICKET_FIELDS = [
|
|
2487
|
+
"id",
|
|
2488
|
+
"status",
|
|
2489
|
+
"priority",
|
|
2490
|
+
"group_id",
|
|
2491
|
+
"assignee_id",
|
|
2492
|
+
"requester_id",
|
|
2493
|
+
"organization_id",
|
|
2494
|
+
"ticket_form_id",
|
|
2495
|
+
"brand_id",
|
|
2496
|
+
"subject",
|
|
2497
|
+
"tags"
|
|
2498
|
+
];
|
|
2499
|
+
/** Streaming parse; large descriptions/comments never survive the current line. */
|
|
2500
|
+
const readRows = async (path, fields) => {
|
|
2501
|
+
const handle = await open$1(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
2502
|
+
const latest = /* @__PURE__ */ new Map();
|
|
2503
|
+
try {
|
|
2504
|
+
const lines = createInterface({
|
|
2505
|
+
input: handle.createReadStream({ autoClose: false }),
|
|
2506
|
+
crlfDelay: Number.POSITIVE_INFINITY
|
|
2507
|
+
});
|
|
2508
|
+
for await (const line of lines) {
|
|
2509
|
+
if (!line.trim()) continue;
|
|
2510
|
+
const row = JSON.parse(line);
|
|
2511
|
+
if (!Number.isSafeInteger(row.id) || row.id <= 0) throw new Error("Invalid snapshot record id");
|
|
2512
|
+
latest.set(row.id, fields ? pick(row, fields) : row);
|
|
2513
|
+
}
|
|
2514
|
+
} finally {
|
|
2515
|
+
await handle.close();
|
|
2516
|
+
}
|
|
2517
|
+
return [...latest.values()];
|
|
2518
|
+
};
|
|
2519
|
+
const readLatestTickets = (snapshot) => readRows(join(snapshot, "tickets.ndjson"), TICKET_FIELDS);
|
|
2520
|
+
const compare = (a, b) => {
|
|
2521
|
+
if (a < b) return -1;
|
|
2522
|
+
return a > b ? 1 : 0;
|
|
2523
|
+
};
|
|
2524
|
+
const stratifiedSample = (tickets, n, keyOf) => {
|
|
2525
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2526
|
+
for (const ticket of tickets) {
|
|
2527
|
+
if (ticket.status === "deleted") continue;
|
|
2528
|
+
const key = keyOf(ticket);
|
|
2529
|
+
const group = groups.get(key) ?? [];
|
|
2530
|
+
group.push(ticket);
|
|
2531
|
+
groups.set(key, group);
|
|
2532
|
+
}
|
|
2533
|
+
const strata = [...groups].sort(([a, x], [b, y]) => y.length - x.length || compare(a, b)).map(([, group]) => group.sort((a, b) => b.id - a.id));
|
|
2534
|
+
const result = [];
|
|
2535
|
+
for (let round = 0; result.length < n; round++) {
|
|
2536
|
+
let added = false;
|
|
2537
|
+
for (const group of strata) {
|
|
2538
|
+
const ticket = group[round];
|
|
2539
|
+
if (ticket && result.length < n) {
|
|
2540
|
+
result.push(ticket);
|
|
2541
|
+
added = true;
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
if (!added) break;
|
|
2545
|
+
}
|
|
2546
|
+
return result;
|
|
2547
|
+
};
|
|
2548
|
+
const macroKey = (t) => JSON.stringify([
|
|
2549
|
+
t["status"],
|
|
2550
|
+
t["ticket_form_id"],
|
|
2551
|
+
t["brand_id"]
|
|
2552
|
+
]);
|
|
2553
|
+
const slaKey = (t) => JSON.stringify([
|
|
2554
|
+
t["status"],
|
|
2555
|
+
t["priority"],
|
|
2556
|
+
t["group_id"],
|
|
2557
|
+
t["ticket_form_id"]
|
|
2558
|
+
]);
|
|
2559
|
+
const slaSample = (tickets, n) => [...stratifiedSample(tickets.filter((t) => t["status"] !== "closed"), n - Math.floor(n * .1), slaKey), ...stratifiedSample(tickets.filter((t) => t["status"] === "closed"), Math.floor(n * .1), slaKey)];
|
|
2560
|
+
const STOPWORDS = /* @__PURE__ */ new Set([
|
|
2561
|
+
"sobre",
|
|
2562
|
+
"entre",
|
|
2563
|
+
"ainda",
|
|
2564
|
+
"todos",
|
|
2565
|
+
"todas",
|
|
2566
|
+
"pelos",
|
|
2567
|
+
"pelas",
|
|
2568
|
+
"quando",
|
|
2569
|
+
"muito",
|
|
2570
|
+
"muita",
|
|
2571
|
+
"minha",
|
|
2572
|
+
"tenho",
|
|
2573
|
+
"estou",
|
|
2574
|
+
"favor",
|
|
2575
|
+
"obrigado",
|
|
2576
|
+
"obrigada",
|
|
2577
|
+
"preciso",
|
|
2578
|
+
"poderia",
|
|
2579
|
+
"porque",
|
|
2580
|
+
"depois",
|
|
2581
|
+
"antes",
|
|
2582
|
+
"mesmo",
|
|
2583
|
+
"mesma",
|
|
2584
|
+
"nosso",
|
|
2585
|
+
"nossa",
|
|
2586
|
+
"vocês"
|
|
2587
|
+
]);
|
|
2588
|
+
const quote = (text) => `"${text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
2589
|
+
const ranked = (values, n) => {
|
|
2590
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2591
|
+
for (const valuesOfTicket of values) for (const key of new Set(valuesOfTicket)) counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
2592
|
+
return [...counts].sort(([a, x], [b, y]) => y - x || compare(a, b)).slice(0, n).map(([key]) => key);
|
|
2593
|
+
};
|
|
2594
|
+
const buildSearchQueries = (stats, now) => {
|
|
2595
|
+
const tickets = stats.tickets.filter((t) => t["status"] !== "deleted");
|
|
2596
|
+
const terms = [
|
|
2597
|
+
"status:new",
|
|
2598
|
+
"status:open",
|
|
2599
|
+
"status:pending",
|
|
2600
|
+
"status:hold",
|
|
2601
|
+
"status:solved",
|
|
2602
|
+
"status:closed",
|
|
2603
|
+
"status<solved",
|
|
2604
|
+
"priority:urgent",
|
|
2605
|
+
"priority:high",
|
|
2606
|
+
"ticket_type:incident",
|
|
2607
|
+
"ticket_type:problem"
|
|
2608
|
+
];
|
|
2609
|
+
terms.push(...ranked(tickets.map((t) => Array.isArray(t["tags"]) ? t["tags"] : []), 5).map((tag) => `tags:${tag}`));
|
|
2610
|
+
const names = (field, rows, property, count) => {
|
|
2611
|
+
const lookup = new Map(rows.map((row) => [row.id, row[property]]));
|
|
2612
|
+
return ranked(tickets.map((t) => {
|
|
2613
|
+
const name = lookup.get(t[field]);
|
|
2614
|
+
return typeof name === "string" && name ? [name] : [];
|
|
2615
|
+
}), count);
|
|
2616
|
+
};
|
|
2617
|
+
const groups = names("group_id", stats.groups, "name", 4);
|
|
2618
|
+
terms.push(...groups.map((name) => `group:${quote(name)}`));
|
|
2619
|
+
for (const [field, rows, property, count, prefix, quoted] of [
|
|
2620
|
+
[
|
|
2621
|
+
"assignee_id",
|
|
2622
|
+
stats.users,
|
|
2623
|
+
"email",
|
|
2624
|
+
4,
|
|
2625
|
+
"assignee",
|
|
2626
|
+
false
|
|
2627
|
+
],
|
|
2628
|
+
[
|
|
2629
|
+
"requester_id",
|
|
2630
|
+
stats.users,
|
|
2631
|
+
"email",
|
|
2632
|
+
3,
|
|
2633
|
+
"requester",
|
|
2634
|
+
false
|
|
2635
|
+
],
|
|
2636
|
+
[
|
|
2637
|
+
"organization_id",
|
|
2638
|
+
stats.organizations,
|
|
2639
|
+
"name",
|
|
2640
|
+
3,
|
|
2641
|
+
"organization",
|
|
2642
|
+
true
|
|
2643
|
+
],
|
|
2644
|
+
[
|
|
2645
|
+
"brand_id",
|
|
2646
|
+
stats.brands,
|
|
2647
|
+
"name",
|
|
2648
|
+
2,
|
|
2649
|
+
"brand",
|
|
2650
|
+
true
|
|
2651
|
+
],
|
|
2652
|
+
[
|
|
2653
|
+
"ticket_form_id",
|
|
2654
|
+
stats.forms,
|
|
2655
|
+
"name",
|
|
2656
|
+
2,
|
|
2657
|
+
"form",
|
|
2658
|
+
true
|
|
2659
|
+
]
|
|
2660
|
+
]) terms.push(...names(field, rows, property, count).map((name) => `${prefix}:${quoted ? quote(name) : name}`));
|
|
2661
|
+
const date = (days) => (/* @__PURE__ */ new Date(now.getTime() - days * 864e5)).toISOString().slice(0, 10);
|
|
2662
|
+
terms.push(`created>=${date(30)}`, `updated<${date(365)}`, `solved>=${date(30)}`, "status<solved assignee:none");
|
|
2663
|
+
if (groups[0]) terms.push(`status:open group:${quote(groups[0])}`);
|
|
2664
|
+
const words = tickets.map((t) => String(t["subject"] ?? "").toLowerCase().match(/\p{L}{5,}/gu)?.filter((word) => !STOPWORDS.has(word)) ?? []);
|
|
2665
|
+
const top = ranked(words, 6);
|
|
2666
|
+
terms.push(...top);
|
|
2667
|
+
terms.push(...top.filter((word) => word.normalize("NFD").replace(/\p{M}/gu, "") !== word).slice(0, 3).map((word) => word.normalize("NFD").replace(/\p{M}/gu, "")));
|
|
2668
|
+
const bigram = ranked(words.map((list) => list.slice(1).map((word, index) => `${list[index]} ${word}`)), 1)[0];
|
|
2669
|
+
if (bigram) terms.push(quote(bigram));
|
|
2670
|
+
return terms.map((term, index) => ({
|
|
2671
|
+
id: `generated-${String(index + 1).padStart(2, "0")}`,
|
|
2672
|
+
query: `type:ticket ${term}`
|
|
2673
|
+
}));
|
|
2674
|
+
};
|
|
2675
|
+
//#endregion
|
|
2676
|
+
//#region src/export/rate-limit.ts
|
|
2677
|
+
const defaultDeps = {
|
|
2678
|
+
now: Date.now,
|
|
2679
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
2680
|
+
};
|
|
2681
|
+
const resolveRates = (rates = {}) => {
|
|
2682
|
+
const resolved = {
|
|
2683
|
+
incremental: 10,
|
|
2684
|
+
standard: 300,
|
|
2685
|
+
...rates
|
|
2686
|
+
};
|
|
2687
|
+
for (const rate of Object.values(resolved)) if (!Number.isFinite(rate) || rate <= 0) throw new Error("Rates must be positive finite requests per minute");
|
|
2688
|
+
return resolved;
|
|
2689
|
+
};
|
|
2690
|
+
const retryKind = (err) => {
|
|
2691
|
+
if (err instanceof ZendeskApiError && err.status === 429) return "throttle";
|
|
2692
|
+
if (isZendeskNetworkError(err) || err instanceof ZendeskApiError && err.status >= 500) return "error";
|
|
2693
|
+
};
|
|
2694
|
+
/** Sequential reservations also pace concurrent callers in the same bucket. */
|
|
2695
|
+
const createPacedGet = (subdomain, getToken, rates = {}, deps = defaultDeps, onRequest = () => void 0) => {
|
|
2696
|
+
const limits = resolveRates(rates);
|
|
2697
|
+
const next = {
|
|
2698
|
+
incremental: 0,
|
|
2699
|
+
standard: 0
|
|
2700
|
+
};
|
|
2701
|
+
const pace = async (bucket) => {
|
|
2702
|
+
const at = Math.max(deps.now(), next[bucket]);
|
|
2703
|
+
next[bucket] = at + 6e4 / limits[bucket];
|
|
2704
|
+
const wait = at - deps.now();
|
|
2705
|
+
if (wait > 0) await deps.sleep(wait);
|
|
2706
|
+
};
|
|
2707
|
+
const delays = {
|
|
2708
|
+
throttle: deps.rateLimitWaitMs ?? 6e4,
|
|
2709
|
+
error: deps.errorWaitMs ?? 3e4
|
|
2710
|
+
};
|
|
2711
|
+
const maximum = {
|
|
2712
|
+
throttle: 10,
|
|
2713
|
+
error: 5
|
|
2714
|
+
};
|
|
2715
|
+
const pacedGet = async (bucket, path, params) => {
|
|
2716
|
+
const retries = {
|
|
2717
|
+
throttle: 0,
|
|
2718
|
+
error: 0
|
|
2719
|
+
};
|
|
2720
|
+
while (true) {
|
|
2721
|
+
const token = await getToken();
|
|
2722
|
+
await pace(bucket);
|
|
2723
|
+
onRequest();
|
|
2724
|
+
try {
|
|
2725
|
+
return await zendeskGet(subdomain, token, path, params);
|
|
2726
|
+
} catch (err) {
|
|
2727
|
+
const kind = retryKind(err);
|
|
2728
|
+
if (!kind || retries[kind] >= maximum[kind]) throw err;
|
|
2729
|
+
retries[kind] += 1;
|
|
2730
|
+
await deps.sleep(delays[kind]);
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
};
|
|
2734
|
+
return pacedGet;
|
|
2735
|
+
};
|
|
2736
|
+
//#endregion
|
|
2737
|
+
//#region src/export/goldens.ts
|
|
2738
|
+
const GOLDEN_STAGES = [
|
|
2739
|
+
"context",
|
|
2740
|
+
"views",
|
|
2741
|
+
"macros",
|
|
2742
|
+
"slas",
|
|
2743
|
+
"search"
|
|
2744
|
+
];
|
|
2745
|
+
const SUBDOMAIN$2 = /^[a-z0-9][a-z0-9-]*$/i;
|
|
2746
|
+
const NETWORK_PATH$2 = /^[\\/]{2}/;
|
|
2747
|
+
const TICKET_ID_KEY = /^\d+$/;
|
|
2748
|
+
const ITEM_FILE = /^(views|macros)\/\d+\.json$/;
|
|
2749
|
+
const FIXED_FILES = /* @__PURE__ */ new Set([
|
|
2750
|
+
"context.json",
|
|
2751
|
+
"views/index.json",
|
|
2752
|
+
"macros/index.json",
|
|
2753
|
+
"macros/sample.json",
|
|
2754
|
+
"slas/sample.json",
|
|
2755
|
+
"slas.ndjson",
|
|
2756
|
+
"audits.ndjson",
|
|
2757
|
+
"search.json"
|
|
2758
|
+
]);
|
|
2759
|
+
const settingsFor = (options) => {
|
|
2760
|
+
const settings = {
|
|
2761
|
+
viewRowCap: 2e3,
|
|
2762
|
+
macroSample: 25,
|
|
2763
|
+
slaSample: 500,
|
|
2764
|
+
...options
|
|
2765
|
+
};
|
|
2766
|
+
for (const key of [
|
|
2767
|
+
"viewRowCap",
|
|
2768
|
+
"macroSample",
|
|
2769
|
+
"slaSample"
|
|
2770
|
+
]) if (!Number.isSafeInteger(settings[key]) || settings[key] < 0) throw new Error(`${key} must be a nonnegative integer`);
|
|
2771
|
+
if (settings.queries !== void 0 && (!isAbsolute(settings.queries) || NETWORK_PATH$2.test(settings.queries))) throw new Error("Queries must be an absolute local JSON file");
|
|
2772
|
+
return settings;
|
|
2773
|
+
};
|
|
2774
|
+
const validateOptions$1 = (options) => {
|
|
2775
|
+
if (!SUBDOMAIN$2.test(options.subdomain)) throw new Error("Invalid Zendesk subdomain");
|
|
2776
|
+
if (!isAbsolute(options.exportDir) || NETWORK_PATH$2.test(options.exportDir)) throw new Error("Export directory must be an absolute local path");
|
|
2777
|
+
validateResumeName(options.dataSnapshot);
|
|
2778
|
+
if (options.resume !== void 0) validateResumeName(options.resume);
|
|
2779
|
+
if (options.stages && (!options.stages.length || options.stages.some((name) => !GOLDEN_STAGES.includes(name)))) throw new Error("Select one or more supported stages");
|
|
2780
|
+
settingsFor(options.options);
|
|
2781
|
+
resolveRates(options.rates);
|
|
2782
|
+
};
|
|
2783
|
+
const readJson = async (path) => {
|
|
2784
|
+
const handle = await open$1(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
2785
|
+
try {
|
|
2786
|
+
return JSON.parse(await handle.readFile("utf8"));
|
|
2787
|
+
} finally {
|
|
2788
|
+
await handle.close();
|
|
2789
|
+
}
|
|
2790
|
+
};
|
|
2791
|
+
const missing = (err) => err instanceof Error && "code" in err && err.code === "ENOENT";
|
|
2792
|
+
const readOptional = async (path) => {
|
|
2793
|
+
try {
|
|
2794
|
+
return await readJson(path);
|
|
2795
|
+
} catch (err) {
|
|
2796
|
+
if (!missing(err)) throw err;
|
|
2797
|
+
return;
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
2800
|
+
const rowsOf = (body, key) => {
|
|
2801
|
+
const rows = body[key];
|
|
2802
|
+
if (!Array.isArray(rows)) throw new Error(`Expected ${key} array`);
|
|
2803
|
+
return rows;
|
|
2804
|
+
};
|
|
2805
|
+
const sharedView = (v) => v["active"] === true && (v["restriction"] === null || v["restriction"]?.["type"] === "Group");
|
|
2806
|
+
const itemId = (row) => {
|
|
2807
|
+
if (!Number.isSafeInteger(row.id) || row.id <= 0) throw new Error("Invalid API item id");
|
|
2808
|
+
return row.id;
|
|
2809
|
+
};
|
|
2810
|
+
const failureStatus = (err) => err instanceof ZendeskApiError && (err.status === 403 || err.status === 404) ? "skipped" : "failed";
|
|
2811
|
+
const errorOf = (err) => ({
|
|
2812
|
+
status: err instanceof ZendeskApiError ? err.status : null,
|
|
2813
|
+
body: (err instanceof ZendeskApiError ? err.body : String(err)).slice(0, 500)
|
|
2814
|
+
});
|
|
2815
|
+
const validateManifest$1 = (manifest, options) => {
|
|
2816
|
+
if (manifest.kind !== "goldens" || manifest.subdomain !== options.subdomain || manifest.data_snapshot !== options.dataSnapshot || !manifest.stages || !manifest.options || !Number.isFinite(Date.parse(manifest.started_at))) throw new Error("Resume manifest does not belong to this golden snapshot");
|
|
2817
|
+
settingsFor(manifest.options);
|
|
2818
|
+
for (const [key, value] of Object.entries(options.options ?? {})) if (manifest.options[key] !== value) throw new Error("Options cannot change when resuming");
|
|
2819
|
+
if (options.stages?.some((name) => !manifest.stages[name])) throw new Error("Resume stages must exist in the original snapshot");
|
|
2820
|
+
validateStates(manifest.stages);
|
|
2821
|
+
};
|
|
2822
|
+
const validateStates = (stages) => {
|
|
2823
|
+
for (const [name, state] of Object.entries(stages)) {
|
|
2824
|
+
if (!GOLDEN_STAGES.includes(name) || !state?.files || ![
|
|
2825
|
+
"pending",
|
|
2826
|
+
"running",
|
|
2827
|
+
"done",
|
|
2828
|
+
"skipped",
|
|
2829
|
+
"failed"
|
|
2830
|
+
].includes(state.status)) throw new Error("Invalid stage checkpoint");
|
|
2831
|
+
for (const file of Object.keys(state.files)) if (!FIXED_FILES.has(file) && !ITEM_FILE.test(file)) throw new Error("Invalid checkpoint filename");
|
|
2832
|
+
}
|
|
2833
|
+
};
|
|
2834
|
+
/** Preparation reserves a snapshot with a durable manifest before a tool returns. */
|
|
2835
|
+
const prepareGoldens = async (options) => {
|
|
2836
|
+
validateOptions$1(options);
|
|
2837
|
+
const deps = options.deps ?? defaultDeps;
|
|
2838
|
+
const root = resolve(options.exportDir);
|
|
2839
|
+
const input = join(root, options.dataSnapshot);
|
|
2840
|
+
await safeDirectory(input, false);
|
|
2841
|
+
await options.getToken();
|
|
2842
|
+
let manifest;
|
|
2843
|
+
let snapshot = "";
|
|
2844
|
+
if (options.resume !== void 0) {
|
|
2845
|
+
snapshot = join(root, options.resume);
|
|
2846
|
+
await safeDirectory(snapshot, false);
|
|
2847
|
+
manifest = await readManifest(snapshot);
|
|
2848
|
+
validateManifest$1(manifest, options);
|
|
2849
|
+
}
|
|
2850
|
+
const requestState = {};
|
|
2851
|
+
const get = createPacedGet(options.subdomain, options.getToken, options.rates, deps, () => {
|
|
2852
|
+
if (requestState.current) requestState.current.requests++;
|
|
2853
|
+
});
|
|
2854
|
+
const me = await get("standard", "/users/me");
|
|
2855
|
+
const user = me["user"];
|
|
2856
|
+
if (!Number.isSafeInteger(user?.id)) throw new Error("Invalid current user response");
|
|
2857
|
+
if (!manifest) {
|
|
2858
|
+
const at = new Date(deps.now());
|
|
2859
|
+
snapshot = join(root, snapshotDirName(`${options.subdomain}-goldens`, at));
|
|
2860
|
+
await mkdir(snapshot, { mode: 448 });
|
|
2861
|
+
manifest = {
|
|
2862
|
+
subdomain: options.subdomain,
|
|
2863
|
+
kind: "goldens",
|
|
2864
|
+
data_snapshot: options.dataSnapshot,
|
|
2865
|
+
started_at: at.toISOString(),
|
|
2866
|
+
current_user: {
|
|
2867
|
+
id: user.id,
|
|
2868
|
+
role: user.role
|
|
2869
|
+
},
|
|
2870
|
+
options: settingsFor(options.options),
|
|
2871
|
+
stages: {}
|
|
2872
|
+
};
|
|
2873
|
+
for (const name of GOLDEN_STAGES.filter((stage) => options.stages?.includes(stage) ?? true)) manifest.stages[name] = {
|
|
2874
|
+
status: "pending",
|
|
2875
|
+
requests: 0,
|
|
2876
|
+
items: 0,
|
|
2877
|
+
files: {},
|
|
2878
|
+
started_at: at.toISOString(),
|
|
2879
|
+
duration_ms: 0
|
|
2880
|
+
};
|
|
2881
|
+
}
|
|
2882
|
+
if (manifest.current_user.id !== user.id) throw new Error("Resume requires the original recording user");
|
|
2883
|
+
delete manifest.finished_at;
|
|
2884
|
+
await writeManifest$1(snapshot, manifest);
|
|
2885
|
+
const prepared = manifest;
|
|
2886
|
+
return {
|
|
2887
|
+
snapshot,
|
|
2888
|
+
manifest,
|
|
2889
|
+
run: () => executeGoldens(options, snapshot, input, prepared, deps, get, requestState, me)
|
|
2890
|
+
};
|
|
2891
|
+
};
|
|
2892
|
+
const executeGoldens = async (options, snapshot, input, manifest, deps, get, requestState, me) => {
|
|
2893
|
+
const now = () => new Date(deps.now()).toISOString();
|
|
2894
|
+
const checkpoint = async () => {
|
|
2895
|
+
await writeManifest$1(snapshot, manifest);
|
|
2896
|
+
options.onProgress?.(manifest, snapshot);
|
|
2897
|
+
};
|
|
2898
|
+
const adapter = (_subdomain, _token, path, params) => get("standard", path, params);
|
|
2899
|
+
const request = (path, params) => get("standard", path, params);
|
|
2900
|
+
const checkAuth = async (err) => {
|
|
2901
|
+
if (!(err instanceof ZendeskApiError && err.status === 401)) return;
|
|
2902
|
+
try {
|
|
2903
|
+
await request("/users/me");
|
|
2904
|
+
} catch (authError) {
|
|
2905
|
+
if (authError instanceof ZendeskApiError && authError.status === 401) {
|
|
2906
|
+
await checkpoint();
|
|
2907
|
+
throw err;
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
};
|
|
2911
|
+
const safeResult = async (call) => {
|
|
2912
|
+
try {
|
|
2913
|
+
return { result: await call() };
|
|
2914
|
+
} catch (err) {
|
|
2915
|
+
await checkAuth(err);
|
|
2916
|
+
return { error: errorOf(err) };
|
|
2917
|
+
}
|
|
2918
|
+
};
|
|
2919
|
+
const write = async (state, file, value) => {
|
|
2920
|
+
const target = snapshotFile(snapshot, file);
|
|
2921
|
+
await safeDirectory(resolve(target, ".."), true);
|
|
2922
|
+
await writeJsonFile(`${target}.tmp`, value);
|
|
2923
|
+
await rename(`${target}.tmp`, target);
|
|
2924
|
+
state.files[file] = await readFileStats(snapshot, file);
|
|
2925
|
+
};
|
|
2926
|
+
const pages = async (path, consume) => {
|
|
2927
|
+
let body = await firstPage(options.subdomain, "", path, adapter);
|
|
2928
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2929
|
+
while (true) {
|
|
2930
|
+
const stop = consume(body);
|
|
2931
|
+
await checkpoint();
|
|
2932
|
+
if (stop) return;
|
|
2933
|
+
const next = nextRequest(body, options.subdomain);
|
|
2934
|
+
if (!next) return;
|
|
2935
|
+
const key = JSON.stringify(next);
|
|
2936
|
+
if (seen.has(key)) throw new Error("Pagination made no progress");
|
|
2937
|
+
seen.add(key);
|
|
2938
|
+
body = await request(next.path, next.params);
|
|
2939
|
+
}
|
|
2940
|
+
};
|
|
2941
|
+
const list = async (path, key) => {
|
|
2942
|
+
const collected = [];
|
|
2943
|
+
await pages(path, (body) => {
|
|
2944
|
+
collected.push(...rowsOf(body, key));
|
|
2945
|
+
});
|
|
2946
|
+
return collected;
|
|
2947
|
+
};
|
|
2948
|
+
let tickets;
|
|
2949
|
+
const getTickets = async () => {
|
|
2950
|
+
tickets ??= await readLatestTickets(input);
|
|
2951
|
+
return tickets;
|
|
2952
|
+
};
|
|
2953
|
+
const sample = async (state, stage) => {
|
|
2954
|
+
const file = `${stage}/sample.json`;
|
|
2955
|
+
const saved = await readOptional(snapshotFile(snapshot, file));
|
|
2956
|
+
if (saved) return saved;
|
|
2957
|
+
const all = await getTickets();
|
|
2958
|
+
const result = (stage === "macros" ? stratifiedSample(all, manifest.options.macroSample, macroKey) : slaSample(all, manifest.options.slaSample)).map((t) => ({
|
|
2959
|
+
ticket_id: t.id,
|
|
2960
|
+
stratum: stage === "macros" ? macroKey(t) : slaKey(t)
|
|
2961
|
+
}));
|
|
2962
|
+
await write(state, file, result);
|
|
2963
|
+
await checkpoint();
|
|
2964
|
+
return result;
|
|
2965
|
+
};
|
|
2966
|
+
const recordViews = async (state) => {
|
|
2967
|
+
await safeDirectory(join(snapshot, "views"), true);
|
|
2968
|
+
const views = (await list("/views", "views")).filter(sharedView);
|
|
2969
|
+
const index = [];
|
|
2970
|
+
for (const view of views) {
|
|
2971
|
+
const id = itemId(view);
|
|
2972
|
+
const file = `views/${id}.json`;
|
|
2973
|
+
let observation = await readOptional(snapshotFile(snapshot, file));
|
|
2974
|
+
if (!observation) {
|
|
2975
|
+
observation = {
|
|
2976
|
+
view: pick(view, [
|
|
2977
|
+
"id",
|
|
2978
|
+
"title",
|
|
2979
|
+
"restriction",
|
|
2980
|
+
"conditions",
|
|
2981
|
+
"execution",
|
|
2982
|
+
"position"
|
|
2983
|
+
]),
|
|
2984
|
+
started_at: now()
|
|
2985
|
+
};
|
|
2986
|
+
try {
|
|
2987
|
+
observation["count"] = await request(`/views/${id}/count`);
|
|
2988
|
+
const ticketIds = (await list(`/views/${id}/tickets`, "tickets")).map((t) => t.id);
|
|
2989
|
+
observation["ticket_ids"] = ticketIds;
|
|
2990
|
+
observation["ticket_ids_total"] = ticketIds.length;
|
|
2991
|
+
const rows = [];
|
|
2992
|
+
let columns = [];
|
|
2993
|
+
let groups = [];
|
|
2994
|
+
let total;
|
|
2995
|
+
let truncated = false;
|
|
2996
|
+
await pages(`/views/${id}/execute`, (body) => {
|
|
2997
|
+
const executed = body;
|
|
2998
|
+
const values = rowsOf(executed, "rows");
|
|
2999
|
+
columns = executed["columns"];
|
|
3000
|
+
groups = executed["groups"];
|
|
3001
|
+
total = body["count"] ?? null;
|
|
3002
|
+
const remaining = manifest.options.viewRowCap - rows.length;
|
|
3003
|
+
rows.push(...values.slice(0, remaining));
|
|
3004
|
+
const hasNext = nextRequest(body, options.subdomain) !== void 0;
|
|
3005
|
+
truncated = values.length > remaining || rows.length >= manifest.options.viewRowCap && hasNext || typeof total === "number" && total > rows.length;
|
|
3006
|
+
return rows.length >= manifest.options.viewRowCap;
|
|
3007
|
+
});
|
|
3008
|
+
observation["execute"] = {
|
|
3009
|
+
columns,
|
|
3010
|
+
groups,
|
|
3011
|
+
rows,
|
|
3012
|
+
rows_total_reported: total,
|
|
3013
|
+
rows_truncated: truncated
|
|
3014
|
+
};
|
|
3015
|
+
observation["status"] = "done";
|
|
3016
|
+
} catch (err) {
|
|
3017
|
+
await checkAuth(err);
|
|
3018
|
+
observation["error"] = errorOf(err);
|
|
3019
|
+
observation["status"] = failureStatus(err);
|
|
3020
|
+
observation["reason"] = err instanceof ZendeskApiError && failureStatus(err) === "skipped" ? skipReason(err) : String(err);
|
|
3021
|
+
}
|
|
3022
|
+
observation["finished_at"] = now();
|
|
3023
|
+
await write(state, file, observation);
|
|
3024
|
+
}
|
|
3025
|
+
index.push({
|
|
3026
|
+
id,
|
|
3027
|
+
status: observation["status"],
|
|
3028
|
+
count: observation["count"],
|
|
3029
|
+
ticket_ids_total: observation["ticket_ids_total"],
|
|
3030
|
+
error: observation["error"],
|
|
3031
|
+
reason: observation["reason"]
|
|
3032
|
+
});
|
|
3033
|
+
state.items = index.length;
|
|
3034
|
+
await write(state, "views/index.json", index);
|
|
3035
|
+
await checkpoint();
|
|
3036
|
+
}
|
|
3037
|
+
await write(state, "views/index.json", index);
|
|
3038
|
+
};
|
|
3039
|
+
const recordMacros = async (state) => {
|
|
3040
|
+
await safeDirectory(join(snapshot, "macros"), true);
|
|
3041
|
+
const selected = await sample(state, "macros");
|
|
3042
|
+
const macros = (await list("/macros", "macros")).filter((m) => m["active"] === true);
|
|
3043
|
+
const index = [];
|
|
3044
|
+
for (const macro of macros) {
|
|
3045
|
+
const id = itemId(macro);
|
|
3046
|
+
const file = `macros/${id}.json`;
|
|
3047
|
+
let observation = await readOptional(snapshotFile(snapshot, file));
|
|
3048
|
+
if (!observation) {
|
|
3049
|
+
const generic = await safeResult(() => request(`/macros/${id}/apply`));
|
|
3050
|
+
const previews = [];
|
|
3051
|
+
for (const { ticket_id } of selected) previews.push({
|
|
3052
|
+
ticket_id,
|
|
3053
|
+
...await safeResult(() => request(`/tickets/${ticket_id}/macros/${id}/apply`))
|
|
3054
|
+
});
|
|
3055
|
+
observation = {
|
|
3056
|
+
macro: pick(macro, [
|
|
3057
|
+
"id",
|
|
3058
|
+
"title",
|
|
3059
|
+
"actions",
|
|
3060
|
+
"restriction"
|
|
3061
|
+
]),
|
|
3062
|
+
generic: generic["error"] ? generic : generic["result"],
|
|
3063
|
+
previews
|
|
3064
|
+
};
|
|
3065
|
+
await write(state, file, observation);
|
|
3066
|
+
}
|
|
3067
|
+
index.push({
|
|
3068
|
+
id,
|
|
3069
|
+
previews: observation["previews"].length,
|
|
3070
|
+
errors: observation["previews"].filter((p) => p["error"]).length,
|
|
3071
|
+
status: "done"
|
|
3072
|
+
});
|
|
3073
|
+
state.items = index.length;
|
|
3074
|
+
await write(state, "macros/index.json", index);
|
|
3075
|
+
await checkpoint();
|
|
3076
|
+
}
|
|
3077
|
+
await write(state, "macros/index.json", index);
|
|
3078
|
+
};
|
|
3079
|
+
const append = async (state, file, values) => {
|
|
3080
|
+
state.files[file] = await appendPage(snapshot, file, values, state.files[file]);
|
|
3081
|
+
};
|
|
3082
|
+
const recordSlas = async (state) => {
|
|
3083
|
+
await safeDirectory(join(snapshot, "slas"), true);
|
|
3084
|
+
const selected = await sample(state, "slas");
|
|
3085
|
+
const files = Object.fromEntries(["slas.ndjson", "audits.ndjson"].map((file) => [file, state.files[file] ?? {
|
|
3086
|
+
bytes: 0,
|
|
3087
|
+
lines: 0
|
|
3088
|
+
}]));
|
|
3089
|
+
await truncateFiles(snapshot, files);
|
|
3090
|
+
for (const file of Object.keys(files)) await append(state, file, []);
|
|
3091
|
+
await checkpoint();
|
|
3092
|
+
for (let offset = state.sla_offset ?? 0; offset < selected.length; offset += 100) {
|
|
3093
|
+
const batch = selected.slice(offset, offset + 100);
|
|
3094
|
+
const result = await safeResult(() => request("/tickets/show_many", {
|
|
3095
|
+
ids: batch.map((t) => t.ticket_id).join(","),
|
|
3096
|
+
include: "slas,metric_sets,metric_events"
|
|
3097
|
+
}));
|
|
3098
|
+
if (result["error"]) await append(state, "slas.ndjson", batch.map((t) => ({
|
|
3099
|
+
ticket_id: t.ticket_id,
|
|
3100
|
+
recorded_at: now(),
|
|
3101
|
+
error: result["error"]
|
|
3102
|
+
})));
|
|
3103
|
+
else {
|
|
3104
|
+
const body = result["result"];
|
|
3105
|
+
const records = rowsOf(body, "tickets").map((ticket) => ({
|
|
3106
|
+
ticket_id: ticket.id,
|
|
3107
|
+
recorded_at: now(),
|
|
3108
|
+
ticket: pick(ticket, [
|
|
3109
|
+
"id",
|
|
3110
|
+
"status",
|
|
3111
|
+
"priority",
|
|
3112
|
+
"group_id",
|
|
3113
|
+
"assignee_id",
|
|
3114
|
+
"ticket_form_id",
|
|
3115
|
+
"brand_id",
|
|
3116
|
+
"created_at",
|
|
3117
|
+
"updated_at"
|
|
3118
|
+
]),
|
|
3119
|
+
slas: ticket["slas"],
|
|
3120
|
+
metric_set: Array.isArray(body["metric_sets"]) ? body["metric_sets"].find((m) => m["ticket_id"] === ticket.id) : body["metric_sets"],
|
|
3121
|
+
metric_events: matchEvents(body["metric_events"], ticket.id)
|
|
3122
|
+
}));
|
|
3123
|
+
const returned = new Set(records.map((record) => record.ticket_id));
|
|
3124
|
+
const absent = batch.filter((t) => !returned.has(t.ticket_id)).map((t) => ({
|
|
3125
|
+
ticket_id: t.ticket_id,
|
|
3126
|
+
recorded_at: now(),
|
|
3127
|
+
error: {
|
|
3128
|
+
status: 404,
|
|
3129
|
+
body: "Ticket omitted from show_many response"
|
|
3130
|
+
}
|
|
3131
|
+
}));
|
|
3132
|
+
await append(state, "slas.ndjson", [...records, ...absent]);
|
|
3133
|
+
}
|
|
3134
|
+
state.sla_offset = offset + batch.length;
|
|
3135
|
+
state.items = state.sla_offset;
|
|
3136
|
+
await checkpoint();
|
|
3137
|
+
}
|
|
3138
|
+
for (let offset = state.audit_offset ?? 0; offset < selected.length; offset++) {
|
|
3139
|
+
const ticket_id = selected[offset]?.ticket_id;
|
|
3140
|
+
const result = await safeResult(() => list(`/tickets/${ticket_id}/audits`, "audits"));
|
|
3141
|
+
await append(state, "audits.ndjson", [{
|
|
3142
|
+
ticket_id,
|
|
3143
|
+
...result["error"] ? result : { audits: result["result"] }
|
|
3144
|
+
}]);
|
|
3145
|
+
state.audit_offset = offset + 1;
|
|
3146
|
+
await checkpoint();
|
|
3147
|
+
}
|
|
3148
|
+
};
|
|
3149
|
+
const recordSearch = async (state) => {
|
|
3150
|
+
const queryFile = manifest.options.queries;
|
|
3151
|
+
if (!queryFile) await safeDirectory(join(input, "config"), false);
|
|
3152
|
+
const queries = queryFile ? validateQueries(await readJson(queryFile)) : buildSearchQueries({
|
|
3153
|
+
tickets: await getTickets(),
|
|
3154
|
+
users: await readRows(join(input, "users.ndjson"), ["id", "email"]),
|
|
3155
|
+
groups: await readRows(join(input, "groups.ndjson"), ["id", "name"]),
|
|
3156
|
+
organizations: await readRows(join(input, "organizations.ndjson"), ["id", "name"]),
|
|
3157
|
+
brands: await readJson(join(input, "config/brands.json")),
|
|
3158
|
+
forms: await readJson(join(input, "config/ticket_forms.json"))
|
|
3159
|
+
}, new Date((await readManifest(input)).started_at));
|
|
3160
|
+
const results = [];
|
|
3161
|
+
for (const { id, query } of queries) {
|
|
3162
|
+
const recorded = {
|
|
3163
|
+
id,
|
|
3164
|
+
query,
|
|
3165
|
+
origin: queryFile ? "file" : "generated",
|
|
3166
|
+
recorded_at: now()
|
|
3167
|
+
};
|
|
3168
|
+
const result = await safeResult(async () => ({
|
|
3169
|
+
count: await request("/search/count", { query }),
|
|
3170
|
+
top_relevance_ids: rowsOf(await request("/search", {
|
|
3171
|
+
query,
|
|
3172
|
+
per_page: "100"
|
|
3173
|
+
}), "results").map((t) => t.id),
|
|
3174
|
+
top_created_desc_ids: rowsOf(await request("/search", {
|
|
3175
|
+
query,
|
|
3176
|
+
sort_by: "created_at",
|
|
3177
|
+
sort_order: "desc",
|
|
3178
|
+
per_page: "100"
|
|
3179
|
+
}), "results").map((t) => t.id)
|
|
3180
|
+
}));
|
|
3181
|
+
results.push({
|
|
3182
|
+
...recorded,
|
|
3183
|
+
...result["error"] ? result : result["result"]
|
|
3184
|
+
});
|
|
3185
|
+
state.items = results.length;
|
|
3186
|
+
await write(state, "search.json", results);
|
|
3187
|
+
await checkpoint();
|
|
3188
|
+
}
|
|
3189
|
+
await write(state, "search.json", results);
|
|
3190
|
+
};
|
|
3191
|
+
const recorders = {
|
|
3192
|
+
context: async (state) => {
|
|
3193
|
+
await write(state, "context.json", {
|
|
3194
|
+
...me,
|
|
3195
|
+
group_memberships: await list(`/users/${manifest.current_user.id}/group_memberships`, "group_memberships"),
|
|
3196
|
+
recorded_at: now()
|
|
3197
|
+
});
|
|
3198
|
+
state.items = 1;
|
|
3199
|
+
},
|
|
3200
|
+
views: recordViews,
|
|
3201
|
+
macros: recordMacros,
|
|
3202
|
+
slas: recordSlas,
|
|
3203
|
+
search: recordSearch
|
|
3204
|
+
};
|
|
3205
|
+
for (const name of GOLDEN_STAGES) {
|
|
3206
|
+
const state = manifest.stages[name];
|
|
3207
|
+
if (!state || state.status === "done" || options.stages && !options.stages.includes(name)) continue;
|
|
3208
|
+
requestState.current = state;
|
|
3209
|
+
if (state.status === "pending") state.started_at = now();
|
|
3210
|
+
state.status = "running";
|
|
3211
|
+
delete state.reason;
|
|
3212
|
+
delete state.finished_at;
|
|
3213
|
+
await checkpoint();
|
|
3214
|
+
try {
|
|
3215
|
+
await recorders[name](state);
|
|
3216
|
+
state.status = "done";
|
|
3217
|
+
} catch (err) {
|
|
3218
|
+
state.status = failureStatus(err);
|
|
3219
|
+
state.reason = err instanceof ZendeskApiError && state.status === "skipped" ? skipReason(err) : String(err);
|
|
3220
|
+
await checkAuth(err);
|
|
3221
|
+
}
|
|
3222
|
+
state.finished_at = now();
|
|
3223
|
+
state.duration_ms = deps.now() - Date.parse(state.started_at);
|
|
3224
|
+
await checkpoint();
|
|
3225
|
+
}
|
|
3226
|
+
manifest.finished_at = now();
|
|
3227
|
+
await checkpoint();
|
|
3228
|
+
return {
|
|
3229
|
+
snapshot,
|
|
3230
|
+
manifest,
|
|
3231
|
+
duration_ms: deps.now() - Date.parse(manifest.started_at)
|
|
3232
|
+
};
|
|
3233
|
+
};
|
|
3234
|
+
const matchEvents = (raw, id) => {
|
|
3235
|
+
if (Array.isArray(raw) && raw.every((item) => item && typeof item === "object" && "ticket_id" in item)) return raw.filter((item) => item["ticket_id"] === id);
|
|
3236
|
+
if (raw && typeof raw === "object" && !Array.isArray(raw) && Object.keys(raw).every((key) => TICKET_ID_KEY.test(key))) return raw[String(id)] ?? [];
|
|
3237
|
+
return raw;
|
|
3238
|
+
};
|
|
3239
|
+
const validateQueries = (value) => {
|
|
3240
|
+
if (!Array.isArray(value) || value.some((q) => !q || typeof q.id !== "string" || !q.id || typeof q.query !== "string" || !q.query.startsWith("type:ticket ")) || new Set(value.map((q) => q.id)).size !== value.length) throw new Error("Queries must be an array of unique { id, query } entries prefixed type:ticket");
|
|
3241
|
+
return value;
|
|
3242
|
+
};
|
|
3243
|
+
const runGoldens = async (options) => (await prepareGoldens(options)).run();
|
|
3244
|
+
//#endregion
|
|
3245
|
+
//#region src/export/data-export.ts
|
|
3246
|
+
const DATA_STREAMS = [
|
|
3247
|
+
"tickets",
|
|
3248
|
+
"users",
|
|
3249
|
+
"organizations",
|
|
3250
|
+
"ticket_events",
|
|
3251
|
+
"ticket_metric_events",
|
|
3252
|
+
"side_conversation_events",
|
|
3253
|
+
"groups",
|
|
3254
|
+
"group_memberships",
|
|
3255
|
+
"organization_memberships",
|
|
3256
|
+
"satisfaction_ratings",
|
|
3257
|
+
"custom_object_records",
|
|
3258
|
+
"config"
|
|
3259
|
+
];
|
|
3260
|
+
const EPOCH = /^\d+$/;
|
|
3261
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}(?:T.*)?$/;
|
|
3262
|
+
const SUBDOMAIN$1 = /^[a-z0-9][a-z0-9-]*$/i;
|
|
3263
|
+
const NETWORK_PATH$1 = /^[\\/]{2}/;
|
|
3264
|
+
const JSON_SUFFIX = /\.json$/;
|
|
3265
|
+
const parseSince = (value) => {
|
|
3266
|
+
if (value === void 0) return 0;
|
|
3267
|
+
let seconds;
|
|
3268
|
+
if (typeof value === "number" || EPOCH.test(value)) seconds = Number(value);
|
|
3269
|
+
else seconds = ISO_DATE.test(value) ? Date.parse(value) / 1e3 : NaN;
|
|
3270
|
+
if (!Number.isSafeInteger(seconds) || seconds < 0 || seconds > 864e10) throw new Error("Since must be an ISO date or non-negative epoch seconds");
|
|
3271
|
+
return seconds;
|
|
3272
|
+
};
|
|
3273
|
+
const incremental = (name) => DATA_STREAMS.indexOf(name) < 6;
|
|
3274
|
+
const timeBased = (name) => incremental(name) && name !== "tickets" && name !== "users";
|
|
3275
|
+
const streamPath = (name) => {
|
|
3276
|
+
if (name === "side_conversation_events") return "/tickets/side_conversations/events";
|
|
3277
|
+
if (name === "custom_object_records") return "/custom_objects";
|
|
3278
|
+
if (name === "tickets" || name === "users") return `/incremental/${name}/cursor`;
|
|
3279
|
+
return incremental(name) ? `/incremental/${name}` : `/${name}`;
|
|
3280
|
+
};
|
|
3281
|
+
const supportScope = (name) => name === "tickets" || name === "ticket_events" ? { support_type_scope: "all" } : {};
|
|
3282
|
+
const initialRequest = (name, since) => ({
|
|
3283
|
+
path: streamPath(name),
|
|
3284
|
+
params: incremental(name) ? {
|
|
3285
|
+
start_time: String(since),
|
|
3286
|
+
...incrementalPageSize(name),
|
|
3287
|
+
...supportScope(name)
|
|
3288
|
+
} : { "page[size]": "100" }
|
|
3289
|
+
});
|
|
3290
|
+
const incrementalPageSize = (name) => name === "side_conversation_events" ? {} : { per_page: "1000" };
|
|
3291
|
+
const defaultInclude = (name) => {
|
|
3292
|
+
if (name === "tickets") return "metric_sets,dates,slas";
|
|
3293
|
+
if (name === "users") return "identities";
|
|
3294
|
+
if (name === "ticket_events") return "comment_events";
|
|
3295
|
+
};
|
|
3296
|
+
const streamFiles = (name) => {
|
|
3297
|
+
const files = [`${name}.ndjson`];
|
|
3298
|
+
if (name === "tickets") files.push("ticket_metrics.ndjson", "ticket_slas.ndjson");
|
|
3299
|
+
if (name === "users") files.push("user_identities.ndjson");
|
|
3300
|
+
if (name === "ticket_events") files.push("comments.ndjson", "attachments.ndjson");
|
|
3301
|
+
if (name === "side_conversation_events") files.push("side_conversations.ndjson");
|
|
3302
|
+
return files;
|
|
3303
|
+
};
|
|
3304
|
+
const allowedFiles = (name) => name === "config" ? RESOURCE_KEYS.map((resource) => `config/${resource}.json`) : streamFiles(name);
|
|
3305
|
+
const rows = (body, key) => {
|
|
3306
|
+
const value = body[key];
|
|
3307
|
+
if (!Array.isArray(value) || value.some((item) => !item || typeof item !== "object" || Array.isArray(item))) throw new Error(`Expected ${key} to be an array of objects`);
|
|
3308
|
+
return value;
|
|
3309
|
+
};
|
|
3310
|
+
const optionalRows = (body, key) => body[key] === void 0 ? [] : rows(body, key);
|
|
3311
|
+
const commentFiles = (items) => {
|
|
3312
|
+
const comments = [];
|
|
3313
|
+
const attachments = [];
|
|
3314
|
+
for (const event of items) for (const child of optionalRows(event, "child_events")) {
|
|
3315
|
+
if (child["event_type"] !== "Comment" && child["type"] !== "Comment") continue;
|
|
3316
|
+
comments.push({
|
|
3317
|
+
ticket_id: event["ticket_id"],
|
|
3318
|
+
ticket_event_id: event["id"],
|
|
3319
|
+
timestamp: event["timestamp"],
|
|
3320
|
+
...child
|
|
3321
|
+
});
|
|
3322
|
+
for (const attachment of optionalRows(child, "attachments")) attachments.push({
|
|
3323
|
+
ticket_id: event["ticket_id"],
|
|
3324
|
+
comment_id: child["id"],
|
|
3325
|
+
...attachment
|
|
3326
|
+
});
|
|
3327
|
+
}
|
|
3328
|
+
return {
|
|
3329
|
+
"comments.ndjson": comments,
|
|
3330
|
+
"attachments.ndjson": attachments
|
|
3331
|
+
};
|
|
3332
|
+
};
|
|
3333
|
+
const pageFiles = (name, body, items) => {
|
|
3334
|
+
const files = { [`${name}.ndjson`]: items };
|
|
3335
|
+
if (name === "tickets") {
|
|
3336
|
+
files["ticket_metrics.ndjson"] = optionalRows(body, "metric_sets");
|
|
3337
|
+
const slas = body["slas"];
|
|
3338
|
+
if (slas !== void 0 && slas !== null) {
|
|
3339
|
+
const values = Array.isArray(slas) ? slas : [slas];
|
|
3340
|
+
if (values.length) files["ticket_slas.ndjson"] = values;
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
if (name === "users") {
|
|
3344
|
+
const identities = optionalRows(body, "identities");
|
|
3345
|
+
if (identities.length) files["user_identities.ndjson"] = identities;
|
|
3346
|
+
}
|
|
3347
|
+
if (name === "ticket_events") Object.assign(files, commentFiles(items));
|
|
3348
|
+
return files;
|
|
3349
|
+
};
|
|
3350
|
+
const nextPosition = (name, body, current, subdomain) => {
|
|
3351
|
+
if (!incremental(name)) return nextRequest(body, subdomain) ?? null;
|
|
3352
|
+
if (body["end_of_stream"] === true) return null;
|
|
3353
|
+
if (name === "tickets" || name === "users") {
|
|
3354
|
+
const cursor = body["after_cursor"];
|
|
3355
|
+
if (typeof cursor !== "string" || !cursor || cursor === current.params["cursor"]) throw new Error("Missing or repeated incremental cursor");
|
|
3356
|
+
return {
|
|
3357
|
+
path: streamPath(name),
|
|
3358
|
+
params: {
|
|
3359
|
+
cursor,
|
|
3360
|
+
per_page: "1000",
|
|
3361
|
+
...supportScope(name)
|
|
3362
|
+
}
|
|
3363
|
+
};
|
|
3364
|
+
}
|
|
3365
|
+
if (name === "ticket_metric_events" && body["end_of_stream"] === void 0 && rows(body, responseKey(name)).length < 1e3) return null;
|
|
3366
|
+
if (name === "side_conversation_events" && body["next_page"] === null) return null;
|
|
3367
|
+
const next = nextRequest(body, subdomain);
|
|
3368
|
+
if (next) return {
|
|
3369
|
+
...next,
|
|
3370
|
+
params: {
|
|
3371
|
+
...next.params,
|
|
3372
|
+
...supportScope(name)
|
|
3373
|
+
}
|
|
3374
|
+
};
|
|
3375
|
+
if (typeof body["end_time"] !== "number") throw new Error("Missing end_time on an unfinished incremental page");
|
|
3376
|
+
return {
|
|
3377
|
+
path: streamPath(name),
|
|
3378
|
+
params: {
|
|
3379
|
+
start_time: String(body["end_time"]),
|
|
3380
|
+
...incrementalPageSize(name),
|
|
3381
|
+
...supportScope(name)
|
|
3382
|
+
}
|
|
3383
|
+
};
|
|
3384
|
+
};
|
|
3385
|
+
const responseKey = (name) => name === "side_conversation_events" ? "events" : name;
|
|
3386
|
+
const recordPath = (key) => `/custom_objects/${encodeURIComponent(key)}/records`;
|
|
3387
|
+
const samePath = (path, expected) => path === expected || path === `${expected}.json`;
|
|
3388
|
+
const validRequest = (request, paths) => paths.some((path) => samePath(request.path, path)) && Boolean(request.params) && Object.values(request.params).every((value) => typeof value === "string");
|
|
3389
|
+
const validObjectKey = (key) => typeof key === "string" && key.length > 0 && key !== "." && key !== "..";
|
|
3390
|
+
const validatePosition = (name, position) => {
|
|
3391
|
+
const paths = [streamPath(name)];
|
|
3392
|
+
const objects = position.custom_objects;
|
|
3393
|
+
if (objects !== void 0) {
|
|
3394
|
+
if (name !== "custom_object_records" || !objects || !Array.isArray(objects.keys) || !objects.keys.every(validObjectKey) || objects.next_list !== null && !validRequest(objects.next_list, ["/custom_objects"])) throw new Error("Invalid custom object checkpoint");
|
|
3395
|
+
if (objects.keys[0]) paths.push(recordPath(objects.keys[0]));
|
|
3396
|
+
}
|
|
3397
|
+
if (position.request && !validRequest(position.request, paths)) throw new Error("Invalid resume request");
|
|
3398
|
+
};
|
|
3399
|
+
const validateOptions = (options) => {
|
|
3400
|
+
if (!SUBDOMAIN$1.test(options.subdomain)) throw new Error("Invalid Zendesk subdomain");
|
|
3401
|
+
if (!isAbsolute(options.exportDir) || NETWORK_PATH$1.test(options.exportDir)) throw new Error("Export directory must be an absolute local path");
|
|
3402
|
+
if (options.resume !== void 0) validateResumeName(options.resume);
|
|
3403
|
+
if (options.streams && (!options.streams.length || options.streams.some((name) => !DATA_STREAMS.includes(name)))) throw new Error("Select one or more supported streams");
|
|
3404
|
+
resolveRates(options.rates);
|
|
3405
|
+
parseSince(options.since);
|
|
3406
|
+
};
|
|
3407
|
+
const validateManifest = (manifest, options) => {
|
|
3408
|
+
if (manifest.kind !== "data" || manifest.subdomain !== options.subdomain || !manifest.streams || typeof manifest.streams !== "object") throw new Error("Resume manifest does not belong to this account data export");
|
|
3409
|
+
parseSince(manifest.since);
|
|
3410
|
+
resolveRates(manifest.rates);
|
|
3411
|
+
if (options.since !== void 0 && parseSince(options.since) !== manifest.since) throw new Error("Since cannot change when resuming");
|
|
3412
|
+
for (const [name, state] of Object.entries(manifest.streams)) {
|
|
3413
|
+
if (!DATA_STREAMS.includes(name) || !state?.files || !state.position || !Array.isArray(state.position.previous_ids)) throw new Error("Invalid stream checkpoint");
|
|
3414
|
+
validatePosition(name, state.position);
|
|
3415
|
+
if (Object.keys(state.files).some((file) => !allowedFiles(name).includes(file))) throw new Error("Invalid checkpoint filename");
|
|
3416
|
+
}
|
|
3417
|
+
};
|
|
3418
|
+
/** Reserve the folder before a background job returns to its MCP caller. */
|
|
3419
|
+
const prepareDataExport = async (options) => {
|
|
3420
|
+
validateOptions(options);
|
|
3421
|
+
const deps = options.deps ?? defaultDeps;
|
|
3422
|
+
const since = parseSince(options.since);
|
|
3423
|
+
await options.getToken();
|
|
3424
|
+
const root = resolve(options.exportDir);
|
|
3425
|
+
await safeDirectory(root, true);
|
|
3426
|
+
let manifest;
|
|
3427
|
+
let snapshot = "";
|
|
3428
|
+
if (options.resume !== void 0) {
|
|
3429
|
+
snapshot = join(root, validateResumeName(options.resume));
|
|
3430
|
+
await safeDirectory(snapshot, false);
|
|
3431
|
+
manifest = await readManifest(snapshot);
|
|
3432
|
+
validateManifest(manifest, options);
|
|
3433
|
+
if (options.streams?.some((name) => !manifest?.streams[name])) throw new Error("Resume streams must exist in the original snapshot");
|
|
3434
|
+
}
|
|
3435
|
+
const rates = resolveRates({
|
|
3436
|
+
...manifest?.rates,
|
|
3437
|
+
...options.rates
|
|
3438
|
+
});
|
|
3439
|
+
const requestState = { current: void 0 };
|
|
3440
|
+
const get = createPacedGet(options.subdomain, options.getToken, rates, deps, () => {
|
|
3441
|
+
if (requestState.current) requestState.current.requests += 1;
|
|
3442
|
+
});
|
|
3443
|
+
const me = await get("standard", "/users/me");
|
|
3444
|
+
if (!manifest) {
|
|
3445
|
+
const at = new Date(deps.now());
|
|
3446
|
+
snapshot = join(root, snapshotDirName(`${options.subdomain}-data`, at));
|
|
3447
|
+
await mkdir(snapshot, { mode: 448 });
|
|
3448
|
+
const selected = DATA_STREAMS.filter((name) => options.streams?.includes(name) ?? true);
|
|
3449
|
+
manifest = {
|
|
3450
|
+
subdomain: options.subdomain,
|
|
3451
|
+
kind: "data",
|
|
3452
|
+
started_at: at.toISOString(),
|
|
3453
|
+
since,
|
|
3454
|
+
rates,
|
|
3455
|
+
current_user: {
|
|
3456
|
+
id: me.user.id,
|
|
3457
|
+
role: me.user.role
|
|
3458
|
+
},
|
|
3459
|
+
streams: {}
|
|
3460
|
+
};
|
|
3461
|
+
for (const name of selected) manifest.streams[name] = {
|
|
3462
|
+
status: "pending",
|
|
3463
|
+
files: {},
|
|
3464
|
+
requests: 0,
|
|
3465
|
+
pages: 0,
|
|
3466
|
+
position: {
|
|
3467
|
+
request: initialRequest(name, since),
|
|
3468
|
+
previous_ids: []
|
|
3469
|
+
},
|
|
3470
|
+
started_at: at.toISOString(),
|
|
3471
|
+
duration_ms: 0,
|
|
3472
|
+
...defaultInclude(name) !== void 0 ? { include_used: defaultInclude(name) } : {}
|
|
3473
|
+
};
|
|
3474
|
+
}
|
|
3475
|
+
manifest.current_user = {
|
|
3476
|
+
id: me.user.id,
|
|
3477
|
+
role: me.user.role
|
|
3478
|
+
};
|
|
3479
|
+
manifest.rates = rates;
|
|
3480
|
+
delete manifest.finished_at;
|
|
3481
|
+
await writeManifest$1(snapshot, manifest);
|
|
3482
|
+
const preparedManifest = manifest;
|
|
3483
|
+
return {
|
|
3484
|
+
snapshot,
|
|
3485
|
+
manifest,
|
|
3486
|
+
run: () => executeExport(options, snapshot, preparedManifest, deps, get, requestState)
|
|
3487
|
+
};
|
|
3488
|
+
};
|
|
3489
|
+
const getPage = async (name, state, request, get, subdomain) => {
|
|
3490
|
+
const params = {
|
|
3491
|
+
...request.params,
|
|
3492
|
+
...supportScope(name),
|
|
3493
|
+
...state.include_used ? { include: state.include_used } : {}
|
|
3494
|
+
};
|
|
3495
|
+
try {
|
|
3496
|
+
if (!incremental(name) && state.pages === 0) {
|
|
3497
|
+
const adapter = (_subdomain, _token, path, query) => get("standard", path, query);
|
|
3498
|
+
return await firstPage(subdomain, "", request.path, adapter);
|
|
3499
|
+
}
|
|
3500
|
+
return await get(incremental(name) ? "incremental" : "standard", request.path, params);
|
|
3501
|
+
} catch (err) {
|
|
3502
|
+
if (!(err instanceof ZendeskApiError && err.status === 400 && state.include_used && (name === "tickets" || name === "users"))) throw err;
|
|
3503
|
+
state.include_used = name === "tickets" && state.include_used !== "metric_sets" ? "metric_sets" : null;
|
|
3504
|
+
return getPage(name, state, request, get, subdomain);
|
|
3505
|
+
}
|
|
3506
|
+
};
|
|
3507
|
+
const exportPages = async (name, state, snapshot, options, get, checkpoint) => {
|
|
3508
|
+
const recorded = Object.fromEntries(streamFiles(name).map((file) => [file, state.files[file] ?? {
|
|
3509
|
+
lines: 0,
|
|
3510
|
+
bytes: 0
|
|
3511
|
+
}]));
|
|
3512
|
+
await truncateFiles(snapshot, recorded);
|
|
3513
|
+
const conversations = name === "side_conversation_events" ? await readConversationKeys(snapshot, state.files["side_conversations.ndjson"]) : /* @__PURE__ */ new Set();
|
|
3514
|
+
while (state.position.request) {
|
|
3515
|
+
const current = state.position.request;
|
|
3516
|
+
const body = await getPage(name, state, current, get, options.subdomain);
|
|
3517
|
+
const raw = rows(body, responseKey(name));
|
|
3518
|
+
const previous = new Set(state.position.previous_ids);
|
|
3519
|
+
const items = timeBased(name) ? raw.filter((item) => !previous.has(item["id"])) : raw;
|
|
3520
|
+
let next = nextPosition(name, body, current, options.subdomain);
|
|
3521
|
+
if (name === "side_conversation_events" && body["end_of_stream"] === void 0 && items.length === 0 && next?.params["start_time"] === current.params["start_time"]) next = null;
|
|
3522
|
+
const ids = raw.map((item) => item["id"]).filter((id) => typeof id === "number" || typeof id === "string");
|
|
3523
|
+
if (next && JSON.stringify(next) === JSON.stringify(current) && items.length === 0) throw new Error("Incremental pagination made no progress");
|
|
3524
|
+
const files = pageFiles(name, body, items);
|
|
3525
|
+
if (name === "side_conversation_events") files["side_conversations.ndjson"] = await conversationRows(items, conversations, get);
|
|
3526
|
+
const updated = { ...state.files };
|
|
3527
|
+
for (const [file, values] of Object.entries(files)) updated[file] = await appendPage(snapshot, file, values, state.files[file]);
|
|
3528
|
+
state.files = updated;
|
|
3529
|
+
state.position = {
|
|
3530
|
+
request: next,
|
|
3531
|
+
previous_ids: ids
|
|
3532
|
+
};
|
|
3533
|
+
state.pages += 1;
|
|
3534
|
+
state.duration_ms = (options.deps ?? defaultDeps).now() - Date.parse(state.started_at);
|
|
3535
|
+
await checkpoint();
|
|
3536
|
+
}
|
|
3537
|
+
};
|
|
3538
|
+
const conversationKey = (ticket, conversation) => JSON.stringify([ticket, conversation]);
|
|
3539
|
+
/** Rebuild deduplication from committed rows after truncation, including on resume. */
|
|
3540
|
+
const readConversationKeys = async (snapshot, stats) => {
|
|
3541
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3542
|
+
if (!stats?.bytes) return seen;
|
|
3543
|
+
const handle = await open$1(join(snapshot, "side_conversations.ndjson"), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
3544
|
+
try {
|
|
3545
|
+
const input = createInterface({
|
|
3546
|
+
input: handle.createReadStream({ autoClose: false }),
|
|
3547
|
+
crlfDelay: Number.POSITIVE_INFINITY
|
|
3548
|
+
});
|
|
3549
|
+
for await (const line of input) {
|
|
3550
|
+
const row = JSON.parse(line);
|
|
3551
|
+
seen.add(conversationKey(row["ticket_id"], row["id"]));
|
|
3552
|
+
}
|
|
3553
|
+
} finally {
|
|
3554
|
+
await handle.close();
|
|
3555
|
+
}
|
|
3556
|
+
return seen;
|
|
3557
|
+
};
|
|
3558
|
+
const conversationRows = async (items, seen, get) => {
|
|
3559
|
+
const conversations = [];
|
|
3560
|
+
for (const event of items) {
|
|
3561
|
+
const ticket = event["ticket_id"];
|
|
3562
|
+
const id = event["side_conversation_id"];
|
|
3563
|
+
if (!Number.isSafeInteger(ticket) || ticket <= 0 || !validObjectKey(id)) throw new Error("Missing ticket_id or side_conversation_id on side conversation event");
|
|
3564
|
+
const key = conversationKey(ticket, id);
|
|
3565
|
+
if (seen.has(key)) continue;
|
|
3566
|
+
const conversation = (await get("standard", `/tickets/${ticket}/side_conversations/${encodeURIComponent(id)}`))["side_conversation"];
|
|
3567
|
+
if (!conversation || typeof conversation !== "object" || Array.isArray(conversation)) throw new Error("Expected side_conversation to be an object");
|
|
3568
|
+
conversations.push({
|
|
3569
|
+
...conversation,
|
|
3570
|
+
ticket_id: ticket
|
|
3571
|
+
});
|
|
3572
|
+
seen.add(key);
|
|
3573
|
+
}
|
|
3574
|
+
return conversations;
|
|
3575
|
+
};
|
|
3576
|
+
const cursorNext = (body, current, subdomain) => {
|
|
3577
|
+
const meta = body["meta"];
|
|
3578
|
+
if (meta?.has_more === false) return null;
|
|
3579
|
+
const next = nextRequest(body, subdomain);
|
|
3580
|
+
if (!next) {
|
|
3581
|
+
if (meta?.has_more === true) throw new Error("Missing next custom object cursor");
|
|
3582
|
+
return null;
|
|
3583
|
+
}
|
|
3584
|
+
if (!samePath(next.path, current.path.replace(JSON_SUFFIX, ""))) throw new Error("Invalid custom object pagination path");
|
|
3585
|
+
const request = {
|
|
3586
|
+
...next,
|
|
3587
|
+
params: {
|
|
3588
|
+
...next.params,
|
|
3589
|
+
"page[size]": "100"
|
|
3590
|
+
}
|
|
3591
|
+
};
|
|
3592
|
+
if (JSON.stringify(request) === JSON.stringify(current)) throw new Error("Custom object pagination made no progress");
|
|
3593
|
+
return request;
|
|
3594
|
+
};
|
|
3595
|
+
const objectRequest = (objects) => {
|
|
3596
|
+
const key = objects.keys[0];
|
|
3597
|
+
return key ? {
|
|
3598
|
+
path: recordPath(key),
|
|
3599
|
+
params: { "page[size]": "100" }
|
|
3600
|
+
} : objects.next_list;
|
|
3601
|
+
};
|
|
3602
|
+
const customObjectPage = (body, position, next) => {
|
|
3603
|
+
if (position.request && samePath(position.request.path, "/custom_objects")) {
|
|
3604
|
+
const keys = rows(body, "custom_objects").map((object) => object["key"]);
|
|
3605
|
+
if (!keys.every(validObjectKey)) throw new Error("Invalid custom object key");
|
|
3606
|
+
const objects = {
|
|
3607
|
+
keys,
|
|
3608
|
+
next_list: next
|
|
3609
|
+
};
|
|
3610
|
+
return {
|
|
3611
|
+
values: [],
|
|
3612
|
+
position: {
|
|
3613
|
+
request: objectRequest(objects),
|
|
3614
|
+
previous_ids: [],
|
|
3615
|
+
custom_objects: objects
|
|
3616
|
+
}
|
|
3617
|
+
};
|
|
3618
|
+
}
|
|
3619
|
+
const objects = position.custom_objects;
|
|
3620
|
+
const key = objects?.keys[0];
|
|
3621
|
+
if (!objects || !key) throw new Error("Missing custom object checkpoint key");
|
|
3622
|
+
const values = rows(body, "custom_object_records").map((record) => ({
|
|
3623
|
+
...record,
|
|
3624
|
+
custom_object_key: key
|
|
3625
|
+
}));
|
|
3626
|
+
const remaining = next ? objects : {
|
|
3627
|
+
...objects,
|
|
3628
|
+
keys: objects.keys.slice(1)
|
|
3629
|
+
};
|
|
3630
|
+
return {
|
|
3631
|
+
values,
|
|
3632
|
+
position: {
|
|
3633
|
+
request: next ?? objectRequest(remaining),
|
|
3634
|
+
previous_ids: [],
|
|
3635
|
+
custom_objects: remaining
|
|
3636
|
+
}
|
|
3637
|
+
};
|
|
3638
|
+
};
|
|
3639
|
+
const exportCustomObjectRecords = async (state, snapshot, options, get, checkpoint) => {
|
|
3640
|
+
const file = "custom_object_records.ndjson";
|
|
3641
|
+
await truncateFiles(snapshot, { [file]: state.files[file] ?? {
|
|
3642
|
+
lines: 0,
|
|
3643
|
+
bytes: 0
|
|
3644
|
+
} });
|
|
3645
|
+
while (state.position.request) {
|
|
3646
|
+
const current = state.position.request;
|
|
3647
|
+
const body = await get("standard", current.path, {
|
|
3648
|
+
...current.params,
|
|
3649
|
+
"page[size]": "100"
|
|
3650
|
+
});
|
|
3651
|
+
const next = cursorNext(body, current, options.subdomain);
|
|
3652
|
+
const { values, position } = customObjectPage(body, state.position, next);
|
|
3653
|
+
const stats = await appendPage(snapshot, file, values, state.files[file]);
|
|
3654
|
+
state.files = {
|
|
3655
|
+
...state.files,
|
|
3656
|
+
[file]: stats
|
|
3657
|
+
};
|
|
3658
|
+
state.position = position;
|
|
3659
|
+
state.pages += 1;
|
|
3660
|
+
state.duration_ms = (options.deps ?? defaultDeps).now() - Date.parse(state.started_at);
|
|
3661
|
+
await checkpoint();
|
|
3662
|
+
}
|
|
3663
|
+
};
|
|
3664
|
+
const exportConfig = async (options, snapshot, state, get, checkpoint) => {
|
|
3665
|
+
const folder = join(snapshot, "config");
|
|
3666
|
+
await safeDirectory(folder, true);
|
|
3667
|
+
await truncateFiles(snapshot, state.files);
|
|
3668
|
+
let prior = [];
|
|
3669
|
+
try {
|
|
3670
|
+
prior = (await readManifest(folder)).resources.filter((entry) => entry.status !== "failed");
|
|
3671
|
+
if (prior.some((entry) => !RESOURCE_KEYS.includes(entry.resource) || entry.status === "exported" && entry.file !== `${entry.resource}.json`)) throw new Error("Invalid configuration checkpoint");
|
|
3672
|
+
} catch (err) {
|
|
3673
|
+
if (!(err instanceof Error && "code" in err && err.code === "ENOENT")) throw err;
|
|
3674
|
+
}
|
|
3675
|
+
const record = async (entry) => {
|
|
3676
|
+
if (entry.status === "exported") state.files[`config/${entry.file}`] = await readFileStats(folder, entry.file);
|
|
3677
|
+
};
|
|
3678
|
+
for (const entry of prior) await record(entry);
|
|
3679
|
+
const selected = RESOURCE_KEYS.filter((name) => !prior.some((entry) => entry.resource === name));
|
|
3680
|
+
const request = (_subdomain, _token, path, params) => get("standard", path, params);
|
|
3681
|
+
const failed = (await runConfigExport(options, folder, selected, request, void 0, async (entry) => {
|
|
3682
|
+
await record(entry);
|
|
3683
|
+
state.pages += 1;
|
|
3684
|
+
await checkpoint();
|
|
3685
|
+
}, prior)).filter((entry) => entry.status === "failed");
|
|
3686
|
+
if (failed.length) throw new Error(`Configuration resources failed: ${failed.map((entry) => entry.resource).join(", ")}`);
|
|
3687
|
+
state.position.request = null;
|
|
3688
|
+
};
|
|
3689
|
+
const recordFailure = async (err, state, get, checkpoint) => {
|
|
3690
|
+
state.status = "failed";
|
|
3691
|
+
state.reason = err instanceof Error ? err.message : String(err);
|
|
3692
|
+
if (!(err instanceof ZendeskApiError)) return;
|
|
3693
|
+
if (err.status === 403 || err.status === 404) {
|
|
3694
|
+
state.status = "skipped";
|
|
3695
|
+
state.reason = skipReason(err);
|
|
3696
|
+
}
|
|
3697
|
+
if (err.status !== 401) return;
|
|
3698
|
+
try {
|
|
3699
|
+
await get("standard", "/users/me");
|
|
3700
|
+
} catch (meError) {
|
|
3701
|
+
if (meError instanceof ZendeskApiError && meError.status === 401) {
|
|
3702
|
+
await checkpoint();
|
|
3703
|
+
throw err;
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
3706
|
+
};
|
|
3707
|
+
const executeExport = async (options, snapshot, manifest, deps, get, requestState) => {
|
|
3708
|
+
const checkpoint = async () => {
|
|
3709
|
+
await writeManifest$1(snapshot, manifest);
|
|
3710
|
+
options.onProgress?.(manifest, snapshot);
|
|
3711
|
+
};
|
|
3712
|
+
for (const name of DATA_STREAMS) {
|
|
3713
|
+
const state = manifest.streams[name];
|
|
3714
|
+
if (!state || options.streams && !options.streams.includes(name) || state.status === "exported" || state.status === "skipped") continue;
|
|
3715
|
+
requestState.current = state;
|
|
3716
|
+
if (state.status === "pending") state.started_at = new Date(deps.now()).toISOString();
|
|
3717
|
+
state.status = "running";
|
|
3718
|
+
delete state.reason;
|
|
3719
|
+
delete state.finished_at;
|
|
3720
|
+
await checkpoint();
|
|
3721
|
+
try {
|
|
3722
|
+
if (name === "config") await exportConfig(options, snapshot, state, get, checkpoint);
|
|
3723
|
+
else if (name === "custom_object_records") await exportCustomObjectRecords(state, snapshot, options, get, checkpoint);
|
|
3724
|
+
else await exportPages(name, state, snapshot, options, get, checkpoint);
|
|
3725
|
+
state.status = "exported";
|
|
3726
|
+
} catch (err) {
|
|
3727
|
+
state.finished_at = new Date(deps.now()).toISOString();
|
|
3728
|
+
state.duration_ms = deps.now() - Date.parse(state.started_at);
|
|
3729
|
+
await recordFailure(err, state, get, checkpoint);
|
|
3730
|
+
}
|
|
3731
|
+
state.finished_at = new Date(deps.now()).toISOString();
|
|
3732
|
+
state.duration_ms = deps.now() - Date.parse(state.started_at);
|
|
3733
|
+
await checkpoint();
|
|
3734
|
+
}
|
|
3735
|
+
manifest.finished_at = new Date(deps.now()).toISOString();
|
|
3736
|
+
await checkpoint();
|
|
3737
|
+
return {
|
|
3738
|
+
snapshot,
|
|
3739
|
+
manifest,
|
|
3740
|
+
duration_ms: deps.now() - Date.parse(manifest.started_at)
|
|
3741
|
+
};
|
|
3742
|
+
};
|
|
3743
|
+
const runDataExport = async (options) => (await prepareDataExport(options)).run();
|
|
3744
|
+
//#endregion
|
|
3745
|
+
//#region src/export/tool.ts
|
|
3746
|
+
const jobs = /* @__PURE__ */ new Map();
|
|
3747
|
+
const createDataExportTool = (ctx, exportDir) => ({
|
|
3748
|
+
name: "export_data",
|
|
3749
|
+
namespace: "admin",
|
|
3750
|
+
readOnly: true,
|
|
3751
|
+
title: "Export Zendesk Account Data",
|
|
3752
|
+
description: "Start a resumable bulk export of Zendesk account data to local NDJSON files in the background. Use it for backups, audits or migration of tickets (including AI agents), users, organizations, events, custom object records, side conversations and configuration; use export_account_config for configuration alone, or search_tickets for an interactive lookup. Requires an admin sign-in and an operator-configured export directory on a stdio server. It only reads Zendesk with GET requests, creates a new timestamped local snapshot and never downloads attachment content; resume appends after truncating unfinished files to their last checkpoint. Returns the snapshot path and manifest progress immediately; call again while running to check the same job instead of starting another. Exports may take hours and stop when the server process exits; use resume with the returned folder name after a restart. A skipped stream lacks access or availability; failed streams can be resumed. Read manifest.json in the snapshot directory for page counts, file sizes and errors.",
|
|
3753
|
+
inputSchema: z.object({
|
|
3754
|
+
since: z.string().optional().describe("Earliest incremental record date as ISO 8601 (for example 2025-01-01T00:00:00Z) or epoch seconds. Omit for all history; list streams always export in full. Must match the original date when resuming."),
|
|
3755
|
+
streams: z.array(z.enum(DATA_STREAMS)).min(1).optional().describe("Streams to export, for example [\"tickets\", \"users\", \"ticket_events\", \"custom_object_records\", \"side_conversation_events\"]. Omit for all data streams and configuration. Custom object records always export in full; side conversation events also export the referenced conversations. Resume may select only streams in the original snapshot."),
|
|
3756
|
+
resume: z.string().min(1).optional().describe("Existing snapshot folder name returned by an earlier export, inside the configured export directory. No path separators or .. allowed. Resumes unfinished streams using their saved cursors and file byte offsets.")
|
|
3757
|
+
}),
|
|
3758
|
+
annotations: {
|
|
3759
|
+
readOnlyHint: false,
|
|
3760
|
+
destructiveHint: false,
|
|
3761
|
+
idempotentHint: false,
|
|
3762
|
+
openWorldHint: true
|
|
3763
|
+
},
|
|
3764
|
+
handler: async (params) => {
|
|
3765
|
+
const job = await backgroundJob(ctx, exportDir, () => prepareDataExport({
|
|
3766
|
+
...params,
|
|
3767
|
+
subdomain: ctx.subdomain,
|
|
3768
|
+
getToken: ctx.getToken,
|
|
3769
|
+
exportDir
|
|
3770
|
+
}));
|
|
3771
|
+
const manifest = await readManifest(job.snapshot);
|
|
3772
|
+
return { content: [{
|
|
3773
|
+
type: "text",
|
|
3774
|
+
text: JSON.stringify({
|
|
3775
|
+
...job,
|
|
3776
|
+
manifest,
|
|
3777
|
+
progress: `Read ${job.snapshot}/manifest.json or call export_data again while running. Resume after interruption with the snapshot folder name.`
|
|
3778
|
+
})
|
|
3779
|
+
}] };
|
|
3780
|
+
}
|
|
3781
|
+
});
|
|
3782
|
+
const backgroundJob = async (ctx, exportDir, prepare) => {
|
|
3783
|
+
const key = `${ctx.subdomain}:${resolve(exportDir)}`;
|
|
3784
|
+
let pending = jobs.get(key);
|
|
3785
|
+
if (!pending) {
|
|
3786
|
+
pending = startJob(prepare, key);
|
|
3787
|
+
jobs.set(key, pending);
|
|
3788
|
+
const reserved = pending;
|
|
3789
|
+
pending.catch(() => {
|
|
3790
|
+
if (jobs.get(key) === reserved) jobs.delete(key);
|
|
3791
|
+
});
|
|
3792
|
+
}
|
|
3793
|
+
return pending;
|
|
3794
|
+
};
|
|
3795
|
+
const startJob = async (prepare, key) => {
|
|
3796
|
+
const prepared = await prepare();
|
|
3797
|
+
const job = {
|
|
3798
|
+
snapshot: prepared.snapshot,
|
|
3799
|
+
running: true
|
|
3800
|
+
};
|
|
3801
|
+
prepared.run().catch((err) => {
|
|
3802
|
+
job.error = err instanceof Error ? err.message : String(err);
|
|
3803
|
+
}).finally(() => {
|
|
3804
|
+
job.running = false;
|
|
3805
|
+
jobs.delete(key);
|
|
3806
|
+
});
|
|
3807
|
+
return job;
|
|
3808
|
+
};
|
|
3809
|
+
//#endregion
|
|
3810
|
+
//#region src/export/goldens-tool.ts
|
|
3811
|
+
const createGoldensTool = (ctx, exportDir) => ({
|
|
3812
|
+
name: "record_goldens",
|
|
3813
|
+
namespace: "admin",
|
|
3814
|
+
readOnly: true,
|
|
3815
|
+
title: "Record Zendesk Golden Observations",
|
|
3816
|
+
description: "Record read-only golden master observations of Zendesk views, macro previews, SLAs, audits and search in local files in the background. Use after export_data to characterize current Zendesk behavior for replacement-system tests; use export_data for the underlying data snapshot and interactive tools for ticket work. Requires an admin sign-in and an operator-configured export directory on a stdio server. Only GET requests reach Zendesk; macro previews do not apply changes. Creates a new timestamped local folder containing potentially sensitive ticket, user and query data. Returns the snapshot path and manifest progress immediately. Call again while running to poll the same account/export-directory job, shared with export_data. The job stops when the server exits; resume skips done stages and already recorded views/macros. Read manifest.json and item indexes for errors, skipped items and progress.",
|
|
3817
|
+
inputSchema: z.object({
|
|
3818
|
+
data_snapshot: z.string().min(1).describe("Required data snapshot folder name produced by export_data, inside the configured export directory. No separators or .. allowed. Latest ticket versions determine samples and generated searches."),
|
|
3819
|
+
stages: z.array(z.enum(GOLDEN_STAGES)).min(1).optional().describe("Observation stages to record, for example [\"views\", \"macros\"]. Omit for context, views, macros, slas and search in order. Resume may select only originally selected stages."),
|
|
3820
|
+
resume: z.string().min(1).optional().describe("Existing golden snapshot folder name inside the configured export directory, without separators or double dots. Skips done stages and written view/macro files; requires the same data snapshot and recording user.")
|
|
3821
|
+
}),
|
|
3822
|
+
annotations: {
|
|
3823
|
+
readOnlyHint: false,
|
|
3824
|
+
destructiveHint: false,
|
|
3825
|
+
idempotentHint: false,
|
|
3826
|
+
openWorldHint: true
|
|
3827
|
+
},
|
|
3828
|
+
handler: async (params) => {
|
|
3829
|
+
const values = params;
|
|
3830
|
+
const job = await backgroundJob(ctx, exportDir, () => prepareGoldens({
|
|
3831
|
+
subdomain: ctx.subdomain,
|
|
3832
|
+
getToken: ctx.getToken,
|
|
3833
|
+
exportDir,
|
|
3834
|
+
dataSnapshot: values.data_snapshot,
|
|
3835
|
+
...values.stages ? { stages: values.stages } : {},
|
|
3836
|
+
...values.resume !== void 0 ? { resume: values.resume } : {}
|
|
3837
|
+
}));
|
|
3838
|
+
return { content: [{
|
|
3839
|
+
type: "text",
|
|
3840
|
+
text: JSON.stringify({
|
|
3841
|
+
...job,
|
|
3842
|
+
manifest: await readManifest(job.snapshot),
|
|
3843
|
+
progress: `Read ${job.snapshot}/manifest.json or call record_goldens again while running. Resume with the snapshot folder name and original data_snapshot.`
|
|
3844
|
+
})
|
|
3845
|
+
}] };
|
|
3846
|
+
}
|
|
3847
|
+
});
|
|
3848
|
+
//#endregion
|
|
3849
|
+
//#region src/tools/admin.ts
|
|
3850
|
+
/**
|
|
3851
|
+
* Opt-in: the admin namespace exists only when the operator configured an export
|
|
3852
|
+
* directory (`--export-dir`, stdio only). Off by default, so no existing surface
|
|
3853
|
+
* changes, notably a `single`-mode proxy's read-only hints.
|
|
3854
|
+
*/
|
|
3855
|
+
const createAdminTools = (ctx) => ctx.exportDir ? [
|
|
3856
|
+
exportAccountConfig(ctx, ctx.exportDir),
|
|
3857
|
+
createDataExportTool(ctx, ctx.exportDir),
|
|
3858
|
+
createGoldensTool(ctx, ctx.exportDir),
|
|
3859
|
+
listApiTokens(ctx)
|
|
3860
|
+
] : [];
|
|
3861
|
+
const listApiTokens = (ctx) => ({
|
|
3862
|
+
name: "list_api_tokens",
|
|
3863
|
+
namespace: "admin",
|
|
3864
|
+
readOnly: true,
|
|
3865
|
+
title: "List Zendesk API Tokens",
|
|
3866
|
+
description: "List all account API tokens with their metadata and last usage time, excluding token values. Use it to audit active or unused API credentials; returns id, description, active, created_at, updated_at and last_used (null if never used), following every page. Only reads Zendesk and writes no local files; never returns token or visible_token values. Requires an admin or an agent with Manage APIs permission; available only when the server was started with an export directory. The Zendesk endpoint is deprecated and scheduled for removal on April 30, 2027.",
|
|
3867
|
+
inputSchema: z.object({}),
|
|
3868
|
+
annotations: {
|
|
3869
|
+
readOnlyHint: true,
|
|
3870
|
+
destructiveHint: false,
|
|
3871
|
+
idempotentHint: true,
|
|
3872
|
+
openWorldHint: true
|
|
3873
|
+
},
|
|
3874
|
+
handler: async () => {
|
|
3875
|
+
const token = await ctx.getToken();
|
|
3876
|
+
const apiTokens = [];
|
|
3877
|
+
let next = {
|
|
3878
|
+
path: "/api_tokens",
|
|
3879
|
+
params: {}
|
|
3880
|
+
};
|
|
3881
|
+
while (next) {
|
|
3882
|
+
const body = await zendeskGet(ctx.subdomain, token, next.path, next.params);
|
|
3883
|
+
apiTokens.push(...body.api_tokens.map(({ id, description, active, created_at, updated_at, last_used }) => ({
|
|
3884
|
+
id,
|
|
3885
|
+
description,
|
|
3886
|
+
active,
|
|
3887
|
+
created_at,
|
|
3888
|
+
updated_at,
|
|
3889
|
+
last_used
|
|
3890
|
+
})));
|
|
3891
|
+
next = nextRequest(body, ctx.subdomain);
|
|
3892
|
+
}
|
|
3893
|
+
return { content: [{
|
|
3894
|
+
type: "text",
|
|
3895
|
+
text: JSON.stringify({ api_tokens: apiTokens }, null, 2)
|
|
3896
|
+
}] };
|
|
3897
|
+
}
|
|
3898
|
+
});
|
|
3899
|
+
const exportAccountConfig = (ctx, exportDir) => ({
|
|
3900
|
+
name: "export_account_config",
|
|
3901
|
+
namespace: "admin",
|
|
3902
|
+
readOnly: true,
|
|
3903
|
+
title: "Export Zendesk Account Configuration",
|
|
3904
|
+
description: "Export the Zendesk account's admin configuration to local JSON files, one per resource: business rules (triggers, automations, macros, views), ticket forms, fields and statuses, SLA policies, schedules with their holidays, groups, roles, brands, webhooks, account settings and more. Use it to audit, document, diff or migrate a Zendesk setup, then read the files for the details instead of pulling them through the conversation; to look up one field id, macro, view or SLA policy during ticket work, use list_ticket_fields, list_macros, list_views or list_sla_policies instead. It only reads from Zendesk; on the machine running the server it creates a new timestamped folder in the operator's export directory and never overwrites an earlier export. Returns the folder path and a per-resource summary: items exported, or why a resource was skipped (403: no access; 404: not available on this account) or failed. Needs an admin sign-in; available only when the server was started with an export directory.",
|
|
3905
|
+
inputSchema: z.object({ resources: z.array(z.enum(RESOURCE_KEYS)).min(1).optional().describe("Which parts of the configuration to export, by catalog name (e.g. [\"triggers\", \"automations\", \"ticket_forms\"]). Omit to export the whole catalog, which is what an audit or a migration needs.") }),
|
|
3906
|
+
annotations: {
|
|
3907
|
+
readOnlyHint: false,
|
|
3908
|
+
destructiveHint: false,
|
|
3909
|
+
idempotentHint: false,
|
|
3910
|
+
openWorldHint: true
|
|
3911
|
+
},
|
|
3912
|
+
handler: async (params) => {
|
|
3913
|
+
const { resources } = params;
|
|
3914
|
+
const selected = RESOURCE_KEYS.filter((key) => resources?.includes(key) ?? true);
|
|
3915
|
+
await ctx.getToken();
|
|
3916
|
+
const exportedAt = /* @__PURE__ */ new Date();
|
|
3917
|
+
await mkdir(exportDir, { recursive: true });
|
|
3918
|
+
const snapshot = join(exportDir, snapshotDirName(ctx.subdomain, exportedAt));
|
|
3919
|
+
await mkdir(snapshot);
|
|
3920
|
+
const entries = await runConfigExport(ctx, snapshot, selected, void 0, exportedAt);
|
|
3921
|
+
return { content: [{
|
|
3922
|
+
type: "text",
|
|
3923
|
+
text: formatSummary(ctx.subdomain, snapshot, entries)
|
|
3924
|
+
}] };
|
|
3925
|
+
}
|
|
3926
|
+
});
|
|
3927
|
+
//#endregion
|
|
1896
3928
|
//#region src/utils/article-order.ts
|
|
1897
3929
|
const hasPositionInversion = (order) => {
|
|
1898
3930
|
for (let i = 0; i < order.length - 1; i += 1) {
|
|
@@ -4374,7 +6406,8 @@ const createAllTools = (ctx) => [
|
|
|
4374
6406
|
...createTicketRelationTools(ctx),
|
|
4375
6407
|
...createSearchTools(ctx),
|
|
4376
6408
|
...createHelpCenterTools(ctx),
|
|
4377
|
-
...createUserTools(ctx)
|
|
6409
|
+
...createUserTools(ctx),
|
|
6410
|
+
...createAdminTools(ctx)
|
|
4378
6411
|
];
|
|
4379
6412
|
//#endregion
|
|
4380
6413
|
//#region src/utils/validation.ts
|
|
@@ -4443,6 +6476,10 @@ const NAMESPACE_LABELS = {
|
|
|
4443
6476
|
users: {
|
|
4444
6477
|
toolName: "zendesk_users",
|
|
4445
6478
|
title: "Zendesk Users"
|
|
6479
|
+
},
|
|
6480
|
+
admin: {
|
|
6481
|
+
toolName: "zendesk_admin",
|
|
6482
|
+
title: "Zendesk Admin"
|
|
4446
6483
|
}
|
|
4447
6484
|
};
|
|
4448
6485
|
const summarizeDescription = (description) => {
|
|
@@ -4624,7 +6661,8 @@ const createMcpServer = (config, getToken, logger = silentLogger, onUnauthorized
|
|
|
4624
6661
|
const tools = createAllTools({
|
|
4625
6662
|
subdomain: config.subdomain,
|
|
4626
6663
|
getToken,
|
|
4627
|
-
localFileAccess: config.transport === "stdio"
|
|
6664
|
+
localFileAccess: config.transport === "stdio",
|
|
6665
|
+
exportDir: config.exportDir
|
|
4628
6666
|
});
|
|
4629
6667
|
registerToolset(server, {
|
|
4630
6668
|
config,
|
|
@@ -4665,6 +6703,10 @@ const TOOL_MODULES = [
|
|
|
4665
6703
|
{
|
|
4666
6704
|
file: "users.ts",
|
|
4667
6705
|
factory: "createUserTools"
|
|
6706
|
+
},
|
|
6707
|
+
{
|
|
6708
|
+
file: "admin.ts",
|
|
6709
|
+
factory: "createAdminTools"
|
|
4668
6710
|
}
|
|
4669
6711
|
];
|
|
4670
6712
|
/**
|
|
@@ -4696,7 +6738,8 @@ const createReloadableServer = (config, getToken, logger = silentLogger, onUnaut
|
|
|
4696
6738
|
const ctx = {
|
|
4697
6739
|
subdomain: config.subdomain,
|
|
4698
6740
|
getToken,
|
|
4699
|
-
localFileAccess: config.transport === "stdio"
|
|
6741
|
+
localFileAccess: config.transport === "stdio",
|
|
6742
|
+
exportDir: config.exportDir
|
|
4700
6743
|
};
|
|
4701
6744
|
const params = {
|
|
4702
6745
|
config,
|
|
@@ -4737,7 +6780,7 @@ const createReloadableServer = (config, getToken, logger = silentLogger, onUnaut
|
|
|
4737
6780
|
const registerReloadTool = (server, reload, logger = silentLogger) => {
|
|
4738
6781
|
server.registerTool("reload_tools", {
|
|
4739
6782
|
title: "Reload tools from source (dev)",
|
|
4740
|
-
description: "Dev only. Re-imports the Zendesk tool modules from source and re-registers them on this live session, so tool code you just edited takes effect without restarting the server or reconnecting the client. Call it once at the end of an edit cycle, before testing your changes; the refreshed tool list is announced via tools/list_changed. Only the tool modules (tickets, search, help_center, users) are reloaded — edits to shared infrastructure (HTTP client, shared definitions, server wiring) still require a full restart. Takes no arguments and makes no Zendesk API calls.",
|
|
6783
|
+
description: "Dev only. Re-imports the Zendesk tool modules from source and re-registers them on this live session, so tool code you just edited takes effect without restarting the server or reconnecting the client. Call it once at the end of an edit cycle, before testing your changes; the refreshed tool list is announced via tools/list_changed. Only the tool modules (tickets, search, help_center, users, admin) are reloaded — edits to shared infrastructure (HTTP client, shared definitions, server wiring) still require a full restart. Takes no arguments and makes no Zendesk API calls.",
|
|
4741
6784
|
inputSchema: z.object({}).strict(),
|
|
4742
6785
|
annotations: {
|
|
4743
6786
|
readOnlyHint: false,
|
|
@@ -4787,6 +6830,177 @@ const startDevServer = async (config, getToken, logger = silentLogger, onUnautho
|
|
|
4787
6830
|
};
|
|
4788
6831
|
/* v8 ignore stop */
|
|
4789
6832
|
//#endregion
|
|
6833
|
+
//#region src/export/cli.ts
|
|
6834
|
+
const USAGE = "Usage: zendesk-mcp-server export-data <subdomain> --export-dir <absolute-local-path> [--since <ISO|epoch>] [--only <stream>]... [--resume <folder>] [--rate <requests/min>] [--log-level <debug|info|warn|error>]";
|
|
6835
|
+
const GOLDENS_USAGE = "Usage: zendesk-mcp-server record-goldens <subdomain> --export-dir <absolute-local-path> --data-snapshot <folder-name> [--only <stage>]... [--view-row-cap <rows>] [--macro-sample <tickets>] [--sla-sample <tickets>] [--queries <absolute-json-path>] [--resume <folder>] [--rate <requests/min>] [--log-level <debug|info|warn|error>]";
|
|
6836
|
+
const SUBDOMAIN = /^[a-z0-9][a-z0-9-]*$/i;
|
|
6837
|
+
const NETWORK_PATH = /^[\\/]{2}/;
|
|
6838
|
+
const parseStrictOptions = (args, goldens = false) => {
|
|
6839
|
+
try {
|
|
6840
|
+
const parsed = parseArgs({
|
|
6841
|
+
args,
|
|
6842
|
+
strict: true,
|
|
6843
|
+
allowPositionals: true,
|
|
6844
|
+
options: {
|
|
6845
|
+
"export-dir": { type: "string" },
|
|
6846
|
+
"data-snapshot": { type: "string" },
|
|
6847
|
+
"view-row-cap": { type: "string" },
|
|
6848
|
+
"macro-sample": { type: "string" },
|
|
6849
|
+
"sla-sample": { type: "string" },
|
|
6850
|
+
queries: { type: "string" },
|
|
6851
|
+
since: { type: "string" },
|
|
6852
|
+
only: {
|
|
6853
|
+
type: "string",
|
|
6854
|
+
multiple: true
|
|
6855
|
+
},
|
|
6856
|
+
resume: { type: "string" },
|
|
6857
|
+
rate: { type: "string" },
|
|
6858
|
+
"log-level": { type: "string" }
|
|
6859
|
+
}
|
|
6860
|
+
});
|
|
6861
|
+
const goldenFlags = [
|
|
6862
|
+
"data-snapshot",
|
|
6863
|
+
"view-row-cap",
|
|
6864
|
+
"macro-sample",
|
|
6865
|
+
"sla-sample",
|
|
6866
|
+
"queries"
|
|
6867
|
+
];
|
|
6868
|
+
if (Object.keys(parsed.values).some((key) => goldens ? key === "since" : goldenFlags.includes(key))) throw new Error("Unsupported flag");
|
|
6869
|
+
return parsed;
|
|
6870
|
+
} catch (err) {
|
|
6871
|
+
throw new Error(goldens ? GOLDENS_USAGE : USAGE, { cause: err });
|
|
6872
|
+
}
|
|
6873
|
+
};
|
|
6874
|
+
const parseExportArgs = (args) => {
|
|
6875
|
+
const { values, positionals } = parseStrictOptions(args);
|
|
6876
|
+
const [subdomain] = positionals;
|
|
6877
|
+
const exportDir = values["export-dir"];
|
|
6878
|
+
if (positionals.length !== 1 || !subdomain || !SUBDOMAIN.test(subdomain) || !exportDir || !isAbsolute(exportDir) || NETWORK_PATH.test(exportDir)) throw new Error(USAGE);
|
|
6879
|
+
if (values.only?.some((name) => !DATA_STREAMS.includes(name))) throw new Error(`Unknown stream. ${USAGE}`);
|
|
6880
|
+
const standard = values.rate === void 0 ? 300 : Number(values.rate);
|
|
6881
|
+
resolveRates({ standard });
|
|
6882
|
+
if (values.since !== void 0) parseSince(values.since);
|
|
6883
|
+
if (values.resume !== void 0) validateResumeName(values.resume);
|
|
6884
|
+
const logLevel = LogLevel.parse(values["log-level"] ?? "info");
|
|
6885
|
+
return {
|
|
6886
|
+
subdomain,
|
|
6887
|
+
exportDir,
|
|
6888
|
+
...values.since !== void 0 ? { since: values.since } : {},
|
|
6889
|
+
...values.only ? { streams: values.only } : {},
|
|
6890
|
+
...values.resume !== void 0 ? { resume: values.resume } : {},
|
|
6891
|
+
rates: { standard },
|
|
6892
|
+
logLevel
|
|
6893
|
+
};
|
|
6894
|
+
};
|
|
6895
|
+
const parseGoldensArgs = (args) => {
|
|
6896
|
+
const { values, positionals } = parseStrictOptions(args, true);
|
|
6897
|
+
const shared = parseExportArgs([
|
|
6898
|
+
...positionals,
|
|
6899
|
+
"--export-dir",
|
|
6900
|
+
values["export-dir"] ?? "",
|
|
6901
|
+
"--rate",
|
|
6902
|
+
values.rate ?? "300",
|
|
6903
|
+
"--log-level",
|
|
6904
|
+
values["log-level"] ?? "info",
|
|
6905
|
+
...values.resume !== void 0 ? ["--resume", values.resume] : []
|
|
6906
|
+
]);
|
|
6907
|
+
if (!values["data-snapshot"]) throw new Error("record-goldens requires --data-snapshot <folder name>");
|
|
6908
|
+
validateResumeName(values["data-snapshot"]);
|
|
6909
|
+
if (values.only?.some((name) => !GOLDEN_STAGES.includes(name))) throw new Error("Unknown golden stage");
|
|
6910
|
+
const numberFlag = (value) => {
|
|
6911
|
+
const parsed = Number(value);
|
|
6912
|
+
if (!value.trim() || !Number.isSafeInteger(parsed) || parsed < 0) throw new Error("Sample sizes and row caps must be nonnegative integers");
|
|
6913
|
+
return parsed;
|
|
6914
|
+
};
|
|
6915
|
+
const queries = values.queries;
|
|
6916
|
+
if (queries !== void 0 && (!isAbsolute(queries) || NETWORK_PATH.test(queries))) throw new Error("Queries must be an absolute local JSON file");
|
|
6917
|
+
return {
|
|
6918
|
+
...shared,
|
|
6919
|
+
dataSnapshot: values["data-snapshot"],
|
|
6920
|
+
...values.only ? { stages: values.only } : {},
|
|
6921
|
+
options: {
|
|
6922
|
+
...values["view-row-cap"] !== void 0 ? { viewRowCap: numberFlag(values["view-row-cap"]) } : {},
|
|
6923
|
+
...values["macro-sample"] !== void 0 ? { macroSample: numberFlag(values["macro-sample"]) } : {},
|
|
6924
|
+
...values["sla-sample"] !== void 0 ? { slaSample: numberFlag(values["sla-sample"]) } : {},
|
|
6925
|
+
...queries !== void 0 ? { queries } : {}
|
|
6926
|
+
}
|
|
6927
|
+
};
|
|
6928
|
+
};
|
|
6929
|
+
const waitForExportToken = async (getToken, stderr, deps = defaultDeps) => {
|
|
6930
|
+
const deadline = deps.now() + 6e5;
|
|
6931
|
+
let shown;
|
|
6932
|
+
while (true) try {
|
|
6933
|
+
return await getToken();
|
|
6934
|
+
} catch (err) {
|
|
6935
|
+
if (!isAuthRequiredError(err)) throw err;
|
|
6936
|
+
if (shown !== err.authorizeUrl) {
|
|
6937
|
+
stderr(`Authorize read-only export: ${err.authorizeUrl}`);
|
|
6938
|
+
shown = err.authorizeUrl;
|
|
6939
|
+
}
|
|
6940
|
+
if (deps.now() >= deadline) throw new Error("Timed out waiting for Zendesk authorization", { cause: err });
|
|
6941
|
+
await deps.sleep(3e3);
|
|
6942
|
+
}
|
|
6943
|
+
};
|
|
6944
|
+
const runCli = async (args, goldens) => {
|
|
6945
|
+
const goldenOptions = goldens ? parseGoldensArgs(args) : void 0;
|
|
6946
|
+
const options = goldenOptions ?? parseExportArgs(args);
|
|
6947
|
+
const config = loadConfig([
|
|
6948
|
+
options.subdomain,
|
|
6949
|
+
"--export-dir",
|
|
6950
|
+
options.exportDir,
|
|
6951
|
+
"--read-only",
|
|
6952
|
+
"--log-level",
|
|
6953
|
+
options.logLevel
|
|
6954
|
+
]);
|
|
6955
|
+
const logger = createLogger(config.logLevel);
|
|
6956
|
+
const tokenSource = config.zendeskEmail && config.zendeskApiToken ? {
|
|
6957
|
+
getToken: () => buildBasicAuthHeader(config.zendeskEmail ?? "", config.zendeskApiToken ?? ""),
|
|
6958
|
+
dispose: () => void 0
|
|
6959
|
+
} : createTokenStore({
|
|
6960
|
+
subdomain: config.subdomain,
|
|
6961
|
+
oauthClientId: config.oauthClientId,
|
|
6962
|
+
callbackPort: config.callbackPort,
|
|
6963
|
+
readOnly: true
|
|
6964
|
+
}, logger);
|
|
6965
|
+
try {
|
|
6966
|
+
const auth = { getToken: () => waitForExportToken(tokenSource.getToken, (line) => console.error(line)) };
|
|
6967
|
+
if (goldenOptions) {
|
|
6968
|
+
const result = await runGoldens({
|
|
6969
|
+
...goldenOptions,
|
|
6970
|
+
...auth,
|
|
6971
|
+
onProgress: (manifest) => {
|
|
6972
|
+
const progress = Object.entries(manifest.stages).map(([name, state]) => `${name}: ${state.status}, ${state.items} items`).join("; ");
|
|
6973
|
+
logger.info("goldens_progress", { progress });
|
|
6974
|
+
}
|
|
6975
|
+
});
|
|
6976
|
+
console.log(JSON.stringify({
|
|
6977
|
+
snapshot: result.snapshot,
|
|
6978
|
+
stages: result.manifest.stages,
|
|
6979
|
+
duration_ms: result.duration_ms
|
|
6980
|
+
}));
|
|
6981
|
+
return Object.values(result.manifest.stages).some((stage) => stage.status === "failed") ? 1 : 0;
|
|
6982
|
+
}
|
|
6983
|
+
const result = await runDataExport({
|
|
6984
|
+
...options,
|
|
6985
|
+
...auth,
|
|
6986
|
+
onProgress: (manifest) => {
|
|
6987
|
+
const progress = Object.entries(manifest.streams).map(([name, state]) => `${name}: ${state.status}, ${state.pages} pages`).join("; ");
|
|
6988
|
+
logger.info("export_progress", { progress });
|
|
6989
|
+
}
|
|
6990
|
+
});
|
|
6991
|
+
console.log(JSON.stringify({
|
|
6992
|
+
snapshot: result.snapshot,
|
|
6993
|
+
streams: result.manifest.streams,
|
|
6994
|
+
duration_ms: result.duration_ms
|
|
6995
|
+
}));
|
|
6996
|
+
return Object.values(result.manifest.streams).some((stream) => stream.status === "failed") ? 1 : 0;
|
|
6997
|
+
} finally {
|
|
6998
|
+
tokenSource.dispose();
|
|
6999
|
+
}
|
|
7000
|
+
};
|
|
7001
|
+
const runExportCli = (args) => runCli(args, false);
|
|
7002
|
+
const runGoldensCli = (args) => runCli(args, true);
|
|
7003
|
+
//#endregion
|
|
4790
7004
|
//#region src/transports/http.ts
|
|
4791
7005
|
const WILDCARD_HOSTS = /* @__PURE__ */ new Set([
|
|
4792
7006
|
"0.0.0.0",
|
|
@@ -5247,6 +7461,14 @@ const connectStdio = async (config, tokenSource, logger) => {
|
|
|
5247
7461
|
return server;
|
|
5248
7462
|
};
|
|
5249
7463
|
const main = async () => {
|
|
7464
|
+
if (process.argv[2] === "record-goldens") {
|
|
7465
|
+
process.exitCode = await runGoldensCli(process.argv.slice(3));
|
|
7466
|
+
return;
|
|
7467
|
+
}
|
|
7468
|
+
if (process.argv[2] === "export-data") {
|
|
7469
|
+
process.exitCode = await runExportCli(process.argv.slice(3));
|
|
7470
|
+
return;
|
|
7471
|
+
}
|
|
5250
7472
|
const config = loadConfig();
|
|
5251
7473
|
const logger = createLogger(config.logLevel);
|
|
5252
7474
|
if (config.transport === "stdio") {
|