|
| 1 | +package io.kestra.plugin.trello.cards; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import io.kestra.core.http.HttpRequest; |
| 5 | +import io.kestra.core.http.HttpResponse; |
| 6 | +import io.kestra.core.http.client.HttpClient; |
| 7 | +import io.kestra.core.models.annotations.Example; |
| 8 | +import io.kestra.core.models.annotations.Plugin; |
| 9 | +import io.kestra.core.models.property.Property; |
| 10 | +import io.kestra.core.runners.RunContext; |
| 11 | +import io.kestra.core.serializers.JacksonMapper; |
| 12 | +import io.kestra.plugin.trello.AbstractTrelloTask; |
| 13 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 14 | +import jakarta.validation.constraints.NotNull; |
| 15 | +import lombok.*; |
| 16 | +import lombok.experimental.SuperBuilder; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.net.URLEncoder; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +@SuperBuilder |
| 23 | +@NoArgsConstructor |
| 24 | +@Getter |
| 25 | +@ToString |
| 26 | +@EqualsAndHashCode |
| 27 | + |
| 28 | +@Schema( |
| 29 | + title = "Add a comment to a Trello card", |
| 30 | + description = "Add a comment/action to an existing Trello card" |
| 31 | +) |
| 32 | +@Plugin( |
| 33 | + examples = { |
| 34 | + @Example( |
| 35 | + title = "Add a comment to a Trello card", |
| 36 | + full = true, |
| 37 | + code = """ |
| 38 | + id: trello_add_comment |
| 39 | + namespace: company.team |
| 40 | +
|
| 41 | + tasks: |
| 42 | + - id: add_comment |
| 43 | + type: io.kestra.plugin.trello.cards.Comment |
| 44 | + apiKey: "{{ secret('TRELLO_API_KEY') }}" |
| 45 | + apiToken: "{{ secret('TRELLO_API_TOKEN') }}" |
| 46 | + cardId: "5abbe4b7ddc1b351ef961414" |
| 47 | + text: "This is my comment on the card" |
| 48 | + """ |
| 49 | + ) |
| 50 | + } |
| 51 | +) |
| 52 | +public class Comment extends AbstractTrelloTask { |
| 53 | + |
| 54 | + @Schema(title = "Card ID", description = "The ID of the card to add a comment to") |
| 55 | + @NotNull |
| 56 | + protected Property<String> cardId; |
| 57 | + |
| 58 | + @Schema(title = "Comment Text", description = "The text of the comment") |
| 59 | + @NotNull |
| 60 | + protected Property<String> text; |
| 61 | + |
| 62 | + @Override |
| 63 | + public io.kestra.core.models.tasks.Output run(RunContext runContext) throws Exception { |
| 64 | + String rId = runContext.render(this.cardId).as(String.class).orElseThrow(); |
| 65 | + String rText = runContext.render(this.text).as(String.class).orElseThrow(); |
| 66 | + |
| 67 | + String url = buildApiUrl(runContext, "cards/" + rId + "/actions/comments") + "?text=" |
| 68 | + + URLEncoder.encode(rText, StandardCharsets.UTF_8); |
| 69 | + |
| 70 | + HttpRequest.HttpRequestBuilder requestBuilder = HttpRequest.builder() |
| 71 | + .method("POST") |
| 72 | + .uri(URI.create(url)) |
| 73 | + .addHeader("Accept", "application/json"); |
| 74 | + |
| 75 | + HttpRequest request = addAuthHeaders(runContext, requestBuilder).build(); |
| 76 | + |
| 77 | + try (HttpClient httpClient = HttpClient.builder() |
| 78 | + .runContext(runContext) |
| 79 | + .build()) { |
| 80 | + HttpResponse<String> response = httpClient.request(request, String.class); |
| 81 | + |
| 82 | + if (response.getStatus().getCode() != 200) { |
| 83 | + throw new RuntimeException( |
| 84 | + "Failed to add comment: " + response.getStatus().getCode() + " - " |
| 85 | + + response.getBody()); |
| 86 | + } |
| 87 | + |
| 88 | + JsonNode jsonNode = JacksonMapper.ofJson().readTree(response.getBody()); |
| 89 | + |
| 90 | + return Output.builder() |
| 91 | + .commentId(jsonNode.has("id")?jsonNode.get("id").asText():null) |
| 92 | + .build(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Builder |
| 97 | + @Getter |
| 98 | + public static class Output implements io.kestra.core.models.tasks.Output { |
| 99 | + @Schema(title = "Comment ID") |
| 100 | + private final String commentId; |
| 101 | + } |
| 102 | +} |
0 commit comments