@theholocron/holocron-plugin-sentry 5.0.0-alpha.25 → 5.0.0-alpha.250
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/dist/index.d.mts +33 -30
- package/dist/index.mjs +499 -14
- package/package.json +18 -16
package/dist/index.d.mts
CHANGED
|
@@ -1,28 +1,40 @@
|
|
|
1
|
-
import { AuthError, Errors, ResolveTokenInput } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, EnsureRelayInput, EnsureRelayResult, Errors, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
|
|
2
2
|
import { SentryClient, SentryClientOptions, createSentryClient } from "@theholocron/sentry-client";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
export declare const resolveToken: (input?: ResolveTokenInput) => string;
|
|
4
|
+
/**
|
|
5
|
+
* The token for the Comlink relay: an integration, its client secret and an alert rule need more than the general token
|
|
6
|
+
* (`alerts:write`, `org:write`, `event:read`), so it has its own.
|
|
7
|
+
*/
|
|
8
|
+
export declare const resolveComlinkToken: (input?: ResolveTokenInput) => string;
|
|
9
|
+
/**
|
|
10
|
+
* The token for the relay: the Comlink one when there is one, else the general Sentry token as the last resort. The
|
|
11
|
+
* fallback only helps if the general token happens to carry the scopes; otherwise Sentry's answer names what is missing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveRelayToken(input?: ResolveTokenInput): string;
|
|
7
14
|
interface SentryErrorsOptions {
|
|
8
15
|
/** Sentry organization slug. Required for `whoami` and `ensureProject`. */
|
|
9
16
|
org?: string;
|
|
10
17
|
/** Default team slug for project creation. Defaults to org slug. */
|
|
11
18
|
team?: string;
|
|
12
19
|
}
|
|
13
|
-
declare class SentryErrors implements Errors {
|
|
20
|
+
export declare class SentryErrors implements Errors {
|
|
14
21
|
private readonly client;
|
|
15
22
|
private readonly opts;
|
|
23
|
+
/** The client for the relay, made with the Comlink token (see `resolveRelayToken`). */
|
|
24
|
+
private readonly relayClient;
|
|
16
25
|
readonly key: "errors";
|
|
17
26
|
readonly providerName = "sentry";
|
|
18
|
-
constructor(client: () => SentryClient, opts: SentryErrorsOptions
|
|
27
|
+
constructor(client: () => SentryClient, opts: SentryErrorsOptions,
|
|
28
|
+
/** The client for the relay, made with the Comlink token (see `resolveRelayToken`). */
|
|
29
|
+
relayClient?: () => SentryClient);
|
|
30
|
+
ensureRelay(input: EnsureRelayInput): Promise<EnsureRelayResult>;
|
|
19
31
|
describe(): Promise<{
|
|
20
32
|
provider: string;
|
|
21
33
|
envKeys: string[];
|
|
22
34
|
}>;
|
|
23
35
|
whoami(): Promise<{
|
|
24
36
|
org: string;
|
|
25
|
-
}>;
|
|
37
|
+
} | null>;
|
|
26
38
|
ensureProject(input: {
|
|
27
39
|
name: string;
|
|
28
40
|
platform?: string;
|
|
@@ -37,25 +49,12 @@ declare class SentryErrors implements Errors {
|
|
|
37
49
|
*/
|
|
38
50
|
private requireOrg;
|
|
39
51
|
}
|
|
40
|
-
//#endregion
|
|
41
|
-
//#region src/verify-token.d.ts
|
|
42
|
-
interface VerifyTokenSuccess {
|
|
43
|
-
ok: true;
|
|
44
|
-
subject: string;
|
|
45
|
-
}
|
|
46
|
-
interface VerifyTokenFailure {
|
|
47
|
-
ok: false;
|
|
48
|
-
message: string;
|
|
49
|
-
}
|
|
50
|
-
type VerifyTokenResult = VerifyTokenSuccess | VerifyTokenFailure;
|
|
51
52
|
interface VerifyTokenOptions {
|
|
52
53
|
baseUrl?: string;
|
|
53
54
|
fetch?: typeof fetch;
|
|
54
55
|
}
|
|
55
|
-
declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult>;
|
|
56
|
-
|
|
57
|
-
//#region src/index.d.ts
|
|
58
|
-
interface SentryPluginOptions extends ResolveTokenInput, SentryErrorsOptions {
|
|
56
|
+
export declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult$1>;
|
|
57
|
+
export interface SentryPluginOptions extends ResolveTokenInput, SentryErrorsOptions {
|
|
59
58
|
/**
|
|
60
59
|
* Sentry organization slug. Optional at load time — the `errors` capability
|
|
61
60
|
* activates from env vars alone — but required for `whoami` / `ensureProject`
|
|
@@ -66,18 +65,22 @@ interface SentryPluginOptions extends ResolveTokenInput, SentryErrorsOptions {
|
|
|
66
65
|
baseUrl?: string;
|
|
67
66
|
fetch?: typeof fetch;
|
|
68
67
|
}
|
|
69
|
-
interface PluginContext {
|
|
68
|
+
export interface PluginContext {
|
|
70
69
|
options: SentryPluginOptions;
|
|
71
70
|
client: () => SentryClient;
|
|
71
|
+
/** The client for the Comlink relay: its own token (`sentry.comlink`), the general one only as a last resort. */
|
|
72
|
+
relayClient: () => SentryClient;
|
|
72
73
|
}
|
|
73
|
-
declare function createContext(options: SentryPluginOptions): PluginContext;
|
|
74
|
-
declare function errors(ctx: PluginContext): Errors;
|
|
75
|
-
declare function createPlugin(options: SentryPluginOptions): {
|
|
74
|
+
export declare function createContext(options: SentryPluginOptions): PluginContext;
|
|
75
|
+
export declare function errors(ctx: PluginContext): Errors;
|
|
76
|
+
export declare function createPlugin(options: SentryPluginOptions): {
|
|
76
77
|
name: string;
|
|
78
|
+
probes: {
|
|
79
|
+
connection: import("@theholocron/cli").Probe;
|
|
80
|
+
};
|
|
77
81
|
capabilities: {
|
|
78
82
|
errors: () => Errors;
|
|
79
83
|
};
|
|
80
84
|
};
|
|
81
|
-
declare const AUTH_HINT: string;
|
|
82
|
-
|
|
83
|
-
export { AUTH_HINT, AuthError, PluginContext, type ResolveTokenInput, type SentryClient, type SentryClientOptions, SentryErrors, type SentryErrorsOptions, SentryPluginOptions, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createContext, createPlugin, createSentryClient, errors, resolveToken, verifyToken };
|
|
85
|
+
export declare const AUTH_HINT: string;
|
|
86
|
+
export { AuthError, type ResolveTokenInput, type SentryClient, type SentryClientOptions, type SentryErrorsOptions, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createSentryClient };
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,251 @@
|
|
|
1
|
-
import { AuthError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, ProviderApiError, connectionProbe, createFeatureResolver, createResolveToken } from "@theholocron/cli";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { createSentryClient } from "@theholocron/sentry-client";
|
|
3
|
-
|
|
4
|
+
import { errorMessage } from "@theholocron/misc-utils";
|
|
5
|
+
// src/auth.ts
|
|
4
6
|
const resolveToken = createResolveToken({
|
|
5
7
|
envName: "HOLOCRON_SENTRY_TOKEN",
|
|
6
8
|
vendorEnvName: "SENTRY_AUTH_TOKEN",
|
|
7
9
|
keyringService: "sentry",
|
|
8
10
|
errorMessage: "no Sentry auth token found. Pass --token <TOKEN>, set HOLOCRON_SENTRY_TOKEN / SENTRY_AUTH_TOKEN, or run: holocron auth set sentry <TOKEN>"
|
|
9
11
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The token for the Comlink relay: an integration, its client secret and an alert rule need more than the general token
|
|
14
|
+
* (`alerts:write`, `org:write`, `event:read`), so it has its own.
|
|
15
|
+
*/
|
|
16
|
+
const resolveComlinkToken = createFeatureResolver({
|
|
17
|
+
envName: "HOLOCRON_SENTRY_COMLINK_TOKEN",
|
|
18
|
+
keyringKey: "sentry.comlink",
|
|
19
|
+
vendor: "Sentry"
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* The token for the relay: the Comlink one when there is one, else the general Sentry token as the last resort. The
|
|
23
|
+
* fallback only helps if the general token happens to carry the scopes; otherwise Sentry's answer names what is missing.
|
|
24
|
+
*/
|
|
25
|
+
function resolveRelayToken(input = {}) {
|
|
26
|
+
try {
|
|
27
|
+
return resolveComlinkToken(input);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
if (!(err instanceof AuthError)) throw err;
|
|
30
|
+
return resolveToken(input);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// src/capabilities/relay.ts
|
|
34
|
+
/** What the integration needs to read: the alert's event. It is also why reading its secret needs the same scope. */
|
|
35
|
+
const INTEGRATION_SCOPES = ["event:read"];
|
|
36
|
+
const DEFAULT_FREQUENCY = 5;
|
|
37
|
+
/**
|
|
38
|
+
* The alert action that sends through an integration, in the shape Sentry's own page writes when "Send a notification via
|
|
39
|
+
* an integration" is picked: a `webhook` action named by the integration's slug. (A `sentry_app` action with the numeric
|
|
40
|
+
* id is accepted too, but the page shows it as an unknown integration.)
|
|
41
|
+
*/
|
|
42
|
+
const integrationAction = (slug) => ({
|
|
43
|
+
type: "webhook",
|
|
44
|
+
config: {
|
|
45
|
+
targetType: null,
|
|
46
|
+
targetDisplay: null,
|
|
47
|
+
targetIdentifier: slug
|
|
48
|
+
},
|
|
49
|
+
data: {}
|
|
50
|
+
});
|
|
51
|
+
const sendsThrough = (action, slug) => action.type === "webhook" && action.config["targetIdentifier"] === slug;
|
|
52
|
+
/** The `sentry_app` form an earlier version wrote for this integration, which the page cannot name. */
|
|
53
|
+
const isLegacyAction = (action, appId) => action.type === "sentry_app" && String(action.config["targetIdentifier"]) === String(appId);
|
|
54
|
+
/** A workflow as Sentry returned it, as the body to send back (the nested ids are not part of an update). */
|
|
55
|
+
function toInput(wf) {
|
|
56
|
+
return {
|
|
57
|
+
name: wf.name,
|
|
58
|
+
enabled: wf.enabled,
|
|
59
|
+
environment: wf.environment,
|
|
60
|
+
config: wf.config,
|
|
61
|
+
detectorIds: wf.detectorIds,
|
|
62
|
+
triggers: {
|
|
63
|
+
logicType: wf.triggers.logicType,
|
|
64
|
+
conditions: wf.triggers.conditions.map((c) => ({
|
|
65
|
+
type: c.type,
|
|
66
|
+
comparison: c.comparison,
|
|
67
|
+
conditionResult: c.conditionResult
|
|
68
|
+
}))
|
|
69
|
+
},
|
|
70
|
+
actionFilters: wf.actionFilters.map((f) => ({
|
|
71
|
+
logicType: f.logicType,
|
|
72
|
+
conditions: f.conditions.map((c) => ({
|
|
73
|
+
type: c.type,
|
|
74
|
+
comparison: c.comparison,
|
|
75
|
+
conditionResult: c.conditionResult
|
|
76
|
+
})),
|
|
77
|
+
actions: f.actions.map((a) => ({
|
|
78
|
+
type: a.type,
|
|
79
|
+
config: a.config,
|
|
80
|
+
data: a.data
|
|
81
|
+
}))
|
|
82
|
+
}))
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Sentry's own "high priority issues" rule, which is the default for a new alert. */
|
|
86
|
+
function newWorkflow(name, frequency, detectorIds, slug) {
|
|
87
|
+
return {
|
|
88
|
+
name,
|
|
89
|
+
enabled: true,
|
|
90
|
+
environment: null,
|
|
91
|
+
config: { frequency },
|
|
92
|
+
detectorIds,
|
|
93
|
+
triggers: {
|
|
94
|
+
logicType: "any-short",
|
|
95
|
+
conditions: [{
|
|
96
|
+
type: "new_high_priority_issue",
|
|
97
|
+
comparison: true,
|
|
98
|
+
conditionResult: true
|
|
99
|
+
}, {
|
|
100
|
+
type: "existing_high_priority_issue",
|
|
101
|
+
comparison: true,
|
|
102
|
+
conditionResult: true
|
|
103
|
+
}]
|
|
104
|
+
},
|
|
105
|
+
actionFilters: [{
|
|
106
|
+
logicType: "all",
|
|
107
|
+
conditions: [],
|
|
108
|
+
actions: [integrationAction(slug)]
|
|
109
|
+
}]
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** The integration: found by name, made if missing, and its webhook and alert action brought in line. */
|
|
113
|
+
async function ensureIntegration(client, org, input) {
|
|
114
|
+
const existing = (await client.sentryApps.list(org)).find((a) => a.name === input.name);
|
|
115
|
+
if (!existing) return {
|
|
116
|
+
app: await client.sentryApps.create(org, {
|
|
117
|
+
name: input.name,
|
|
118
|
+
scopes: INTEGRATION_SCOPES,
|
|
119
|
+
webhookUrl: input.webhookUrl,
|
|
120
|
+
isAlertable: true
|
|
121
|
+
}),
|
|
122
|
+
created: true,
|
|
123
|
+
updated: []
|
|
124
|
+
};
|
|
125
|
+
const updated = [];
|
|
126
|
+
if (existing.webhookUrl !== input.webhookUrl) updated.push("webhook URL");
|
|
127
|
+
if (!existing.isAlertable) updated.push("alert action");
|
|
128
|
+
const scopes = [.../* @__PURE__ */ new Set([...existing.scopes, ...INTEGRATION_SCOPES])];
|
|
129
|
+
if (scopes.length !== existing.scopes.length) updated.push("scopes");
|
|
130
|
+
if (updated.length === 0) return {
|
|
131
|
+
app: existing,
|
|
132
|
+
created: false,
|
|
133
|
+
updated
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
app: await client.sentryApps.update(existing.slug, {
|
|
137
|
+
name: existing.name,
|
|
138
|
+
scopes,
|
|
139
|
+
webhookUrl: input.webhookUrl,
|
|
140
|
+
isAlertable: true
|
|
141
|
+
}),
|
|
142
|
+
created: false,
|
|
143
|
+
updated
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** The issue stream detector of each project, so a wrong project fails before anything is changed. */
|
|
147
|
+
async function detectorsFor(client, org, slugs) {
|
|
148
|
+
const [projects, detectors] = await Promise.all([client.projects.list(org), client.workflows.detectors(org)]);
|
|
149
|
+
return slugs.map((slug) => {
|
|
150
|
+
const project = projects.find((p) => p.slug === slug);
|
|
151
|
+
if (!project) throw new Error(`Sentry project \`${slug}\` was not found in ${org}`);
|
|
152
|
+
const detector = detectors.find((d) => d.projectId === project.id && d.type === "issue_stream");
|
|
153
|
+
if (!detector) throw new Error(`Sentry project \`${slug}\` has no issue stream detector to attach an alert to`);
|
|
154
|
+
return detector.id;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/** The alert rule: one workflow for every project, found by name, made if missing, reconciled if not. */
|
|
158
|
+
async function ensureRule(client, org, input, install, detectorIds) {
|
|
159
|
+
const workflows = await client.workflows.list(org);
|
|
160
|
+
const frequency = input.rule.frequency ?? DEFAULT_FREQUENCY;
|
|
161
|
+
const existing = workflows.find((w) => w.name === input.rule.name);
|
|
162
|
+
if (!existing) {
|
|
163
|
+
await client.workflows.create(org, newWorkflow(input.rule.name, frequency, detectorIds, install.slug));
|
|
164
|
+
return {
|
|
165
|
+
created: true,
|
|
166
|
+
updated: []
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const next = toInput(existing);
|
|
170
|
+
const updated = [];
|
|
171
|
+
if (!existing.enabled) {
|
|
172
|
+
next.enabled = true;
|
|
173
|
+
updated.push("enabled");
|
|
174
|
+
}
|
|
175
|
+
if (input.rule.frequency !== void 0 && existing.config.frequency !== input.rule.frequency) {
|
|
176
|
+
next.config = {
|
|
177
|
+
...existing.config,
|
|
178
|
+
frequency: input.rule.frequency
|
|
179
|
+
};
|
|
180
|
+
updated.push("frequency");
|
|
181
|
+
}
|
|
182
|
+
const missing = detectorIds.filter((id) => !existing.detectorIds.includes(id));
|
|
183
|
+
if (missing.length > 0) {
|
|
184
|
+
next.detectorIds = [...existing.detectorIds, ...missing];
|
|
185
|
+
updated.push("projects");
|
|
186
|
+
}
|
|
187
|
+
const sends = existing.actionFilters.some((f) => f.actions.some((a) => sendsThrough(a, install.slug)));
|
|
188
|
+
const legacy = existing.actionFilters.some((f) => f.actions.some((a) => isLegacyAction(a, install.appId)));
|
|
189
|
+
if (legacy) for (const f of next.actionFilters) f.actions = f.actions.filter((a) => !isLegacyAction(a, install.appId));
|
|
190
|
+
if (!sends) {
|
|
191
|
+
const open = next.actionFilters.find((f) => f.conditions.length === 0);
|
|
192
|
+
if (open) open.actions.push(integrationAction(install.slug));
|
|
193
|
+
else next.actionFilters.push({
|
|
194
|
+
logicType: "all",
|
|
195
|
+
conditions: [],
|
|
196
|
+
actions: [integrationAction(install.slug)]
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (legacy || !sends) updated.push("action");
|
|
200
|
+
if (updated.length === 0) return {
|
|
201
|
+
created: false,
|
|
202
|
+
updated
|
|
203
|
+
};
|
|
204
|
+
await client.workflows.update(org, existing.id, next);
|
|
205
|
+
return {
|
|
206
|
+
created: false,
|
|
207
|
+
updated
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** The signing secret, if the token may see it: a new integration's answer omits it, so it is read again. */
|
|
211
|
+
async function secretOf(client, app) {
|
|
212
|
+
if (app.clientSecret) return app.clientSecret;
|
|
213
|
+
return (await client.sentryApps.get(app.slug)).clientSecret || void 0;
|
|
214
|
+
}
|
|
215
|
+
async function ensureSentryRelay(client, org, input) {
|
|
216
|
+
if (input.projects.length === 0) throw new Error("relay needs at least one Sentry project to alert on");
|
|
217
|
+
const detectorIds = await detectorsFor(client, org, input.projects);
|
|
218
|
+
const integration = await ensureIntegration(client, org, input);
|
|
219
|
+
const install = (await client.sentryApps.installations(org)).find((i) => i.app.slug === integration.app.slug);
|
|
220
|
+
if (!install) throw new Error(`the integration \`${integration.app.name}\` is not installed on ${org}`);
|
|
221
|
+
const rule = await ensureRule(client, org, input, {
|
|
222
|
+
slug: install.app.slug,
|
|
223
|
+
appId: install.app.sentryAppId
|
|
224
|
+
}, detectorIds);
|
|
225
|
+
const clientSecret = await secretOf(client, integration.app);
|
|
226
|
+
return {
|
|
227
|
+
integration: {
|
|
228
|
+
created: integration.created,
|
|
229
|
+
updated: integration.updated
|
|
230
|
+
},
|
|
231
|
+
rule,
|
|
232
|
+
...clientSecret ? { clientSecret } : {}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
// src/capabilities/errors.ts
|
|
12
236
|
var SentryErrors = class {
|
|
13
237
|
client;
|
|
14
238
|
opts;
|
|
239
|
+
relayClient;
|
|
15
240
|
key = "errors";
|
|
16
241
|
providerName = "sentry";
|
|
17
|
-
constructor(client, opts) {
|
|
242
|
+
constructor(client, opts, relayClient = client) {
|
|
18
243
|
this.client = client;
|
|
19
244
|
this.opts = opts;
|
|
245
|
+
this.relayClient = relayClient;
|
|
246
|
+
}
|
|
247
|
+
async ensureRelay(input) {
|
|
248
|
+
return ensureSentryRelay(this.relayClient(), this.requireOrg(), input);
|
|
20
249
|
}
|
|
21
250
|
async describe() {
|
|
22
251
|
return {
|
|
@@ -25,7 +254,8 @@ var SentryErrors = class {
|
|
|
25
254
|
};
|
|
26
255
|
}
|
|
27
256
|
async whoami() {
|
|
28
|
-
|
|
257
|
+
if (!this.opts.org) return null;
|
|
258
|
+
return { org: (await this.client().auth.getOrg(this.opts.org)).slug };
|
|
29
259
|
}
|
|
30
260
|
async ensureProject(input) {
|
|
31
261
|
const org = this.requireOrg();
|
|
@@ -68,8 +298,242 @@ var SentryErrors = class {
|
|
|
68
298
|
function toSlug(name) {
|
|
69
299
|
return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
70
300
|
}
|
|
71
|
-
|
|
72
|
-
|
|
301
|
+
// src/probes/probe-deps.type.ts
|
|
302
|
+
function requireOrg(deps) {
|
|
303
|
+
if (!deps.org) throw new Error("the Sentry probes need `org` in the sentry provider's options");
|
|
304
|
+
return deps.org;
|
|
305
|
+
}
|
|
306
|
+
// src/probes/integrations.ts
|
|
307
|
+
/** Where a webhook points, without a query string that could carry a token. */
|
|
308
|
+
function where(url) {
|
|
309
|
+
if (!url) return "no webhook";
|
|
310
|
+
try {
|
|
311
|
+
const u = new URL(url);
|
|
312
|
+
return `${u.host}${u.pathname}`;
|
|
313
|
+
} catch {
|
|
314
|
+
return "an unreadable URL";
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function integrationsProbe(deps) {
|
|
318
|
+
return {
|
|
319
|
+
summary: "List the custom integrations: scopes, alert action, webhook, and whether the client secret is visible.",
|
|
320
|
+
writes: false,
|
|
321
|
+
async run(probe) {
|
|
322
|
+
const apps = await deps.client().sentryApps.list(requireOrg(deps));
|
|
323
|
+
if (apps.length === 0) probe.say({
|
|
324
|
+
status: "info",
|
|
325
|
+
label: "no custom integrations"
|
|
326
|
+
});
|
|
327
|
+
for (const app of apps) {
|
|
328
|
+
probe.secret(app.clientSecret);
|
|
329
|
+
probe.say({
|
|
330
|
+
status: "ok",
|
|
331
|
+
label: `${app.name} (${app.slug})`,
|
|
332
|
+
detail: [
|
|
333
|
+
`scopes: ${app.scopes.join(", ") || "none"}`,
|
|
334
|
+
`alert action: ${app.isAlertable ? "on" : "off"}`,
|
|
335
|
+
`webhook: ${where(app.webhookUrl)}`,
|
|
336
|
+
`secret visible: ${app.clientSecret ? "yes" : "no"}`
|
|
337
|
+
].join(" | ")
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
// src/probes/rule-update.ts
|
|
344
|
+
const FROM = 5;
|
|
345
|
+
const TO = 10;
|
|
346
|
+
/** The detector the throwaway rule hangs on: the issue stream of `HOLOCRON_SENTRY_PROBE_PROJECT`, else of any project. */
|
|
347
|
+
async function detectorFor(deps, org) {
|
|
348
|
+
const client = deps.client();
|
|
349
|
+
const slug = (deps.env ?? process.env)["HOLOCRON_SENTRY_PROBE_PROJECT"];
|
|
350
|
+
const [detectors, projects] = await Promise.all([client.workflows.detectors(org), slug ? client.projects.list(org) : Promise.resolve([])]);
|
|
351
|
+
const streams = detectors.filter((d) => d.type === "issue_stream");
|
|
352
|
+
if (slug) {
|
|
353
|
+
const project = projects.find((p) => p.slug === slug);
|
|
354
|
+
if (!project) throw new Error(`Sentry project \`${slug}\` was not found in ${org}`);
|
|
355
|
+
const own = streams.find((d) => d.projectId === project.id);
|
|
356
|
+
if (!own) throw new Error(`Sentry project \`${slug}\` has no issue stream detector`);
|
|
357
|
+
return own.id;
|
|
358
|
+
}
|
|
359
|
+
const any = streams[0];
|
|
360
|
+
if (!any) throw new Error("no project has an issue stream detector to attach a throwaway rule to");
|
|
361
|
+
return any.id;
|
|
362
|
+
}
|
|
363
|
+
function ruleUpdateProbe(deps) {
|
|
364
|
+
return {
|
|
365
|
+
summary: "Make a disabled throwaway alert rule, change it the way the relay's adopt path does, read it back, delete it.",
|
|
366
|
+
writes: true,
|
|
367
|
+
async run(probe) {
|
|
368
|
+
const org = requireOrg(deps);
|
|
369
|
+
const client = deps.client();
|
|
370
|
+
const detectorId = await detectorFor(deps, org);
|
|
371
|
+
const body = {
|
|
372
|
+
name: `Holocron probe: throwaway rule (${(/* @__PURE__ */ new Date()).toISOString()})`,
|
|
373
|
+
enabled: false,
|
|
374
|
+
environment: null,
|
|
375
|
+
config: { frequency: FROM },
|
|
376
|
+
detectorIds: [detectorId],
|
|
377
|
+
triggers: {
|
|
378
|
+
logicType: "any-short",
|
|
379
|
+
conditions: [{
|
|
380
|
+
type: "new_high_priority_issue",
|
|
381
|
+
comparison: true,
|
|
382
|
+
conditionResult: true
|
|
383
|
+
}]
|
|
384
|
+
},
|
|
385
|
+
actionFilters: [{
|
|
386
|
+
logicType: "all",
|
|
387
|
+
conditions: [],
|
|
388
|
+
actions: []
|
|
389
|
+
}]
|
|
390
|
+
};
|
|
391
|
+
const created = await probe.write("create a disabled throwaway alert rule", async () => {
|
|
392
|
+
const wf = await client.workflows.create(org, body);
|
|
393
|
+
probe.cleanup(`the throwaway rule ${wf.id}`, () => client.workflows.delete(org, wf.id));
|
|
394
|
+
return wf;
|
|
395
|
+
});
|
|
396
|
+
if (!created) {
|
|
397
|
+
probe.say({
|
|
398
|
+
status: "info",
|
|
399
|
+
label: `would then change its frequency ${FROM} to ${TO} with PUT, read it back, and delete it`
|
|
400
|
+
});
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
probe.say({
|
|
404
|
+
status: "ok",
|
|
405
|
+
label: "created",
|
|
406
|
+
detail: `rule ${created.id}, disabled`
|
|
407
|
+
});
|
|
408
|
+
const next = toInput(created);
|
|
409
|
+
next.config = {
|
|
410
|
+
...created.config,
|
|
411
|
+
frequency: TO
|
|
412
|
+
};
|
|
413
|
+
await client.workflows.update(org, created.id, next);
|
|
414
|
+
probe.say({
|
|
415
|
+
status: "ok",
|
|
416
|
+
label: "PUT accepted"
|
|
417
|
+
});
|
|
418
|
+
const read = await client.workflows.get(org, created.id);
|
|
419
|
+
const kept = !read.enabled && read.detectorIds.includes(detectorId);
|
|
420
|
+
probe.say(read.config.frequency === TO && kept ? {
|
|
421
|
+
status: "ok",
|
|
422
|
+
label: "read back",
|
|
423
|
+
detail: `frequency ${read.config.frequency}, still disabled and attached`
|
|
424
|
+
} : {
|
|
425
|
+
status: "fail",
|
|
426
|
+
label: "read back differs",
|
|
427
|
+
detail: `frequency ${read.config.frequency} (sent ${TO}), enabled ${read.enabled}, detectors ${read.detectorIds.join(",")}`
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
// src/probes/rules.ts
|
|
433
|
+
function rulesProbe(deps) {
|
|
434
|
+
return {
|
|
435
|
+
summary: "List the alert rules: enabled, projects attached, and what each action sends to.",
|
|
436
|
+
writes: false,
|
|
437
|
+
async run(probe) {
|
|
438
|
+
const org = requireOrg(deps);
|
|
439
|
+
const client = deps.client();
|
|
440
|
+
const [workflows, apps] = await Promise.all([client.workflows.list(org), client.sentryApps.list(org)]);
|
|
441
|
+
const alertable = new Set(apps.filter((a) => a.isAlertable).map((a) => a.slug));
|
|
442
|
+
if (workflows.length === 0) probe.say({
|
|
443
|
+
status: "info",
|
|
444
|
+
label: "no alert rules"
|
|
445
|
+
});
|
|
446
|
+
for (const wf of workflows) {
|
|
447
|
+
const actions = wf.actionFilters.flatMap((f) => f.actions);
|
|
448
|
+
const described = actions.map((a) => {
|
|
449
|
+
const target = String(a.config["targetIdentifier"] ?? a.config["targetType"] ?? "");
|
|
450
|
+
return target ? `${a.type} ${target}` : a.type;
|
|
451
|
+
});
|
|
452
|
+
const problems = actions.flatMap((a) => {
|
|
453
|
+
if (a.type === "sentry_app") return [`a sentry_app action shows as an unknown integration`];
|
|
454
|
+
if (a.type === "webhook" && !alertable.has(String(a.config["targetIdentifier"]))) return [`webhook ${String(a.config["targetIdentifier"])} is not an alertable integration`];
|
|
455
|
+
return [];
|
|
456
|
+
});
|
|
457
|
+
probe.say({
|
|
458
|
+
status: problems.length > 0 || !wf.enabled ? "fail" : "ok",
|
|
459
|
+
label: wf.name,
|
|
460
|
+
detail: [
|
|
461
|
+
wf.enabled ? "enabled" : "DISABLED",
|
|
462
|
+
`detectors: ${wf.detectorIds.length}`,
|
|
463
|
+
`actions: ${described.join(", ") || "none"}`,
|
|
464
|
+
...problems
|
|
465
|
+
].join(" | ")
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
// src/probes/send-error.ts
|
|
472
|
+
/** Which DSN to send to: `SENTRY_DSN`, else the first client key of the project named in `HOLOCRON_SENTRY_PROBE_PROJECT`. */
|
|
473
|
+
async function dsnFor(deps) {
|
|
474
|
+
const env = deps.env ?? process.env;
|
|
475
|
+
if (env["SENTRY_DSN"]) return env["SENTRY_DSN"];
|
|
476
|
+
const project = env["HOLOCRON_SENTRY_PROBE_PROJECT"];
|
|
477
|
+
if (!project) throw new Error("set SENTRY_DSN, or HOLOCRON_SENTRY_PROBE_PROJECT to a project slug, to say where to send the error");
|
|
478
|
+
const dsn = (await deps.client().projects.keys(requireOrg(deps), project))[0]?.dsn.public;
|
|
479
|
+
if (!dsn) throw new Error(`project \`${project}\` has no client key`);
|
|
480
|
+
return dsn;
|
|
481
|
+
}
|
|
482
|
+
function sendErrorProbe(deps) {
|
|
483
|
+
return {
|
|
484
|
+
summary: "Send one labelled test error to a project, to follow it through the alert rule to Discord.",
|
|
485
|
+
writes: true,
|
|
486
|
+
async run(probe) {
|
|
487
|
+
const dsn = new URL(await dsnFor(deps));
|
|
488
|
+
probe.secret(dsn.username);
|
|
489
|
+
const projectId = dsn.pathname.replace(/^\/+/, "");
|
|
490
|
+
const eventId = randomUUID().replaceAll("-", "");
|
|
491
|
+
const message = `Holocron probe: sent on purpose (${(/* @__PURE__ */ new Date()).toISOString()})`;
|
|
492
|
+
const envelope = [
|
|
493
|
+
JSON.stringify({
|
|
494
|
+
event_id: eventId,
|
|
495
|
+
sent_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
496
|
+
}),
|
|
497
|
+
JSON.stringify({ type: "event" }),
|
|
498
|
+
JSON.stringify({
|
|
499
|
+
event_id: eventId,
|
|
500
|
+
level: "error",
|
|
501
|
+
platform: "node",
|
|
502
|
+
environment: "holocron-probe",
|
|
503
|
+
message: { formatted: message },
|
|
504
|
+
exception: { values: [{
|
|
505
|
+
type: "HolocronProbeError",
|
|
506
|
+
value: message
|
|
507
|
+
}] },
|
|
508
|
+
tags: { probe: "sentry.send-error" }
|
|
509
|
+
})
|
|
510
|
+
].join("\n");
|
|
511
|
+
if (await probe.write("send a test error", async () => {
|
|
512
|
+
const res = await (deps.fetch ?? fetch)(`${dsn.protocol}//${dsn.host}/api/${projectId}/envelope/?sentry_version=7&sentry_key=${dsn.username}&sentry_client=holocron-probe/1`, {
|
|
513
|
+
method: "POST",
|
|
514
|
+
headers: { "content-type": "application/x-sentry-envelope" },
|
|
515
|
+
body: envelope
|
|
516
|
+
});
|
|
517
|
+
if (!res.ok) throw new Error(`Sentry answered ${res.status}`);
|
|
518
|
+
return true;
|
|
519
|
+
})) probe.say({
|
|
520
|
+
status: "ok",
|
|
521
|
+
label: "sent HolocronProbeError",
|
|
522
|
+
detail: "look for it in the project's issues and, through the rule, in #errors; resolve the issue by hand (a probe cannot delete it)"
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
// src/probes/index.ts
|
|
528
|
+
function sentryProbes(deps) {
|
|
529
|
+
return {
|
|
530
|
+
integrations: integrationsProbe(deps),
|
|
531
|
+
rules: rulesProbe(deps),
|
|
532
|
+
"rule-update": ruleUpdateProbe(deps),
|
|
533
|
+
"send-error": sendErrorProbe(deps)
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
// src/verify-token.ts
|
|
73
537
|
async function verifyToken(token, opts = {}) {
|
|
74
538
|
const client = createSentryClient({
|
|
75
539
|
token,
|
|
@@ -84,20 +548,25 @@ async function verifyToken(token, opts = {}) {
|
|
|
84
548
|
} catch (err) {
|
|
85
549
|
return {
|
|
86
550
|
ok: false,
|
|
87
|
-
message:
|
|
551
|
+
message: errorMessage(err)
|
|
88
552
|
};
|
|
89
553
|
}
|
|
90
554
|
}
|
|
91
|
-
|
|
92
|
-
//#region src/index.ts
|
|
555
|
+
// src/index.ts
|
|
93
556
|
function createContext(options) {
|
|
94
557
|
let client;
|
|
558
|
+
let relayClient;
|
|
95
559
|
return {
|
|
96
560
|
options,
|
|
97
561
|
client: () => client ??= createSentryClient({
|
|
98
562
|
token: resolveToken(options),
|
|
99
563
|
baseUrl: options.baseUrl,
|
|
100
564
|
fetch: options.fetch
|
|
565
|
+
}),
|
|
566
|
+
relayClient: () => relayClient ??= createSentryClient({
|
|
567
|
+
token: resolveRelayToken(options),
|
|
568
|
+
baseUrl: options.baseUrl,
|
|
569
|
+
fetch: options.fetch
|
|
101
570
|
})
|
|
102
571
|
};
|
|
103
572
|
}
|
|
@@ -105,15 +574,31 @@ function errors(ctx) {
|
|
|
105
574
|
return new SentryErrors(ctx.client, {
|
|
106
575
|
org: ctx.options.org,
|
|
107
576
|
team: ctx.options.team
|
|
108
|
-
});
|
|
577
|
+
}, ctx.relayClient);
|
|
109
578
|
}
|
|
110
579
|
function createPlugin(options) {
|
|
111
580
|
const ctx = createContext(options);
|
|
112
581
|
return {
|
|
113
582
|
name: "@theholocron/holocron-plugin-sentry",
|
|
583
|
+
probes: {
|
|
584
|
+
...sentryProbes({
|
|
585
|
+
org: options.org,
|
|
586
|
+
client: ctx.relayClient,
|
|
587
|
+
fetch: options.fetch
|
|
588
|
+
}),
|
|
589
|
+
connection: connectionProbe({
|
|
590
|
+
provider: "Sentry",
|
|
591
|
+
url: options.baseUrl ?? "https://sentry.io",
|
|
592
|
+
fetch: options.fetch,
|
|
593
|
+
token: () => resolveToken(options),
|
|
594
|
+
verify: (token) => verifyToken(token, {
|
|
595
|
+
baseUrl: options.baseUrl,
|
|
596
|
+
fetch: options.fetch
|
|
597
|
+
})
|
|
598
|
+
})
|
|
599
|
+
},
|
|
114
600
|
capabilities: { errors: () => errors(ctx) }
|
|
115
601
|
};
|
|
116
602
|
}
|
|
117
603
|
const AUTH_HINT = "generate an auth token at https://sentry.io/settings/account/api/auth-tokens/ with project:read, project:write, and org:read scopes, then run: holocron auth set sentry <TOKEN>";
|
|
118
|
-
|
|
119
|
-
export { AUTH_HINT, AuthError, SentryErrors, createContext, createPlugin, createSentryClient, errors, resolveToken, verifyToken };
|
|
604
|
+
export { AUTH_HINT, AuthError, SentryErrors, createContext, createPlugin, createSentryClient, errors, resolveComlinkToken, resolveRelayToken, resolveToken, verifyToken };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/holocron-plugin-sentry",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.250",
|
|
4
4
|
"description": "Holocron plugin for Sentry. Implements the errors capability against Sentry's management API — project provisioning and DSN retrieval.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"errors",
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
"plugin",
|
|
9
9
|
"sentry"
|
|
10
10
|
],
|
|
11
|
-
"homepage": "https://github.com/theholocron/holocron/tree/main/
|
|
11
|
+
"homepage": "https://github.com/theholocron/holocron/tree/main/plugins/holocron-plugin-sentry#readme",
|
|
12
12
|
"bugs": "https://github.com/theholocron/holocron/issues",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
15
|
"url": "git+https://github.com/theholocron/holocron.git",
|
|
16
|
-
"directory": "
|
|
16
|
+
"directory": "plugins/holocron-plugin-sentry"
|
|
17
17
|
},
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"author": "Newton Koumantzelis",
|
|
@@ -30,28 +30,30 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@theholocron/eslint-config": "^8.
|
|
34
|
-
"@theholocron/http-client": "^1.
|
|
35
|
-
"@theholocron/
|
|
36
|
-
"@theholocron/
|
|
37
|
-
"@theholocron/
|
|
38
|
-
"@theholocron/
|
|
33
|
+
"@theholocron/eslint-config": "^8.8.2",
|
|
34
|
+
"@theholocron/http-client": "^1.57.0",
|
|
35
|
+
"@theholocron/misc-utils": "^1.16.0",
|
|
36
|
+
"@theholocron/sentry-client": "^1.56.0",
|
|
37
|
+
"@theholocron/tsconfig": "^8.8.2",
|
|
38
|
+
"@theholocron/tsdown-config": "^8.8.2",
|
|
39
|
+
"@theholocron/vitest-config": "^8.8.2",
|
|
39
40
|
"@types/node": "^26",
|
|
40
|
-
"@vitest/coverage-v8": "^
|
|
41
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
41
42
|
"@vitest/eslint-plugin": "^1.6.27",
|
|
42
43
|
"chalk": "^6.0.0",
|
|
43
44
|
"eslint": "^10.8.1",
|
|
44
45
|
"eslint-plugin-n": "^18.3.0",
|
|
45
46
|
"globals": "^17.11.0",
|
|
46
|
-
"tsdown": "^0.
|
|
47
|
-
"tsx": "4.23.
|
|
47
|
+
"tsdown": "^0.23.3",
|
|
48
|
+
"tsx": "4.23.15",
|
|
48
49
|
"typescript": "^5.9.3",
|
|
49
|
-
"vitest": "^
|
|
50
|
-
"@theholocron/cli": "5.0.0-alpha.
|
|
50
|
+
"vitest": "^5.0.5",
|
|
51
|
+
"@theholocron/cli": "5.0.0-alpha.250"
|
|
51
52
|
},
|
|
52
53
|
"peerDependencies": {
|
|
53
|
-
"@theholocron/
|
|
54
|
-
"@theholocron/
|
|
54
|
+
"@theholocron/misc-utils": "^1.16.0",
|
|
55
|
+
"@theholocron/sentry-client": "^1.56.0",
|
|
56
|
+
"@theholocron/cli": "5.0.0-alpha.250"
|
|
55
57
|
},
|
|
56
58
|
"peerDependenciesMeta": {
|
|
57
59
|
"@theholocron/sentry-client": {}
|