1
0

auto-git:

[change] main.js
This commit is contained in:
2025-06-01 12:21:56 +02:00
parent 42b847cc11
commit b4d902a1db

69
main.js
View File

@@ -13,7 +13,6 @@ const simpleGit = require('simple-git');
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const micromatch = require('micromatch'); const micromatch = require('micromatch');
const ignore = require('ignore'); const ignore = require('ignore');
const fetch = require('node-fetch');
const store = new Store({ const store = new Store({
defaults: { defaults: {
@@ -2180,39 +2179,38 @@ Source Code:
ipcMain.handle('push-to-gitea', async (_evt, folderPath) => { ipcMain.handle('push-to-gitea', async (_evt, folderPath) => {
try { try {
// 1) Determine the repo name from the folders basename // 1) Repo name = folders basename
const repoName = path.basename(folderPath); const repoName = path.basename(folderPath);
// 2) You need a Gitea API token. Store it as an environment variable, e.g. GITEA_TOKEN // 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 in environment');
} }
// 3) Your Gitea base URL (no trailing slash). Adapt if different. // 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) Check if the repo already exists for the authenticated user. // 4) Fetch /user to get the username
// For simplicity, we assume we want to create it under “your own user account.” const userResp = await fetch(`${GITEA_BASE}/user`, {
// If you instead want to create under an organization, adjust the endpoint accordingly.
// Well fetch “GET /user” to discover the username associated with the token:
const userResponse = await fetch(`${GITEA_BASE}/user`, {
headers: { Authorization: `token ${GITEA_TOKEN}` } headers: { Authorization: `token ${GITEA_TOKEN}` }
}); });
if (!userResponse.ok) { if (!userResp.ok) {
throw new Error(`Cannot fetch Gitea user: ${userResponse.status} ${await userResponse.text()}`); const text = await userResp.text();
throw new Error(`Gitea /user failed: ${userResp.status} ${text}`);
} }
const userData = await userResponse.json(); const userData = await userResp.json();
const username = userData.login; const username = userData.login;
// 5) Now check if /repos/{username}/{repoName} exists // 5) Check if /repos/{username}/{repoName} already exists
const checkResp = await fetch(`${GITEA_BASE}/repos/${username}/${repoName}`, { const checkResp = await fetch(
headers: { Authorization: `token ${GITEA_TOKEN}` } `${GITEA_BASE}/repos/${username}/${repoName}`,
}); { headers: { Authorization: `token ${GITEA_TOKEN}` } }
);
let repoUrl; let repoUrl;
if (checkResp.status === 404) { if (checkResp.status === 404) {
// Repo does not exist → create it via POST /user/repos // Repo does not exist → create it
const createResp = await fetch(`${GITEA_BASE}/user/repos`, { const createResp = await fetch(`${GITEA_BASE}/user/repos`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -2221,45 +2219,41 @@ Source Code:
}, },
body: JSON.stringify({ body: JSON.stringify({
name: repoName, name: repoName,
private: false, // or true, as you prefer private: false,
description: `Repo for ${repoName}` description: `Autocreated for ${repoName}`
}) })
}); });
if (!createResp.ok) { if (!createResp.ok) {
const bodyText = await createResp.text(); const txt = await createResp.text();
throw new Error(`Unable to create repo: ${createResp.status} ${bodyText}`); throw new Error(`Could not create repo: ${createResp.status} ${txt}`);
} }
const created = await createResp.json(); const created = await createResp.json();
repoUrl = created.clone_url; // usually something like: https://giers10.uber.space/username/repo.git repoUrl = created.clone_url;
} else if (checkResp.ok) { } else if (checkResp.ok) {
// Repo already exists // Repo already exists
const existing = await checkResp.json(); const existing = await checkResp.json();
repoUrl = existing.clone_url; repoUrl = existing.clone_url;
} else { } else {
// Some other error // Some other error code
const bodyText = await checkResp.text(); const txt = await checkResp.text();
throw new Error(`Error checking repo: ${checkResp.status} ${bodyText}`); throw new Error(`Error checking repo: ${checkResp.status} ${txt}`);
} }
// 6) Now configure local Git in folderPath to push to that remote // 6) Configure local Git remote “origin” to point at repoUrl
const git = simpleGit(folderPath); const git = simpleGit(folderPath);
// If an “origin” remote already exists, well replace it. Otherwise, add it.
try { try {
await git.removeRemote('origin'); await git.removeRemote('origin');
} catch (err) { /* ignore if no origin existed */ } } catch (e) {
// ignore if “origin” didnt exist
// Add origin pointing at Gitea }
await git.addRemote('origin', repoUrl); await git.addRemote('origin', repoUrl);
// 7) Push the current default branch (e.g. “master” or “main”) // 7) Detect current branch and push
// We can detect the default branch via `revparse --abbrev-ref HEAD`.
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:
// Forcepush all branches and tags:
// (If you only want to push the current branch, use: await git.push('origin', currentBranch);)
await git.push(['-u', 'origin', currentBranch, '--force', '--tags']); await git.push(['-u', 'origin', currentBranch, '--force', '--tags']);
// 8) Return success + the repo URL so the renderer can display it // 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) };
@@ -2277,7 +2271,6 @@ Source Code:
// … Ende der IPC-Handler … // … Ende der IPC-Handler …