Files
homebrewery-oidc/server/storage/mongo/git.js
T
dev-bot BotandClaude Sonnet 5.5 448c67912f
CI / App (npm ci + Build, ESLint, Stylelint, Jest) (push) Successful in 3m25s
CI / Storage (mongo) (push) Successful in 1m24s
CI / Storage (postgres) (push) Successful in 1m13s
Image / Image bauen und pushen (push) Successful in 1m3s
feat(git): Git-Backend über HTTPS + PAT (ADR 0003, FLW-86) (#19)
Repositories gitConnections/brewGitLinks (Mongo/PG), Krypto, SSRF-Policy mit net.BlockList, git-Client ohne Arbeitskopie, Sync/Konflikte, /api/git/*, Client (Verbindungen, Git-Status, Konfliktdialog), Autosave 60 s für Git-Brews (F6), Tests und Doku.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Dev (Paperclip) <dev-bot@noreply.git.morlana.online>
2026-11-25 21:16:42 +00:00

121 lines
5.1 KiB
JavaScript

import { randomUUID } from 'crypto';
import mongoose from 'mongoose';
// Fork-Entitäten "GitConnection" und "BrewGitLink" (ADR 0003 §2). Eigene Collections, keine Upstream-Modelle.
const ConnectionSchema = new mongoose.Schema({
id : { type: String, required: true, unique: true },
ownerUserId : { type: String, required: true, index: true },
label : { type: String, required: true },
repoUrl : { type: String, required: true },
authUsername : { type: String, default: 'git' },
tokenEnc : { type: String, required: true },
defaultBranch : { type: String, default: 'main' },
brewDir : { type: String, default: 'brews' },
assetsDir : { type: String, default: 'assets' },
createdAt : { type: Date, default: Date.now },
updatedAt : { type: Date, default: Date.now },
lastVerifiedAt : { type: Date }
}, { collection: 'gitconnections', versionKey: false });
const LinkSchema = new mongoose.Schema({
brewId : { type: String, required: true, unique: true },
connectionId : { type: String, required: true, index: true },
branch : { type: String, required: true },
path : { type: String, required: true },
lastCommitSha : { type: String },
lastBlobSha : { type: String },
lastSyncedAt : { type: Date },
lastError : { type: String }
}, { collection: 'brewgitlinks', versionKey: false });
LinkSchema.index({ connectionId: 1, branch: 1, path: 1 }, { unique: true });
export const getGitModels = (connection = mongoose.connection)=>({
Connection : connection.models.ForkGitConnection ?? connection.model('ForkGitConnection', ConnectionSchema),
Link : connection.models.ForkBrewGitLink ?? connection.model('ForkBrewGitLink', LinkSchema)
});
const nul = (v)=>v ?? null;
const connToPlain = (doc)=>{
if(!doc) return null;
const o = doc.toObject ? doc.toObject() : doc;
return {
id : o.id,
ownerUserId : o.ownerUserId,
label : o.label,
repoUrl : o.repoUrl,
authUsername : o.authUsername,
tokenEnc : o.tokenEnc,
defaultBranch : o.defaultBranch,
brewDir : o.brewDir,
assetsDir : o.assetsDir,
createdAt : o.createdAt,
updatedAt : o.updatedAt,
lastVerifiedAt : nul(o.lastVerifiedAt)
};
};
const linkToPlain = (doc)=>{
if(!doc) return null;
const o = doc.toObject ? doc.toObject() : doc;
return {
brewId : o.brewId,
connectionId : o.connectionId,
branch : o.branch,
path : o.path,
lastCommitSha : nul(o.lastCommitSha),
lastBlobSha : nul(o.lastBlobSha),
lastSyncedAt : nul(o.lastSyncedAt),
lastError : nul(o.lastError)
};
};
const CONN_FIELDS = ['label', 'repoUrl', 'authUsername', 'tokenEnc', 'defaultBranch', 'brewDir', 'assetsDir', 'lastVerifiedAt'];
const LINK_FIELDS = ['connectionId', 'branch', 'path', 'lastCommitSha', 'lastBlobSha', 'lastSyncedAt', 'lastError'];
const pick = (obj, fields)=>Object.fromEntries(fields.filter((f)=>obj[f] !== undefined).map((f)=>[f, obj[f]]));
export const createGitConnectionRepository = (connection = mongoose.connection)=>{
const { Connection, Link } = getGitModels(connection);
return {
Model : Connection,
async create({ id, ownerUserId, ...rest }) {
return connToPlain(await Connection.create({ id: id ?? randomUUID(), ownerUserId: String(ownerUserId), ...pick(rest, CONN_FIELDS) }));
},
async findById(id) { return connToPlain(await Connection.findOne({ id: String(id) })); },
async findByIdForOwner(id, userId) {
return connToPlain(await Connection.findOne({ id: String(id), ownerUserId: String(userId) }));
},
async listByOwner(userId) {
return (await Connection.find({ ownerUserId: String(userId) }).sort({ createdAt: 1, id: 1 })).map(connToPlain);
},
async update(id, patch) {
return connToPlain(await Connection.findOneAndUpdate({ id: String(id) }, { $set: { ...pick(patch, CONN_FIELDS), updatedAt: new Date() } }, { returnDocument: 'after' }));
},
async delete(id) {
const res = await Connection.deleteOne({ id: String(id) });
if(res.deletedCount > 0) await Link.deleteMany({ connectionId: String(id) });
return res.deletedCount > 0;
}
};
};
export const createBrewGitLinkRepository = (connection = mongoose.connection)=>{
const { Link } = getGitModels(connection);
return {
Model : Link,
async upsert({ brewId, ...rest }) {
return linkToPlain(await Link.findOneAndUpdate({ brewId: String(brewId) }, { $set: pick(rest, LINK_FIELDS) },
{ upsert: true, returnDocument: 'after', setDefaultsOnInsert: true }));
},
async findByBrewId(brewId) { return linkToPlain(await Link.findOne({ brewId: String(brewId) })); },
async findByLocation(connectionId, branch, path) {
return linkToPlain(await Link.findOne({ connectionId: String(connectionId), branch, path }));
},
async listByConnection(connectionId) { return (await Link.find({ connectionId: String(connectionId) })).map(linkToPlain); },
async update(brewId, patch) {
return linkToPlain(await Link.findOneAndUpdate({ brewId: String(brewId) }, { $set: pick(patch, LINK_FIELDS) }, { returnDocument: 'after' }));
},
async delete(brewId) { return (await Link.deleteOne({ brewId: String(brewId) })).deletedCount > 0; }
};
};