From 3e77dd62f7dd2ebf81de832facabff662bd5da66 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 23 Oct 2024 10:46:48 +0200 Subject: [PATCH 1/2] * chore(gitea-git-clone): update changelog for version 1.2.1 * fix(gitea-git-clone): cloning into existing subpath working again * fix(gitea-git-clone): update German translation for targetExists message * fix(gitea-git-clone): update English translation for targetExists message * fix(gitea-git-clone): update cloneGiteaRepository function to check if target directory already exists before cloning --- gitea-git-clone/CHANGELOG.md | 7 +++++++ gitea-git-clone/i18n/package.nls.de.json | 3 ++- gitea-git-clone/i18n/package.nls.json | 3 ++- gitea-git-clone/src/extension.ts | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/gitea-git-clone/CHANGELOG.md b/gitea-git-clone/CHANGELOG.md index ad9fa42..9cfd8df 100644 --- a/gitea-git-clone/CHANGELOG.md +++ b/gitea-git-clone/CHANGELOG.md @@ -10,6 +10,13 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how - if working in PR show different Icon with hint - fix progress bar in notification +## 1.2.1 + +### Fixed + +- cloning into existing subpath working again + + ## 1.2.0 ### Added diff --git a/gitea-git-clone/i18n/package.nls.de.json b/gitea-git-clone/i18n/package.nls.de.json index 354b369..cece4b5 100644 --- a/gitea-git-clone/i18n/package.nls.de.json +++ b/gitea-git-clone/i18n/package.nls.de.json @@ -33,6 +33,7 @@ "giteaClone.selectPullRequest": "Wähle einen Pull Request zum Anzeigen aus", "giteaClone.repoInfoError": "Konnte Repository-Informationen nicht abrufen.", "giteaClone.pullRequestsError": "Fehler beim Abrufen der Pull Requests.", - "giteaClone.showOpenPullRequestsTooltip": "Offene Pull Requests anzeigen" + "giteaClone.showOpenPullRequestsTooltip": "Offene Pull Requests anzeigen", + "giteaClone.targetExists": "Das Zielverzeichnis \"{0}\" existiert bereits." } \ No newline at end of file diff --git a/gitea-git-clone/i18n/package.nls.json b/gitea-git-clone/i18n/package.nls.json index 203762f..9905b9a 100644 --- a/gitea-git-clone/i18n/package.nls.json +++ b/gitea-git-clone/i18n/package.nls.json @@ -33,6 +33,7 @@ "giteaClone.selectPullRequest": "Select a pull request to view", "giteaClone.repoInfoError": "Could not retrieve repository information.", "giteaClone.pullRequestsError": "Error retrieving pull requests.", - "giteaClone.showOpenPullRequestsTooltip": "Show open pull requests" + "giteaClone.showOpenPullRequestsTooltip": "Show open pull requests", + "giteaClone.targetExists": "The target directory \"{0}\" already exists." } \ No newline at end of file diff --git a/gitea-git-clone/src/extension.ts b/gitea-git-clone/src/extension.ts index 7ee1fba..354119b 100644 --- a/gitea-git-clone/src/extension.ts +++ b/gitea-git-clone/src/extension.ts @@ -2,6 +2,8 @@ import * as vscode from 'vscode'; import axios from 'axios'; import { exec } from 'child_process'; import * as nls from 'vscode-nls'; +import * as path from 'path'; +import * as fs from 'fs'; // Initialisiere die Lokalisierung const localize = nls.config({ messageFormat: nls.MessageFormat.file })(); @@ -203,6 +205,14 @@ async function cloneGiteaRepository() { if (folderUri && folderUri[0]) { const folderPath = folderUri[0].fsPath; + const repoName = repo.name; + const targetPath = path.join(folderPath, repoName); + + // Überprüfen, ob das Zielverzeichnis bereits existiert + if (fs.existsSync(targetPath)) { + vscode.window.showErrorMessage(localize('giteaClone.targetExists', `The target directory "${targetPath}" already exists.`)); + return; + } vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -212,7 +222,7 @@ async function cloneGiteaRepository() { progress.report({ message: localize('giteaClone.cloningProgress', 'Cloning in progress...'), increment: 0 }); return new Promise((resolve, reject) => { - exec(`git clone ${repo.ssh_url} ${folderPath}`, (error, stdout, stderr) => { + exec(`git clone ${repo.ssh_url} "${targetPath}"`, (error, stdout, stderr) => { if (error) { vscode.window.showErrorMessage(localize('giteaClone.cloneError', 'Error cloning the repository.')); console.error(stderr); @@ -223,7 +233,7 @@ async function cloneGiteaRepository() { // Öffne das geklonte Repository im VSCode try { - vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(folderPath), true) + vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(targetPath), true) .then(() => resolve()); } catch (err: unknown) { vscode.window.showErrorMessage(localize('giteaClone.openRepoError', 'Error opening the cloned repository.')); From ec64c46117b428ab8f0c25b58dd00293403a0338 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 23 Oct 2024 11:24:16 +0200 Subject: [PATCH 2/2] * chore(gitea-git-clone): update changelog and package version * feat(gitea-git-clone): add more verbose error handling * fix(gitea-git-clone): fix error message for pull request creation * fix(gitea-git-clone): fix error message for default branch retrieval * fix(gitea-git-clone): fix error message for repository retrieval * fix(gitea-git-clone): fix error message for authentication failure * fix(gitea-git-clone): fix error message for pull request retrieval * fix(gitea-git-clone): fix error message for pull request creation * fix(gitea-git-clone): fix error message for pull request retrieval * fix(gitea-git-clone): fix error message for pull request creation * fix(gitea-git-clone): fix error message for default branch retrieval * fix(gitea-git-clone): fix error message for repository --- gitea-git-clone/CHANGELOG.md | 4 + gitea-git-clone/i18n/package.nls.de.json | 12 +- gitea-git-clone/i18n/package.nls.json | 12 +- gitea-git-clone/package-lock.json | 4 +- gitea-git-clone/package.json | 2 +- gitea-git-clone/src/extension.ts | 139 ++++++++++++++++++++--- 6 files changed, 141 insertions(+), 32 deletions(-) diff --git a/gitea-git-clone/CHANGELOG.md b/gitea-git-clone/CHANGELOG.md index 9cfd8df..e976ac1 100644 --- a/gitea-git-clone/CHANGELOG.md +++ b/gitea-git-clone/CHANGELOG.md @@ -16,6 +16,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how - cloning into existing subpath working again +### Added + +- more verbose error handling + ## 1.2.0 diff --git a/gitea-git-clone/i18n/package.nls.de.json b/gitea-git-clone/i18n/package.nls.de.json index cece4b5..f061e30 100644 --- a/gitea-git-clone/i18n/package.nls.de.json +++ b/gitea-git-clone/i18n/package.nls.de.json @@ -4,18 +4,14 @@ "giteaClone.noRemoteUrl": "Konnte die Git-Remote-URL nicht abrufen.", "giteaClone.noBranch": "Branch konnte nicht ermittelt werden.", "giteaClone.pullRequestCreated": "Pull-Request erfolgreich erstellt und im Browser geöffnet.", - "giteaClone.pullRequestError": "Fehler beim Erstellen des Pull Requests: {0}", "giteaClone.commitError": "Fehler beim Abrufen des letzten Commits.", "giteaClone.branchError": "Fehler beim Abrufen des Branches.", - "giteaClone.defaultBranchError": "Konnte den base-Branch nicht ermitteln. Standardmäßig wird \"main\" verwendet.", "giteaClone.createPullRequest": "Gitea PR erstellen", "giteaClone.createPullRequestTooltip": "Pull Request in Gitea erstellen", "giteaClone.authSuccess": "Authentifizierung erfolgreich: {0}", - "giteaClone.authFailed": "Authentifizierung fehlgeschlagen.", "giteaClone.configUpdated": "Gitea-Konfiguration aktualisiert.", "giteaClone.enterInstanceUrl": "Gitea-Instanz-URL eingeben", "giteaClone.enterToken": "Gitea Personal Access Token eingeben", - "giteaClone.repoError": "Fehler beim Abrufen der Repositories.", "giteaClone.apiError": "Fehler bei der Verbindung mit der Gitea API.", "giteaClone.noRepos": "Keine Repositories gefunden.", "giteaClone.selectRepo": "Wähle ein Repository zum Klonen aus", @@ -32,8 +28,12 @@ "giteaClone.noOpenPRs": "Keine offenen Pull Requests vorhanden.", "giteaClone.selectPullRequest": "Wähle einen Pull Request zum Anzeigen aus", "giteaClone.repoInfoError": "Konnte Repository-Informationen nicht abrufen.", - "giteaClone.pullRequestsError": "Fehler beim Abrufen der Pull Requests.", "giteaClone.showOpenPullRequestsTooltip": "Offene Pull Requests anzeigen", - "giteaClone.targetExists": "Das Zielverzeichnis \"{0}\" existiert bereits." + "giteaClone.targetExists": "Das Zielverzeichnis \"{0}\" existiert bereits.", + "giteaClone.pullRequestError": "Fehler beim Erstellen des Pull Requests: {0}", + "giteaClone.defaultBranchError": "Fehler beim Abrufen des Basis-Branches: {0}", + "giteaClone.repoError": "Fehler beim Abrufen der Repositories: {0}", + "giteaClone.authFailed": "Authentifizierung fehlgeschlagen: {0}", + "giteaClone.pullRequestsError": "Fehler beim Abrufen der Pull Requests: {0}" } \ No newline at end of file diff --git a/gitea-git-clone/i18n/package.nls.json b/gitea-git-clone/i18n/package.nls.json index 9905b9a..cb9c384 100644 --- a/gitea-git-clone/i18n/package.nls.json +++ b/gitea-git-clone/i18n/package.nls.json @@ -4,18 +4,14 @@ "giteaClone.noRemoteUrl": "Could not retrieve Git remote URL.", "giteaClone.noBranch": "Could not determine the branch.", "giteaClone.pullRequestCreated": "Pull request created successfully and opened in the browser.", - "giteaClone.pullRequestError": "Error creating pull request: {0}", "giteaClone.commitError": "Error retrieving the last commit.", "giteaClone.branchError": "Error retrieving the branch.", - "giteaClone.defaultBranchError": "Could not determine the base branch. Defaulting to \"main\".", "giteaClone.createPullRequest": "Create Gitea PR", "giteaClone.createPullRequestTooltip": "Create a Pull Request in Gitea", "giteaClone.authSuccess": "Authentication successful: {0}", - "giteaClone.authFailed": "Authentication failed.", "giteaClone.configUpdated": "Gitea configuration updated.", "giteaClone.enterInstanceUrl": "Enter Gitea instance URL", "giteaClone.enterToken": "Enter Gitea Personal Access Token", - "giteaClone.repoError": "Error retrieving repositories.", "giteaClone.apiError": "Error connecting to Gitea API.", "giteaClone.noRepos": "No repositories found.", "giteaClone.selectRepo": "Select a repository to clone", @@ -32,8 +28,12 @@ "giteaClone.noOpenPRs": "There are no open pull requests.", "giteaClone.selectPullRequest": "Select a pull request to view", "giteaClone.repoInfoError": "Could not retrieve repository information.", - "giteaClone.pullRequestsError": "Error retrieving pull requests.", "giteaClone.showOpenPullRequestsTooltip": "Show open pull requests", - "giteaClone.targetExists": "The target directory \"{0}\" already exists." + "giteaClone.targetExists": "The target directory \"{0}\" already exists.", + "giteaClone.pullRequestError": "Error creating pull request: {0}", + "giteaClone.defaultBranchError": "Error retrieving the base branch: {0}", + "giteaClone.repoError": "Error retrieving repositories: {0}", + "giteaClone.authFailed": "Authentication failed: {0}", + "giteaClone.pullRequestsError": "Error retrieving pull requests: {0}" } \ No newline at end of file diff --git a/gitea-git-clone/package-lock.json b/gitea-git-clone/package-lock.json index df9b830..c454c30 100644 --- a/gitea-git-clone/package-lock.json +++ b/gitea-git-clone/package-lock.json @@ -1,12 +1,12 @@ { "name": "gitea-workflow", - "version": "1.0.2", + "version": "1.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gitea-workflow", - "version": "1.0.2", + "version": "1.2.0", "dependencies": { "axios": "^1.7.7", "open": "^10.1.0", diff --git a/gitea-git-clone/package.json b/gitea-git-clone/package.json index e5ea26a..6316483 100644 --- a/gitea-git-clone/package.json +++ b/gitea-git-clone/package.json @@ -2,7 +2,7 @@ "name": "gitea-workflow", "displayName": "Gitea Workflow", "description": "Clone from Gitea instances; Create PRs", - "version": "1.2.0", + "version": "1.2.1", "publisher": "computerliebe", "engines": { "vscode": "^1.94.0" diff --git a/gitea-git-clone/src/extension.ts b/gitea-git-clone/src/extension.ts index 354119b..858e250 100644 --- a/gitea-git-clone/src/extension.ts +++ b/gitea-git-clone/src/extension.ts @@ -1,10 +1,25 @@ import * as vscode from 'vscode'; import axios from 'axios'; +import { AxiosError } from 'axios'; import { exec } from 'child_process'; import * as nls from 'vscode-nls'; import * as path from 'path'; import * as fs from 'fs'; +//Interface +interface GiteaErrorResponse { + message?: string; + url?: string; + documentation_url?: string; + errors?: Array<{ + resource?: string; + field?: string; + code?: string; + message?: string; + }>; +} + + // Initialisiere die Lokalisierung const localize = nls.config({ messageFormat: nls.MessageFormat.file })(); @@ -88,7 +103,33 @@ async function createGiteaPullRequest() { vscode.env.openExternal(vscode.Uri.parse(prUrl)); vscode.window.showInformationMessage(localize('giteaClone.pullRequestCreated', 'Pull request created successfully and opened in the browser.')); } catch (err: any) { - vscode.window.showErrorMessage(localize('giteaClone.pullRequestError', `Error creating pull request: ${err.message}`)); + let errMessage = ''; + + if (axios.isAxiosError(err)) { + const axiosError = err as AxiosError; + if (axiosError.response && axiosError.response.data) { + const data = axiosError.response.data as GiteaErrorResponse; + if (typeof data === 'string') { + errMessage = data; + } else if (data.message) { + errMessage = data.message; + } + + // Optional: Weitere Details aus 'errors' extrahieren + if (data.errors && Array.isArray(data.errors)) { + data.errors.forEach((errorItem: any) => { + if (errorItem.message) { + errMessage += ` ${errorItem.message}`; + } + }); + } + } + } else { + errMessage = err.message; + } + + let errorMessage = localize('giteaClone.pullRequestError', `Error creating pull request: ${errMessage}`); + vscode.window.showErrorMessage(errorMessage); console.error('Error creating PR:', err); } }); @@ -138,8 +179,25 @@ async function getDefaultBranch(instanceUrl: string, owner: string, repo: string vscode.window.showErrorMessage(localize('giteaClone.defaultBranchError', 'Could not determine the base branch. Defaulting to "main".')); return 'main'; } - } catch (error) { - vscode.window.showErrorMessage(localize('giteaClone.defaultBranchError', 'Error retrieving the base branch. Defaulting to "main".')); + } catch (err: any) { + let errMessage = ''; + + if (axios.isAxiosError(err)) { + const axiosError = err as AxiosError; + if (axiosError.response && axiosError.response.data) { + const data = axiosError.response.data as GiteaErrorResponse; + if (typeof data === 'string') { + errMessage = data; + } else if (data.message) { + errMessage = data.message; + } + } + } else { + errMessage = err.message; + } + + let errorMessage = localize('giteaClone.defaultBranchError', `Error retrieving the base branch: ${errMessage}`); + vscode.window.showErrorMessage(errorMessage); return 'main'; } } @@ -167,9 +225,26 @@ async function getGiteaRepositories(): Promise { vscode.window.showErrorMessage(localize('giteaClone.repoError', 'Error retrieving repositories.')); return []; } - } catch (error) { - vscode.window.showErrorMessage(localize('giteaClone.apiError', 'Error connecting to Gitea API.')); - console.error(error); + } catch (err: any) { + let errMessage = ''; + + if (axios.isAxiosError(err)) { + const axiosError = err as AxiosError; + if (axiosError.response && axiosError.response.data) { + const data = axiosError.response.data as GiteaErrorResponse; + if (typeof data === 'string') { + errMessage = data; + } else if (data.message) { + errMessage = data.message; + } + } + } else { + errMessage = err.message; + } + + let errorMessage = localize('giteaClone.repoError', `Error retrieving repositories: ${errMessage}`); + vscode.window.showErrorMessage(errorMessage); + console.error(err); return []; } } @@ -282,9 +357,26 @@ async function authenticateGitea() { if (response.status === 200) { vscode.window.showInformationMessage(localize('giteaClone.authSuccess', `Authentication successful: ${response.data.username}`)); } - } catch (error) { - vscode.window.showErrorMessage(localize('giteaClone.authFailed', 'Authentication failed.')); - console.error(error); + } catch (err: any) { + let errMessage = ''; + + if (axios.isAxiosError(err)) { + const axiosError = err as AxiosError; + if (axiosError.response && axiosError.response.data) { + const data = axiosError.response.data as GiteaErrorResponse; + if (typeof data === 'string') { + errMessage = data; + } else if (data.message) { + errMessage = data.message; + } + } + } else { + errMessage = err.message; + } + + let errorMessage = localize('giteaClone.authFailed', `Authentication failed: ${errMessage}`); + vscode.window.showErrorMessage(errorMessage); + console.error(err); } } @@ -350,9 +442,26 @@ async function getOpenPullRequests(): Promise { vscode.window.showErrorMessage(localize('giteaClone.pullRequestsError', 'Fehler beim Abrufen der Pull Requests.')); return []; } - } catch (error) { - vscode.window.showErrorMessage(localize('giteaClone.apiError', 'Fehler bei der Verbindung zur Gitea API.')); - console.error(error); + } catch (err: any) { + let errMessage = ''; + + if (axios.isAxiosError(err)) { + const axiosError = err as AxiosError; + if (axiosError.response && axiosError.response.data) { + const data = axiosError.response.data as GiteaErrorResponse; + if (typeof data === 'string') { + errMessage = data; + } else if (data.message) { + errMessage = data.message; + } + } + } else { + errMessage = err.message; + } + + let errorMessage = localize('giteaClone.pullRequestsError', `Error retrieving pull requests: ${errMessage}`); + vscode.window.showErrorMessage(errorMessage); + console.error(err); return []; } } @@ -408,12 +517,9 @@ async function showOpenPullRequests() { } } - -let extensionContext: vscode.ExtensionContext; - -// Funktion zum Aktualisieren des PR-Statusleisten-Icons let prStatusBarItem: vscode.StatusBarItem; +// Funktion zum Aktualisieren des PR-Statusleisten-Icons async function updatePRStatusBarItem(context: vscode.ExtensionContext) { try { const pullRequests = await getOpenPullRequests(); @@ -451,7 +557,6 @@ function startPRStatusBarItemUpdater(context: vscode.ExtensionContext) { setInterval(() => updatePRStatusBarItem(context), 300000); // 5 Minuten } - // Aktivierungsfunktion des Plugins export function activate(context: vscode.ExtensionContext) { // Registriert den Befehl zur Authentifizierung