From d52b6c07b4a084fc81d1dad32bacf204369f1de3 Mon Sep 17 00:00:00 2001 From: Bas van den Brink Date: Mon, 5 Jun 2023 11:14:12 +0200 Subject: [PATCH] Skip copy goal if no container catalog artifact is found (#83) --- .../podman/AbstractCatalogSupport.java | 7 ++++-- .../java/nl/lexemmens/podman/CopyMojo.java | 23 +++++++++++-------- .../nl/lexemmens/podman/CopyMojoTest.java | 10 ++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/nl/lexemmens/podman/AbstractCatalogSupport.java b/src/main/java/nl/lexemmens/podman/AbstractCatalogSupport.java index 2c72c4ad..4112feee 100644 --- a/src/main/java/nl/lexemmens/podman/AbstractCatalogSupport.java +++ b/src/main/java/nl/lexemmens/podman/AbstractCatalogSupport.java @@ -17,6 +17,7 @@ import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.transfer.ArtifactNotFoundException; import java.io.File; import java.io.IOException; @@ -63,8 +64,7 @@ protected List readRemoteCatalog(RepositorySystemSession repositorySyste ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, artifactRequest); if (artifactResult.isMissing()) { - throw new MojoExecutionException("Cannot find container catalog. All repositories were successfully " + - "queried, but no such artifact was returned."); + return Collections.emptyList(); } if (artifactResult.isResolved()) { return readCatalogContent(Paths.get(artifactResult.getArtifact().getFile().toURI()), false); @@ -73,6 +73,9 @@ protected List readRemoteCatalog(RepositorySystemSession repositorySyste } } catch (ArtifactResolutionException e) { + if (e.getCause() instanceof ArtifactNotFoundException) { + return Collections.emptyList(); + } throw new MojoExecutionException("Failed retrieving container catalog file", e); } } diff --git a/src/main/java/nl/lexemmens/podman/CopyMojo.java b/src/main/java/nl/lexemmens/podman/CopyMojo.java index 5f37ae2c..b7a84f04 100644 --- a/src/main/java/nl/lexemmens/podman/CopyMojo.java +++ b/src/main/java/nl/lexemmens/podman/CopyMojo.java @@ -55,17 +55,22 @@ private void performCopyUsingCatalogFile(ServiceHub hub) throws MojoExecutionExc File tempRepo = tempSession.repo; List cataloguedImages = readRemoteCatalog(tempSession.session); - Map transformedImages = performTransformation(cataloguedImages); + if (cataloguedImages.isEmpty()) { + getLog().info("Not copying container images, because no container-catalog.txt artifact was " + + "found."); + } else { + Map transformedImages = performTransformation(cataloguedImages); - for (Map.Entry imageEntry : transformedImages.entrySet()) { - copyImage(hub, imageEntry.getKey(), imageEntry.getValue()); - } + for (Map.Entry imageEntry : transformedImages.entrySet()) { + copyImage(hub, imageEntry.getKey(), imageEntry.getValue()); + } - if (skopeo.getCopy().getDisableLocal() && tempRepo != null) { - try { - FileUtils.deleteDirectory(tempRepo); - } catch (IOException e) { - getLog().warn("Failed to cleanup temporary repository directory: " + tempRepo); + if (skopeo.getCopy().getDisableLocal() && tempRepo != null) { + try { + FileUtils.deleteDirectory(tempRepo); + } catch (IOException e) { + getLog().warn("Failed to cleanup temporary repository directory: " + tempRepo); + } } } } diff --git a/src/test/java/nl/lexemmens/podman/CopyMojoTest.java b/src/test/java/nl/lexemmens/podman/CopyMojoTest.java index 4d4acfd2..3c503db3 100644 --- a/src/test/java/nl/lexemmens/podman/CopyMojoTest.java +++ b/src/test/java/nl/lexemmens/podman/CopyMojoTest.java @@ -28,6 +28,7 @@ import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.transfer.ArtifactNotFoundException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -184,6 +185,15 @@ public void testWithCatalogFile() throws MojoExecutionException { verify(skopeoExecutorService, times(1)).copy("dep2.stage.registry.example.com/project/product:2.1.3", "dep2.release.registry.example.com/project/product:2.1.3"); } + @Test + public void testSkipCopyNoCatalogFile() throws ArtifactResolutionException, MojoExecutionException { + configureMojo(false, false, true, null, new String[]{}, "stage", "release", null, false, false, false); + when(copyMojo.repositorySystem.resolveArtifact(any(RepositorySystemSession.class), any(ArtifactRequest.class))) + .thenThrow(new ArtifactResolutionException(null, null, new ArtifactNotFoundException(null, null))); + assertDoesNotThrow(copyMojo::execute); + verify(skopeoExecutorService, times(0)).copy(anyString(), anyString()); + } + private static void cleanDir(Path dir) throws IOException { LinkedList ioExceptions = new LinkedList<>(); Files.list(dir).forEach(path -> {