1
0

Refactor push-to-gitea handler to include LLM-generated description and improve error handling

This commit is contained in:
2025-06-01 12:25:15 +02:00
parent 24a2d51868
commit 6a360256f3

43
main.js
View File

@@ -2256,38 +2256,35 @@ Source Code:
ipcMain.handle('push-to-gitea', async (_evt, folderPath) => { ipcMain.handle('push-to-gitea', async (_evt, folderPath) => {
try { try {
// 1) Repo name = folders basename
const repoName = path.basename(folderPath); const repoName = path.basename(folderPath);
// 2) Read your Gitea token from the environment
const GITEA_TOKEN = process.env.GITEA_TOKEN; const GITEA_TOKEN = process.env.GITEA_TOKEN;
if (!GITEA_TOKEN) { if (!GITEA_TOKEN) {
throw new Error('No GITEA_TOKEN in environment'); throw new Error('No GITEA_TOKEN set in environment');
} }
// 3) Base URL for your Gitea API (no trailing slash)
const GITEA_BASE = 'https://giers10.uber.space/api/v1'; const GITEA_BASE = 'https://giers10.uber.space/api/v1';
// 4) Fetch /user to get the username // 1) Ask the LLM for a brief (<255 chars) description
const description = await generateRepoDescription(folderPath, BrowserWindow.fromWebContents(_evt.sender));
// 2) Fetch /user to get the Gitea username
const userResp = await fetch(`${GITEA_BASE}/user`, { const userResp = await fetch(`${GITEA_BASE}/user`, {
headers: { Authorization: `token ${GITEA_TOKEN}` } headers: { Authorization: `token ${GITEA_TOKEN}` }
}); });
if (!userResp.ok) { if (!userResp.ok) {
const text = await userResp.text(); const txt = await userResp.text();
throw new Error(`Gitea /user failed: ${userResp.status} ${text}`); throw new Error(`/user request failed: ${userResp.status} ${txt}`);
} }
const userData = await userResp.json(); const userData = await userResp.json();
const username = userData.login; const username = userData.login;
// 5) Check if /repos/{username}/{repoName} already exists // 3) Check if repo already exists
const checkResp = await fetch( const checkResp = await fetch(`${GITEA_BASE}/repos/${username}/${repoName}`, {
`${GITEA_BASE}/repos/${username}/${repoName}`, headers: { Authorization: `token ${GITEA_TOKEN}` }
{ headers: { Authorization: `token ${GITEA_TOKEN}` } } });
);
let repoUrl; let repoUrl;
if (checkResp.status === 404) { if (checkResp.status === 404) {
// Repo does not exist → create it // 4a) Create a new repo (include the LLM description)
const createResp = await fetch(`${GITEA_BASE}/user/repos`, { const createResp = await fetch(`${GITEA_BASE}/user/repos`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -2296,41 +2293,39 @@ Source Code:
}, },
body: JSON.stringify({ body: JSON.stringify({
name: repoName, name: repoName,
description, // ← use our LLMgenerated description here
private: false, private: false,
description: `Autocreated for ${repoName}` auto_init: false // we already initialized locally
}) })
}); });
if (!createResp.ok) { if (!createResp.ok) {
const txt = await createResp.text(); const txt = await createResp.text();
throw new Error(`Could not create repo: ${createResp.status} ${txt}`); throw new Error(`Failed to create repo: ${createResp.status} ${txt}`);
} }
const created = await createResp.json(); const created = await createResp.json();
repoUrl = created.clone_url; repoUrl = created.clone_url;
} else if (checkResp.ok) { } else if (checkResp.ok) {
// Repo already exists // 4b) Repo already exists → use its clone_url
const existing = await checkResp.json(); const existing = await checkResp.json();
repoUrl = existing.clone_url; repoUrl = existing.clone_url;
} else { } else {
// Some other error code
const txt = await checkResp.text(); const txt = await checkResp.text();
throw new Error(`Error checking repo: ${checkResp.status} ${txt}`); throw new Error(`Error checking repo: ${checkResp.status} ${txt}`);
} }
// 6) Configure local Git remote “origin” to point at repoUrl // 5) Configure local Git remote “origin” to point to repoUrl
const git = simpleGit(folderPath); const git = simpleGit(folderPath);
try { try {
await git.removeRemote('origin'); await git.removeRemote('origin');
} catch (e) { } catch (e) {
// ignore if origin” didnt exist // ignore if no origin existed
} }
await git.addRemote('origin', repoUrl); await git.addRemote('origin', repoUrl);
// 7) Detect current branch and push // 6) Push current branch + tags
const currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim(); const currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();
// Push current branch + tags; adjust flags if you only want a simple push:
await git.push(['-u', 'origin', currentBranch, '--force', '--tags']); await git.push(['-u', 'origin', currentBranch, '--force', '--tags']);
// 8) Return success + URL so the renderer can notify the user
return { success: true, repoUrl }; return { success: true, repoUrl };
} catch (err) { } catch (err) {
return { success: false, error: err.message || String(err) }; return { success: false, error: err.message || String(err) };