SPB Git

Repository Public

sources.ts 3.8 KB 67 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : services/control-api/src/core/sources.ts9 * Module           : core10 * Repository       : https://git.spboucher.ai/maclustr-social11 * Project Website  : https://social.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *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: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Git integration through the forge's documented JSON API (GET /api/v1/repos/:name/releases). Polling only; no invented endpoint.43import type { Db } from '../db.ts';44import type { Config } from '../config.ts';4546export type ForgeRelease = { tag: string; date: string; subject: string; assets?: { name: string; url: string; size: number }[] };47export async function pollGitSources(db: Db, cfg: Config, fetchImpl: typeof fetch = fetch): Promise<{ polled: number; created: number; errors: number }> {48  const out = { polled: 0, created: 0, errors: 0 };49  for (const s of (await db.query("SELECT * FROM git_sources WHERE enabled AND (last_polled_at IS NULL OR last_polled_at < now()-interval '15 minutes')")).rows) {50    out.polled++;51    try {52      const res = await fetchImpl(`${cfg.forgeApi}/repos/${encodeURIComponent(s.repo)}/releases`, { signal: AbortSignal.timeout(15000), headers: { accept: 'application/json' } });53      if (!res.ok) throw new Error(`HTTP ${res.status}`); // 404 = private or unknown repo: nothing is read54      const j = (await res.json()) as { releases?: ForgeRelease[] };55      for (const r of (j.releases ?? []).slice(0, 20)) {56        if (typeof r.tag !== 'string' || !/^[\w.+-]{1,80}$/.test(r.tag)) continue;57        // public_ok stays false: a release note becomes publishable only after a human validates it.58        const ins = await db.query("INSERT INTO source_events(brand_id, kind, source, dedupe_key, payload) VALUES ($1,'git_release',$2,$3,$4) ON CONFLICT (dedupe_key) DO NOTHING", [s.brand_id, `${s.repo}@${r.tag}`, `git:${s.brand_id}:${s.repo}:${r.tag}`,59          JSON.stringify({ repo: s.repo, tag: r.tag, date: String(r.date ?? '').slice(0, 40), subject: String(r.subject ?? '').slice(0, 500), assets: (r.assets ?? []).slice(0, 10).map((a) => ({ name: String(a.name).slice(0, 120), url: String(a.url).slice(0, 300) })) })]);60        out.created += ins.rowCount ?? 0;61      }62      await db.query('UPDATE git_sources SET last_polled_at=now(), last_error=NULL WHERE id=$1', [s.id]);63    } catch (e) { out.errors++; await db.query('UPDATE git_sources SET last_polled_at=now(), last_error=$2 WHERE id=$1', [s.id, String((e as Error).message).slice(0, 200)]); }64  }65  return out;66}67
Files main
Menu

Theme