From b0c56eb7a6ae0db5765bc24a09751107516e7c9b Mon Sep 17 00:00:00 2001 From: Mathieu Gabelle Date: Mon, 27 Jan 2025 11:12:14 +0100 Subject: [PATCH] refactor: migrate Sync to dynamic properties --- src/main/java/io/kestra/plugin/git/PushFlows.java | 1 - .../io/kestra/plugin/git/PushNamespaceFiles.java | 1 - src/main/java/io/kestra/plugin/git/Sync.java | 15 ++++++--------- src/test/java/io/kestra/plugin/git/SyncTest.java | 8 ++++---- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/kestra/plugin/git/PushFlows.java b/src/main/java/io/kestra/plugin/git/PushFlows.java index 165963d..57a2c95 100644 --- a/src/main/java/io/kestra/plugin/git/PushFlows.java +++ b/src/main/java/io/kestra/plugin/git/PushFlows.java @@ -143,7 +143,6 @@ public class PushFlows extends AbstractPushTask { title = "The branch to which files should be committed and pushed.", description = "If the branch doesn't exist yet, it will be created." ) - @PluginProperty(dynamic = true) @Builder.Default private Property branch = Property.of("main"); diff --git a/src/main/java/io/kestra/plugin/git/PushNamespaceFiles.java b/src/main/java/io/kestra/plugin/git/PushNamespaceFiles.java index 967f96c..9dd4271 100644 --- a/src/main/java/io/kestra/plugin/git/PushNamespaceFiles.java +++ b/src/main/java/io/kestra/plugin/git/PushNamespaceFiles.java @@ -107,7 +107,6 @@ public class PushNamespaceFiles extends AbstractPushTask branch = Property.of("main"); diff --git a/src/main/java/io/kestra/plugin/git/Sync.java b/src/main/java/io/kestra/plugin/git/Sync.java index 9eb41fa..f711108 100644 --- a/src/main/java/io/kestra/plugin/git/Sync.java +++ b/src/main/java/io/kestra/plugin/git/Sync.java @@ -88,22 +88,19 @@ public class Sync extends AbstractCloningTask implements RunnableTask gitDirectory; @Schema( title = "Namespace files directory to which files from Git should be synced. It defaults to the root directory of the namespace." ) - @PluginProperty(dynamic = true) - private String namespaceFilesDirectory; + private Property namespaceFilesDirectory; private Property branch; @Schema( title = "If true, the task will only display modifications without syncing any files yet. If false (default), all namespace files and flows will be overwritten based on the state in Git." ) - @PluginProperty - private Boolean dryRun; + private Property dryRun; @Override public VoidOutput run(RunContext runContext) throws Exception { @@ -111,7 +108,7 @@ public VoidOutput run(RunContext runContext) throws Exception { Map flowProps = (Map) runContext.getVariables().get("flow"); String namespace = flowProps.get("namespace"); String tenantId = flowProps.get("tenantId"); - boolean dryRun = this.dryRun != null && this.dryRun; + boolean dryRun = this.dryRun != null && runContext.render(this.dryRun).as(Boolean.class).orElse(false); Clone clone = Clone.builder() .depth(1) @@ -127,7 +124,7 @@ public VoidOutput run(RunContext runContext) throws Exception { clone.run(runContext); // we should synchronize git flows with current namespace flows - Path absoluteGitDirPath = runContext.workingDir().resolve(Optional.ofNullable(runContext.render(this.gitDirectory)).map(Path::of).orElse(null)); + Path absoluteGitDirPath = runContext.workingDir().resolve(runContext.render(this.gitDirectory).as(String.class).map(Path::of).orElse(null)); Path flowsDirectoryBasePath = absoluteGitDirPath.resolve(FLOWS_DIRECTORY); KestraIgnore kestraIgnore = new KestraIgnore(absoluteGitDirPath); @@ -216,7 +213,7 @@ public VoidOutput run(RunContext runContext) throws Exception { StorageInterface storage = ((DefaultRunContext)runContext).getApplicationContext().getBean(StorageInterface.class); URI namespaceFilePrefix = URI.create("kestra://" + StorageContext.namespaceFilePrefix(namespace) + "/"); if (this.namespaceFilesDirectory != null) { - String renderedNamespaceFilesDirectory = runContext.render(this.namespaceFilesDirectory); + String renderedNamespaceFilesDirectory = runContext.render(this.namespaceFilesDirectory).as(String.class).orElseThrow(); renderedNamespaceFilesDirectory = renderedNamespaceFilesDirectory.startsWith("/") ? renderedNamespaceFilesDirectory.substring(1) : renderedNamespaceFilesDirectory; renderedNamespaceFilesDirectory = renderedNamespaceFilesDirectory.endsWith("/") ? renderedNamespaceFilesDirectory : renderedNamespaceFilesDirectory + "/"; namespaceFilePrefix = namespaceFilePrefix.resolve(renderedNamespaceFilesDirectory); diff --git a/src/test/java/io/kestra/plugin/git/SyncTest.java b/src/test/java/io/kestra/plugin/git/SyncTest.java index 68baaa8..38884f0 100644 --- a/src/test/java/io/kestra/plugin/git/SyncTest.java +++ b/src/test/java/io/kestra/plugin/git/SyncTest.java @@ -177,8 +177,8 @@ void reconcileNsFilesAndFlows() throws Exception { .username(new Property<>(pat)) .password(new Property<>(pat)) .branch(new Property<>(BRANCH)) - .gitDirectory(clonedGitDirectory) - .namespaceFilesDirectory(destinationDirectory) + .gitDirectory(Property.of(clonedGitDirectory)) + .namespaceFilesDirectory(Property.of(destinationDirectory)) .build(); task.run(runContextFactory.of(Map.of("flow", Map.of( "namespace", NAMESPACE, @@ -379,8 +379,8 @@ void reconcile_DryRun_ShouldDoNothing() throws Exception { .username(new Property<>(pat)) .password(new Property<>(pat)) .branch(new Property<>(BRANCH)) - .gitDirectory("to_clone") - .dryRun(true) + .gitDirectory(Property.of("to_clone")) + .dryRun(Property.of(true)) .build(); RunContext runContext = TestsUtils.mockRunContext(runContextFactory, task, Collections.emptyMap()); task.run(runContext);