SPB Git

Repository Public

route.ts 3.2 KB 68 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/app/api/hooks/release/route.ts9 * Module           : app10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { audit } from "@/lib/audit";44import { env } from "@/lib/env";45import { runSync } from "@/lib/releases/sync";46import { clientIp, json, route } from "@/lib/security/http";47import { rateLimit } from "@/lib/security/ratelimit";48import { verifyWebhook } from "@/lib/webhook";4950/**51 * "A release was published on SPB Git": triggers a synchronization of one repository. The request only tells52 * the site to look; everything it learns still comes from the forge and still waits for approval.53 */54export const POST = route(async (req) => {55  rateLimit("hook", clientIp(req), 12, 60);56  const body = (await req.text()).slice(0, 4000);57  const v = verifyWebhook({ secret: env.webhookSecret, timestamp: req.headers.get("x-orix-timestamp"), nonce: req.headers.get("x-orix-nonce"), signature: req.headers.get("x-orix-signature"), body });58  if (!v.ok) {59    audit("webhook.rejected", { ip: clientIp(req), detail: { reason: v.reason } });60    return json({ error: { code: v.reason } }, { status: v.reason === "disabled" ? 404 : 401 });61  }62  let repo: string | undefined;63  try { const p = JSON.parse(body) as { repo?: unknown }; if (typeof p.repo === "string" && /^[A-Za-z0-9._-]{1,100}$/.test(p.repo)) repo = p.repo; } catch { /* no body: sync everything */ }64  audit("webhook.accepted", { ip: clientIp(req), detail: { repo: repo ?? null } });65  try { const r = await runSync({ trigger: "webhook", only: repo }); return json({ ok: r.ok, summary: r.summary }); }66  catch (e) { return json({ ok: false, error: e instanceof Error ? e.message : "sync failed" }, { status: 409 }); }67});68
Files main
Menu

Theme