Skip to content

Commit

Permalink
Add GIT_ARCHIVE results to TaskResult
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Nov 28, 2023
1 parent 755eb93 commit eeb8b2d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,19 @@ public DeployCommand(BeanManager beanManager,

public void run() {
try {
Set<String> gavs = new HashSet<>();
Map<String, Set<String>> contaminatedPaths = new HashMap<>();
Map<String, Set<String>> contaminatedGavs = new HashMap<>();
Map<String, String> 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<String> gavs = new HashSet<>();
Map<String, Set<String>> contaminatedPaths = new HashMap<>();
Map<String, Contaminates> 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<Path> toRemove = new HashSet<>();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> add(Path path, String commit, String imageId)
throws IOException;

/**
Expand Down Expand Up @@ -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<String, String> 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
Expand All @@ -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<PushResult> results = jGit.push().setForce(true).setRemote("origin")
.add(jRepo.getBranch()) // Push the default branch else GitHub doesn't show the code.
.add(tagRefStable)
Expand All @@ -95,14 +99,19 @@ 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());
}
});
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -85,11 +88,11 @@ public void create(String scmUri)
}

@Override
public void add(Path path, String commit, String imageId) {
public Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -66,12 +68,12 @@ public void create(String scmUri)
}

@Override
public void add(Path path, String commit, String imageId)
public Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -74,19 +78,20 @@ public void create(String name) {
}

@Override
public void add(Path path, String commit, String imageId) {
public Map<String, String> add(Path path, String commit, String imageId) {
return null;
}

@Override
public String groupSplit() {
return null;
}
};
test.pushRepository(
Map<String, String> tagResults = test.pushRepository(
initialRepo,
testRepoURI,
"c396268fb90335bde5c9272b9a194c3d4302bf24",
"75ecd81c7a2b384151c990975eb1dd10");
imageID);

List<LogRecord> logRecords = LogCollectingTestResource.current().getRecords();

Expand All @@ -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<Ref> 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);
}
});
}
}

Expand Down

0 comments on commit eeb8b2d

Please sign in to comment.