From c5520e6b07678d86bdbeeb73757c9992ab1b4f58 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 15 Jan 2020 08:49:14 +0100 Subject: [PATCH 1/3] Fix error handling on git push Failing git push does not lead to an GitAPIException. Instead we have to check the dedicated push update results. By the way this adds a message for the internal repository exception. --- .../InternalRepositoryException.java | 4 ++- .../repository/spi/AbstractGitCommand.java | 28 +++++++++++++++---- .../main/resources/locales/de/plugins.json | 8 ++++-- .../main/resources/locales/en/plugins.json | 8 ++++-- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/repository/InternalRepositoryException.java b/scm-core/src/main/java/sonia/scm/repository/InternalRepositoryException.java index c53ae32750..966480c7a0 100644 --- a/scm-core/src/main/java/sonia/scm/repository/InternalRepositoryException.java +++ b/scm-core/src/main/java/sonia/scm/repository/InternalRepositoryException.java @@ -7,6 +7,8 @@ import java.util.List; public class InternalRepositoryException extends ExceptionWithContext { + public static final String CODE = "8LRncum0S1"; + public InternalRepositoryException(ContextEntry.ContextBuilder context, String message) { this(context, message, null); } @@ -29,6 +31,6 @@ public class InternalRepositoryException extends ExceptionWithContext { @Override public String getCode() { - return null; + return CODE; } } diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java index 1c807c6fc7..ba25d840bd 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java @@ -45,6 +45,8 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.PushResult; +import org.eclipse.jgit.transport.RemoteRefUpdate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.repository.GitUtil; @@ -55,6 +57,8 @@ import sonia.scm.repository.util.WorkingCopy; import sonia.scm.user.User; import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; @@ -72,7 +76,7 @@ import static sonia.scm.NotFoundException.notFound; */ class AbstractGitCommand { - + /** * the logger for AbstractGitCommand */ @@ -106,7 +110,7 @@ class AbstractGitCommand { return context.open(); } - + ObjectId getCommitOrDefault(Repository gitRepository, String requestedCommit) throws IOException { ObjectId commit; if ( Strings.isNullOrEmpty(requestedCommit) ) { @@ -255,10 +259,24 @@ class AbstractGitCommand void push() { try { - clone.push().call(); + Iterable pushResult = clone.push().call(); + Iterator pushResultIterator = pushResult.iterator(); + if (!pushResultIterator.hasNext()) { + throw new InternalRepositoryException(repository, "got no result from push"); + } + Collection remoteUpdates = pushResultIterator.next().getRemoteUpdates(); + if (remoteUpdates.isEmpty()) { + throw new InternalRepositoryException(repository, "push created no update"); + } + remoteUpdates + .stream() + .filter(remoteRefUpdate -> remoteRefUpdate.getStatus() != RemoteRefUpdate.Status.OK) + .findAny() + .ifPresent(remoteRefUpdate -> { + throw new IntegrateChangesFromWorkdirException(repository, "could not push changes into central repository: " + remoteRefUpdate.getStatus()); + }); } catch (GitAPIException e) { - throw new IntegrateChangesFromWorkdirException(repository, - "could not push changes into central repository", e); + throw new InternalRepositoryException(repository, "could not push changes into central repository", e); } logger.debug("pushed changes"); } diff --git a/scm-webapp/src/main/resources/locales/de/plugins.json b/scm-webapp/src/main/resources/locales/de/plugins.json index 3cb6522922..95179a94e2 100644 --- a/scm-webapp/src/main/resources/locales/de/plugins.json +++ b/scm-webapp/src/main/resources/locales/de/plugins.json @@ -165,8 +165,8 @@ "description": "Die eingegebenen Daten konnten nicht validiert werden. Bitte korrigieren Sie die Eingaben und senden Sie sie erneut." }, "CHRM7IQzo1": { - "displayName": "Änderung fehlgeschlagen", - "description": "Die Änderung ist fehlgeschlagen. Bitte wenden Sie sich an ihren Administrator für weitere Hinweise." + "displayName": "Änderung des Repositories nicht möglich", + "description": "Die gewünschte Änderung am Repository konnte nicht durchgeführt werden. Höchst wahrscheinlich liegt dieses an installierten Plugins mit Hooks oder nativen Hooks." }, "thbsUFokjk": { "displayName": "Unerlaubte Änderung eines Schlüsselwerts", @@ -195,6 +195,10 @@ "1yRiASshD1": { "displayName": "Fehler beim Löschen des anonymen Nutzers", "description": "Der anonyme Nutzer kann nicht gelöscht werden, solange der anonyme Zugriff in den Einstellungen aktiviert ist." + }, + "8LRncum0S1": { + "displayName": "Interner Fehler im Repository", + "description": "Bei der Bearbeitung des internen Repositories ist ein Fehler oder ein unerwarteter Zustand aufgetreten. Bitte prüfen Sie die Logs für weitere Informationen." } }, "namespaceStrategies": { diff --git a/scm-webapp/src/main/resources/locales/en/plugins.json b/scm-webapp/src/main/resources/locales/en/plugins.json index 83b463b8b6..2353184ada 100644 --- a/scm-webapp/src/main/resources/locales/en/plugins.json +++ b/scm-webapp/src/main/resources/locales/en/plugins.json @@ -165,8 +165,8 @@ "description": "The values could not be validated. Please correct your input and try again." }, "CHRM7IQzo1": { - "displayName": "Change failed", - "description": "The change failed. Please contact your administrator for further assistance." + "displayName": "Could not modify repository", + "description": "The requested modification to the repository was rejected. Most probably this was due to plugins with repository hooks or native hooks." }, "thbsUFokjk": { "displayName": "Illegal change of an identifier", @@ -195,6 +195,10 @@ "1yRiASshD1": { "displayName": "Error deleting the anonymous user", "description": "The anonymous user cannot be deleted since the anonymous access is still enabled in the configuration." + }, + "8LRncum0S1": { + "displayName": "Internal repository error", + "description": "There was an error or an unexpected condition while handling the native repository. Please consult the logs for further information." } }, "namespaceStrategies": { From b4b4f29e42e4513be3415dd36436f66f897f6e4a Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 15 Jan 2020 09:51:00 +0100 Subject: [PATCH 2/3] Log messages from failed push result --- .../java/sonia/scm/repository/spi/AbstractGitCommand.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java index ba25d840bd..24f900dfc2 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/AbstractGitCommand.java @@ -259,12 +259,13 @@ class AbstractGitCommand void push() { try { - Iterable pushResult = clone.push().call(); - Iterator pushResultIterator = pushResult.iterator(); + Iterable pushResults = clone.push().call(); + Iterator pushResultIterator = pushResults.iterator(); if (!pushResultIterator.hasNext()) { throw new InternalRepositoryException(repository, "got no result from push"); } - Collection remoteUpdates = pushResultIterator.next().getRemoteUpdates(); + PushResult pushResult = pushResultIterator.next(); + Collection remoteUpdates = pushResult.getRemoteUpdates(); if (remoteUpdates.isEmpty()) { throw new InternalRepositoryException(repository, "push created no update"); } @@ -273,6 +274,7 @@ class AbstractGitCommand .filter(remoteRefUpdate -> remoteRefUpdate.getStatus() != RemoteRefUpdate.Status.OK) .findAny() .ifPresent(remoteRefUpdate -> { + logger.info("message for failed push: {}", pushResult.getMessages()); throw new IntegrateChangesFromWorkdirException(repository, "could not push changes into central repository: " + remoteRefUpdate.getStatus()); }); } catch (GitAPIException e) { From 071095b74f160e3a994efade209fb615b4af1351 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 15 Jan 2020 12:40:02 +0000 Subject: [PATCH 3/3] Close branch bugfix/push_error_handling