Files
FWS-DiscordBot/src/web/api/index.js
T
nocodeandClaude Opus 5 901ca56959 feat(projects): add a Kanban board per repository and one for the organization
Boards live in SQLite rather than in Gitea, whose project API is not stable
across versions: a board that stops working whenever the forge hiccups is
worse than no board, and this one has hiccupped already.

A card can still point at an issue — `owner/repo#12` — and its real state is
resolved when the forge answers, one request per repository rather than per
card. Open issues can also be imported in bulk, skipping those already on
the board.

Columns follow the ones the README describes, and carry an optional card
limit which warns rather than refuses: stopping someone mid-gesture to
enforce a convention is not what a limit is for.

Moving a card rebuilds the order explicitly — take the column without it,
insert at the wanted rank, renumber — because fractional positions get the
same-column case wrong, as the tests showed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 23:20:08 +02:00

121 lines
4.0 KiB
JavaScript

import { readFileSync, readdirSync, statSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ACCESS_LEVELS } from '../../config/constants.js';
import { registerAuthRoutes } from '../auth/routes.js';
import { openEventStream } from '../events.js';
import { registerCommunityRoutes } from './community.js';
import { registerConfigurationRoutes } from './configuration.js';
import { registerDeploymentRoutes } from './deployments.js';
import { registerDiscordRoutes } from './discord.js';
import { registerLogRoutes } from './logs.js';
import { registerOrganizationRoutes } from './organization.js';
import { registerOverviewRoutes } from './overview.js';
import { registerProjectRoutes } from './projects.js';
import { registerReleaseRoutes } from './releases.js';
import { registerRepositoryRoutes } from './repositories.js';
import { registerSettingsRoutes } from './settings.js';
import { registerWikiRoutes } from './wiki.js';
const here = path.dirname(fileURLToPath(import.meta.url));
const VERSION = (() => {
try {
return JSON.parse(readFileSync(path.join(here, '../../../package.json'), 'utf8')).version;
} catch {
return 'inconnue';
}
})();
/**
* Newest file of the served interface.
*
* Deployments that ship an archive make it easy to look at a stale copy and
* wonder why a fix is missing; this gives the page something to state.
*/
/**
* Newest file of the served interface.
*
* Read from disk rather than frozen at startup: the front-end is served from
* disk on every request, so a marker captured at boot would keep announcing an
* old build after an upload — the exact confusion it exists to prevent. Cached
* briefly so a health check does not walk the tree each time.
*/
const BUILD_TTL_MS = 10_000;
let buildCache = { at: null, readAt: 0 };
function buildAt() {
if (buildCache.readAt && Date.now() - buildCache.readAt < BUILD_TTL_MS) return buildCache.at;
const root = path.join(here, '../public');
let newest = 0;
const walk = (directory) => {
for (const entry of readdirSync(directory, { withFileTypes: true })) {
const target = path.join(directory, entry.name);
if (entry.isDirectory()) walk(target);
else newest = Math.max(newest, statSync(target).mtimeMs);
}
};
try {
walk(root);
} catch {
buildCache = { at: null, readAt: Date.now() };
return null;
}
buildCache = { at: newest > 0 ? new Date(newest).toISOString() : null, readAt: Date.now() };
return buildCache.at;
}
/**
* Route registration order matters only for readability: the router matches on
* the full pattern, not on declaration order.
*/
export function registerApiRoutes(router, { setupOnly = false } = {}) {
// Liveness probe for a reverse proxy or an uptime monitor. Registered first so
// it answers even in bootstrap mode, when nothing else is mounted.
router.get(
'/api/health',
({ context }) => ({
status: context.client?.isReady?.() ? 'ok' : 'starting',
setupOnly: context.setupOnly,
uptimeMs: Date.now() - context.startedAt,
version: VERSION,
buildAt: buildAt(),
}),
{ public: true },
);
registerAuthRoutes(router);
registerConfigurationRoutes(router);
// The public documentation stays reachable even while the bot is being set up.
registerWikiRoutes(router);
// Long-lived stream the pages listen to instead of polling (M16-T12).
router.get(
'/api/events',
({ request, response, actor }) => {
openEventStream(request, response, actor);
},
{ access: ACCESS_LEVELS.STAFF },
);
// In bootstrap mode the bot is not connected and the database may be empty:
// only the wizard makes sense.
if (setupOnly) return router;
registerOverviewRoutes(router);
registerOrganizationRoutes(router);
registerProjectRoutes(router);
registerRepositoryRoutes(router);
registerReleaseRoutes(router);
registerDeploymentRoutes(router);
registerDiscordRoutes(router);
registerCommunityRoutes(router);
registerSettingsRoutes(router);
registerLogRoutes(router);
return router;
}