Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add workflow template node (#40571)

Joffrey JAFFEUX7233b4fd447b
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 82a4db2e10a..8775058dc7b 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -162,6 +162,7 @@ en:
"action:limit": "Limit"
"action:log": "Log"
"action:markdown_table": "Markdown table"
+ "action:template": "Template"
"action:respond_to_webhook": "Respond to webhook"
"trigger:topic_admin_button": "Topic admin button"
"trigger:error": "Error trigger"
@@ -200,6 +201,7 @@ en:
"action:limit": "Keep only the first or last N items"
"action:log": "Record key/value data to the execution log without affecting the workflow"
"action:markdown_table": "Build a markdown table from a list of items"
+ "action:template": "Render text with a Mustache template"
"action:respond_to_webhook": "Send a custom HTTP response to the webhook caller"
"trigger:topic_admin_button": "Adds a button to the topic admin menu that triggers this workflow"
"trigger:error": "Fires when another workflow fails"
@@ -720,6 +722,14 @@ en:
header: "Header"
value: "Value"
add_column: "Add column"
+ template:
+ mode: "Mode"
+ mode_description: "Choose whether the template renders once per item or once for all items"
+ mode_run_once_for_each_item: "Run once for each item"
+ mode_run_once_for_all_items: "Run once for all items"
+ template: "Template"
+ template_description: "In all-items mode, use <code>&#123;&#123;#items&#125;&#125;...&#123;&#123;/items&#125;&#125;</code> to loop over items. Inside the loop each item is available, and <code>item_index</code> is the current loop index starting at 1. In each-item mode, the current item is available at the top level. For example, if you have topic as input, you can access the title with <code>&#123;&#123;topic.title&#125;&#125;</code>. <code>items_count</code>, <code>vars</code>, <code>workflow</code>, <code>execution</code>, and <code>site_settings</code> are also accessible. Workflow expressions are not evaluated inside this field. If you need to modify your items, use the Set fields node before the template node."
+ template_placeholder: "Hello name"
form:
page_type: "Page type"
page_type_page: "Next form page"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 6b6eb45b0d5..72d3a73464f 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -69,6 +69,8 @@ en:
set_fields:
json_blank: "JSON string is blank."
json_must_be_object: "JSON must be an object."
+ template:
+ invalid_template: "Invalid Mustache template"
http_request:
url_required: "URL is required."
unsupported_scheme: "Only HTTP and HTTPS URLs are supported."
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb
new file mode 100644
index 00000000000..3bca28f54ce
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb
@@ -0,0 +1,193 @@
+# frozen_string_literal: true
+
+require "mustache"
+
+module DiscourseWorkflows
+ module Nodes
+ module Template
+ class V1 < NodeType
+ DEFAULT_TEMPLATE = <<~MUSTACHE
+ Items:
+ {{#items}}
+ - {{item_index}}: {{name}}
+ {{/items}}
+ MUSTACHE
+
+ RUN_ONCE_FOR_ALL_ITEMS = "runOnceForAllItems"
+ RUN_ONCE_FOR_EACH_ITEM = "runOnceForEachItem"
+
+ description(
+ name: "action:template",
+ version: "1.0",
+ defaults: {
+ icon: "code",
+ color: "green",
+ },
+ group: "data",
+ capabilities: {
+ run_scope: {
+ parameter: "mode",
+ values: {
+ RUN_ONCE_FOR_EACH_ITEM => "per_item",
+ RUN_ONCE_FOR_ALL_ITEMS => "all_items",
+ },
+ },
+ },
+ properties: {
+ mode: {
+ type: :options,
+ default: RUN_ONCE_FOR_ALL_ITEMS,
+ options: [RUN_ONCE_FOR_EACH_ITEM, RUN_ONCE_FOR_ALL_ITEMS],
+ no_data_expression: true,
+ },
+ template: {
+ type: :string,
+ required: true,
+ default: DEFAULT_TEMPLATE,
+ no_data_expression: true,
+ ui: {
+ control: :code,
+ },
+ control_options: {
+ height: 240,
+ lang: :text,
+ },
+ },
+ },
+ )
+
+ def execute(exec_ctx)
+ template =
+ exec_ctx.get_node_parameter(
+ "template",
+ 0,
+ default: DEFAULT_TEMPLATE,
+ options: {
+ raw_expressions: true,
+ },
+ ).to_s
+ mode = exec_ctx.get_node_parameter("mode", 0, default: RUN_ONCE_FOR_ALL_ITEMS)
+ validate_mode!(mode)
+
+ [
+ (
+ if mode == RUN_ONCE_FOR_ALL_ITEMS
+ render_all_items(template, exec_ctx)
+ else
+ render_each_item(template, exec_ctx)
+ end
+ ),
+ ]
+ end
+
+ private
+
+ def validate_mode!(mode)
+ return if [RUN_ONCE_FOR_ALL_ITEMS, RUN_ONCE_FOR_EACH_ITEM].include?(mode)
+
+ raise_node_error!(
+ "Invalid Template mode",
+ description: "#{mode.inspect} is not a supported Template execution mode.",
+ )
+ end
+
+ def render_all_items(template, exec_ctx)
+ context = base_template_context(exec_ctx)
+
+ [
+ wrap(
+ { "template" => render_template(template, context) },
+ paired_item: paired_items(exec_ctx),
+ ),
+ ]
+ end
+
+ def render_each_item(template, exec_ctx)
+ base_context = base_template_context(exec_ctx)
+
+ exec_ctx.input_items.map.with_index do |input_item, item_index|
+ wrap(
+ {
+ "template" =>
+ render_template(
+ template,
+ item_template_context(base_context, input_item, item_index),
+ ),
+ },
+ paired_item: exec_ctx.paired_item_for(input_item),
+ )
+ end
+ end
+
+ def render_template(template, context)
+ Mustache.render(template, context)
+ rescue Mustache::Parser::SyntaxError => e
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.template.invalid_template"),
+ description: e.message,
+ )
+ end
+
+ def base_template_context(exec_ctx)
+ {
+ "items" => input_items(exec_ctx),
+ "items_count" => exec_ctx.input_items.length,
+ "vars" => (exec_ctx.vars || {}).deep_stringify_keys,
+ "workflow" => workflow_context(exec_ctx),
+ "execution" => execution_context(exec_ctx),
+ "site_settings" => site_settings_context,
+ }
+ end
+
+ def item_template_context(base_context, input_item, item_index)
+ input_item
+ .fetch("json") { {} }
+ .deep_stringify_keys
+ .merge(base_context)
+ .merge("item" => input_item.deep_stringify_keys, "item_index" => item_index + 1)
+ end
+
+ def site_settings_context
+ settings =
+ SiteSetting
+ .all_settings
+ .each_with_object({}) do |setting, result|
+ name = setting[:setting].to_s
+ result[name] = setting[:secret] ? "[FILTERED]" : setting[:value]
+ end
+
+ SiteSetting.hidden_settings.each { |name| settings[name.to_s] ||= "[FILTERED]" }
+ SiteSetting.secret_settings.each { |name| settings[name.to_s] = "[FILTERED]" }
+
+ settings
+ end
+
+ def input_items(exec_ctx)
+ exec_ctx.input_items.map.with_index do |input_item, item_index|
+ input_item
+ .fetch("json") { {} }
+ .deep_stringify_keys
+ .merge("item" => input_item.deep_stringify_keys, "item_index" => item_index + 1)
+ end
+ end
+
+ def paired_items(exec_ctx)
+ exec_ctx.input_items.map { |item| exec_ctx.paired_item_for(item) }
+ end
+
+ def workflow_context(exec_ctx)
+ exec_ctx.get_workflow.to_h
+ end
+
+ def execution_context(exec_ctx)
+ workflow = workflow_context(exec_ctx)
+ {
+ "id" => exec_ctx.execution_id,
+ "workflow_id" => workflow["id"],
+ "workflow_name" => workflow["name"],
+ }.compact
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb
new file mode 100644
index 00000000000..e10f1e91d72
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb
@@ -0,0 +1,193 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::Template::V1 do
+ fab!(:admin)
+
+ fab!(:workflow) do
+ Fabricate(:discourse_workflows_workflow, name: "Template workflow", created_by: admin)
+ end
+
+ fab!(:execution) { Fabricate(:discourse_workflows_execution, workflow: workflow) }
+
+ def execute_template(
+ template = nil,
+ mode: nil,
+ input_items: [{ "json" => {} }],
+ vars: {},
+ workflow: nil,
+ execution_id: nil
+ )
+ parameters = template.nil? ? {} : { "template" => template }
+ parameters["mode"] = mode if mode
+ resolver_context = { "$json" => input_items.first&.dig("json") || {} }
+ sandbox = DiscourseWorkflows::JsSandbox.new(resolver_context, vars: vars)
+ resolver = DiscourseWorkflows::ExpressionResolver.new(resolver_context, sandbox: sandbox)
+ exec_ctx =
+ DiscourseWorkflows::Executor::NodeExecutionContext.new(
+ input_items: input_items,
+ parameters: parameters,
+ property_schema: described_class.property_schema,
+ resolver: resolver,
+ vars: vars,
+ workflow: workflow,
+ execution_id: execution_id,
+ )
+
+ described_class.new.execute(exec_ctx).first
+ ensure
+ resolver&.dispose
+ sandbox&.dispose
+ end
+
+ describe "#execute" do
+ it "renders a single output from all input items" do
+ result =
+ execute_template(
+ "{{#items}}{{name}} {{/items}}",
+ input_items: [{ "json" => { "name" => "Alice" } }, { "json" => { "name" => "Bob" } }],
+ )
+
+ expect(result.map { |item| item["json"] }).to eq([{ "template" => "Alice Bob " }])
+ end
+
+ it "renders one output per input item in each-item mode" do
+ SiteSetting.title = "Each Item Forum"
+
+ result =
+ execute_template(
+ "{{name}} {{item_index}}/{{items_count}} {{site_settings.title}}",
+ mode: "runOnceForEachItem",
+ input_items: [
+ { "json" => { "name" => "Alice", "item_index" => 99 } },
+ { "json" => { "name" => "Bob", "items_count" => 99 } },
+ ],
+ )
+
+ expect(result.map { |item| item["json"] }).to eq(
+ [
+ { "template" => "Alice 1/2 Each Item Forum" },
+ { "template" => "Bob 2/2 Each Item Forum" },
+ ],
+ )
+ expect(result.map { |item| item["pairedItem"] }).to eq([{ "item" => 0 }, { "item" => 1 }])
+ end
+
+ it "exposes workflow metadata and all input items" do
+ input_items = [
+ {
+ "json" => {
+ "name" => "Alice",
+ "vars" => {
+ "project" => "Ignored",
+ },
+ "item" => {
+ "json" => {
+ "name" => "Ignored",
+ },
+ },
+ "item_index" => 99,
+ },
+ },
+ { "json" => { "name" => "Bob" } },
+ ]
+
+ result =
+ execute_template(
+ "{{#items}}{{name}} {{/items}}" \
+ "count={{items_count}} var={{vars.project}} " \
+ "workflow={{workflow.name}} execution={{execution.id}} " \
+ "{{#items}}item={{item.json.name}} index={{item_index}} {{/items}}",
+ input_items: input_items,
+ vars: {
+ "project" => "Workflows",
+ },
+ workflow: workflow,
+ execution_id: execution.id,
+ )
+
+ expect(result.first["json"]).to eq(
+ "template" =>
+ "Alice Bob count=2 var=Workflows workflow=Template workflow execution=#{execution.id} " \
+ "item=Alice index=1 item=Bob index=2 ",
+ )
+ end
+
+ it "exposes site settings and filters private settings" do
+ SiteSetting.title = "Mustache Forum"
+
+ result =
+ execute_template(
+ "title={{site_settings.title}} " \
+ "secret={{site_settings.discourse_connect_secret}} " \
+ "hidden={{site_settings.vapid_public_key}}",
+ )
+
+ expect(result.first["json"]).to eq(
+ "template" => "title=Mustache Forum secret=[FILTERED] hidden=[FILTERED]",
+ )
+ end
+
+ it "emits only the template field" do
+ result =
+ execute_template(
+ "{{#items}}{{name}}{{/items}}",
+ input_items: [{ "json" => { "name" => "Alice", "extra" => "ignored" } }],
+ )
+
+ expect(result.first["json"]).to eq("template" => "Alice")
+ end
+
+ it "links the output item to every source item" do
+ result =
+ execute_template(
+ "{{#items}}{{name}}{{/items}}",
+ input_items: [{ "json" => { "name" => "Alice" } }, { "json" => { "name" => "Bob" } }],
+ )
+
+ expect(result.first["pairedItem"]).to eq([{ "item" => 0 }, { "item" => 1 }])
+ end
+
+ it "renders missing variables as blank strings" do
+ result = execute_template("Hello {{missing}}")
+
+ expect(result.first["json"]).to eq("template" => "Hello ")
+ end
+
+ it "uses default Mustache escaping rules" do
+ result =
+ execute_template(
+ "{{#items}}{{value}} {{{value}}}{{/items}}",
+ input_items: [{ "json" => { "value" => "<b>bold</b>" } }],
+ )
+
+ expect(result.first["json"]).to eq("template" => "&lt;b&gt;bold&lt;/b&gt; <b>bold</b>")
+ end
+
+ it "defaults to a template showing item loop syntax" do
+ result =
+ execute_template(
+ input_items: [{ "json" => { "name" => "Alice" } }, { "json" => { "name" => "Bob" } }],
+ )
+
+ expect(result.first["json"]).to eq("template" => <<~TEXT)
+ Items:
+ - 1: Alice
+ - 2: Bob
+ TEXT
+ end
+
+ it "raises a node error for invalid Mustache syntax" do
+ expect { execute_template("{{#items}}") }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /Invalid Mustache template/,
+ )
+ end
+
+ it "raises a node error for invalid mode" do
+ expect { execute_template("Hello", mode: "invalid") }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /Invalid Template mode/,
+ )
+ end
+ end
+end