Skip to content

Commit

Permalink
Skip copy goal if no container catalog artifact is found (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
basvandenbrink committed Jun 5, 2023
1 parent 98d9f71 commit d52b6c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/main/java/nl/lexemmens/podman/AbstractCatalogSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,8 +64,7 @@ protected List<String> 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);
Expand All @@ -73,6 +73,9 @@ protected List<String> readRemoteCatalog(RepositorySystemSession repositorySyste
}

} catch (ArtifactResolutionException e) {
if (e.getCause() instanceof ArtifactNotFoundException) {
return Collections.emptyList();
}
throw new MojoExecutionException("Failed retrieving container catalog file", e);
}
}
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/nl/lexemmens/podman/CopyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ private void performCopyUsingCatalogFile(ServiceHub hub) throws MojoExecutionExc
File tempRepo = tempSession.repo;

List<String> cataloguedImages = readRemoteCatalog(tempSession.session);
Map<String, String> transformedImages = performTransformation(cataloguedImages);
if (cataloguedImages.isEmpty()) {
getLog().info("Not copying container images, because no container-catalog.txt artifact was " +
"found.");
} else {
Map<String, String> transformedImages = performTransformation(cataloguedImages);

for (Map.Entry<String, String> imageEntry : transformedImages.entrySet()) {
copyImage(hub, imageEntry.getKey(), imageEntry.getValue());
}
for (Map.Entry<String, String> 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);
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/nl/lexemmens/podman/CopyMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IOException> ioExceptions = new LinkedList<>();
Files.list(dir).forEach(path -> {
Expand Down

0 comments on commit d52b6c0

Please sign in to comment.