MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / POST

Function POST

apps/web/src/app/api/webhooks/gitlab/route.ts:25–193  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

23 * GitLab webhooks use a simple secret token for verification (not HMAC like GitHub).
24 * The token is configured per-project in GitLab and stored in our integration metadata.
25 */
26export async function POST(request: NextRequest) {
27 try {
28 // 1. Get the webhook token from header
29 const webhookToken = request.headers.get('x-gitlab-token');
30
31 if (!webhookToken) {
32 logExceptInTest('Missing X-Gitlab-Token header');
33 return new NextResponse('Unauthorized - Missing token', { status: 401 });
34 }
35
36 // 2. Find integration by webhook token
37 const integration = await findGitLabIntegrationByWebhookToken(webhookToken);
38
39 if (!integration) {
40 logExceptInTest('No integration found for webhook token');
41 return new NextResponse('Unauthorized - Invalid token', { status: 401 });
42 }
43
44 // Get the expected token from the resolved integration
45 const expectedToken = integration.webhook_secret;
46
47 // Verify the token matches (double-check with a constant-time comparison)
48 if (!verifyGitLabWebhookToken(webhookToken, expectedToken)) {
49 logExceptInTest('Webhook token verification failed');
50 return new NextResponse('Unauthorized', { status: 401 });
51 }
52
53 // 3. Only an active integration may act for its owner
54 if (!isPlatformIntegrationActive(integration)) {
55 logExceptInTest('Integration not active, skipping event');
56 return NextResponse.json({ message: 'Integration suspended' }, { status: 200 });
57 }
58
59 // 4. Parse JSON payload
60 let payload: unknown;
61 try {
62 payload = await request.json();
63 } catch (error) {
64 logExceptInTest('Error parsing GitLab webhook JSON body:', error);
65 captureException(error, {
66 tags: { source: 'gitlab_webhook_parse_json' },
67 });
68 return NextResponse.json({ error: 'Invalid JSON payload' }, { status: 400 });
69 }
70
71 // 5. Get event type from header
72 const eventType = request.headers.get('x-gitlab-event') || '';
73 const eventSignature = request.headers.get('x-gitlab-event-uuid') || `gitlab-${Date.now()}`;
74 const headers = redactSensitiveHeaders(Object.fromEntries(request.headers));
75
76 if (!eventType) {
77 return NextResponse.json({ error: 'Missing X-Gitlab-Event header' }, { status: 400 });
78 }
79
80 logExceptInTest('GitLab webhook received:', {
81 eventType,
82 integrationId: integration.id,

Callers

nothing calls this directly

Calls 8

verifyGitLabWebhookTokenFunction · 0.90
redactSensitiveHeadersFunction · 0.90
handleMergeRequestFunction · 0.90
updateWebhookEventFunction · 0.90
logWebhookFunction · 0.70
getMethod · 0.65
jsonMethod · 0.65

Tested by

no test coverage detected