* feat(extension.ts): add support for creating a pull request in Gitea

* feat(extension.ts): add status bar icon for creating a pull request
* feat(extension.ts): implement function to create a Gitea pull request
* feat(extension.ts): implement function to add status bar icon
This commit is contained in:
2024-10-20 20:17:35 +02:00
parent 8d53bc17ed
commit 6822001ae8
3 changed files with 212 additions and 2 deletions

View File

@ -2,6 +2,68 @@ import * as vscode from 'vscode';
import axios from 'axios';
import { exec } from 'child_process';
// Funktion zum Abrufen des Gitea Repositories und Erstellen eines Pull Requests
async function createGiteaPullRequest() {
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;
}
// Aktuellen Git-Ordner abrufen
const currentWorkspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath;
if (!currentWorkspaceFolder) {
vscode.window.showErrorMessage('Kein gültiges Git-Repository im aktuellen Workspace gefunden.');
return;
}
// Git-Repository-Informationen abrufen
exec(`git config --get remote.origin.url`, { cwd: currentWorkspaceFolder }, async (error, stdout, stderr) => {
if (error || !stdout) {
vscode.window.showErrorMessage('Konnte die Git-Remote-URL nicht abrufen.');
return;
}
let repoUrl = stdout.trim();
// URL umwandeln, wenn sie im SSH-Format vorliegt (git@gitea.com:owner/repo.git)
if (repoUrl.startsWith('git@')) {
// SSH-URL in HTTP(S)-URL umwandeln
const parts = repoUrl.split(':');
const domain = parts[0].replace('git@', '');
const path = parts[1].replace('.git', '');
repoUrl = `https://${domain}/${path}`;
} else {
// Falls es eine HTTPS-URL ist, einfach .git entfernen
repoUrl = repoUrl.replace('.git', '');
}
// URL für Pull Request Seite generieren
const pullRequestUrl = `${repoUrl}/pulls/new`;
try {
// Verwende die VSCode API, um die URL im Browser zu öffnen
await vscode.env.openExternal(vscode.Uri.parse(pullRequestUrl));
vscode.window.showInformationMessage('Pull-Request-Seite im Browser geöffnet.');
} catch (err: unknown) {
vscode.window.showErrorMessage('Fehler beim Öffnen der Pull-Request-Seite.');
console.error('Fehler beim Öffnen des Browsers:', err);
}
});
}
// Funktion zum Hinzufügen des Statusbar-Icons
function addStatusBarIcon() {
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBar.text = `$(git-pull-request) Create PR`;
statusBar.tooltip = 'Create a Pull Request in Gitea';
statusBar.command = 'gitea.createPullRequest';
statusBar.show();
return statusBar;
}
// Funktion zur Authentifizierung bei Gitea mit dem Personal Access Token (PAT)
async function authenticateGitea() {
const instanceUrl = vscode.workspace.getConfiguration().get<string>('gitea.instanceUrl');
@ -161,6 +223,14 @@ export function activate(context: vscode.ExtensionContext) {
// 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
let pullRequestCommand = vscode.commands.registerCommand('gitea.createPullRequest', createGiteaPullRequest);
context.subscriptions.push(pullRequestCommand);
// Statusbar-Icon hinzufügen
const statusBar = addStatusBarIcon();
context.subscriptions.push(statusBar);
}
// Deaktivierungsfunktion des Plugins