Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate Sync to dynamic properties #117

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/java/io/kestra/plugin/git/PushFlows.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public class PushFlows extends AbstractPushTask<PushFlows.Output> {
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<String> branch = Property.of("main");

Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/kestra/plugin/git/PushNamespaceFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public class PushNamespaceFiles extends AbstractPushTask<PushNamespaceFiles.Outp
title = "The branch to which Namespace Files should be committed and pushed.",
description = "If the branch doesn’t exist yet, it will be created. If not set, the task will push the files to the `kestra` branch."
)
@PluginProperty(dynamic = true)
@Builder.Default
private Property<String> branch = Property.of("main");

Expand Down
15 changes: 6 additions & 9 deletions src/main/java/io/kestra/plugin/git/Sync.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,27 @@ public class Sync extends AbstractCloningTask implements RunnableTask<VoidOutput
@Schema(
title = "Git directory to sync code from. If not specified, all files from a Git repository will be synchronized."
)
@PluginProperty(dynamic = true)
private String gitDirectory;
private Property<String> 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<String> namespaceFilesDirectory;

private Property<String> 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<Boolean> dryRun;

@Override
public VoidOutput run(RunContext runContext) throws Exception {
Logger logger = runContext.logger();
Map<String, String> flowProps = (Map<String, String>) 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)
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/io/kestra/plugin/git/SyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Loading