A platform for community discussion. Free, open, simple.
FEATURE: assigned trigger for workflows (#40831)
Joffrey JAFFEUXf34447f51f98
diff --git a/plugins/discourse-assign/config/locales/client.en.yml b/plugins/discourse-assign/config/locales/client.en.ymlindex e79346cbf97..132e5ee93b5 100644--- a/plugins/discourse-assign/config/locales/client.en.yml+++ b/plugins/discourse-assign/config/locales/client.en.yml@@ -204,8 +204,10 @@ en:requires_assign: "Requires the Assign plugin to be enabled"nodes:"action:assign_topic": "Assign topic"+ "trigger:assigned": "Assigned"node_descriptions:"action:assign_topic": "Assign or unassign a topic"+ "trigger:assigned": "Fires when a topic or post is assigned"assign_topic:operation: "Operation"operations:@@ -214,3 +216,6 @@ en:topic_id: "Topic ID"assignee: "Assignee"replace_existing: "Replace existing assignment"+ assigned:+ topic_assignments_only: "Only trigger when the assignment target is the topic, not an individual post"+ topic_assignments_only_description: "Topic assignments only"diff --git a/plugins/discourse-assign/lib/assigner.rb b/plugins/discourse-assign/lib/assigner.rbindex d596e79e104..03b641f5c2e 100644--- a/plugins/discourse-assign/lib/assigner.rb+++ b/plugins/discourse-assign/lib/assigner.rb@@ -384,6 +384,8 @@ class ::AssignerWebHook.enqueue_assign_hooks(assigned_to_type, payload.to_json)end+ DiscourseEvent.trigger(:assigned, assignment)+{ success: true }enddiff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rbnew file mode 100644index 00000000000..2364cd147a5--- /dev/null+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb@@ -0,0 +1,92 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module Assigned+ class V1 < DiscourseWorkflows::NodeType+ description(+ name: "trigger:assigned",+ version: "1.0",+ defaults: {+ icon: "user-plus",+ color: "cyan",+ },+ group: "discourse_triggers",+ events: [:assigned],+ available: -> { SiteSetting.assign_enabled },+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_assign",+ properties: {+ topic_assignments_only: {+ type: :boolean,+ default: false,+ ui: {+ control: :checkbox,+ },+ },+ },+ )++ def initialize(assignment, *)+ super(parameters: {})+ @assignment = assignment+ end++ def valid?+ @assignment.present? && @assignment.topic.present? &&+ @assignment.assigned_to.present? && post.present?+ end++ def output+ {+ assignment: assignment_data,+ post: serialize_post(post),+ topic: serialize_record(@assignment.topic, TopicListItemSerializer),+ }+ end++ def matches?(trigger_ctx)+ !trigger_ctx.get_node_parameter("topic_assignments_only", false) || topic_assignment?+ end++ private++ def assignment_data+ {+ id: @assignment.id,+ target_type: @assignment.target_type,+ target_id: @assignment.target_id,+ topic_id: @assignment.topic_id,+ topic_assignment: topic_assignment?,+ assigned_to_id: @assignment.assigned_to_id,+ assigned_to_type: @assignment.assigned_to_type,+ assigned_to: assignee_data(@assignment.assigned_to),+ assigned_by_user: serialize_record(@assignment.assigned_by_user, BasicUserSerializer),+ note: @assignment.note,+ status: @assignment.status,+ }+ end++ def assignee_data(assignee)+ case assignee+ when ::User+ { type: "user", user: serialize_record(assignee, BasicUserSerializer), group: {} }+ when ::Group+ { type: "group", user: {}, group: serialize_record(assignee, BasicGroupSerializer) }+ else+ { type: nil, user: {}, group: {} }+ end+ end++ def post+ @post ||= @assignment.post+ end++ def topic_assignment?+ @assignment.target_type == "Topic"+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-assign/plugin.rb b/plugins/discourse-assign/plugin.rbindex af708f5d05e..f43aa846c8f 100644--- a/plugins/discourse-assign/plugin.rb+++ b/plugins/discourse-assign/plugin.rb@@ -27,7 +27,13 @@ after_initialize doif respond_to?(:register_discourse_workflows_node)register_discourse_workflows_node dorequire_relative "lib/discourse_workflows/nodes/assign_topic/v1"- DiscourseWorkflows::Nodes::AssignTopic::V1+ require_relative "lib/discourse_workflows/nodes/assigned/v1"+ [DiscourseWorkflows::Nodes::AssignTopic::V1, DiscourseWorkflows::Nodes::Assigned::V1]+ end++ on(:assigned) do |assignment|+ require_relative "lib/discourse_workflows/nodes/assigned/v1"+ DiscourseWorkflows::EventListener.handle(DiscourseWorkflows::Nodes::Assigned::V1, assignment)endendUserUpdater::OPTION_ATTR.push(:notification_level_when_assigned)diff --git a/plugins/discourse-assign/spec/lib/assigner_spec.rb b/plugins/discourse-assign/spec/lib/assigner_spec.rbindex e6e905f0a41..65267b342c6 100644--- a/plugins/discourse-assign/spec/lib/assigner_spec.rb+++ b/plugins/discourse-assign/spec/lib/assigner_spec.rb@@ -148,6 +148,44 @@ RSpec.describe Assigner do)end+ it "triggers assigned event after assigning" do+ event = DiscourseEvent.track(:assigned) { assigner.assign(moderator) }++ assignment = event[:params].first+ expect(assignment).to eq(topic.assignment)+ expect(assignment.target).to eq(topic)+ end++ it "enqueues assigned workflows matching the assignment target" do+ SiteSetting.discourse_workflows_enabled = true+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.clear+ DiscourseWorkflows::Registry.reset_indexes!++ create_published_workflow("all-assignments")+ create_published_workflow("topic-assignments", "topic_assignments_only" => true)++ post_assignment_topic = Fabricate(:post).topic+ post_assignment_post = Fabricate(:post, topic: post_assignment_topic)++ described_class.new(post_assignment_post, moderator_2).assign(moderator)++ expect(enqueued_workflow_trigger_node_ids).to contain_exactly("all-assignments")++ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.clear++ described_class.new(topic, moderator_2).assign(moderator)++ expect(enqueued_workflow_trigger_node_ids).to contain_exactly(+ "all-assignments",+ "topic-assignments",+ )++ trigger_data =+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.last["args"].first["trigger_data"]+ expect(trigger_data["assignment"]["target_type"]).to eq("Topic")+ expect(trigger_data["post"]["id"]).to eq(topic.first_post.id)+ end+it "does not update notification level if already watching" doTopicUser.change(moderator.id,@@ -217,6 +255,37 @@ RSpec.describe Assigner doassigner.assign(assignee).fetch(:success)end+ def create_published_workflow(trigger_node_id, configuration = {})+ Fabricate(+ :discourse_workflows_workflow,+ created_by: moderator_2,+ published: true,+ nodes: [+ {+ "id" => trigger_node_id,+ "type" => "trigger:assigned",+ "typeVersion" => "1.0",+ "name" => trigger_node_id.humanize,+ "position" => {+ "x" => 0,+ "y" => 0,+ },+ "parameters" => configuration.deep_stringify_keys,+ "credentials" => {+ },+ },+ ],+ connections: {+ },+ )+ end++ def enqueued_workflow_trigger_node_ids+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.map do |job|+ job["args"].first["trigger_node_id"]+ end+ end+describe "forbidden reasons" doit "doesn't assign if the topic has more than 5 assignments" doother_post = nildiff --git a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rbnew file mode 100644index 00000000000..fd8cf89310a--- /dev/null+++ b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb@@ -0,0 +1,112 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::Assigned::V1 do+ fab!(:post)+ fab!(:topic) { post.topic }+ fab!(:assigned_by_user, :admin)+ fab!(:assignee, :moderator)+ fab!(:group)++ before do+ SiteSetting.assign_enabled = true+ SiteSetting.discourse_workflows_enabled = true+ end++ describe "#valid?" do+ it "returns true when an assignment is present" do+ assignment =+ Fabricate(+ :post_assignment,+ post:,+ assigned_to: assignee,+ assigned_by_user: assigned_by_user,+ )++ expect(described_class.new(assignment)).to be_valid+ end++ it "returns false when assignment is nil" do+ expect(described_class.new(nil)).not_to be_valid+ end+ end++ describe "#output" do+ it "returns assignment, post, topic, and assignee data for post assignments" do+ assignment =+ Fabricate(+ :post_assignment,+ post: post,+ assigned_to: assignee,+ assigned_by_user: assigned_by_user,+ note: "Please handle this",+ status: "In Progress",+ )++ output = described_class.new(assignment).output++ expect(output[:assignment]).to include(+ id: assignment.id,+ target_type: "Post",+ target_id: post.id,+ topic_id: topic.id,+ topic_assignment: false,+ assigned_to_id: assignee.id,+ assigned_to_type: "User",+ note: "Please handle this",+ status: "In Progress",+ )+ expect(output[:assignment][:assigned_to][:type]).to eq("user")+ expect(output[:assignment][:assigned_to][:user][:username]).to eq(assignee.username)+ expect(output[:assignment][:assigned_by_user][:username]).to eq(assigned_by_user.username)+ expect(output[:post][:id]).to eq(post.id)+ expect(output[:topic][:id]).to eq(topic.id)+ end++ it "uses the topic first post for topic assignments" do+ assignment =+ Fabricate(:topic_assignment, topic:, assigned_to: group, assigned_by_user: assigned_by_user)++ output = described_class.new(assignment).output++ expect(output[:assignment]).to include(+ target_type: "Topic",+ target_id: topic.id,+ topic_assignment: true,+ assigned_to_type: "Group",+ )+ expect(output[:assignment][:assigned_to][:type]).to eq("group")+ expect(output[:assignment][:assigned_to][:group][:name]).to eq(group.name)+ expect(output[:post][:id]).to eq(topic.first_post.id)+ end+ end++ describe "#matches?" do+ fab!(:post_assignment) do+ Fabricate(:post_assignment, post:, assigned_to: assignee, assigned_by_user: assigned_by_user)+ end+ fab!(:topic_assignment) do+ Fabricate(+ :topic_assignment,+ topic:,+ assigned_to: assignee,+ assigned_by_user: assigned_by_user,+ )+ end++ it "matches all assignments by default" do+ expect(described_class.new(post_assignment).matches?(trigger_context({}))).to eq(true)+ expect(described_class.new(topic_assignment).matches?(trigger_context({}))).to eq(true)+ end++ it "matches only topic assignments when configured" do+ context = trigger_context("topic_assignments_only" => true)++ expect(described_class.new(post_assignment).matches?(context)).to eq(false)+ expect(described_class.new(topic_assignment).matches?(context)).to eq(true)+ end+ end++ def trigger_context(parameters)+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })+ end+end