From eeb8b2de0b7956afc6a0048f9625b06c78b188d8 Mon Sep 17 00:00:00 2001 From: Nick Cross Date: Thu, 23 Nov 2023 15:53:43 +0000 Subject: [PATCH] Add GIT_ARCHIVE results to TaskResult --- .../analyser/deploy/DeployCommand.java | 14 +++++--- .../container/analyser/deploy/git/Git.java | 15 +++++++-- .../container/analyser/deploy/git/GitHub.java | 7 ++-- .../container/analyser/deploy/git/GitLab.java | 6 ++-- .../analyser/deploy/git/GitTest.java | 33 +++++++++++++++---- 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java index 50cd26265..7c05c4610 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/DeployCommand.java @@ -157,17 +157,19 @@ public DeployCommand(BeanManager beanManager, public void run() { try { + Set gavs = new HashSet<>(); + Map> contaminatedPaths = new HashMap<>(); + Map> contaminatedGavs = new HashMap<>(); + Map archivedSourceTags = new HashMap<>(); + // Save the source first regardless of deployment checks if (isNotEmpty(gitIdentity) && gitToken.isPresent()) { Log.infof("Git credentials are identity '%s' and URL '%s'", gitIdentity, gitURL); var git = Git.builder(gitURL, gitIdentity, gitToken.get(), gitDisableSSLVerification); git.create(scmUri); - git.add(sourcePath, commit, imageId); + archivedSourceTags = git.add(sourcePath, commit, imageId); } - Set gavs = new HashSet<>(); - Map> contaminatedPaths = new HashMap<>(); - Map contaminatedGavs = new HashMap<>(); // Represents directories that should not be deployed i.e. if a single artifact (barring test jars) is // contaminated then none of the artifacts will be deployed. Set toRemove = new HashSet<>(); @@ -337,13 +339,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { newContaminates.add(i.getValue()); } String serialisedContaminants = ResultsUpdater.MAPPER.writeValueAsString(newContaminates); + String serialisedGitArchive = ResultsUpdater.MAPPER.writeValueAsString(archivedSourceTags); Log.infof("Updating results %s with contaminants %s and deployed resources %s", taskRun, serialisedContaminants, gavs); resultsUpdater.updateResults(taskRun, Map.of( "CONTAMINANTS", serialisedContaminants, "DEPLOYED_RESOURCES", String.join(",", gavs), "IMAGE_URL", imageName == null ? "" : imageName, - "IMAGE_DIGEST", imageDigest == null ? "" : "sha256:" + imageDigest)); + "IMAGE_DIGEST", imageDigest == null ? "" : "sha256:" + imageDigest, + "GIT_ARCHIVE", serialisedGitArchive)); } } catch (Exception e) { Log.error("Deployment failed", e); diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/Git.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/Git.java index 71bc59c4e..e51e6cdba 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/Git.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/Git.java @@ -4,10 +4,13 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; +import java.util.Collections; +import java.util.Map; import java.util.UUID; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.PushResult; @@ -24,7 +27,7 @@ public abstract class Git { public abstract void create(String name) throws IOException, URISyntaxException; - public abstract void add(Path path, String commit, String imageId) + public abstract Map add(Path path, String commit, String imageId) throws IOException; /** @@ -62,7 +65,7 @@ public static Git builder(String endpoint, String identity, String token, boolea } } - protected void pushRepository(Path path, String httpTransportUrl, String commit, String imageId) { + protected Map pushRepository(Path path, String httpTransportUrl, String commit, String imageId) { try (var jGit = org.eclipse.jgit.api.Git.init().setDirectory(path.toFile()).call()) { // Find the tag name associated with the commit. Then append the unique imageId. This is from the Go code // and is a hash of abr.Status.SCMInfo.SCMURL + abr.Status.SCMInfo.Tag + abr.Status.SCMInfo.Path @@ -87,6 +90,7 @@ protected void pushRepository(Path path, String httpTransportUrl, String commit, Ref tagRefStable = jGit.tag().setAnnotated(true).setName(tagName + "-" + imageId).setForceUpdate(true).call(); Ref tagRefUnique = jGit.tag().setAnnotated(true).setName(tagName + "-" + UUID.randomUUID()).setForceUpdate(true) .call(); + Iterable results = jGit.push().setForce(true).setRemote("origin") .add(jRepo.getBranch()) // Push the default branch else GitHub doesn't show the code. .add(tagRefStable) @@ -95,7 +99,8 @@ protected void pushRepository(Path path, String httpTransportUrl, String commit, for (PushResult result : results) { result.getRemoteUpdates().forEach(r -> { - if (!r.getStatus().equals(RemoteRefUpdate.Status.OK)) { + if (!r.getStatus().equals(RemoteRefUpdate.Status.OK) + && !r.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)) { Log.errorf("Push failure " + r); throw new RuntimeException("Failed to push updates due to " + r.getMessage()); } @@ -103,6 +108,10 @@ protected void pushRepository(Path path, String httpTransportUrl, String commit, Log.debugf("Pushed " + result.getMessages() + " " + result.getURI() + " updates: " + result.getRemoteUpdates()); } + + return Collections.singletonMap(Repository.shortenRefName(tagRefUnique.getName()), + jRepo.getRefDatabase().peel(tagRefUnique).getPeeledObjectId().getName()); + } catch (GitAPIException | IOException e) { throw new RuntimeException(e); } diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitHub.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitHub.java index 240bf72fc..23d0cb197 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitHub.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitHub.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; +import java.util.Map; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.kohsuke.github.GHRepository; @@ -62,6 +63,7 @@ public void create(String scmUri) if (type == Type.USER) { repository = github.getUser(owner).getRepository(name); if (repository == null) { + Log.infof("Creating repository with name %s", name); repository = github.createRepository(name) .wiki(false) .defaultBranch("main") @@ -73,6 +75,7 @@ public void create(String scmUri) } else { repository = github.getOrganization(owner).getRepository(name); if (repository == null) { + Log.infof("Creating repository with name %s", name); repository = github.getOrganization(owner).createRepository(name) .wiki(false) .defaultBranch("main") @@ -85,11 +88,11 @@ public void create(String scmUri) } @Override - public void add(Path path, String commit, String imageId) { + public Map add(Path path, String commit, String imageId) { if (repository == null) { throw new RuntimeException("Call create first"); } - pushRepository(path, repository.getHttpTransportUrl(), commit, imageId); + return pushRepository(path, repository.getHttpTransportUrl(), commit, imageId); } @Override diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitLab.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitLab.java index ba726eeb5..04bb6d3e6 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitLab.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/deploy/git/GitLab.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; +import java.util.Map; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.gitlab4j.api.GitLabApi; @@ -49,6 +50,7 @@ public void create(String scmUri) Log.warnf("Repository %s already exists", name); } else { // Can't set public visibility after creation for some reason with this API. + Log.infof("Creating repository with name %s", name); project = gitLabApi.getProjectApi().createProject(name, null, null, @@ -66,12 +68,12 @@ public void create(String scmUri) } @Override - public void add(Path path, String commit, String imageId) + public Map add(Path path, String commit, String imageId) throws IOException { if (project == null) { throw new RuntimeException("Call create first"); } - pushRepository(path, project.getHttpUrlToRepo(), commit, imageId); + return pushRepository(path, project.getHttpUrlToRepo(), commit, imageId); } @Override diff --git a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/deploy/git/GitTest.java b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/deploy/git/GitTest.java index 31095c655..3809b00d4 100644 --- a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/deploy/git/GitTest.java +++ b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/deploy/git/GitTest.java @@ -10,11 +10,14 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.logging.LogRecord; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.TagOpt; import org.eclipse.jgit.transport.URIish; import org.junit.jupiter.api.BeforeEach; @@ -54,6 +57,7 @@ public void testPush() Path initialRepo = Files.createTempDirectory("initial-repo"); Path testRepo = Files.createTempDirectory("test-repo"); String testRepoURI = "file://" + testRepo; + String imageID = "75ecd81c7a2b384151c990975eb1dd10"; try (var testRepository = org.eclipse.jgit.api.Git.init().setDirectory(testRepo.toFile()).call(); var initialRepository = org.eclipse.jgit.api.Git.init().setDirectory(initialRepo.toFile()).call()) { Path repoRoot = Paths.get(Objects.requireNonNull(getClass().getResource("/")).toURI()).getParent().getParent() @@ -74,7 +78,8 @@ public void create(String name) { } @Override - public void add(Path path, String commit, String imageId) { + public Map add(Path path, String commit, String imageId) { + return null; } @Override @@ -82,11 +87,11 @@ public String groupSplit() { return null; } }; - test.pushRepository( + Map tagResults = test.pushRepository( initialRepo, testRepoURI, "c396268fb90335bde5c9272b9a194c3d4302bf24", - "75ecd81c7a2b384151c990975eb1dd10"); + imageID); List logRecords = LogCollectingTestResource.current().getRecords(); @@ -98,9 +103,25 @@ public String groupSplit() { .anyMatch( r -> LogCollectingTestResource.format(r).matches("Updating current origin of.*to " + testRepoURI))); - assertEquals(2, testRepository.tagList().call().size()); - assertTrue(testRepository.tagList().call().stream() - .anyMatch(r -> r.getName().equals("refs/tags/0.1-75ecd81c7a2b384151c990975eb1dd10"))); + List tags = testRepository.tagList().call(); + assertEquals(2, tags.size()); + assertEquals(1, tagResults.size()); + + assertTrue(tags.stream().anyMatch(r -> r.getName().equals("refs/tags/0.1-75ecd81c7a2b384151c990975eb1dd10"))); + + tagResults.forEach((k, v) -> { + var found = tags.stream().filter(t -> Repository.shortenRefName(t.getName()).matches(k)).findFirst(); + assertTrue(found.isPresent()); + try { + assertTrue(v.matches(testRepository.getRepository() + .getRefDatabase() + .peel(found.get()) + .getPeeledObjectId() + .getName())); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } }