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: dynamic properties #15

Merged
merged 1 commit into from
Dec 11, 2024
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
8 changes: 4 additions & 4 deletions src/main/java/io/kestra/plugin/linear/LinearConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
Expand Down Expand Up @@ -34,14 +35,13 @@ public abstract class LinearConnection extends Task {
@Schema(
title = "Linear API token"
)
@PluginProperty(dynamic = true)
private String token;
private Property<String> token;

protected HttpResponse<String> makeCall(RunContext runContext, String query) throws IllegalVariableEvaluationException {
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(LINEAR_API_URL))
.header("Authorization", runContext.render(this.token))
.header("Authorization", runContext.render(this.token).as(String.class).orElse(null))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(query))
.build();
Expand All @@ -66,7 +66,7 @@ protected CompletableFuture<HttpResponse<String>> makeAsyncCall(RunContext runCo
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(LINEAR_API_URL))
.header("Authorization", runContext.render(this.token))
.header("Authorization", runContext.render(this.token).as(String.class).orElse(null))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(query))
.build();
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/io/kestra/plugin/linear/issues/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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.plugin.linear.LinearConnection;
Expand Down Expand Up @@ -53,7 +54,7 @@
code = """
id: create_ticket_on_failure
namespace: system

tasks:
- id: create_issue
type: io.kestra.plugin.linear.issues.Create
Expand All @@ -64,7 +65,7 @@
labels:
- Bug
- Workflow

triggers:
- id: on_failure
type: io.kestra.plugin.core.trigger.Flow
Expand All @@ -85,14 +86,12 @@ public class Create extends LinearConnection implements RunnableTask<Create.Outp
@Schema(
title = "Team name"
)
@PluginProperty(dynamic = true)
private String team;
private Property<String> team;

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

@Schema(
title = "Issue description"
Expand All @@ -103,8 +102,7 @@ public class Create extends LinearConnection implements RunnableTask<Create.Outp
@Schema(
title = "Names of labels"
)
@PluginProperty(dynamic = true)
private List<String> labels;
private Property<List<String>> labels;

@Override
public Create.Output run(RunContext runContext) throws Exception {
Expand All @@ -113,7 +111,7 @@ public Create.Output run(RunContext runContext) throws Exception {

String query = buildInputQuery(
teamId,
runContext.render(this.title),
runContext.render(this.title).as(String.class).orElse(null),
runContext.render(this.description),
labelsIds
);
Expand Down Expand Up @@ -151,7 +149,7 @@ private List<String> getLabelsIds(RunContext runContext) throws Exception {

LabelsResponse labelsResponse = mapper.readValue(response.body(), LabelsResponse.class);

List<String> names = runContext.render(this.labels);
List<String> names = runContext.render(this.labels).asList(String.class);

return labelsResponse
.getLabels()
Expand All @@ -170,7 +168,7 @@ private String getTeamId(RunContext runContext) throws Exception {

TeamsResponse teamsResponse = mapper.readValue(response.body(), TeamsResponse.class);

String teamName = runContext.render(this.team);
String teamName = runContext.render(this.team).as(String.class).orElse(null);

return teamsResponse
.getTeams()
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/io/kestra/plugin/linear/issues/CreateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Strings;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.models.property.Property;
import io.kestra.core.runners.RunContext;
import io.kestra.core.runners.RunContextFactory;
import jakarta.inject.Inject;
Expand All @@ -28,11 +29,11 @@ void run() throws Exception {
RunContext runContext = runContextFactory.of();

Create create = Create.builder()
.token(getToken())
.team(getTeamName())
.title("Kestra test")
.token(Property.of(getToken()))
.team(Property.of(getTeamName()))
.title(Property.of("Kestra test"))
.description("Test issue created by Kestra Unit test")
.labels(getLabels())
.labels(Property.of(getLabels()))
.build();

Create.Output output = create.run(runContext);
Expand Down
Loading