Skip to content

Commit

Permalink
fix(tasks): push task was not properly computing diffs on dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mulier-p committed May 21, 2024
1 parent a29440a commit c0e7fb3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 9 deletions.
18 changes: 12 additions & 6 deletions src/main/java/io/kestra/plugin/git/AbstractPushTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.DiffCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.api.errors.EmptyCommitException;
Expand Down Expand Up @@ -126,16 +127,21 @@ protected void writeResourceFile(Path path, InputStream inputStream) throws IOEx
Files.copy(inputStream, path, REPLACE_EXISTING);
}

private static URI createDiffFile(RunContext runContext, Git git) throws IOException, GitAPIException {
private URI createDiffFile(RunContext runContext, Git git) throws IOException, GitAPIException {
File diffFile = runContext.tempFile(".ion").toFile();
try (DiffFormatter diffFormatter = new DiffFormatter(null);
BufferedWriter diffWriter = new BufferedWriter(new FileWriter(diffFile))) {
diffFormatter.setRepository(git.getRepository());

git.diff()
.setOldTree(treeIterator(git, "HEAD~1"))
.setNewTree(treeIterator(git, "HEAD"))
.call()
DiffCommand diff = git.diff();
if (this.dryRun) {
diff = diff.setCached(true);
} else {
diff = diff.setOldTree(treeIterator(git, "HEAD~1"))
.setNewTree(treeIterator(git, "HEAD"));
}

diff.call()
.stream().sorted(Comparator.comparing(AbstractPushTask::getPath))
.map(throwFunction(diffEntry -> {
EditList editList = diffFormatter.toFileHeader(diffEntry).toEditList();
Expand Down Expand Up @@ -287,7 +293,7 @@ public O run(RunContext runContext) throws Exception {

Output pushOutput = this.push(git, runContext, gitService);

URI diffFileStorageUri = AbstractPushTask.createDiffFile(runContext, git);
URI diffFileStorageUri = this.createDiffFile(runContext, git);

git.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Git cloneBranch(RunContext runContext, String branch, Boolean withSubmodu
return git;
}

private boolean branchExists(RunContext runContext, String branch) throws Exception {
public boolean branchExists(RunContext runContext, String branch) throws Exception {
return gitTask.authentified(Git.lsRemoteRepository().setRemote(runContext.render(gitTask.getUrl())), runContext)
.callAsMap()
.containsKey(R_HEADS + branch);
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/io/kestra/plugin/git/PushFlowsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.Rethrow;
import io.kestra.plugin.git.services.GitService;
import io.micronaut.context.annotation.Value;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -105,6 +106,8 @@ void defaultCase_SingleRegex() throws Exception {

try {
PushFlows.Output pushOutput = pushFlows.run(runContext);
GitService gitService = new GitService(pushFlows);
assertThat(gitService.branchExists(runContext, branch), is(true));

Clone clone = Clone.builder()
.id("clone")
Expand Down Expand Up @@ -144,6 +147,56 @@ void defaultCase_SingleRegex() throws Exception {
}
}

@Test
void defaultCase_SingleRegexDryRun() throws Exception {
String tenantId = "my-tenant";
String sourceNamespace = IdUtils.create().toLowerCase();
String targetNamespace = IdUtils.create().toLowerCase();
String branch = IdUtils.create();
String gitDirectory = "my-flows";
String authorEmail = "bmulier@kestra.io";
String authorName = "brianmulier";
String url = "https://github.com/kestra-io/unit-tests";
RunContext runContext = runContext(tenantId, url, authorEmail, authorName, branch, sourceNamespace, targetNamespace, gitDirectory);

FlowWithSource createdFlow = this.createFlow(tenantId, "first-flow", sourceNamespace);
String subNamespace = "sub-namespace";
FlowWithSource createdSubNsFlow = this.createFlow(tenantId, "second-flow", sourceNamespace + "." + subNamespace);

PushFlows pushFlows = PushFlows.builder()
.id("pushFlows")
.type(PushFlows.class.getName())
.branch("{{branch}}")
.url("{{url}}")
.commitMessage("Push from CI - {{description}}")
.username("{{pat}}")
.password("{{pat}}")
.authorEmail("{{email}}")
.authorName("{{name}}")
.sourceNamespace("{{sourceNamespace}}")
.targetNamespace("{{targetNamespace}}")
.flows("second.*")
.includeChildNamespaces(true)
.gitDirectory("{{gitDirectory}}")
.dryRun(true)
.build();

PushFlows.Output pushOutput = pushFlows.run(runContext);

GitService gitService = new GitService(pushFlows);
assertThat(gitService.branchExists(runContext, branch), is(false));

assertThat(pushOutput.getCommitURL(), nullValue());

assertDiffs(
runContext,
pushOutput.diffFileUri(),
List.of(
Map.of("additions", "+10", "deletions", "-0", "changes", "0", "file", gitDirectory + "/sub-namespace/second-flow.yml")
)
);
}

@Test
void defaultCase_SingleRegex_DeleteScopedToRegex() throws Exception {
String tenantId = "my-tenant";
Expand Down
55 changes: 53 additions & 2 deletions src/test/java/io/kestra/plugin/git/PushNamespaceFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.Rethrow;
import io.kestra.plugin.git.services.GitService;
import io.micronaut.context.annotation.Value;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -100,6 +101,8 @@ void defaultCase_SingleRegex() throws Exception {

try {
PushNamespaceFiles.Output pushOutput = pushNamespaceFiles.run(runContext);
GitService gitService = new GitService(pushNamespaceFiles);
assertThat(gitService.branchExists(runContext, branch), is(true));

Clone clone = Clone.builder()
.id("clone")
Expand Down Expand Up @@ -139,6 +142,54 @@ void defaultCase_SingleRegex() throws Exception {
}
}

@Test
void defaultCase_SingleRegexDryRun() throws Exception {
String tenantId = "my-tenant";
String namespace = IdUtils.create().toLowerCase();
String branch = IdUtils.create();
String gitDirectory = "my-files";
String authorEmail = "bmulier@kestra.io";
String authorName = "brianmulier";
String url = "https://github.com/kestra-io/unit-tests";
RunContext runContext = runContext(tenantId, url, authorEmail, authorName, branch, namespace, gitDirectory);

String firstFilePath = "first-file.txt";
storage.put(tenantId, URI.create("kestra://" + StorageContext.namespaceFilePrefix(namespace) + "/" + firstFilePath), new ByteArrayInputStream("First file".getBytes()));
String secondFilePath = "nested/second-file.txt";
String secondFileContent = "Second file";
storage.put(tenantId, URI.create("kestra://" + StorageContext.namespaceFilePrefix(namespace) + "/" + secondFilePath), new ByteArrayInputStream(secondFileContent.getBytes()));

PushNamespaceFiles pushNamespaceFiles = PushNamespaceFiles.builder()
.id("pushNamespaceFiles")
.type(PushNamespaceFiles.class.getName())
.branch("{{branch}}")
.url("{{url}}")
.commitMessage("Push from CI - {{description}}")
.username("{{pat}}")
.password("{{pat}}")
.authorEmail("{{email}}")
.authorName("{{name}}")
.namespace("{{namespace}}")
.files("second.*")
.gitDirectory("{{gitDirectory}}")
.dryRun(true)
.build();

PushNamespaceFiles.Output pushOutput = pushNamespaceFiles.run(runContext);
GitService gitService = new GitService(pushNamespaceFiles);
assertThat(gitService.branchExists(runContext, branch), is(false));

assertThat(pushOutput.getCommitURL(), nullValue());

assertDiffs(
runContext,
pushOutput.diffFileUri(),
List.of(
Map.of("additions", "+1", "deletions", "-0", "changes", "0", "file", gitDirectory + "/" + secondFilePath)
)
);
}

@Test
void defaultCase_SingleRegex_DeleteScopedToRegex() throws Exception {
String tenantId = "my-tenant";
Expand Down Expand Up @@ -472,7 +523,7 @@ private RunContext runContext(String tenantId, String url, String authorEmail, S

private static RevCommit assertIsLastCommit(RunContext cloneRunContext, PushNamespaceFiles.Output pushOutput) throws IOException, GitAPIException {
RevCommit revCommit;
try(Git git = Git.open(cloneRunContext.tempDir().toFile())) {
try (Git git = Git.open(cloneRunContext.tempDir().toFile())) {
revCommit = StreamSupport.stream(git.log().setMaxCount(1).call().spliterator(), false).findFirst().orElse(null);
}
assertThat(revCommit.getId().getName(), is(pushOutput.getCommitId()));
Expand All @@ -498,7 +549,7 @@ private static void assertDiffs(RunContext runContext, URI diffFileUri, List<Map
}

private void deleteRemoteBranch(Path gitDirectory, String branchName) throws GitAPIException, IOException {
try(Git git = Git.open(gitDirectory.toFile())) {
try (Git git = Git.open(gitDirectory.toFile())) {
git.checkout().setName("tmp").setCreateBranch(true).call();
git.branchDelete().setBranchNames(R_HEADS + branchName).call();
RefSpec refSpec = new RefSpec()
Expand Down

0 comments on commit c0e7fb3

Please sign in to comment.