-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathwrapAction.ts
More file actions
116 lines (106 loc) · 2.91 KB
/
wrapAction.ts
File metadata and controls
116 lines (106 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Stainless from "@stainless-api/sdk";
import { getInput } from "./compat/input";
import { getStainlessAuth } from "./compat";
import { createAutoRefreshFetch, getStainlessClient } from "./stainless";
import { logger } from "./logger";
const accumulatedBuildIds = new Set<string>();
export function addBuildIdForTelemetry(buildId: string) {
accumulatedBuildIds.add(buildId);
}
/**
* Wrap the body of an action, providing the Stainless client and additionally reporting
* success/error results if telemetry is enabled.
*
* **Important:** The action must have a `project` input and a Stainless auth token
* for this to work.
*/
export function wrapAction(
actionType: string,
fn: (stainless: Stainless) => Promise<void>,
): () => Promise<void> {
return async () => {
let stainless: Stainless | undefined;
let projectName: string | undefined;
try {
projectName = getInput("project", { required: true });
const auth = await getStainlessAuth();
stainless = getStainlessClient(actionType, {
project: projectName,
apiKey: auth.key,
logLevel: "warn",
logger,
fetch: createAutoRefreshFetch(auth, getStainlessAuth),
});
await fn(stainless);
await maybeReportResult({
stainless,
projectName,
actionType,
successOrError: { result: "success" },
});
} catch (error) {
logger.fatal("Error in action:", error);
if (stainless) {
await maybeReportResult({
stainless,
projectName,
actionType,
successOrError: serializeError(error),
});
}
process.exit(1);
}
};
}
type ReportResultSuccessOrError =
| { result: "success" }
| {
result: "error";
error_message: string;
error_stack?: string;
error_name?: string;
};
function serializeError(
error: unknown,
): Extract<ReportResultSuccessOrError, { result: "error" }> {
const maybeTypedError = error instanceof Error ? error : undefined;
return {
result: "error",
error_message: maybeTypedError?.message ?? String(error),
error_name: maybeTypedError?.name,
error_stack: maybeTypedError?.stack,
};
}
type ReportResultBody = {
project?: string;
build_ids?: string[];
action_type: string;
} & ReportResultSuccessOrError;
async function maybeReportResult({
stainless,
projectName,
actionType,
successOrError,
}: {
stainless: Stainless;
projectName?: string;
actionType: string;
successOrError: ReportResultSuccessOrError;
}) {
if (process.env.STAINLESS_DISABLE_TELEMETRY) {
return;
}
try {
const body: ReportResultBody = {
project: projectName,
build_ids: [...accumulatedBuildIds],
action_type: actionType,
...successOrError,
};
await stainless.post("/api/reports/action-result", {
body,
});
} catch (error) {
logger.error("Error reporting result to Stainless", error);
}
}