Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
66 lines
3.8 KiB
JavaScript
66 lines
3.8 KiB
JavaScript
import express from 'express';
|
|
import asyncHandler from 'express-async-handler';
|
|
import checkClientVersion from '../../middleware/check-client-version.js';
|
|
import dbCheck from '../../middleware/dbCheck.js';
|
|
import { GitError } from './errors.js';
|
|
import { publicConnection } from './brewSync.js';
|
|
|
|
// /api/git/* (ADR 0003 §4, §5, §7). Verwaltungsrouten verlangen ein OIDC-Konto (req.account.uid);
|
|
// Status und Pull verlangen nur das Bearbeitungsrecht am Brew.
|
|
export const createGitRouter = (svc, { logger = console } = {})=>{
|
|
const router = express.Router();
|
|
|
|
const send = (res, status, body)=>res.status(status).json(body);
|
|
|
|
const requireEnabled = (req, res, next)=>{
|
|
if(!svc.enabled) return send(res, 503, new GitError('G10').toBody());
|
|
next();
|
|
};
|
|
const requireOidc = (req, res, next)=>{
|
|
if(!req.account?.uid) return send(res, 403, new GitError('G07').toBody());
|
|
next();
|
|
};
|
|
const requireAccount = (req, res, next)=>{
|
|
if(!req.account) return send(res, 401, { HBErrorCode: '04', message: 'Nicht angemeldet.' });
|
|
next();
|
|
};
|
|
const handle = (fn)=>asyncHandler(async (req, res)=>{
|
|
try {
|
|
await fn(req, res);
|
|
} catch (err) {
|
|
if(err instanceof GitError || (err?.HBErrorCode && err?.status)) {
|
|
if(err.detail) logger.warn?.(`[git] ${req.method} ${req.path}: ${err.HBErrorCode} ${err.detail}`);
|
|
return send(res, err.status, err.toBody ? err.toBody() : { HBErrorCode: err.HBErrorCode, message: err.message });
|
|
}
|
|
logger.error?.(`[git] ${req.method} ${req.path}:`, err?.message);
|
|
send(res, 500, { HBErrorCode: 'G05', message: 'Interner Fehler im Git-Backend.' });
|
|
}
|
|
});
|
|
|
|
router.use('/api/git', dbCheck, checkClientVersion, requireEnabled);
|
|
|
|
// Verbindungen
|
|
router.get('/api/git/connections', requireOidc, handle(async (req, res)=>send(res, 200, await svc.listConnections(req.account))));
|
|
router.post('/api/git/connections', requireOidc, handle(async (req, res)=>send(res, 201, await svc.createConnection(req.account, req.body))));
|
|
router.patch('/api/git/connections/:id', requireOidc, handle(async (req, res)=>send(res, 200, await svc.updateConnection(req.account, req.params.id, req.body))));
|
|
router.delete('/api/git/connections/:id', requireOidc, handle(async (req, res)=>{ await svc.deleteConnection(req.account, req.params.id); res.status(204).end(); }));
|
|
router.post('/api/git/connections/:id/test', requireOidc, handle(async (req, res)=>send(res, 200, await svc.testConnection(req.account, req.params.id))));
|
|
router.get('/api/git/connections/:id/branches', requireOidc, handle(async (req, res)=>send(res, 200, await svc.listBranches(req.account, req.params.id))));
|
|
router.get('/api/git/connections/:id/files', requireOidc, handle(async (req, res)=>send(res, 200, await svc.listFiles(req.account, req.params.id, { branch: req.query.branch, dir: req.query.dir }))));
|
|
|
|
// Brews
|
|
router.post('/api/git/brews', requireOidc, handle(async (req, res)=>send(res, 200, await svc.createBrew(req.account, req.body ?? {}))));
|
|
router.post('/api/git/brews/import', requireOidc, handle(async (req, res)=>send(res, 200, await svc.importBrew(req.account, req.body ?? {}))));
|
|
router.post('/api/git/brews/:editId/link', requireOidc, handle(async (req, res)=>send(res, 200, await svc.linkBrew(req.account, req.params.editId, req.body ?? {}))));
|
|
router.delete('/api/git/brews/:editId/link', requireOidc, handle(async (req, res)=>{ await svc.unlinkBrew(req.account, req.params.editId); res.status(204).end(); }));
|
|
router.get('/api/git/brews/:editId', requireAccount, handle(async (req, res)=>send(res, 200, await svc.status(req.account, req.params.editId))));
|
|
router.post('/api/git/brews/:editId/pull', requireAccount, handle(async (req, res)=>send(res, 200, await svc.pull(req.account, req.params.editId))));
|
|
|
|
// Abgleich beim Öffnen, muss vor pageRoutes registriert sein
|
|
router.get('/edit/:id', svc.openSyncMiddleware());
|
|
|
|
return router;
|
|
};
|
|
|
|
export { publicConnection };
|