@sentry/junior-vercel 0.301.0 → 0.303.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/dist/handler-yrda69iT.js +299 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +278 -351
- package/dist/testing.d.ts +404 -3
- package/dist/testing.js +1 -10
- package/package.json +9 -12
- package/dist/chunk-QCT7VK3J.js +0 -334
- package/dist/events/deployment.d.ts +0 -37
- package/dist/project.d.ts +0 -3
- package/dist/tools/actions.d.ts +0 -1024
- package/dist/tools/deployment.d.ts +0 -220
- package/dist/webhooks/events.d.ts +0 -14
- package/dist/webhooks/handler.d.ts +0 -6
- package/dist/webhooks/secret.d.ts +0 -2
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { PluginToolInputError, definePluginTool, eventMatchFieldsSchema, pluginToolOutputSchema, subscribableResourceSchema } from "@sentry/junior-plugin-api";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
4
|
+
// src/project.ts
|
|
5
|
+
/** Accept the opaque project identifier returned by Vercel. */
|
|
6
|
+
const vercelProjectIdSchema = z.string().trim().min(1);
|
|
7
|
+
// src/events/deployment.ts
|
|
8
|
+
const VERCEL_DEPLOYMENT_EVENTS = [
|
|
9
|
+
"deployment.succeeded",
|
|
10
|
+
"deployment.error",
|
|
11
|
+
"deployment.canceled"
|
|
12
|
+
];
|
|
13
|
+
const VERCEL_DEPLOYMENT_SUGGESTED_EVENTS = [
|
|
14
|
+
"deployment.succeeded",
|
|
15
|
+
"deployment.error",
|
|
16
|
+
"deployment.canceled"
|
|
17
|
+
];
|
|
18
|
+
/** The deployment targets that Vercel webhooks report. */
|
|
19
|
+
const VERCEL_DEPLOYMENT_TARGETS = [
|
|
20
|
+
"preview",
|
|
21
|
+
"production",
|
|
22
|
+
"staging"
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Trusted values that every Vercel deployment event sets. `commitSha` is left
|
|
26
|
+
* out when the deployment has no Git commit, so a filter on it does not match.
|
|
27
|
+
* Vercel ids keep the case that Vercel sends, the same as identifiers,
|
|
28
|
+
* because they are case-sensitive.
|
|
29
|
+
*/
|
|
30
|
+
const VERCEL_DEPLOYMENT_MATCH_FIELDS = eventMatchFieldsSchema.parse({
|
|
31
|
+
projectId: {
|
|
32
|
+
kind: "string",
|
|
33
|
+
description: "Vercel project id, such as prj_abc123",
|
|
34
|
+
events: [...VERCEL_DEPLOYMENT_EVENTS]
|
|
35
|
+
},
|
|
36
|
+
deploymentId: {
|
|
37
|
+
kind: "string",
|
|
38
|
+
description: "Vercel deployment id, such as dpl_abc123",
|
|
39
|
+
events: [...VERCEL_DEPLOYMENT_EVENTS]
|
|
40
|
+
},
|
|
41
|
+
target: {
|
|
42
|
+
kind: "string",
|
|
43
|
+
description: "where Vercel deployed: preview, production, or staging",
|
|
44
|
+
enum: [...VERCEL_DEPLOYMENT_TARGETS],
|
|
45
|
+
events: [...VERCEL_DEPLOYMENT_EVENTS]
|
|
46
|
+
},
|
|
47
|
+
commitSha: {
|
|
48
|
+
kind: "string",
|
|
49
|
+
case: "lower",
|
|
50
|
+
description: "full lowercase Git commit SHA of the deployment, when it came from a Git commit",
|
|
51
|
+
events: [...VERCEL_DEPLOYMENT_EVENTS]
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
/** Build the stable deployment identity shared by tools and webhooks. */
|
|
55
|
+
function vercelDeploymentResource(input) {
|
|
56
|
+
const commitSha = input.commitSha?.toLowerCase();
|
|
57
|
+
const target = input.target;
|
|
58
|
+
const parts = [input.projectId];
|
|
59
|
+
if (target) parts.push(target);
|
|
60
|
+
if (commitSha) parts.push(commitSha);
|
|
61
|
+
let label;
|
|
62
|
+
if (target && commitSha) label = `Vercel ${target} deployment for ${input.projectId} at ${commitSha.slice(0, 12)}`;
|
|
63
|
+
else if (target) label = `Vercel ${target} deployments for ${input.projectId}`;
|
|
64
|
+
else label = `Vercel deployments for ${input.projectId}`;
|
|
65
|
+
return {
|
|
66
|
+
label,
|
|
67
|
+
namespace: "vercel",
|
|
68
|
+
identifier: parts.join(":")
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Describe a deployment resource that a conversation can watch.
|
|
73
|
+
* Return undefined when the host cannot create Watches.
|
|
74
|
+
*/
|
|
75
|
+
function vercelDeploymentSubscribable(input) {
|
|
76
|
+
if (!input.canSubscribe) return void 0;
|
|
77
|
+
if (input.commitSha && !input.target) return void 0;
|
|
78
|
+
return {
|
|
79
|
+
...vercelDeploymentResource(input),
|
|
80
|
+
match: {
|
|
81
|
+
project_id: input.projectId,
|
|
82
|
+
...input.target ? { target: input.target } : void 0,
|
|
83
|
+
...input.commitSha ? { commit_sha: input.commitSha.toLowerCase() } : void 0
|
|
84
|
+
},
|
|
85
|
+
suggested_events: VERCEL_DEPLOYMENT_SUGGESTED_EVENTS,
|
|
86
|
+
supported_events: [...VERCEL_DEPLOYMENT_EVENTS],
|
|
87
|
+
type: "deployment"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// src/tools/deployment.ts
|
|
91
|
+
const commitShaSchema$1 = z.string().regex(/^[0-9a-f]{40}$/i);
|
|
92
|
+
const targetSchema = z.enum([
|
|
93
|
+
"preview",
|
|
94
|
+
"production",
|
|
95
|
+
"staging"
|
|
96
|
+
]);
|
|
97
|
+
const nonEmptyStringSchema = z.string().trim().min(1);
|
|
98
|
+
const inputSchema = z.object({
|
|
99
|
+
commit_sha: commitShaSchema$1.describe("Optional full 40-character Git commit SHA. Provide it to watch one deployment; omit it to watch every matching deployment for the project.").optional(),
|
|
100
|
+
project: nonEmptyStringSchema.describe("Vercel project name or prj_ ID."),
|
|
101
|
+
target: targetSchema.describe("Optional deployment target such as \"production\". Omit to watch every target for the project. Defaults to \"production\" when commit_sha is set.").optional(),
|
|
102
|
+
team: nonEmptyStringSchema.describe("Optional Vercel team slug or team_ ID that owns the project.").optional()
|
|
103
|
+
}).strict();
|
|
104
|
+
const deploymentSchema = z.object({
|
|
105
|
+
commit_sha: commitShaSchema$1.nullable(),
|
|
106
|
+
deployment_target: targetSchema.nullable(),
|
|
107
|
+
project_id: vercelProjectIdSchema,
|
|
108
|
+
subscribable: subscribableResourceSchema.optional()
|
|
109
|
+
});
|
|
110
|
+
const outputSchema = pluginToolOutputSchema.merge(deploymentSchema.extend({ target: z.literal("deployment") }));
|
|
111
|
+
async function readJson(response) {
|
|
112
|
+
const text = await response.text();
|
|
113
|
+
if (!text) return void 0;
|
|
114
|
+
try {
|
|
115
|
+
return JSON.parse(text);
|
|
116
|
+
} catch {
|
|
117
|
+
return text;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function projectLookupUrl(project, team) {
|
|
121
|
+
const url = new URL(`https://api.vercel.com/v9/projects/${encodeURIComponent(project)}`);
|
|
122
|
+
if (team) url.searchParams.set(team.startsWith("team_") ? "teamId" : "slug", team);
|
|
123
|
+
return url.toString();
|
|
124
|
+
}
|
|
125
|
+
/** Resolve and return the resource identity for one Vercel deployment. */
|
|
126
|
+
const vercelDeploymentTool = definePluginTool({
|
|
127
|
+
annotations: {
|
|
128
|
+
destructiveHint: false,
|
|
129
|
+
idempotentHint: true,
|
|
130
|
+
openWorldHint: true,
|
|
131
|
+
readOnlyHint: true
|
|
132
|
+
},
|
|
133
|
+
description: "Resolve a Vercel project name or ID and describe a subscribable deployment resource for one deployment or every deployment of the project. Use the user's explicit project and team, otherwise the vercel.project and vercel.team conversation defaults.",
|
|
134
|
+
inputSchema,
|
|
135
|
+
outputSchema,
|
|
136
|
+
async execute(input, _options, ctx) {
|
|
137
|
+
const response = await ctx.egress.fetch({
|
|
138
|
+
operation: "vercel.project.get",
|
|
139
|
+
provider: "vercel",
|
|
140
|
+
request: new Request(projectLookupUrl(input.project, input.team))
|
|
141
|
+
});
|
|
142
|
+
const parsed = await readJson(response);
|
|
143
|
+
if (!response.ok) {
|
|
144
|
+
const message = `Vercel project lookup failed with HTTP ${response.status}`;
|
|
145
|
+
if (response.status === 404) throw new PluginToolInputError(message);
|
|
146
|
+
throw new Error(message);
|
|
147
|
+
}
|
|
148
|
+
const projectId = z.object({ id: vercelProjectIdSchema }).parse(parsed).id;
|
|
149
|
+
const commitSha = input.commit_sha?.toLowerCase();
|
|
150
|
+
const deploymentTarget = input.target ?? (commitSha ? "production" : void 0);
|
|
151
|
+
const subscribable = vercelDeploymentSubscribable({
|
|
152
|
+
canSubscribe: ctx.events.canSubscribe,
|
|
153
|
+
commitSha,
|
|
154
|
+
projectId,
|
|
155
|
+
target: deploymentTarget
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
target: "deployment",
|
|
159
|
+
commit_sha: commitSha ?? null,
|
|
160
|
+
deployment_target: deploymentTarget ?? null,
|
|
161
|
+
project_id: projectId,
|
|
162
|
+
...subscribable ? { subscribable } : void 0
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
// src/webhooks/events.ts
|
|
167
|
+
const deploymentIdSchema = z.string().regex(/^dpl_[A-Za-z0-9]+$/);
|
|
168
|
+
const commitShaSchema = z.string().regex(/^[0-9a-f]{40}$/i).transform((sha) => sha.toLowerCase());
|
|
169
|
+
const deploymentTargetSchema = z.string().nullable().transform((target) => target ?? "preview").pipe(z.enum(VERCEL_DEPLOYMENT_TARGETS));
|
|
170
|
+
const metaCommitShaSchema = commitShaSchema.optional().catch(void 0);
|
|
171
|
+
const deploymentMetaSchema = z.object({
|
|
172
|
+
githubCommitSha: metaCommitShaSchema,
|
|
173
|
+
gitlabCommitSha: metaCommitShaSchema,
|
|
174
|
+
bitbucketCommitSha: metaCommitShaSchema,
|
|
175
|
+
gitCommitSha: metaCommitShaSchema
|
|
176
|
+
}).transform((meta) => ({ commitSha: meta.githubCommitSha ?? meta.gitlabCommitSha ?? meta.bitbucketCommitSha ?? meta.gitCommitSha }));
|
|
177
|
+
const webhookEnvelopeSchema = z.object({
|
|
178
|
+
createdAt: z.number().finite(),
|
|
179
|
+
id: z.string().min(1),
|
|
180
|
+
payload: z.unknown(),
|
|
181
|
+
type: z.enum(VERCEL_DEPLOYMENT_EVENTS)
|
|
182
|
+
});
|
|
183
|
+
const deploymentPayloadSchema = z.object({
|
|
184
|
+
deployment: z.object({
|
|
185
|
+
id: deploymentIdSchema,
|
|
186
|
+
meta: deploymentMetaSchema
|
|
187
|
+
}),
|
|
188
|
+
project: z.object({ id: vercelProjectIdSchema }),
|
|
189
|
+
target: deploymentTargetSchema
|
|
190
|
+
}).transform((payload) => ({
|
|
191
|
+
commitSha: payload.deployment.meta.commitSha,
|
|
192
|
+
deploymentId: payload.deployment.id,
|
|
193
|
+
projectId: payload.project.id,
|
|
194
|
+
target: payload.target
|
|
195
|
+
}));
|
|
196
|
+
/** Trusted data on every deployment event. */
|
|
197
|
+
const deploymentEventDataSchema = z.object({
|
|
198
|
+
projectId: vercelProjectIdSchema,
|
|
199
|
+
deploymentId: deploymentIdSchema,
|
|
200
|
+
target: z.enum(VERCEL_DEPLOYMENT_TARGETS),
|
|
201
|
+
commitSha: commitShaSchema.optional()
|
|
202
|
+
}).strict();
|
|
203
|
+
const DEPLOYMENT_OUTCOMES = {
|
|
204
|
+
"deployment.succeeded": "succeeded",
|
|
205
|
+
"deployment.error": "failed",
|
|
206
|
+
"deployment.canceled": "was canceled"
|
|
207
|
+
};
|
|
208
|
+
/** Address one deployment through project, target, and optional commit watches. */
|
|
209
|
+
function deploymentTargets(input) {
|
|
210
|
+
const targets = [{
|
|
211
|
+
completeOnTerminalEvent: false,
|
|
212
|
+
resource: vercelDeploymentResource({ projectId: input.projectId })
|
|
213
|
+
}, {
|
|
214
|
+
completeOnTerminalEvent: false,
|
|
215
|
+
resource: vercelDeploymentResource({
|
|
216
|
+
projectId: input.projectId,
|
|
217
|
+
target: input.target
|
|
218
|
+
})
|
|
219
|
+
}];
|
|
220
|
+
if (input.commitSha) targets.push({
|
|
221
|
+
completeOnTerminalEvent: true,
|
|
222
|
+
resource: vercelDeploymentResource({
|
|
223
|
+
commitSha: input.commitSha,
|
|
224
|
+
projectId: input.projectId,
|
|
225
|
+
target: input.target
|
|
226
|
+
})
|
|
227
|
+
});
|
|
228
|
+
return targets;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Normalize one verified Vercel delivery into deployment events. Target,
|
|
232
|
+
* commit, and deployment id come from Vercel, so they go in trusted data.
|
|
233
|
+
*/
|
|
234
|
+
function normalizeVercelEvents(args) {
|
|
235
|
+
const envelope = webhookEnvelopeSchema.safeParse(args.body);
|
|
236
|
+
if (!envelope.success) return [];
|
|
237
|
+
const payload = deploymentPayloadSchema.safeParse(envelope.data.payload);
|
|
238
|
+
if (!payload.success) return [];
|
|
239
|
+
const eventType = envelope.data.type;
|
|
240
|
+
const { commitSha, deploymentId, projectId, target } = payload.data;
|
|
241
|
+
const data = deploymentEventDataSchema.parse({
|
|
242
|
+
projectId,
|
|
243
|
+
deploymentId,
|
|
244
|
+
target,
|
|
245
|
+
...commitSha ? { commitSha } : void 0
|
|
246
|
+
});
|
|
247
|
+
return deploymentTargets({
|
|
248
|
+
commitSha,
|
|
249
|
+
projectId,
|
|
250
|
+
target
|
|
251
|
+
}).map(({ completeOnTerminalEvent, resource }) => ({
|
|
252
|
+
data,
|
|
253
|
+
eventKey: `vercel:${envelope.data.id}:${eventType}`,
|
|
254
|
+
eventType,
|
|
255
|
+
identifier: resource.identifier,
|
|
256
|
+
objectType: "deployment",
|
|
257
|
+
occurredAtMs: envelope.data.createdAt,
|
|
258
|
+
...completeOnTerminalEvent ? { terminal: true } : void 0,
|
|
259
|
+
trustedSummary: `${resource.label} (${deploymentId}) ${DEPLOYMENT_OUTCOMES[eventType]}.`
|
|
260
|
+
}));
|
|
261
|
+
}
|
|
262
|
+
// src/webhooks/handler.ts
|
|
263
|
+
/**
|
|
264
|
+
* Owns signed Vercel webhook ingress for the plugin.
|
|
265
|
+
*
|
|
266
|
+
* Verification precedes parsing, only verified events reach normalization, and
|
|
267
|
+
* publish failures propagate so Vercel can retry the delivery.
|
|
268
|
+
*/
|
|
269
|
+
/** Verify Vercel's SHA-1 signature against the untouched request body. */
|
|
270
|
+
function verifyVercelSignature(body, signature, secret) {
|
|
271
|
+
if (!secret || !/^[0-9a-f]{40}$/i.test(signature)) return false;
|
|
272
|
+
const actual = Buffer.from(signature.toLowerCase());
|
|
273
|
+
const expected = Buffer.from(createHmac("sha1", secret).update(body).digest("hex"));
|
|
274
|
+
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
275
|
+
}
|
|
276
|
+
function parseJson(body) {
|
|
277
|
+
try {
|
|
278
|
+
return JSON.parse(body);
|
|
279
|
+
} catch {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** Create the public, signed Vercel deployment webhook route. */
|
|
284
|
+
function createVercelWebhookRoute(args) {
|
|
285
|
+
return {
|
|
286
|
+
method: "POST",
|
|
287
|
+
path: "/api/webhooks/vercel",
|
|
288
|
+
async handler(request) {
|
|
289
|
+
const rawBody = await request.text();
|
|
290
|
+
if (!verifyVercelSignature(rawBody, request.headers.get("x-vercel-signature") ?? "", args.webhookSecret())) return new Response("Unauthorized", { status: 401 });
|
|
291
|
+
const body = parseJson(rawBody);
|
|
292
|
+
if (body === void 0) return new Response("Malformed Vercel webhook", { status: 400 });
|
|
293
|
+
const events = normalizeVercelEvents({ body });
|
|
294
|
+
for (const event of events) await args.events.publish(event);
|
|
295
|
+
return new Response(events.length ? "Accepted" : "Ignored", { status: 202 });
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
export { VERCEL_DEPLOYMENT_MATCH_FIELDS as a, VERCEL_DEPLOYMENT_EVENTS as i, normalizeVercelEvents as n, VERCEL_DEPLOYMENT_SUGGESTED_EVENTS as o, vercelDeploymentTool as r, createVercelWebhookRoute as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* webhook normalization, and deployment tools. Junior core owns the resulting
|
|
6
6
|
* conversation subscriptions and event delivery.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { PluginRegistration } from "@sentry/junior-plugin-api";
|
|
9
9
|
/** Register Vercel runtime metadata, tools, and signed webhook ingress. */
|
|
10
|
-
export declare function vercelPlugin(): PluginRegistration;
|
|
10
|
+
export declare function vercelPlugin(): PluginRegistration;
|