All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestra.plugin.jira.issues.JiraTemplate Maven / Gradle / Ivy

The newest version!
package io.kestra.plugin.jira.issues;

import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.VoidOutput;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;

import java.util.*;

@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
public abstract class JiraTemplate extends JiraClient {

    @Schema(
        title = "Template to use",
        hidden = true
    )
    @PluginProperty(dynamic = true)
    protected String templateUri;

    @Schema(
        title = "Atlassian project's key"
    )
    @PluginProperty(dynamic = true)
    @NotBlank
    protected String projectKey;

    @Schema(
        title = "Summary of the ticket"
    )
    @PluginProperty(dynamic = true)
    protected String summary;

    @Schema(
        title = "Description of the ticket to be created"
    )
    @PluginProperty(dynamic = true)
    protected String description;

    @Schema(
        title = "Labels associated with opened ticket"
    )
    @PluginProperty(dynamic = true)
    protected List labels;
    @SuppressWarnings("unchecked")
    @Override
    public VoidOutput run(RunContext runContext) throws Exception {
        if (payload != null && !payload.isBlank()) {
            return super.run(runContext);
        }

        Map mainMap = new HashMap<>();
        Map renderedAttributesMap = Map.of(
            "projectKey", runContext.render(projectKey),
            "summary", runContext.render(summary),
            "labels", runContext.render(labels),
            "description", runContext.render(description)
        );

        if (this.templateUri != null) {
            String template = IOUtils.toString(
                Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream(this.templateUri)),
                Charsets.UTF_8
            );

            String render = runContext.render(template, renderedAttributesMap);

            mainMap = (Map) JacksonMapper.ofJson().readValue(render, Object.class);
        }

        this.payload = JacksonMapper.ofJson().writeValueAsString(mainMap);

        return super.run(runContext);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy