From f0e8769369904822b6e4b83bb007420869b9a05e Mon Sep 17 00:00:00 2001 From: amvanbaren Date: Wed, 20 Mar 2024 13:30:07 +0200 Subject: [PATCH] Handle download NullPointerException for migrations --- ...nsionVersionSignatureJobRequestHandler.java | 4 ++++ .../ExtractResourcesJobRequestHandler.java | 4 ++++ .../openvsx/migration/MigrationService.java | 18 ++++++++++++++---- .../SetPreReleaseJobRequestHandler.java | 8 +++++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/eclipse/openvsx/migration/ExtensionVersionSignatureJobRequestHandler.java b/server/src/main/java/org/eclipse/openvsx/migration/ExtensionVersionSignatureJobRequestHandler.java index 4a75a50c8..bbed86528 100644 --- a/server/src/main/java/org/eclipse/openvsx/migration/ExtensionVersionSignatureJobRequestHandler.java +++ b/server/src/main/java/org/eclipse/openvsx/migration/ExtensionVersionSignatureJobRequestHandler.java @@ -58,6 +58,10 @@ public void run(MigrationJobRequest jobRequest) throws Exception { } var entry = migrations.getDownload(extVersion); + if(entry == null) { + return; + } + try(var extensionFile = migrations.getExtensionFile(entry)) { var download = entry.getKey(); var keyPair = repositories.findActiveKeyPair(); diff --git a/server/src/main/java/org/eclipse/openvsx/migration/ExtractResourcesJobRequestHandler.java b/server/src/main/java/org/eclipse/openvsx/migration/ExtractResourcesJobRequestHandler.java index 0053fd0c4..8ee48eb14 100644 --- a/server/src/main/java/org/eclipse/openvsx/migration/ExtractResourcesJobRequestHandler.java +++ b/server/src/main/java/org/eclipse/openvsx/migration/ExtractResourcesJobRequestHandler.java @@ -40,6 +40,10 @@ public void run(MigrationJobRequest jobRequest) throws Exception { service.deleteResources(extVersion); var entry = migrations.getDownload(extVersion); + if(entry == null) { + return; + } + var download = entry.getKey(); try( var extensionFile = migrations.getExtensionFile(entry); diff --git a/server/src/main/java/org/eclipse/openvsx/migration/MigrationService.java b/server/src/main/java/org/eclipse/openvsx/migration/MigrationService.java index 7f6759731..8e227488e 100644 --- a/server/src/main/java/org/eclipse/openvsx/migration/MigrationService.java +++ b/server/src/main/java/org/eclipse/openvsx/migration/MigrationService.java @@ -9,6 +9,8 @@ * ****************************************************************************** */ package org.eclipse.openvsx.migration; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; import org.eclipse.openvsx.entities.ExtensionVersion; import org.eclipse.openvsx.entities.FileResource; import org.eclipse.openvsx.entities.MigrationItem; @@ -16,17 +18,18 @@ import org.eclipse.openvsx.storage.AzureBlobStorageService; import org.eclipse.openvsx.storage.GoogleCloudStorageService; import org.eclipse.openvsx.storage.IStorageService; +import org.eclipse.openvsx.util.NamingUtil; import org.eclipse.openvsx.util.TempFile; import org.jobrunr.jobs.lambdas.JobRequestHandler; import org.jobrunr.scheduling.JobRequestScheduler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import jakarta.persistence.EntityManager; -import jakarta.transaction.Transactional; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -37,6 +40,8 @@ @Component public class MigrationService { + protected final Logger logger = LoggerFactory.getLogger(MigrationService.class); + @Autowired RestTemplate backgroundRestTemplate; @@ -143,8 +148,13 @@ public FileResource getFileResource(ExtensionVersion extVersion, String type) { @Transactional public Map.Entry getDownload(ExtensionVersion extVersion) { var download = repositories.findFileByType(extVersion, FileResource.DOWNLOAD); - var content = download.getStorageType().equals(FileResource.STORAGE_DB) ? download.getContent() : null; - return new AbstractMap.SimpleEntry<>(download, content); + if(download != null) { + var content = download.getStorageType().equals(FileResource.STORAGE_DB) ? download.getContent() : null; + return new AbstractMap.SimpleEntry<>(download, content); + } else { + logger.warn("Could not find download for: {}", NamingUtil.toLogFormat(extVersion)); + return null; + } } @Transactional diff --git a/server/src/main/java/org/eclipse/openvsx/migration/SetPreReleaseJobRequestHandler.java b/server/src/main/java/org/eclipse/openvsx/migration/SetPreReleaseJobRequestHandler.java index 58dbed224..9cc98dd03 100644 --- a/server/src/main/java/org/eclipse/openvsx/migration/SetPreReleaseJobRequestHandler.java +++ b/server/src/main/java/org/eclipse/openvsx/migration/SetPreReleaseJobRequestHandler.java @@ -22,7 +22,7 @@ @ConditionalOnProperty(value = "ovsx.data.mirror.enabled", havingValue = "false", matchIfMissing = true) public class SetPreReleaseJobRequestHandler implements JobRequestHandler { - protected final Logger logger = new JobRunrDashboardLogger(LoggerFactory.getLogger(ExtractResourcesJobRequestHandler.class)); + protected final Logger logger = new JobRunrDashboardLogger(LoggerFactory.getLogger(SetPreReleaseJobRequestHandler.class)); @Autowired MigrationService migrations; @@ -36,8 +36,10 @@ public void run(MigrationJobRequest jobRequest) throws Exception { var extVersions = service.getExtensionVersions(jobRequest, logger); for(var extVersion : extVersions) { var entry = migrations.getDownload(extVersion); - try (var extensionFile = migrations.getExtensionFile(entry)) { - service.updatePreviewAndPreRelease(extVersion, extensionFile); + if(entry != null) { + try (var extensionFile = migrations.getExtensionFile(entry)) { + service.updatePreviewAndPreRelease(extVersion, extensionFile); + } } } }