Skip to content

Commit

Permalink
refactor: dynamic properties (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle authored Dec 12, 2024
1 parent 320a656 commit a048fb5
Show file tree
Hide file tree
Showing 25 changed files with 302 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.github.GHObject;
import org.kohsuke.github.GitHub;

import java.io.BufferedReader;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/kestra/plugin/github/GithubConnection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.kestra.plugin.github;

import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.Task;
import jakarta.annotation.Nullable;
import lombok.EqualsAndHashCode;
Expand All @@ -15,11 +16,11 @@
@NoArgsConstructor
public abstract class GithubConnection extends Task implements GithubConnectionInterface {

private String login;
private Property<String> login;

private String oauthToken;
private Property<String> oauthToken;

private String jwtToken;
private Property<String> jwtToken;

public record GithubClientConfig(
@Nullable String login,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.kestra.plugin.github;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;

Expand All @@ -11,38 +11,34 @@ public interface GithubConnectionInterface {
title = "GitHub login",
description = "Requires additional field: oauthToken, to log-in"
)
@PluginProperty(dynamic = true)
String getLogin();
Property<String> getLogin();

@Schema(
title = "GitHub oauthToken",
description = "GitHub Personal Access Token. In addition, can be used with login or by its own"
)
@PluginProperty(dynamic = true)
String getOauthToken();
Property<String> getOauthToken();

@Schema(
title = "GitHub JWT token",
description = "Does not requires additional fields to log-in"
)
@PluginProperty(dynamic = true)
String getJwtToken();
Property<String> getJwtToken();

@Schema(
title = "GitHub repository",
description = "Repository where issue/ticket should be created. It's a string of Username + / + Repository name",
example = "kestra-io/plugin-github"
)
@PluginProperty(dynamic = true)
default String getRepository() {
default Property<String> getRepository() {
return null;
}

default GithubConnection.GithubClientConfig getClientConfig(RunContext runContext) throws IllegalVariableEvaluationException {
return new GithubConnection.GithubClientConfig(
runContext.render(this.getLogin()),
runContext.render(this.getOauthToken()),
runContext.render(this.getJwtToken())
runContext.render(this.getLogin()).as(String.class).orElse(null),
runContext.render(this.getOauthToken()).as(String.class).orElse(null),
runContext.render(this.getJwtToken()).as(String.class).orElse(null)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.kestra.plugin.github;

import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.FileSerde;
import io.kestra.plugin.github.model.IssueDetails;
Expand Down
64 changes: 26 additions & 38 deletions src/main/java/io/kestra/plugin/github/code/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.FileSerde;
Expand Down Expand Up @@ -55,7 +55,7 @@
code = """
id: github_code_search_flow
namespace: company.team
tasks:
- id: search_code
type: io.kestra.plugin.github.code.Search
Expand Down Expand Up @@ -99,62 +99,52 @@ public enum Fork {
title = "The query contains one or more search keywords and qualifiers.",
description = "Allow you to limit your search to specific areas of GitHub."
)
@PluginProperty(dynamic = true)
private String query;
private Property<String> query;

@Schema(
title = "The GitHub repository."
)
@PluginProperty(dynamic = true)
private String repository;
private Property<String> repository;

@Schema(
title = "Search commits in all repositories owned by a certain user."
)
@PluginProperty(dynamic = true)
private String user;
private Property<String> user;

@Schema(
title = "In"
)
@PluginProperty(dynamic = true)
private String in;
private Property<String> in;

@Schema(
title = "The language."
)
@PluginProperty(dynamic = true)
private String language;
private Property<String> language;

@Schema(
title = "The file extension."
)
@PluginProperty(dynamic = true)
private String extension;
private Property<String> extension;

@Schema(
description = "Whether to include forks."
)
@PluginProperty
private Fork fork;
private Property<Fork> fork;

@Schema(
title = "The file name."
)
@PluginProperty(dynamic = true)
private String filename;
private Property<String> filename;

@Schema(
title = "The file path."
)
@PluginProperty(dynamic = true)
private String path;
private Property<String> path;

@Schema(
title = "The file size."
)
@PluginProperty(dynamic = true)
private String size;
private Property<String> size;

@Schema(
name = "order",
Expand All @@ -165,8 +155,7 @@ public enum Fork {
"""
)
@Builder.Default
@PluginProperty
private Order order = Order.ASC;
private Property<Order> order = Property.of(Order.ASC);

@Schema(
name = "sort",
Expand All @@ -177,8 +166,7 @@ public enum Fork {
"""
)
@Builder.Default
@PluginProperty
private Sort sort = Sort.BEST_MATCH;
private Property<Sort> sort = Property.of(Sort.BEST_MATCH);

@Override
public Output run(RunContext runContext) throws Exception {
Expand Down Expand Up @@ -215,47 +203,47 @@ private GHContentSearchBuilder setupSearchParameters(RunContext runContext, GitH
GHContentSearchBuilder searchBuilder = gitHub.searchContent();

searchBuilder
.sort(this.sort.value)
.order(this.order.direction);
.sort(runContext.render(this.sort).as(Sort.class).orElseThrow().value)
.order(runContext.render(this.order).as(Order.class).orElseThrow().direction);

if (this.query != null) {
searchBuilder.q(runContext.render(this.query));
searchBuilder.q(runContext.render(this.query).as(String.class).orElseThrow());
}

if (this.repository != null) {
searchBuilder.repo(runContext.render(this.repository));
searchBuilder.repo(runContext.render(this.repository).as(String.class).orElseThrow());
}

if (this.user != null) {
searchBuilder.user(runContext.render(this.user));
searchBuilder.user(runContext.render(this.user).as(String.class).orElseThrow());
}

if (this.in != null) {
searchBuilder.in(runContext.render(this.in));
searchBuilder.in(runContext.render(this.in).as(String.class).orElseThrow());
}

if (this.language != null) {
searchBuilder.language(runContext.render(this.language));
searchBuilder.language(runContext.render(this.language).as(String.class).orElseThrow());
}

if (this.extension != null) {
searchBuilder.extension(runContext.render(this.extension));
searchBuilder.extension(runContext.render(this.extension).as(String.class).orElseThrow());
}

if (this.fork != null) {
searchBuilder.fork(this.fork.value);
searchBuilder.fork(runContext.render(this.fork).as(Fork.class).orElseThrow().value);
}

if (this.filename != null) {
searchBuilder.filename(runContext.render(this.filename));
searchBuilder.filename(runContext.render(this.filename).as(String.class).orElseThrow());
}

if (this.path != null) {
searchBuilder.path(runContext.render(this.path));
searchBuilder.path(runContext.render(this.path).as(String.class).orElseThrow());
}

if (this.size != null) {
searchBuilder.size(runContext.render(this.size));
searchBuilder.size(runContext.render(this.size).as(String.class).orElseThrow());
}
return searchBuilder;
}
Expand Down
Loading

0 comments on commit a048fb5

Please sign in to comment.