Skip to content

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

DEV: Give error workflows the trigger data of the run that failed

Régis Hanola210094b28f8
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/error_workflow_trigger.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/error_workflow_trigger.rb
index 2b401e1c11c..4c128839df4 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/error_workflow_trigger.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/error_workflow_trigger.rb
@@ -47,6 +47,7 @@ module DiscourseWorkflows
def build_error_data(error)
{
+ "trigger" => failed_run_trigger_data,
"workflow" => {
"id" => @workflow.id.to_s,
"name" => @workflow.name,
@@ -62,6 +63,13 @@ module DiscourseWorkflows
}
end
+ # chained error workflows would otherwise nest the payload on every hop
+ def failed_run_trigger_data
+ return if @execution_mode == :error_mode
+
+ @execution&.trigger_data
+ end
+
def execution_url
return nil unless @execution
DiscourseWorkflows::Execution.admin_execution_url(@workflow.id, @execution.id)
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/error_workflow_trigger_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/error_workflow_trigger_spec.rb
index 5864506a12d..a38a15801cd 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/error_workflow_trigger_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/error_workflow_trigger_spec.rb
@@ -155,6 +155,29 @@ RSpec.describe DiscourseWorkflows::Executor::ErrorWorkflowTrigger do
)
end
+ it "passes the failed run's trigger data through so the error workflow can re-drive it" do
+ error_wf = build_error_workflow
+ workflow = Fabricate(:discourse_workflows_workflow, created_by: user, published: true)
+ workflow.update!(error_workflow_id: error_wf.id)
+ execution =
+ Fabricate(
+ :discourse_workflows_error_execution,
+ workflow: workflow,
+ trigger_data: {
+ "post" => {
+ "id" => 42,
+ },
+ },
+ )
+ handler = build_handler(workflow, build_steps, execution: execution)
+
+ handler.trigger_error_workflow(StandardError.new("boom"))
+
+ trigger_data =
+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.last["args"].first["trigger_data"]
+ expect(trigger_data["trigger"]).to eq("post" => { "id" => 42 })
+ end
+
it "exposes execution.error with null id/url when no execution record is available" do
error_wf = build_error_workflow
workflow = Fabricate(:discourse_workflows_workflow, created_by: user, published: true)