Skip to content

Commit 86e8df8

Browse files
author
François Delbrayelle
committed
fix: use onInvalidSyntax properly when invalid flows
1 parent 54ca47c commit 86e8df8

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/main/java/io/kestra/plugin/git/NamespaceSync.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public Output run(RunContext runContext) throws Exception {
234234
}
235235

236236
// Fetch Kestra state limited to target namespace only
237-
KestraState kestraState = loadKestraState(runContext, rNamespace);
237+
KestraState kestraState = loadKestraState(runContext, rNamespace, rOnInvalidSyntax);
238238
Map<String, byte[]> namespaceFiles = listNamespaceFiles(runContext, rNamespace);
239239

240240
List<DiffLine> diffs = new ArrayList<>();
@@ -323,7 +323,7 @@ private void planFlows(RunContext rc, Path baseDir, GitTree gitTree, KestraState
323323
throw new FlowProcessingException(flowValidated.getConstraints());
324324
}
325325
fs.importFlow(tenant, gitNode.rawYaml, false);
326-
} catch (FlowProcessingException e) {
326+
} catch (Exception e) {
327327
handleInvalid(rc, rInvalid, "FLOW " + key, e);
328328
}
329329
});
@@ -383,7 +383,7 @@ private void planFlows(RunContext rc, Path baseDir, GitTree gitTree, KestraState
383383
}
384384

385385
fs.importFlow(tenant, gitNode.rawYaml, false);
386-
} catch (FlowProcessingException e) {
386+
} catch (Exception e) {
387387
handleInvalid(rc, rInvalid, "FLOW " + key, e);
388388
}
389389
});
@@ -478,11 +478,19 @@ private static class GitTree {
478478
private record KestraState(Map<String, FlowWithSource> flows) {
479479
}
480480

481-
private KestraState loadKestraState(RunContext rc, String rootNamespace) {
481+
private KestraState loadKestraState(RunContext rc, String rootNamespace, OnInvalidSyntax rOnInvalidSyntax) {
482482
FlowService fs = flowService(rc);
483483
String tenant = rc.flowInfo().tenantId();
484484

485-
Map<String, FlowWithSource> flowsWithSource = fs.findByNamespaceWithSource(tenant, rootNamespace).stream()
485+
List<FlowWithSource> allFlows;
486+
try {
487+
allFlows = fs.findByNamespaceWithSource(tenant, rootNamespace);
488+
} catch (Exception e) {
489+
handleInvalid(rc, rOnInvalidSyntax, "flows for namespace " + rootNamespace, e);
490+
allFlows = List.of();
491+
}
492+
493+
Map<String, FlowWithSource> flowsWithSource = allFlows.stream()
486494
.collect(Collectors.toMap(f -> key(f.getNamespace(), f.getId()), Function.identity(), (a, b) -> a));
487495

488496
return new KestraState(flowsWithSource);

src/main/java/io/kestra/plugin/git/TenantSync.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.kestra.core.models.property.Property;
1010
import io.kestra.core.models.tasks.RunnableTask;
1111
import io.kestra.core.runners.RunContext;
12+
import io.kestra.core.serializers.YamlParser;
1213
import io.kestra.plugin.git.services.GitService;
1314
import io.kestra.sdk.KestraClient;
1415
import io.kestra.sdk.api.FilesApi;
@@ -34,6 +35,8 @@
3435
import java.nio.file.Path;
3536
import java.util.*;
3637
import java.util.stream.Collectors;
38+
import java.util.zip.ZipEntry;
39+
import java.util.zip.ZipInputStream;
3740

3841
import static org.eclipse.jgit.transport.RemoteRefUpdate.Status.*;
3942

@@ -242,7 +245,7 @@ public Output run(RunContext runContext) throws Exception {
242245
}
243246
}
244247

245-
List<FlowWithSource> kestraFlows = fetchFlowsFromKestra(kestraClient, runContext, namespace);
248+
List<FlowWithSource> kestraFlows = fetchFlowsFromKestra(kestraClient, runContext, namespace, rOnInvalidSyntax);
246249
Map<String, byte[]> kestraFiles = listNamespaceFiles(kestraClient, runContext, namespace);
247250

248251
planNamespace(
@@ -728,7 +731,7 @@ private void planDashboards(
728731
}
729732
}
730733

731-
private List<FlowWithSource> fetchFlowsFromKestra(KestraClient kestraClient, RunContext runContext, String namespace) {
734+
private List<FlowWithSource> fetchFlowsFromKestra(KestraClient kestraClient, RunContext runContext, String namespace, OnInvalidSyntax rOnInvalidSyntax) {
732735
try {
733736
// Export all flows from Kestra for the given namespace (including sub-namespaces)
734737
byte[] zippedFlows = kestraClient.flows().exportFlowsByQuery(
@@ -740,17 +743,24 @@ private List<FlowWithSource> fetchFlowsFromKestra(KestraClient kestraClient, Run
740743

741744
try (
742745
var bais = new ByteArrayInputStream(zippedFlows);
743-
var zis = new java.util.zip.ZipInputStream(bais)
746+
var zis = new ZipInputStream(bais)
744747
) {
745-
java.util.zip.ZipEntry entry;
748+
ZipEntry entry;
746749
while ((entry = zis.getNextEntry()) != null) {
747750
if (!entry.getName().endsWith(".yml") && !entry.getName().endsWith(".yaml")) {
748751
continue;
749752
}
750753

751-
String yaml = new String(zis.readAllBytes(), StandardCharsets.UTF_8);
754+
var entryName = entry.getName();
755+
var yaml = new String(zis.readAllBytes(), StandardCharsets.UTF_8);
752756

753-
io.kestra.core.models.flows.Flow parsed = io.kestra.core.serializers.YamlParser.parse(yaml, io.kestra.core.models.flows.Flow.class);
757+
io.kestra.core.models.flows.Flow parsed;
758+
try {
759+
parsed = YamlParser.parse(yaml, io.kestra.core.models.flows.Flow.class);
760+
} catch (Exception e) {
761+
handleInvalid(runContext, rOnInvalidSyntax, "FLOW from entry " + entryName, e);
762+
continue;
763+
}
754764

755765
if (!namespace.equals(parsed.getNamespace())) {
756766
continue;
@@ -762,6 +772,9 @@ private List<FlowWithSource> fetchFlowsFromKestra(KestraClient kestraClient, Run
762772

763773
return flows;
764774

775+
} catch (KestraRuntimeException e) {
776+
// Re-throw KestraRuntimeException from handleInvalid(FAIL) without wrapping
777+
throw e;
765778
} catch (Exception e) {
766779
throw new KestraRuntimeException("Failed to export flows from Kestra for namespace " + namespace, e);
767780
}

0 commit comments

Comments
 (0)