mirror of
https://gitea.computerliebe.org/ComputerLiebe_ORG_private/Gitea-VSCode-Clone-Plugin.git
synced 2024-12-28 08:01:52 +00:00
* refactor(extension.ts): remove unused code and comments
* feat(extension.ts): add helper functions to get last commit and current branch
This commit is contained in:
parent
6822001ae8
commit
b5f132db7c
@ -2,7 +2,7 @@ import * as vscode from 'vscode';
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
|
|
||||||
// Funktion zum Abrufen des Gitea Repositories und Erstellen eines Pull Requests
|
// Funktion zum Erstellen eines Pull Requests
|
||||||
async function createGiteaPullRequest() {
|
async function createGiteaPullRequest() {
|
||||||
const instanceUrl = vscode.workspace.getConfiguration().get<string>('gitea.instanceUrl');
|
const instanceUrl = vscode.workspace.getConfiguration().get<string>('gitea.instanceUrl');
|
||||||
const token = vscode.workspace.getConfiguration().get<string>('gitea.personalAccessToken');
|
const token = vscode.workspace.getConfiguration().get<string>('gitea.personalAccessToken');
|
||||||
@ -30,27 +30,92 @@ async function createGiteaPullRequest() {
|
|||||||
|
|
||||||
// URL umwandeln, wenn sie im SSH-Format vorliegt (git@gitea.com:owner/repo.git)
|
// URL umwandeln, wenn sie im SSH-Format vorliegt (git@gitea.com:owner/repo.git)
|
||||||
if (repoUrl.startsWith('git@')) {
|
if (repoUrl.startsWith('git@')) {
|
||||||
// SSH-URL in HTTP(S)-URL umwandeln
|
|
||||||
const parts = repoUrl.split(':');
|
const parts = repoUrl.split(':');
|
||||||
const domain = parts[0].replace('git@', '');
|
const domain = parts[0].replace('git@', '');
|
||||||
const path = parts[1].replace('.git', '');
|
const path = parts[1].replace('.git', '');
|
||||||
repoUrl = `https://${domain}/${path}`;
|
repoUrl = `https://${domain}/${path}`;
|
||||||
} else {
|
} else {
|
||||||
// Falls es eine HTTPS-URL ist, einfach .git entfernen
|
|
||||||
repoUrl = repoUrl.replace('.git', '');
|
repoUrl = repoUrl.replace('.git', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL für Pull Request Seite generieren
|
// Extract repository owner and name from the URL
|
||||||
const pullRequestUrl = `${repoUrl}/pulls/new`;
|
const [owner, repo] = repoUrl.split('/').slice(-2);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verwende die VSCode API, um die URL im Browser zu öffnen
|
// Den letzten Commit und den Branch abrufen
|
||||||
await vscode.env.openExternal(vscode.Uri.parse(pullRequestUrl));
|
const { title, body } = await getLastCommit(currentWorkspaceFolder);
|
||||||
vscode.window.showInformationMessage('Pull-Request-Seite im Browser geöffnet.');
|
const branch = await getCurrentBranch(currentWorkspaceFolder);
|
||||||
} catch (err: unknown) {
|
|
||||||
vscode.window.showErrorMessage('Fehler beim Öffnen der Pull-Request-Seite.');
|
// Debugging-Ausgabe zur Überprüfung
|
||||||
console.error('Fehler beim Öffnen des Browsers:', err);
|
console.log('Branch:', branch);
|
||||||
|
console.log('Title:', title);
|
||||||
|
console.log('Body:', body);
|
||||||
|
|
||||||
|
if (!branch) {
|
||||||
|
vscode.window.showErrorMessage('Branch konnte nicht ermittelt werden.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API-Request-Daten vorbereiten
|
||||||
|
const prData = {
|
||||||
|
title: title, // Der letzte Commit als Titel
|
||||||
|
body: body || '', // Commit-Kommentare als Body
|
||||||
|
head: branch, // Der aktuelle Branch als "head"
|
||||||
|
base: 'main' // Standardmäßig auf 'main' als Basisbranch
|
||||||
|
};
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
method: 'post',
|
||||||
|
maxBodyLength: Infinity,
|
||||||
|
url: `${instanceUrl}/api/v1/repos/${owner}/${repo}/pulls`,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `token ${token}`
|
||||||
|
},
|
||||||
|
data: JSON.stringify(prData)
|
||||||
|
};
|
||||||
|
|
||||||
|
// API-Request zum Erstellen des Pull Requests
|
||||||
|
const response = await axios.request(config);
|
||||||
|
|
||||||
|
const prUrl = response.data.html_url;
|
||||||
|
|
||||||
|
// Öffne die URL des erstellten PRs im Browser
|
||||||
|
vscode.env.openExternal(vscode.Uri.parse(prUrl));
|
||||||
|
vscode.window.showInformationMessage('Pull-Request erfolgreich erstellt und im Browser geöffnet.');
|
||||||
|
} catch (err: unknown) {
|
||||||
|
vscode.window.showErrorMessage('Fehler beim Erstellen des Pull Requests.');
|
||||||
|
console.error('Fehler beim Erstellen des PRs:', err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion, um den letzten Commit zu ermitteln
|
||||||
|
async function getLastCommit(folderPath: string): Promise<{ title: string, body: string }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
exec(`git log -1 --pretty=format:"%s%n%b"`, { cwd: folderPath }, (error, stdout) => {
|
||||||
|
if (error) {
|
||||||
|
reject('Fehler beim Abrufen des letzten Commits.');
|
||||||
|
} else {
|
||||||
|
const output = stdout.split('\n');
|
||||||
|
const title = output[0]; // Commit-Message als Titel
|
||||||
|
const body = output.slice(1).join('\n'); // Commit-Kommentar als Body
|
||||||
|
resolve({ title: title.trim(), body: body.trim() });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion, um den aktuellen Branch zu ermitteln
|
||||||
|
async function getCurrentBranch(folderPath: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
exec(`git rev-parse --abbrev-ref HEAD`, { cwd: folderPath }, (error, stdout) => {
|
||||||
|
if (error) {
|
||||||
|
reject('Fehler beim Abrufen des Branches.');
|
||||||
|
} else {
|
||||||
|
resolve(stdout.trim());
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,106 +175,6 @@ async function configureGitea() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Funktion zum Abrufen der Repositories des Benutzers von Gitea
|
|
||||||
async function getGiteaRepositories(): Promise<any[]> {
|
|
||||||
const instanceUrl = vscode.workspace.getConfiguration().get<string>('gitea.instanceUrl');
|
|
||||||
const token = vscode.workspace.getConfiguration().get<string>('gitea.personalAccessToken');
|
|
||||||
|
|
||||||
if (!instanceUrl || !token) {
|
|
||||||
vscode.window.showErrorMessage('Gitea URL oder Token ist nicht konfiguriert.');
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await axios.get(`${instanceUrl}/api/v1/user/repos`, {
|
|
||||||
headers: {
|
|
||||||
'Authorization': `token ${token}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
return response.data;
|
|
||||||
} else {
|
|
||||||
vscode.window.showErrorMessage('Fehler beim Abrufen der Repositories.');
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
vscode.window.showErrorMessage('Fehler bei der Verbindung mit der Gitea API.');
|
|
||||||
console.error(error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Funktion zum Klonen eines Repositories via SSH
|
|
||||||
async function cloneGiteaRepository() {
|
|
||||||
const repos = await getGiteaRepositories();
|
|
||||||
|
|
||||||
if (repos.length === 0) {
|
|
||||||
vscode.window.showInformationMessage('Keine Repositories gefunden.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const repoNames = repos.map(repo => repo.full_name);
|
|
||||||
const selectedRepo = await vscode.window.showQuickPick(repoNames, {
|
|
||||||
placeHolder: 'Wähle ein Repository zum Klonen aus'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!selectedRepo) {
|
|
||||||
vscode.window.showInformationMessage('Kein Repository ausgewählt.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const repo = repos.find(r => r.full_name === selectedRepo);
|
|
||||||
|
|
||||||
if (repo && repo.ssh_url) {
|
|
||||||
const folderUri = await vscode.window.showOpenDialog({
|
|
||||||
canSelectFiles: false,
|
|
||||||
canSelectFolders: true,
|
|
||||||
canSelectMany: false,
|
|
||||||
openLabel: 'Ordner zum Klonen auswählen'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (folderUri && folderUri[0]) {
|
|
||||||
const folderPath = folderUri[0].fsPath;
|
|
||||||
|
|
||||||
vscode.window.withProgress({
|
|
||||||
location: vscode.ProgressLocation.Notification,
|
|
||||||
title: `Cloning ${selectedRepo}`,
|
|
||||||
cancellable: false
|
|
||||||
}, async (progress, token) => {
|
|
||||||
progress.report({ message: 'Klonvorgang läuft...', increment: 0 });
|
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
|
||||||
exec(`git clone ${repo.ssh_url} ${folderPath}`, (error, stdout, stderr) => {
|
|
||||||
if (error) {
|
|
||||||
vscode.window.showErrorMessage('Fehler beim Klonen des Repositories.');
|
|
||||||
console.error(stderr);
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
progress.report({ message: 'Repository geklont.', increment: 100 });
|
|
||||||
vscode.window.showInformationMessage(`Repository ${selectedRepo} erfolgreich geklont.`);
|
|
||||||
|
|
||||||
// Öffne das geklonte Repository im VSCode
|
|
||||||
try {
|
|
||||||
vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(folderPath), true)
|
|
||||||
.then(() => resolve());
|
|
||||||
} catch (err: unknown) {
|
|
||||||
vscode.window.showErrorMessage('Fehler beim Öffnen des geklonten Repositories.');
|
|
||||||
console.error(err);
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
vscode.window.showInformationMessage('Kein Zielordner ausgewählt.');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
vscode.window.showErrorMessage('Konnte die SSH-Klon-URL nicht finden.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Aktivierungsfunktion des Plugins
|
// Aktivierungsfunktion des Plugins
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
// Registriert den Befehl zur Authentifizierung
|
// Registriert den Befehl zur Authentifizierung
|
||||||
@ -220,10 +185,6 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
let configCommand = vscode.commands.registerCommand('gitea.configure', configureGitea);
|
let configCommand = vscode.commands.registerCommand('gitea.configure', configureGitea);
|
||||||
context.subscriptions.push(configCommand);
|
context.subscriptions.push(configCommand);
|
||||||
|
|
||||||
// Befehl zum Klonen eines Repositories
|
|
||||||
let cloneCommand = vscode.commands.registerCommand('gitea.cloneRepository', cloneGiteaRepository);
|
|
||||||
context.subscriptions.push(cloneCommand);
|
|
||||||
|
|
||||||
// Registriere den Befehl zum Erstellen eines Pull Requests
|
// Registriere den Befehl zum Erstellen eines Pull Requests
|
||||||
let pullRequestCommand = vscode.commands.registerCommand('gitea.createPullRequest', createGiteaPullRequest);
|
let pullRequestCommand = vscode.commands.registerCommand('gitea.createPullRequest', createGiteaPullRequest);
|
||||||
context.subscriptions.push(pullRequestCommand);
|
context.subscriptions.push(pullRequestCommand);
|
||||||
|
Loading…
Reference in New Issue
Block a user