Skip to content

Commit

Permalink
fix(Sync): file are not deleted correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Skraye committed Nov 5, 2024
1 parent 7dcb157 commit 8a1e9e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/main/java/io/kestra/plugin/git/AbstractSyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.DefaultRunContext;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.core.utils.KestraIgnore;
Expand All @@ -16,10 +15,8 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jgit.api.Git;
import org.slf4j.Logger;

import java.io.*;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand Down Expand Up @@ -110,7 +107,7 @@ private URI createDiffFile(RunContext runContext, String renderedNamespace, Map<
runContext,
renderedGitDirectory,
renderedNamespace,
gitUriByResourceUri.get(this.toUri(runContext, renderedNamespace, deletedResource)),
gitUriByResourceUri.get(this.toUri(renderedNamespace, deletedResource)),
deletedResource,
null
))).forEach(syncResults::add);
Expand Down Expand Up @@ -164,7 +161,7 @@ public O run(RunContext runContext) throws Exception {
Map<URI, T> beforeUpdateResourcesByUri = this.fetchResources(runContext, renderedNamespace)
.stream()
.collect(Collectors.toMap(
resource -> this.toUri(runContext, renderedNamespace, resource),
resource -> this.toUri(renderedNamespace, resource),
Function.identity()
));
Map<URI, URI> gitUriByResourceUri = new HashMap<>();
Expand All @@ -186,7 +183,7 @@ public O run(RunContext runContext) throws Exception {
(map, pair) -> {
URI uri = pair.getLeft();
T resource = pair.getRight();
URI resourceUri = this.toUri(runContext, renderedNamespace, resource);
URI resourceUri = this.toUri(renderedNamespace, resource);
map.put(resourceUri, resource);
gitUriByResourceUri.put(resourceUri, uri);
},
Expand Down Expand Up @@ -220,7 +217,7 @@ public O run(RunContext runContext) throws Exception {

protected abstract List<T> fetchResources(RunContext runContext, String renderedNamespace) throws IOException;

protected abstract URI toUri(RunContext runContext, String renderedNamespace, T resource);
protected abstract URI toUri(String renderedNamespace, T resource);

protected abstract O output(URI diffFileStorageUri);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/kestra/plugin/git/SyncFlows.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected List<Flow> fetchResources(RunContext runContext, String renderedNamesp
}

@Override
protected URI toUri(RunContext runContext, String renderedNamespace, Flow resource) {
protected URI toUri(String renderedNamespace, Flow resource) {
if (resource == null) {
return null;
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/kestra/plugin/git/SyncNamespaceFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String fetchedNamespace() {

@Override
protected void deleteResource(RunContext runContext, String renderedNamespace, URI instanceUri) throws IOException {
runContext.storage().namespace(renderedNamespace).delete(Path.of(instanceUri.getPath()));
runContext.storage().namespace(renderedNamespace).delete(Path.of(instanceUri.getPath().replace("\\","/")));
}

@Override
Expand Down Expand Up @@ -131,7 +131,6 @@ protected SyncResult wrapper(RunContext runContext, String renderedGitDirectory,
}

String kestraPath = Optional.ofNullable(this.toUri(
runContext,
renderedNamespace,
resourceAfterUpdate == null ? resourceBeforeUpdate : resourceAfterUpdate
)).map(URI::getPath).orElse(null);
Expand All @@ -150,12 +149,12 @@ protected SyncResult wrapper(RunContext runContext, String renderedGitDirectory,
protected List<URI> fetchResources(RunContext runContext, String renderedNamespace) throws IOException {
return runContext.storage().namespace(renderedNamespace).all(true)
.stream()
.map(namespaceFile -> toUri(runContext, renderedNamespace, namespaceFile.uri()))
.map(namespaceFile -> namespaceFile.uri())
.toList();
}

@Override
protected URI toUri(RunContext runContext, String renderedNamespace, URI resource) {
protected URI toUri(String renderedNamespace, URI resource) {
if (resource == null) {
return null;
}
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/io/kestra/plugin/git/SyncNamespaceFilesTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.plugin.git;

import com.fasterxml.jackson.core.type.TypeReference;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.models.executions.LogEntry;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.runners.RunContext;
Expand All @@ -10,8 +11,6 @@
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.utils.KestraIgnore;
import io.kestra.core.utils.Rethrow;
import io.micronaut.context.annotation.Value;
import io.kestra.core.junit.annotations.KestraTest;
import jakarta.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -20,11 +19,15 @@
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

@KestraTest
Expand Down Expand Up @@ -243,7 +246,7 @@ void defaultCase_DryRunWithDeleteFlag_ShouldStillNotifyWhatWouldBeDeleted() thro
assertThat(storage.exists(TENANT_ID, URI.create(StorageContext.namespaceFilePrefix(NAMESPACE) + "/README.md")), is(true));
assertThat(storage.exists(TENANT_ID, URI.create(StorageContext.namespaceFilePrefix(NAMESPACE) + deletedFilePath)), is(true));
assertThat(storage.exists(TENANT_ID, URI.create(StorageContext.namespaceFilePrefix(NAMESPACE) + deletedDirPath)), is(true));
assertThat(storage.exists(TENANT_ID, URI.create(StorageContext.namespaceFilePrefix(NAMESPACE) + deletedDirSubFilePath)), is(true));
assertThat(storage.exists(TENANT_ID, URI.create(deletedDirSubFilePath)), is(true));
assertNamespaceFileContent(clonedFilePath, "{\"old-field\": \"old-value\"}");

assertDiffs(runContext, syncOutput.diffFileUri(), defaultCaseDiffs(true));
Expand All @@ -270,7 +273,7 @@ private static List<Map<String, String>> defaultCaseDiffs(boolean withDeleted) {
this.put("gitPath", null);
}},
new HashMap<>() {{
this.putAll(Map.of("syncState", "DELETED", "kestraPath", "/dir_to_delete/file_to_delete.txt"));
this.putAll(Map.of("syncState", "DELETED", "kestraPath", "/kestra/my/namespace/_files/dir_to_delete/file_to_delete.txt"));
this.put("gitPath", null);
}},
new HashMap<>() {{
Expand Down

0 comments on commit 8a1e9e8

Please sign in to comment.