chore: update src/pages/api/repos/[repoId]/webhooks/index.ts

swadhinbiswas committed 8 months ago
5aff3d3
1 parent 86e4d2d
commit 5aff3d3
1 file changed
+26 -4
▼src/pages/api/repos/[repoId]/webhooks/index.ts
+26-4
27
27
if (!await canWriteRepo(user.id, repo)) return forbidden();
28
28
29
29
const body = await request.json();
30
- const { url, secret, events, active } = body;
30
+ const {
31
+ url,
32
+ secret,
33
+ events,
34
+ active,
35
+ contentType: rawContentType,
36
+ content_type,
37
+ name,
38
+ provider,
39
+ } = body;
31
40
32
41
if (!url || !events) {
33
42
return badRequest("Missing required fields");
34
43
}
44
+ if (!Array.isArray(events) || events.length === 0) {
45
+ return badRequest("events must be a non-empty array");
46
+ }
47
+
48
+ const normalizedContentType = rawContentType || content_type || "json";
49
+ if (!["json", "form"].includes(normalizedContentType)) {
50
+ return badRequest("Invalid contentType. Supported values: json, form");
51
+ }
52
+ const isActive = active === undefined ? true : Boolean(active);
35
53
36
54
const id = generateId();
37
55
await db.insert(schema.webhooks).values({
38
56
id,
39
57
repositoryId: repoId,
58
+ provider: provider || "generic",
59
+ name: name || null,
40
60
url,
41
- secret,
61
+ secret: secret || null,
42
62
events: JSON.stringify(events),
43
- active: active, // Changed from isActive
44
- // contentType: content_type // Add this to schema if needed, defaulting to json for now as it's not in my previous schema definition but was in plan
63
+ active: isActive,
64
+ enabled: isActive,
65
+ contentType: normalizedContentType,
45
66
createdAt: new Date(),
46
67
updatedAt: new Date(),
68
+ createdById: user.id,
47
69
});
48
70
49
71
logger.info({ userId: user.id, repoId, webhookId: id }, "Webhook created");
50
72