feat(git): Client (Verbindungen, Git-Status, Konfliktdialog), DB-Integrationstest, Doku, Compose-/Env-Vorlage
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe9efc155e
commit
e40b4d1499
15 files changed
+621
-6
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import './git.less';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { gitApi } from './gitApi.js';
|
||||
|
||||
// Verbindungen verwalten (ADR 0003 §7): anlegen, testen, löschen, Brews aus dem Repo importieren.
|
||||
// Das Token-Feld ist reines Eingabefeld und wird nie vorbefüllt; der Server gibt den Token nie zurück.
|
||||
const EMPTY = { label: '', repoUrl: '', authUsername: 'git', token: '' };
|
||||
|
||||
const GitConnectionsPanel = ()=>{
|
||||
const [state, setState] = useState('loading'); // loading | off | ready
|
||||
const [conns, setConns] = useState([]);
|
||||
const [form, setForm] = useState(EMPTY);
|
||||
const [msg, setMsg] = useState({}); // pro Verbindung: { ok, text }
|
||||
const [formError, setFormError] = useState('');
|
||||
const [files, setFiles] = useState({}); // pro Verbindung: { branch, files }
|
||||
|
||||
const reload = ()=>gitApi.listConnections().then((list)=>{ setConns(list); setState('ready'); })
|
||||
.catch((err)=>{ if([403, 503].includes(err.status)) setState('off'); else { setFormError(err.message); setState('ready'); } });
|
||||
useEffect(()=>{ reload(); }, []);
|
||||
|
||||
if(state !== 'ready') return null; // Git-Backend deaktiviert oder kein OIDC-Konto
|
||||
|
||||
const set = (id, ok, text)=>setMsg((m)=>({ ...m, [id]: { ok, text } }));
|
||||
|
||||
const create = async (e)=>{
|
||||
e.preventDefault();
|
||||
setFormError('');
|
||||
try {
|
||||
await gitApi.createConnection(form);
|
||||
setForm(EMPTY);
|
||||
await reload();
|
||||
} catch (err) { setFormError(err.message); }
|
||||
};
|
||||
|
||||
const test = (c)=>gitApi.testConnection(c.id)
|
||||
.then((r)=>{ set(c.id, true, `Verbindung ok (Standard-Branch ${r.defaultBranch ?? '?'}, ${r.branchCount} Branches)`); reload(); })
|
||||
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
|
||||
|
||||
const remove = async (c)=>{
|
||||
if(!window.confirm(`Verbindung „${c.label}“ löschen? Verknüpfte Brews bleiben als normale Brews erhalten.`)) return;
|
||||
await gitApi.deleteConnection(c.id).catch((err)=>set(c.id, false, err.message));
|
||||
reload();
|
||||
};
|
||||
|
||||
const showFiles = (c)=>gitApi.listFiles(c.id, c.defaultBranch, c.brewDir)
|
||||
.then((r)=>setFiles((f)=>({ ...f, [c.id]: { branch: r.branch, files: r.files } })))
|
||||
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
|
||||
|
||||
const importFile = (c, branch, path)=>gitApi.importBrew({ connectionId: c.id, branch, path })
|
||||
.then((brew)=>{ window.location.href = `/edit/${brew.editId}`; })
|
||||
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
|
||||
|
||||
return (
|
||||
<div className='dataGroup gitConnections'>
|
||||
<h3>Git-Verbindungen <i className='fab fa-git-alt'></i></h3>
|
||||
<p>Brews in einem beliebigen Git-Repository speichern (HTTPS + Personal Access Token). Der Token wird verschlüsselt gespeichert und nie angezeigt.</p>
|
||||
{conns.map((c)=>(
|
||||
<div className='gitConn' key={c.id}>
|
||||
<div className='label'>{c.label}</div>
|
||||
<div className='url'>{c.repoUrl}</div>
|
||||
<div className='meta'>Standard-Branch {c.defaultBranch}, Ordner {c.brewDir || '/'}{c.lastVerifiedAt ? '' : ' · noch nicht getestet'}</div>
|
||||
<div className='actions'>
|
||||
<button onClick={()=>test(c)}>Testen</button>
|
||||
<button onClick={()=>showFiles(c)}>Brews aus Repo importieren</button>
|
||||
<button onClick={()=>remove(c)}>Löschen</button>
|
||||
</div>
|
||||
{msg[c.id] && <div className={msg[c.id].ok ? 'ok' : 'error'}>{msg[c.id].text}</div>}
|
||||
{files[c.id] && (
|
||||
<ul className='files'>
|
||||
{files[c.id].files.length === 0 && <li>Keine .md-Dateien in {c.brewDir || '/'} auf {files[c.id].branch}.</li>}
|
||||
{files[c.id].files.map((f)=>(
|
||||
<li key={f}>{f} <button onClick={()=>importFile(c, files[c.id].branch, f)}>Importieren</button></li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
<form onSubmit={create}>
|
||||
<strong>Neue Verbindung</strong>
|
||||
<input placeholder='Bezeichnung' value={form.label} required maxLength={80} onChange={(e)=>setForm({ ...form, label: e.target.value })} />
|
||||
<input placeholder='https://git.example.org/nutzer/repo.git' value={form.repoUrl} required onChange={(e)=>setForm({ ...form, repoUrl: e.target.value })} />
|
||||
<input placeholder='Benutzername (Standard: git)' value={form.authUsername} onChange={(e)=>setForm({ ...form, authUsername: e.target.value })} />
|
||||
<input type='password' autoComplete='off' placeholder='Personal Access Token' value={form.token} required onChange={(e)=>setForm({ ...form, token: e.target.value })} />
|
||||
<button type='submit'>Verbindung anlegen</button>
|
||||
{formError && <div className='error'>{formError}</div>}
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default GitConnectionsPanel;
|
||||
@@ -0,0 +1,62 @@
|
||||
import React, { useState } from 'react';
|
||||
import Nav from '@navbar/nav.jsx';
|
||||
import { gitApi, setGitOverwrite, SAVE_EVENT } from './gitApi.js';
|
||||
|
||||
// Fehlertexte und Konfliktdialog für G01–G10 (ADR 0003 §5, §7).
|
||||
const TEXTS = {
|
||||
G02 : 'Anmeldung am Git-Server fehlgeschlagen. Prüfe den Token der Verbindung (Account-Seite).',
|
||||
G03 : 'Repository, Branch oder Datei wurden nicht gefunden.',
|
||||
G04 : 'Der Git-Server ist nicht erreichbar oder zu langsam. Deine Änderungen sind noch nicht gespeichert, bitte später erneut speichern.',
|
||||
G05 : 'Speichern im Repository ist gerade nicht möglich (viele gleichzeitige Änderungen). Bitte gleich erneut speichern.',
|
||||
G06 : 'Die Datei existiert im Repository bereits.',
|
||||
G07 : 'Das Git-Backend steht nur Konten mit OIDC-Anmeldung zur Verfügung.',
|
||||
G08 : 'Ungültige Eingabe für Git.',
|
||||
G09 : 'Die Datei ist für das Git-Backend zu groß.',
|
||||
G10 : 'Das Git-Backend ist auf diesem Server nicht aktiviert.'
|
||||
};
|
||||
|
||||
const downloadOwnVersion = (brew)=>{
|
||||
const blob = new Blob([brew?.text ?? ''], { type: 'text/markdown' });
|
||||
const a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = `${(brew?.title || 'brew').replace(/[^\w-]+/g, '_')}-meine-version.md`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(a.href);
|
||||
};
|
||||
|
||||
const GitErrorNavItem = ({ error, clearError, brew })=>{
|
||||
const body = error.response?.body ?? {};
|
||||
const code = body.HBErrorCode;
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
if(code === 'G01') {
|
||||
const keepMine = ()=>{
|
||||
setGitOverwrite(body.remoteBlobSha ?? 'none');
|
||||
clearError();
|
||||
document.dispatchEvent(new CustomEvent(SAVE_EVENT));
|
||||
};
|
||||
const loadRemote = async ()=>{
|
||||
setBusy(true);
|
||||
try { await gitApi.pull(brew.editId); window.location.reload(); } catch { setBusy(false); }
|
||||
};
|
||||
return <Nav.item className='save error' icon='fas fa-exclamation-triangle'>
|
||||
Konflikt
|
||||
<div className='errorContainer'>
|
||||
Die Datei wurde im Git-Repository geändert{body.remoteCommitSha ? ` (${body.remoteCommitSha.slice(0, 7)})` : ''}.<br />
|
||||
<div className='confirm' onClick={busy ? undefined : keepMine}>Meine Version behalten</div>
|
||||
<div className='deny' onClick={busy ? undefined : ()=>downloadOwnVersion(brew)}>Meine Version sichern</div>
|
||||
<div className='deny' onClick={busy ? undefined : loadRemote}>Remote-Version laden</div>
|
||||
<div className='deny' onClick={clearError}>Abbrechen</div>
|
||||
</div>
|
||||
</Nav.item>;
|
||||
}
|
||||
|
||||
return <Nav.item className='save error' icon='fas fa-exclamation-triangle'>
|
||||
Oops!
|
||||
<div className='errorContainer' onClick={clearError}>
|
||||
{TEXTS[code] ?? body.message ?? 'Git-Fehler.'}
|
||||
</div>
|
||||
</Nav.item>;
|
||||
};
|
||||
|
||||
export default GitErrorNavItem;
|
||||
@@ -0,0 +1,80 @@
|
||||
import './git.less';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import Nav from '@navbar/nav.jsx';
|
||||
import { gitApi } from './gitApi.js';
|
||||
|
||||
// Git-Status im Editor-Navbar (ADR 0003 §7): Branch, Pfad, kurzer SHA, Fehler; Pull; Verknüpfen/Lösen.
|
||||
const short = (sha)=>sha ? sha.slice(0, 7) : '';
|
||||
|
||||
const GitNavItem = ({ brew, lastSavedTime })=>{
|
||||
const editId = brew?.editId;
|
||||
const [status, setStatus] = useState(null); // null: unbekannt/aus
|
||||
const [conns, setConns] = useState(null);
|
||||
const [form, setForm] = useState({ connectionId: '', branch: '', path: '' });
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const refresh = ()=>gitApi.status(editId).then(setStatus).catch(()=>setStatus(null));
|
||||
useEffect(()=>{ if(editId) refresh(); }, [editId, lastSavedTime]);
|
||||
|
||||
// Verbindungen nur laden, wenn verknüpft werden soll (403/503: Git nicht verfügbar → Item bleibt unsichtbar)
|
||||
useEffect(()=>{
|
||||
if(status && !status.linked && conns === null) {
|
||||
gitApi.listConnections().then((list)=>{ setConns(list); setForm((f)=>({ ...f, connectionId: list[0]?.id ?? '' })); }).catch(()=>setConns(false));
|
||||
}
|
||||
}, [status]);
|
||||
|
||||
if(!editId || !status) return null;
|
||||
|
||||
const run = async (fn)=>{
|
||||
setBusy(true); setError('');
|
||||
try { await fn(); await refresh(); } catch (err) { setError(`${err.HBErrorCode ?? ''} ${err.message}`.trim()); }
|
||||
setBusy(false);
|
||||
};
|
||||
|
||||
if(status.linked) {
|
||||
return (
|
||||
<Nav.dropdown className='gitNav'>
|
||||
<Nav.item icon={status.lastError ? 'fas fa-exclamation-triangle' : 'fab fa-git-alt'} className={status.lastError ? 'error' : ''}>
|
||||
{status.branch}
|
||||
</Nav.item>
|
||||
<div className='gitDropdown'>
|
||||
<div className='meta'>{status.connectionLabel}: {status.repoUrl}</div>
|
||||
<div className='meta'>{status.branch} · {status.path}{status.lastCommitSha ? ` · ${short(status.lastCommitSha)}` : ''}</div>
|
||||
{status.lastError === 'G04' && <div className='error'>Git nicht erreichbar, angezeigt wird der zuletzt gespeicherte Stand.</div>}
|
||||
{status.lastError && status.lastError !== 'G04' && <div className='error'>Letzter Fehler: {status.lastError}</div>}
|
||||
{error && <div className='error'>{error}</div>}
|
||||
<button disabled={busy} onClick={()=>run(async ()=>{
|
||||
if(!window.confirm('Remote-Stand laden? Ungespeicherte Änderungen im Editor gehen verloren.')) return;
|
||||
await gitApi.pull(editId);
|
||||
window.location.reload();
|
||||
})}>Pull</button>
|
||||
{status.isConnectionOwner && <button disabled={busy} onClick={()=>run(async ()=>{
|
||||
if(window.confirm('Verknüpfung lösen? Der Brew bleibt erhalten, die Datei im Repository auch.')) await gitApi.unlinkBrew(editId);
|
||||
})}>Verknüpfung lösen</button>}
|
||||
</div>
|
||||
</Nav.dropdown>
|
||||
);
|
||||
}
|
||||
|
||||
if(!conns || conns.length === 0) return null; // nichts zu verknüpfen (kein OIDC-Konto oder keine Verbindung)
|
||||
|
||||
return (
|
||||
<Nav.dropdown className='gitNav'>
|
||||
<Nav.item icon='fab fa-git-alt'>Git</Nav.item>
|
||||
<div className='gitDropdown'>
|
||||
<select value={form.connectionId} onChange={(e)=>setForm({ ...form, connectionId: e.target.value })}>
|
||||
{conns.map((c)=><option key={c.id} value={c.id}>{c.label}</option>)}
|
||||
</select>
|
||||
<input placeholder='Branch (Standard der Verbindung)' value={form.branch} onChange={(e)=>setForm({ ...form, branch: e.target.value })} />
|
||||
<input placeholder='Pfad, z. B. brews/mein-brew.md' value={form.path} onChange={(e)=>setForm({ ...form, path: e.target.value })} />
|
||||
{error && <div className='error'>{error}</div>}
|
||||
<button disabled={busy || !form.connectionId} onClick={()=>run(()=>gitApi.linkBrew(editId, { connectionId: form.connectionId, branch: form.branch || undefined, path: form.path || undefined }))}>
|
||||
In Git speichern
|
||||
</button>
|
||||
</div>
|
||||
</Nav.dropdown>
|
||||
);
|
||||
};
|
||||
|
||||
export default GitNavItem;
|
||||
@@ -0,0 +1,35 @@
|
||||
@import '@sharedStyles/core.less';
|
||||
|
||||
.gitConnections {
|
||||
.gitConn {
|
||||
padding : 8px 0;
|
||||
border-bottom : 1px solid #cccccc;
|
||||
.label { font-weight : 800; }
|
||||
.url, .meta { font-size : 0.9em; color : #555555; word-break : break-all; }
|
||||
.actions button { margin : 4px 6px 0 0; }
|
||||
}
|
||||
form {
|
||||
display : grid;
|
||||
gap : 6px;
|
||||
margin-top : 10px;
|
||||
input { padding : 4px; }
|
||||
}
|
||||
.error { color : @red; font-weight : 800; }
|
||||
.ok { color : @teal; font-weight : 800; }
|
||||
ul.files { list-style : none; padding : 0; li { margin : 2px 0; } }
|
||||
}
|
||||
|
||||
.gitNav {
|
||||
.gitDropdown {
|
||||
display : grid;
|
||||
gap : 6px;
|
||||
min-width : 260px;
|
||||
padding : 8px;
|
||||
font-size : 12px;
|
||||
color : white;
|
||||
background-color : #333333;
|
||||
input, select { padding : 3px; }
|
||||
.error { color : #ff9a9a; font-weight : 800; }
|
||||
.meta { opacity : 0.8; word-break : break-all; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import superagent from 'superagent';
|
||||
import request from '../../homebrew/utils/request-middleware.js';
|
||||
|
||||
const patch = (path)=>superagent.patch(path).set('Homebrewery-Version', global.version);
|
||||
|
||||
// Dünne Hülle um /api/git/* (ADR 0003 §7). Fehler tragen `HBErrorCode` und `message` des Servers.
|
||||
const unwrap = (promise)=>promise.then((res)=>res.body).catch((err)=>{
|
||||
const body = err.response?.body ?? {};
|
||||
const e = new Error(body.message || err.message);
|
||||
e.status = err.response?.status;
|
||||
e.HBErrorCode = body.HBErrorCode;
|
||||
e.body = body;
|
||||
throw e;
|
||||
});
|
||||
|
||||
export const gitApi = {
|
||||
listConnections : ()=>unwrap(request.get('/api/git/connections')),
|
||||
createConnection : (data)=>unwrap(request.post('/api/git/connections').send(data)),
|
||||
updateConnection : (id, data)=>unwrap(patch(`/api/git/connections/${id}`).send(data)),
|
||||
deleteConnection : (id)=>unwrap(request.delete(`/api/git/connections/${id}`)),
|
||||
testConnection : (id)=>unwrap(request.post(`/api/git/connections/${id}/test`)),
|
||||
listBranches : (id)=>unwrap(request.get(`/api/git/connections/${id}/branches`)),
|
||||
listFiles : (id, branch, dir)=>unwrap(request.get(`/api/git/connections/${id}/files`).query({ branch, dir })),
|
||||
importBrew : (data)=>unwrap(request.post('/api/git/brews/import').send(data)),
|
||||
linkBrew : (editId, data)=>unwrap(request.post(`/api/git/brews/${editId}/link`).send(data)),
|
||||
unlinkBrew : (editId)=>unwrap(request.delete(`/api/git/brews/${editId}/link`)),
|
||||
status : (editId)=>unwrap(request.get(`/api/git/brews/${editId}`)),
|
||||
pull : (editId)=>unwrap(request.post(`/api/git/brews/${editId}/pull`))
|
||||
};
|
||||
|
||||
// "Meine Version behalten" (Konfliktdialog G01): der nächste Save hängt ?gitOverwrite=<blob> an.
|
||||
let overwriteBlob = null;
|
||||
export const setGitOverwrite = (blob)=>{ overwriteBlob = blob; };
|
||||
export const consumeGitOverwrite = ()=>{ const b = overwriteBlob; overwriteBlob = null; return b; };
|
||||
export const SAVE_EVENT = 'hb:git-save';
|
||||
Reference in new issue
Block a user