feat: Add support for labels in automations (#11658)

- Add support for using labels as an action event for automation
 - Fix duplicated conversation_updated event dispatch for labels
 

Fixes https://github.com/chatwoot/chatwoot/issues/8539 and multiple
issues around duplication related to label change events.
---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Vishnu Narayanan
2025-09-18 14:17:54 +05:30
committed by GitHub
parent 44dc9ba18e
commit 9527ff6269
13 changed files with 461 additions and 6 deletions

View File

@@ -118,6 +118,45 @@ RSpec.describe AutomationRules::ActionService do
end
end
describe '#perform with add_label action' do
before do
rule.actions << { action_name: 'add_label', action_params: %w[bug feature] }
rule.save
end
it 'will add labels to conversation' do
described_class.new(rule, account, conversation).perform
expect(conversation.reload.label_list).to include('bug', 'feature')
end
it 'will not duplicate existing labels' do
conversation.add_labels(['bug'])
described_class.new(rule, account, conversation).perform
expect(conversation.reload.label_list.count('bug')).to eq(1)
expect(conversation.reload.label_list).to include('feature')
end
end
describe '#perform with remove_label action' do
before do
conversation.add_labels(%w[bug feature support])
rule.actions << { action_name: 'remove_label', action_params: %w[bug feature] }
rule.save
end
it 'will remove specified labels from conversation' do
described_class.new(rule, account, conversation).perform
expect(conversation.reload.label_list).not_to include('bug', 'feature')
expect(conversation.reload.label_list).to include('support')
end
it 'will not fail if labels do not exist on conversation' do
conversation.update_labels(['support']) # Remove bug and feature first
expect { described_class.new(rule, account, conversation).perform }.not_to raise_error
expect(conversation.reload.label_list).to include('support')
end
end
describe '#perform with add_private_note action' do
let(:message_builder) { double }

View File

@@ -134,5 +134,86 @@ RSpec.describe AutomationRules::ConditionsFilterService do
end
end
end
context 'when conditions based on labels' do
before do
conversation.add_labels(['bug'])
end
context 'when filter_operator is equal_to' do
before do
rule.conditions = [
{ 'values': ['bug'], 'attribute_key': 'labels', 'query_operator': nil, 'filter_operator': 'equal_to' }
]
rule.save
end
it 'will return true when conversation has the label' do
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(true)
end
it 'will return false when conversation does not have the label' do
rule.conditions = [
{ 'values': ['feature'], 'attribute_key': 'labels', 'query_operator': nil, 'filter_operator': 'equal_to' }
]
rule.save
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(false)
end
end
context 'when filter_operator is not_equal_to' do
before do
rule.conditions = [
{ 'values': ['feature'], 'attribute_key': 'labels', 'query_operator': nil, 'filter_operator': 'not_equal_to' }
]
rule.save
end
it 'will return true when conversation does not have the label' do
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(true)
end
it 'will return false when conversation has the label' do
conversation.add_labels(['feature'])
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(false)
end
end
context 'when filter_operator is is_present' do
before do
rule.conditions = [
{ 'values': [], 'attribute_key': 'labels', 'query_operator': nil, 'filter_operator': 'is_present' }
]
rule.save
end
it 'will return true when conversation has any labels' do
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(true)
end
it 'will return false when conversation has no labels' do
conversation.update_labels([])
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(false)
end
end
context 'when filter_operator is is_not_present' do
before do
rule.conditions = [
{ 'values': [], 'attribute_key': 'labels', 'query_operator': nil, 'filter_operator': 'is_not_present' }
]
rule.save
end
it 'will return false when conversation has any labels' do
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(false)
end
it 'will return true when conversation has no labels' do
conversation.update_labels([])
expect(described_class.new(rule, conversation, { changed_attributes: {} }).perform).to be(true)
end
end
end
end
end