From a2d82372e542c96a4d84f7c9d1e8ca95e4c78a61 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Thu, 29 Aug 2024 08:50:51 +0200 Subject: [PATCH] Handle NullPointerException in hgrc clean-up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We encountered NullPointerExceptions during hg repository imports. This happened, when neither authentication nor proxy definions had been given. In this case, no hgrc will be created and therefore we get the exception in the clean-up step. Now, the clean-up is only triggered, if the hgrc had been created beforehand. To prevent further error in the clean-up, we no catch other potential exceptions and log them but do not let them break the import. Pushed-by: Rene Pfeuffer Co-authored-by: René Pfeuffer --- gradle/changelog/hg_import.yaml | 2 ++ .../sonia/scm/repository/spi/TemporaryConfigFactory.java | 8 +++++++- .../scm/repository/spi/TemporaryConfigFactoryTest.java | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 gradle/changelog/hg_import.yaml diff --git a/gradle/changelog/hg_import.yaml b/gradle/changelog/hg_import.yaml new file mode 100644 index 0000000000..ef9f21f0fa --- /dev/null +++ b/gradle/changelog/hg_import.yaml @@ -0,0 +1,2 @@ +- type: fixed + description: Import of hg repositories without credentials diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/TemporaryConfigFactory.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/TemporaryConfigFactory.java index 6db85e25a7..1cc11af6d5 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/TemporaryConfigFactory.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/TemporaryConfigFactory.java @@ -94,7 +94,13 @@ public class TemporaryConfigFactory { if (!exists && file.exists() && !file.delete()) { LOG.error("failed to delete temporary hgrc {}", file); } else if (exists && file.exists()) { - cleanUpHgrc(file); + try { + if (hgrc != null) { + cleanUpHgrc(file); + } + } catch (Exception e) { + LOG.warn("error cleaning up hgrc", e); + } } } } diff --git a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/TemporaryConfigFactoryTest.java b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/TemporaryConfigFactoryTest.java index af9824898e..afab58dc26 100644 --- a/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/TemporaryConfigFactoryTest.java +++ b/scm-plugins/scm-hg-plugin/src/test/java/sonia/scm/repository/spi/TemporaryConfigFactoryTest.java @@ -73,6 +73,13 @@ class TemporaryConfigFactoryTest { }); } + @Test + @SuppressWarnings("java:S2699") // test should just ensure that no exception is thrown + void shouldNotFailIfFileExistsButWithoutProxyOrAuthentication() throws IOException { + Files.createFile(hgrc); + configFactory.withContext(commandContext).call(() -> null); + } + @Test void shouldCreateHgrcWithAuthentication() throws IOException { configFactory