Skip to content

Commit

Permalink
Merge pull request #880 from amvanbaren/fix-migration-extension-file-…
Browse files Browse the repository at this point in the history
…nullpointer

Handle 404 status when generating signature
  • Loading branch information
amvanbaren authored Mar 21, 2024
2 parents 58d9eb4 + 7387f69 commit f767eb0
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.file.Files;

@Component
@ConditionalOnProperty(value = "ovsx.data.mirror.enabled", havingValue = "false", matchIfMissing = true)
public class ExtensionVersionSignatureJobRequestHandler implements JobRequestHandler<MigrationJobRequest> {
Expand Down Expand Up @@ -63,6 +65,10 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
}

try(var extensionFile = migrations.getExtensionFile(entry)) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}

var download = entry.getKey();
var keyPair = repositories.findActiveKeyPair();
var signature = integrityService.generateSignature(download, extensionFile, keyPair);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.file.Files;

@Component
@ConditionalOnProperty(value = "ovsx.data.mirror.enabled", havingValue = "false", matchIfMissing = true)
public class ExtractResourcesJobRequestHandler implements JobRequestHandler<MigrationJobRequest> {
Expand All @@ -45,15 +47,17 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
}

var download = entry.getKey();
try(
var extensionFile = migrations.getExtensionFile(entry);
var extProcessor = new ExtensionProcessor(extensionFile)
) {
extProcessor.processEachResource(download.getExtension(), (resource) -> {
resource.setStorageType(download.getStorageType());
migrations.uploadFileResource(resource);
migrations.persistFileResource(resource);
});
try(var extensionFile = migrations.getExtensionFile(entry)) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}
try (var extProcessor = new ExtensionProcessor(extensionFile)) {
extProcessor.processEachResource(download.getExtension(), (resource) -> {
resource.setStorageType(download.getStorageType());
migrations.uploadFileResource(resource);
migrations.persistFileResource(resource);
});
}
}

service.deleteWebResources(extVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.util.AbstractMap;

@Component
Expand All @@ -46,14 +47,17 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
}

var content = migrations.getContent(download);
try(
var extensionFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(download, content));
var extProcessor = new ExtensionProcessor(extensionFile)
) {
var vsixManifest = extProcessor.getVsixManifest(extVersion);
vsixManifest.setStorageType(download.getStorageType());
migrations.uploadFileResource(vsixManifest);
migrations.persistFileResource(vsixManifest);
var entry = new AbstractMap.SimpleEntry<>(download, content);
try(var extensionFile = migrations.getExtensionFile(entry)) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}
try (var extProcessor = new ExtensionProcessor(extensionFile)) {
var vsixManifest = extProcessor.getVsixManifest(extVersion);
vsixManifest.setStorageType(download.getStorageType());
migrations.uploadFileResource(vsixManifest);
migrations.persistFileResource(vsixManifest);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
var extVersion = download.getExtension();
var content = migrations.getContent(download);
try (var extensionFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(download, content))) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}

boolean fixTargetPlatform;
try (var extProcessor = new ExtensionProcessor(extensionFile)) {
fixTargetPlatform = !extProcessor.getMetadata().getTargetPlatform().equals(extVersion.getTargetPlatform());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.util.AbstractMap;

@Component
Expand All @@ -44,14 +45,17 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
}

var content = migrations.getContent(download);
try(
var extensionFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(download, content));
var extProcessor = new ExtensionProcessor(extensionFile)
) {
var checksum = extProcessor.generateSha256Checksum(extVersion);
checksum.setStorageType(download.getStorageType());
migrations.uploadFileResource(checksum);
migrations.persistFileResource(checksum);
var entry = new AbstractMap.SimpleEntry<>(download, content);
try(var extensionFile = migrations.getExtensionFile(entry)) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}
try (var extProcessor = new ExtensionProcessor(extensionFile)) {
var checksum = extProcessor.generateSha256Checksum(extVersion);
checksum.setStorageType(download.getStorageType());
migrations.uploadFileResource(checksum);
migrations.persistFileResource(checksum);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
Expand Down Expand Up @@ -86,13 +88,21 @@ public TempFile getExtensionFile(Map.Entry<FileResource, byte[]> entry) throws I
var download = entry.getKey();
var storage = getStorage(download);
var uri = storage.getLocation(download);
backgroundRestTemplate.execute("{extensionLocation}", HttpMethod.GET, null, response -> {
try(var out = Files.newOutputStream(extensionFile.getPath())) {
response.getBody().transferTo(out);
try {
backgroundRestTemplate.execute("{extensionLocation}", HttpMethod.GET, null, response -> {
try (var out = Files.newOutputStream(extensionFile.getPath())) {
response.getBody().transferTo(out);
}

return extensionFile;
}, Map.of("extensionLocation", uri.toString()));
} catch (HttpClientErrorException e) {
if(e.getStatusCode() == HttpStatus.NOT_FOUND) {
logger.warn("Could not find extension file for: {}", NamingUtil.toLogFormat(download.getExtension()));
} else {
throw e;
}

return extensionFile;
}, Map.of("extensionLocation", uri.toString()));
}
} else {
Files.write(extensionFile.getPath(), content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.util.AbstractMap;

@Component
Expand All @@ -42,7 +43,12 @@ public void run(MigrationJobRequest jobRequest) throws Exception {

logger.info("Renaming download {}", download.getName());
var content = migrations.getContent(download);
try(var extensionFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(download, content))) {
var entry = new AbstractMap.SimpleEntry<>(download, content);
try(var extensionFile = migrations.getExtensionFile(entry)) {
if(Files.size(extensionFile.getPath()) == 0) {
return;
}

var newDownload = service.cloneResource(download, name);
migrations.uploadFileResource(newDownload, extensionFile);
migrations.removeFile(download);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.file.Files;

@Component
@ConditionalOnProperty(value = "ovsx.data.mirror.enabled", havingValue = "false", matchIfMissing = true)
public class SetPreReleaseJobRequestHandler implements JobRequestHandler<MigrationJobRequest> {
Expand All @@ -38,7 +40,9 @@ public void run(MigrationJobRequest jobRequest) throws Exception {
var entry = migrations.getDownload(extVersion);
if(entry != null) {
try (var extensionFile = migrations.getExtensionFile(entry)) {
service.updatePreviewAndPreRelease(extVersion, extensionFile);
if(Files.size(extensionFile.getPath()) > 0) {
service.updatePreviewAndPreRelease(extVersion, extensionFile);
}
}
}
}
Expand Down

0 comments on commit f767eb0

Please sign in to comment.