A platform for community discussion. Free, open, simple.
FEATURE: Card moved trigger node for workflows (#43678)
Martin Brennan7836f8f53b18
diff --git a/plugins/boards/app/controllers/boards/api/cards_controller.rb b/plugins/boards/app/controllers/boards/api/cards_controller.rbindex 391e3446044..d3bdcdeb22d 100644--- a/plugins/boards/app/controllers/boards/api/cards_controller.rb+++ b/plugins/boards/app/controllers/boards/api/cards_controller.rb@@ -79,6 +79,8 @@ module BoardsPublisher.publish_card_moved!(board,publish_response,+ original_column_id,+ acting_user: guardian.user,client_id: message_bus_client_id,)elsediff --git a/plugins/boards/app/models/boards/card.rb b/plugins/boards/app/models/boards/card.rbindex 04709b078cb..c37804eadd8 100644--- a/plugins/boards/app/models/boards/card.rb+++ b/plugins/boards/app/models/boards/card.rb@@ -114,12 +114,8 @@ module Boardstopic&.titleend- def unicode_resolved_title- Emoji.gsub_emoji_to_unicode(resolved_title)- end-def unicode_title- Emoji.gsub_emoji_to_unicode(title)+ Emoji.gsub_emoji_to_unicode(resolved_title)endprivatediff --git a/plugins/boards/app/serializers/boards/card_serializer.rb b/plugins/boards/app/serializers/boards/card_serializer.rbindex ba3229923f4..8e33460cf1c 100644--- a/plugins/boards/app/serializers/boards/card_serializer.rb+++ b/plugins/boards/app/serializers/boards/card_serializer.rb@@ -32,6 +32,10 @@ module Boardsobject.topic? && object.topic.present? && topic_visible?end+ def unicode_title+ object.unicode_title if !object.topic? || topic_visible?+ end+def include_created_at?!object.topic?enddiff --git a/plugins/boards/app/services/boards/move_topic_to_column.rb b/plugins/boards/app/services/boards/move_topic_to_column.rbindex 96722ec004d..42837bd0254 100644--- a/plugins/boards/app/services/boards/move_topic_to_column.rb+++ b/plugins/boards/app/services/boards/move_topic_to_column.rb@@ -95,9 +95,14 @@ module BoardsPublisher.publish_card_created!(board, payload, client_id: params.client_id)end- def publish_card_moved(board:, card:, params:)+ def publish_card_moved(board:, card:, params:, guardian:)payload = CardSerializer.new(card, root: false).as_json- Publisher.publish_card_moved!(board, payload, client_id: params.client_id)+ Publisher.publish_card_moved!(+ board,+ payload,+ acting_user: guardian.user,+ client_id: params.client_id,+ )endendenddiff --git a/plugins/boards/app/services/boards/publisher.rb b/plugins/boards/app/services/boards/publisher.rbindex d57698dd8b9..6cd188c4e02 100644--- a/plugins/boards/app/services/boards/publisher.rb+++ b/plugins/boards/app/services/boards/publisher.rb@@ -12,7 +12,17 @@ module Boardspublish_card_event!(board, "card_updated", card_payload, client_id:)end- def self.publish_card_moved!(board, card_payload, client_id:)+ def self.publish_card_moved!(board, card_payload, old_column_id = nil, acting_user:, client_id:)+ new_column_id = card_payload.with_indifferent_access[:column_id]+ old_column_id ||= new_column_id+ if old_column_id.to_i != new_column_id.to_i+ DiscourseEvent.trigger(+ :boards_card_moved,+ board,+ card_payload.merge(old_column_id:),+ acting_user,+ )+ endpublish_card_event!(board, "card_moved", card_payload, client_id:)enddiff --git a/plugins/boards/config/locales/client.en.yml b/plugins/boards/config/locales/client.en.ymlindex bf62689f565..325c6f52501 100644--- a/plugins/boards/config/locales/client.en.yml+++ b/plugins/boards/config/locales/client.en.yml@@ -6,9 +6,11 @@ en:nodes:"action:create_board_column": "Create board column""action:create_board": "Create board"+ "trigger:card_moved": "Card moved"node_descriptions:"action:create_board_column": "Create a column on a board with an optional icon, color, tag, and default sort""action:create_board": "Create a board with access permissions and optional tag and category constraints"+ "trigger:card_moved": "Fires when a card is moved from one column to another on a board"create_board_column:board_id: "Board"board_id_description: "Select a board or use a board ID from a previous step."@@ -30,6 +32,10 @@ en:actor_username: "Performed by user"acl: "Board access"acl_description: "Choose who can view, edit, and manage the board."+ card_moved:+ board_id: "Board"+ board_id_description: "Choose a board to limit this trigger, or leave blank for all boards."+ board_id_placeholder: "All boards"boards:filter_boards: "Filter boards"filter_boards_no_results: "No boards match your search"diff --git a/plugins/boards/lib/boards/inline_onebox_handler.rb b/plugins/boards/lib/boards/inline_onebox_handler.rbindex b3db4e2cf46..1339499ad03 100644--- a/plugins/boards/lib/boards/inline_onebox_handler.rb+++ b/plugins/boards/lib/boards/inline_onebox_handler.rb@@ -30,7 +30,7 @@ module Boardstitle =I18n.t("boards.onebox.inline_to_card",- card_name: card.unicode_resolved_title,+ card_name: card.unicode_title,board_name: board.unicode_name,)diff --git a/plugins/boards/lib/boards/onebox_handler.rb b/plugins/boards/lib/boards/onebox_handler.rbindex 0a71eac5b7f..dd6912f8a69 100644--- a/plugins/boards/lib/boards/onebox_handler.rb+++ b/plugins/boards/lib/boards/onebox_handler.rb@@ -43,7 +43,7 @@ module Boardsboard_archived: card.board.archived?,archived_label: I18n.t("boards.onebox.archived"),card_url: url,- card_name: card.unicode_resolved_title,+ card_name: card.unicode_title,card_tags: tag_html,card_column_title: card.column.unicode_title,card_column_icon: card.column.icon,diff --git a/plugins/boards/lib/discourse_workflows/nodes/boards_schema.rb b/plugins/boards/lib/discourse_workflows/nodes/boards_schema.rbnew file mode 100644index 00000000000..d1616d27c00--- /dev/null+++ b/plugins/boards/lib/discourse_workflows/nodes/boards_schema.rb@@ -0,0 +1,70 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module BoardsSchema+ CARD_PROPERTIES = JSON.parse(<<~JSON).freeze+ {+ "id": { "type": "integer" },+ "title": { "type": "string" },+ "unicode_title": { "type": "string" },+ "board_id": { "type": "integer" },+ "column_id": { "type": "integer" },+ "assigned_to_id": { "type": ["integer", "null"] },+ "topic_id": { "type": ["integer", "null"] }+ }+ JSON++ COLUMN_PROPERTIES = JSON.parse(<<~JSON).freeze+ {+ "id": { "type": "integer" },+ "position": { "type": "integer" },+ "title": { "type": "string" },+ "unicode_title": { "type": "string" },+ "icon": { "type": ["string", "null"] },+ "color": { "type": ["string", "null"] },+ "board_id": { "type": "integer" },+ "move_to_category_id": { "type": ["integer", "null"] },+ "tag_id": { "type": ["integer", "null"] }+ }+ JSON++ CARD_MOVED_PROPERTIES = {+ "old_column" => {+ "type" => %w[object null],+ "properties" => COLUMN_PROPERTIES,+ },+ "new_column" => {+ "type" => "object",+ "properties" => COLUMN_PROPERTIES,+ },+ }.freeze++ CARD_MOVED_SCHEMA =+ DiscourseWorkflows::Schema.entity(+ "card_moved",+ CARD_MOVED_PROPERTIES,+ "Card moved event details",+ )++ CARD_SCHEMA =+ DiscourseWorkflows::Schema.entity(+ "card",+ CARD_PROPERTIES,+ "Card, with board and column details",+ )++ ACTING_USER_SCHEMA =+ DiscourseWorkflows::Schema.entity(+ "acting_user",+ DiscourseWorkflows::Schema::BASIC_USER_PROPERTIES,+ "User who moved the card",+ )++ CARD_MOVED_OUTPUT_SCHEMA =+ DiscourseWorkflows::Schema.merge(CARD_SCHEMA, CARD_MOVED_SCHEMA, ACTING_USER_SCHEMA)+ end+ end+ end+enddiff --git a/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb b/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rbnew file mode 100644index 00000000000..a1ebcb5e48b--- /dev/null+++ b/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb@@ -0,0 +1,109 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module CardMoved+ class V1 < DiscourseWorkflows::NodeType+ description(+ name: "trigger:card_moved",+ version: "1.0",+ defaults: {+ icon: "arrow-right",+ color: "light-green",+ },+ group: "discourse_triggers",+ event: :boards_card_moved,+ available: -> { SiteSetting.boards_enabled },+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_boards",+ output_contracts: [{ schema: BoardsSchema::CARD_MOVED_OUTPUT_SCHEMA }],+ capabilities: {+ provides_current_user: true,+ },+ properties: {+ board_id: {+ type: :integer,+ required: false,+ type_options: {+ load_options_method: "boards",+ },+ no_data_expression: true,+ ui: {+ control: :combo_box,+ },+ control_options: {+ filterable: true,+ value_property: :id,+ name_property: :name,+ none: "discourse_workflows.card_moved.board_id_placeholder",+ },+ },+ },+ )++ def initialize(board, card_payload, acting_user, *)+ super(parameters: {})+ card_payload = card_payload.with_indifferent_access+ @card = board.cards.find(card_payload[:id])+ @old_column = board.columns.find_by(id: card_payload[:old_column_id])+ @new_column = board.columns.find(card_payload[:column_id])+ @acting_user = acting_user+ end++ def self.load_options_context(context)+ return unless context.method_name == "boards"++ guardian = context.guardian+ return [] unless guardian.can_manage_boards?++ boards = Boards::Board.with_acl_permission(guardian, "manage")+ if context.filter.present?+ boards =+ boards.where(+ "name ILIKE ?",+ "%#{ActiveRecord::Base.sanitize_sql_like(context.filter)}%",+ )+ end+ boards+ .order(:name, :id)+ .select(:id, :name)+ .map { |board| { id: board.id, name: board.unicode_name } }+ end++ def matches?(trigger_ctx)+ board_id = trigger_ctx.get_node_parameter("board_id").presence+ board_id.blank? || board_id.to_i == @card.board_id+ end++ def user_id+ @acting_user.id+ end++ def output+ {+ card:+ @card.as_json(+ only: BoardsSchema::CARD_PROPERTIES.keys - ["unicode_title"],+ methods: [:unicode_title],+ ),+ card_moved: {+ old_column: column_data(@old_column),+ new_column: column_data(@new_column),+ },+ acting_user: serialize_record(@acting_user, BasicUserSerializer),+ }+ end++ private++ def column_data(column)+ column&.as_json(+ only: BoardsSchema::COLUMN_PROPERTIES.keys - ["unicode_title"],+ methods: [:unicode_title],+ )+ end+ end+ end+ end+ end+enddiff --git a/plugins/boards/plugin.rb b/plugins/boards/plugin.rbindex a0d4d1500bc..c77ac72685e 100644--- a/plugins/boards/plugin.rb+++ b/plugins/boards/plugin.rb@@ -29,7 +29,12 @@ after_initialize doregister_discourse_workflows_node dorequire_relative "lib/discourse_workflows/nodes/create_board/v1"require_relative "lib/discourse_workflows/nodes/create_board_column/v1"- [DiscourseWorkflows::Nodes::CreateBoard::V1, DiscourseWorkflows::Nodes::CreateBoardColumn::V1]+ require_relative "lib/discourse_workflows/nodes/card_moved/v1"+ [+ DiscourseWorkflows::Nodes::CreateBoard::V1,+ DiscourseWorkflows::Nodes::CreateBoardColumn::V1,+ DiscourseWorkflows::Nodes::CardMoved::V1,+ ]endenddiff --git a/plugins/boards/spec/lib/discourse_workflows/nodes/card_moved/v1_spec.rb b/plugins/boards/spec/lib/discourse_workflows/nodes/card_moved/v1_spec.rbnew file mode 100644index 00000000000..dadd66a0a3c--- /dev/null+++ b/plugins/boards/spec/lib/discourse_workflows/nodes/card_moved/v1_spec.rb@@ -0,0 +1,122 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::CardMoved::V1 do+ fab!(:admin)+ fab!(:manager, :user)+ fab!(:manage_group, :group)+ fab!(:board) do+ Fabricate(:boards_board, name: "Project board", additional_manage_groups: [manage_group])+ end+ fab!(:old_column) { Fabricate(:boards_column, board:, title: "Backlog") }+ fab!(:new_column) { Fabricate(:boards_column, board:, title: "Done") }+ fab!(:card) { Fabricate(:boards_card, board:, column: new_column) }++ before do+ SiteSetting.enable_discourse_workflows = true+ SiteSetting.boards_enabled = true+ SiteSetting.boards_manage_board_allowed_groups = manage_group.id.to_s+ manage_group.add(manager)+ end++ it "registers an available card moved trigger with an optional board selector" do+ expect(described_class.identifier).to eq("trigger:card_moved")+ expect(described_class.description).to include(+ version: "1.0",+ group: "discourse_triggers",+ event: :boards_card_moved,+ defaults: {+ icon: "arrow-right",+ color: "light-green",+ },+ capabilities: {+ provides_current_user: true,+ },+ )+ expect(DiscourseWorkflows::PropertySchemaValidator.validate_node(described_class)).to be_empty+ expect(described_class.property_schema[:board_id]).to include(+ type: :integer,+ required: false,+ type_options: {+ load_options_method: "boards",+ },+ no_data_expression: true,+ )+ expect(described_class.property_schema[:board_id][:ui]).to include(control: :combo_box)++ expect(described_class).to be_available+ SiteSetting.boards_enabled = false+ expect(described_class).not_to be_available+ end++ describe ".load_options_context" do+ def load_options(user: manager, filter: nil)+ context =+ DiscourseWorkflows::LoadOptionsContext.new(+ method_name: "boards",+ user:,+ guardian: user.guardian,+ filter:,+ node_class: described_class,+ )+ described_class.load_options_context(context)+ end++ it "lists and filters only boards the configuring user can manage" do+ other_board = Fabricate(:boards_board, name: "Other board")+ managed_board =+ Fabricate(:boards_board, name: "Project archive", additional_manage_groups: [manage_group])++ expect(load_options).to contain_exactly(+ { id: board.id, name: board.name },+ { id: managed_board.id, name: managed_board.name },+ )+ expect(load_options(filter: "PROJECT")).to contain_exactly(+ { id: board.id, name: board.name },+ { id: managed_board.id, name: managed_board.name },+ )+ expect(load_options(user: admin)).to contain_exactly(+ { id: board.id, name: board.name },+ { id: managed_board.id, name: managed_board.name },+ { id: other_board.id, name: other_board.name },+ )+ end+ end++ describe "#matches?" do+ subject(:trigger) do+ described_class.new(+ board,+ { id: card.id, old_column_id: old_column.id, column_id: new_column.id },+ manager,+ )+ end++ def trigger_context(board_id = nil)+ DiscourseWorkflows::TriggerNodeContext.new(+ "parameters" => board_id.nil? ? {} : { "board_id" => board_id },+ )+ end++ it "matches every board when no scope is configured" do+ expect(trigger.matches?(trigger_context)).to eq(true)+ end++ it "matches only the configured board" do+ expect(trigger.matches?(trigger_context(board.id))).to eq(true)+ expect(trigger.matches?(trigger_context(Fabricate(:boards_board).id))).to eq(false)+ end++ it "returns card and column data that conforms to the output schema" do+ expect(trigger.output).to match_node_output_schema(described_class)+ expect(trigger.output).to include(+ card: include("id" => card.id, "board_id" => board.id, "column_id" => new_column.id),+ card_moved: {+ old_column: include("id" => old_column.id, "board_id" => board.id),+ new_column: include("id" => new_column.id, "board_id" => board.id),+ },+ acting_user: include(id: manager.id, username: manager.username),+ )+ expect(trigger.user_id).to eq(manager.id)+ end+ end+enddiff --git a/plugins/boards/spec/models/boards/card_spec.rb b/plugins/boards/spec/models/boards/card_spec.rbindex 458f8462d44..f2abcd5fb66 100644--- a/plugins/boards/spec/models/boards/card_spec.rb+++ b/plugins/boards/spec/models/boards/card_spec.rb@@ -313,4 +313,19 @@ RSpec.describe Boards::Card doexpect(card.reload.recency_at.to_i).to eq(updated_at.to_i)endend++ describe "#unicode_title" do+ it "uses the card title for floater cards" do+ card = Fabricate(:boards_card, board:, column:, title: "Launch :rocket:")++ expect(card.unicode_title).to eq("Launch π")+ end++ it "uses the resolved topic title for topic cards" do+ topic.update!(title: "Launch :rocket:")+ card = Fabricate(:boards_topic_card, board:, column:, topic:)++ expect(card.unicode_title).to eq("Launch π")+ end+ endenddiff --git a/plugins/boards/spec/serializers/boards/card_serializer_spec.rb b/plugins/boards/spec/serializers/boards/card_serializer_spec.rbindex 3abb61f29ae..5ab79bd2bfb 100644--- a/plugins/boards/spec/serializers/boards/card_serializer_spec.rb+++ b/plugins/boards/spec/serializers/boards/card_serializer_spec.rb@@ -26,14 +26,21 @@ RSpec.describe Boards::CardSerializer doexpect(payload).to include(topic_id: private_topic.id)expect(payload).not_to have_key(:topic)+ expect(payload[:unicode_title]).to be_nilendit "includes topic details when the scoped user can see the topic" doallowed_group.add(viewer)+ private_topic.update!(title: "Launch :rocket:")payload = described_class.new(card, root: false, scope: Guardian.new(viewer)).as_json- expect(payload[:topic]).to include(title: private_topic.title, slug: private_topic.slug)+ expect(payload).to include(unicode_title: "Launch π")+ expect(payload[:topic]).to include(+ title: private_topic.title,+ unicode_title: "Launch π",+ slug: private_topic.slug,+ )endit "serializes Unicode titles for floater cards" dodiff --git a/plugins/boards/spec/services/boards/move_topic_to_column_spec.rb b/plugins/boards/spec/services/boards/move_topic_to_column_spec.rbindex dcaf9d32deb..fed2bbcd466 100644--- a/plugins/boards/spec/services/boards/move_topic_to_column_spec.rb+++ b/plugins/boards/spec/services/boards/move_topic_to_column_spec.rb@@ -155,11 +155,17 @@ RSpec.describe Boards::MoveTopicToColumn doexpect(result[:card]).to have_attributes(id: existing_card.id, created_by_id: writer.id)end- it "publishes a card_moved event" do+ it "publishes a card_moved message" doexpect(messages).to contain_exactly(have_attributes(data: include(type: "card_moved", client_id: client_id)),)end++ it "does not publish a card moved workflow event for the reorder" do+ events = DiscourseEvent.track_events(:boards_card_moved) { result }++ expect(events).to be_empty+ endendcontext "when moving a topic to a column sorted by recency" dodiff --git a/plugins/boards/spec/services/boards/publisher_spec.rb b/plugins/boards/spec/services/boards/publisher_spec.rbindex d00b15c1c2c..ea888f1e461 100644--- a/plugins/boards/spec/services/boards/publisher_spec.rb+++ b/plugins/boards/spec/services/boards/publisher_spec.rb@@ -25,6 +25,7 @@ RSpec.describe Boards::Publisher doboardendfab!(:column) { board.columns.create!(title: "To Do", position: 0) }+ fab!(:old_column) { board.columns.create!(title: "Backlog", position: 1) }fab!(:card) doboard.cards.create!(card_type: :floater,@@ -117,15 +118,50 @@ RSpec.describe Boards::Publisher doenddescribe ".publish_card_moved!" do- it "publishes a card_moved message" do+ it "publishes a card_moved message and workflow event with the acting user" do+ events = nilmessages =MessageBus.track_publish(channel) do- described_class.publish_card_moved!(board, card_data, client_id: test_client_id)+ events =+ DiscourseEvent.track_events(:boards_card_moved) do+ described_class.publish_card_moved!(+ board,+ card_data,+ old_column.id,+ acting_user: admin,+ client_id: test_client_id,+ )+ endendexpect(messages.size).to eq(1)expect(messages.first.data[:type]).to eq("card_moved")expect(messages.first.data[:card]).to eq(card_data)+ expect(events).to contain_exactly(+ event_name: :boards_card_moved,+ params: [board, card_data.merge(old_column_id: old_column.id), admin],+ )+ end++ it "keeps the card_moved message but skips the workflow event when reordering" do+ events = nil+ messages =+ MessageBus.track_publish(channel) do+ events =+ DiscourseEvent.track_events(:boards_card_moved) do+ described_class.publish_card_moved!(+ board,+ card_data,+ acting_user: admin,+ client_id: test_client_id,+ )+ end+ end++ expect(messages).to contain_exactly(+ have_attributes(data: include(type: "card_moved", card: card_data)),+ )+ expect(events).to be_emptyendend@@ -182,7 +218,12 @@ RSpec.describe Boards::Publisher dosubject(:result) dodescribed_class.publish_card_created!(board, card_data, client_id: test_client_id)described_class.publish_card_updated!(board, card_data, client_id: test_client_id)- described_class.publish_card_moved!(board, card_data, client_id: test_client_id)+ described_class.publish_card_moved!(+ board,+ card_data,+ acting_user: admin,+ client_id: test_client_id,+ )described_class.publish_card_deleted!(board, card.id, client_id: test_client_id)described_class.publish_column_cleared!(board, column.id, client_id: test_client_id)described_class.publish_columns_reordered!(board, [column.id], client_id: test_client_id)