-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: add chat integration reference post #216
Merged
Merged
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
af5e7e8
FEATURE: Add chat integration reference post
Grubba27 c5d7568
DEV: change how `excerpt` method works
Grubba27 3c237ec
feature: Add `send_chat_integration_message` scriptable
Grubba27 b316558
DEV: Add `get_channel_by_name` to every provider
Grubba27 846996d
DEV: Add `get_channel_name` to all providers
Grubba27 8c1513d
DEV: Add removal of old migration data
Grubba27 3d54fd1
DEV: solve review comments
Grubba27 6987ca3
DEV: update test locale strings
Grubba27 96b91bf
DEV: remove empty line to trigger lint
Grubba27 42e6830
DEV: lint applied
Grubba27 1df0946
DEV: Add tests for automation integration
Grubba27 f3eda71
DEV: add rails logger for when automatio error occurs
Grubba27 96f8da9
DEV: move migration to be SQL only
Grubba27 5144318
DEV: update migration with correct table names
Grubba27 b865868
DEV: Update migrate_tag_added_filter_to_all_providers to use smaller …
Grubba27 d5c0774
DEV: update comments in migration file
Grubba27 620a5a9
DEV: update indentation in client.en.yml
Grubba27 2e07334
DEV: update with review comments
Grubba27 e4d464e
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 86979fb
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 3d46798
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 94bdec9
Update spec/integration/automation_spec.rb
Grubba27 3ea5b6c
Update lib/discourse_chat_integration/chat_integration_reference_post.rb
Grubba27 4f681a8
DEV: update specs with review comments
Grubba27 fa2a8eb
DEV: update typos in tests
Grubba27 9a60a45
DEV: inlined functions for getting channel name for provider in migra…
Grubba27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 76 additions & 72 deletions
148
db/migrate/20240808175526_migrate_tag_added_from_filter_to_automation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,96 @@ | ||
# frozen_string_literal: true | ||
|
||
# The next migration file is a migration that migrates the tag_added filter to an automation. | ||
# this one uses ActiveRecord which is not recommended for migrations. | ||
|
||
class MigrateTagAddedFromFilterToAutomation < ActiveRecord::Migration[7.1] | ||
def up | ||
if defined?(DiscourseAutomation) && | ||
DiscourseChatIntegration::Channel.with_provider("slack").exists? | ||
begin | ||
DiscourseChatIntegration::Rule | ||
.where("value::json->>'filter'=?", "tag_added") | ||
.each do |rule| | ||
channel_id = rule.value["channel_id"] | ||
channel_name = | ||
DiscourseChatIntegration::Channel.find(channel_id).value["data"]["identifier"] # it _must_ have a channel_id | ||
# if defined?(DiscourseAutomation) && | ||
# DiscourseChatIntegration::Channel.with_provider("slack").exists? | ||
# begin | ||
# DiscourseChatIntegration::Rule | ||
# .where("value::json->>'filter'=?", "tag_added") | ||
# .each do |rule| | ||
# channel_id = rule.value["channel_id"] | ||
# channel_name = | ||
# DiscourseChatIntegration::Channel.find(channel_id).value["data"]["identifier"] # it _must_ have a channel_id | ||
|
||
category_id = rule.value["category_id"] | ||
tags = rule.value["tags"] | ||
# category_id = rule.value["category_id"] | ||
# tags = rule.value["tags"] | ||
|
||
automation = | ||
DiscourseAutomation::Automation.new( | ||
script: "send_slack_message", | ||
trigger: "topic_tags_changed", | ||
name: "When tags change in topic", | ||
enabled: true, | ||
last_updated_by_id: Discourse.system_user.id, | ||
) | ||
# automation = | ||
# DiscourseAutomation::Automation.new( | ||
# script: "send_slack_message", | ||
# trigger: "topic_tags_changed", | ||
# name: "When tags change in topic", | ||
# enabled: true, | ||
# last_updated_by_id: Discourse.system_user.id, | ||
# ) | ||
|
||
automation.save! | ||
# automation.save! | ||
|
||
# Triggers: | ||
# Watching categories | ||
# # Triggers: | ||
# # Watching categories | ||
|
||
metadata = (category_id ? { "value" => [category_id] } : {}) | ||
# metadata = (category_id ? { "value" => [category_id] } : {}) | ||
|
||
automation.upsert_field!( | ||
"watching_categories", | ||
"categories", | ||
metadata, | ||
target: "trigger", | ||
) | ||
# automation.upsert_field!( | ||
# "watching_categories", | ||
# "categories", | ||
# metadata, | ||
# target: "trigger", | ||
# ) | ||
|
||
# Watching tags | ||
# # Watching tags | ||
|
||
metadata = (tags ? { "value" => tags } : {}) | ||
# metadata = (tags ? { "value" => tags } : {}) | ||
|
||
automation.upsert_field!("watching_tags", "tags", metadata, target: "trigger") | ||
# automation.upsert_field!("watching_tags", "tags", metadata, target: "trigger") | ||
|
||
# Script options: | ||
# Message | ||
automation.upsert_field!( | ||
"message", | ||
"message", | ||
{ "value" => "${ADDED_AND_REMOVED}" }, | ||
target: "script", | ||
) | ||
# # Script options: | ||
# # Message | ||
# automation.upsert_field!( | ||
# "message", | ||
# "message", | ||
# { "value" => "${ADDED_AND_REMOVED}" }, | ||
# target: "script", | ||
# ) | ||
|
||
# URL | ||
automation.upsert_field!( | ||
"url", | ||
"text", | ||
{ "value" => Discourse.current_hostname }, | ||
target: "script", | ||
) | ||
# # URL | ||
# automation.upsert_field!( | ||
# "url", | ||
# "text", | ||
# { "value" => Discourse.current_hostname }, | ||
# target: "script", | ||
# ) | ||
|
||
# Channel | ||
automation.upsert_field!( | ||
"channel", | ||
"text", | ||
{ "value" => channel_name }, | ||
target: "script", | ||
) | ||
end | ||
rescue StandardError | ||
Rails.logger.warn("Failed to migrate tag_added rules to automations") | ||
end | ||
end | ||
# # Channel | ||
# automation.upsert_field!( | ||
# "channel", | ||
# "text", | ||
# { "value" => channel_name }, | ||
# target: "script", | ||
# ) | ||
# end | ||
# rescue StandardError | ||
# Rails.logger.warn("Failed to migrate tag_added rules to automations") | ||
# end | ||
# end | ||
end | ||
|
||
def down | ||
if defined?(DiscourseAutomation) && | ||
DiscourseChatIntegration::Channel.with_provider("slack").exists? | ||
DiscourseAutomation::Automation | ||
.where(script: "send_slack_message", trigger: "topic_tags_changed") | ||
.each do |automation| | ||
# if is the same name as created and message is the same | ||
if automation.name == "When tags change in topic" && | ||
automation.fields.where(name: "message").first.metadata["value"] == | ||
"${ADDED_AND_REMOVED}" | ||
automation.destroy! | ||
end | ||
end | ||
end | ||
# if defined?(DiscourseAutomation) && | ||
# DiscourseChatIntegration::Channel.with_provider("slack").exists? | ||
# DiscourseAutomation::Automation | ||
# .where(script: "send_slack_message", trigger: "topic_tags_changed") | ||
# .each do |automation| | ||
# # if is the same name as created and message is the same | ||
# if automation.name == "When tags change in topic" && | ||
# automation.fields.where(name: "message").first.metadata["value"] == | ||
# "${ADDED_AND_REMOVED}" | ||
# automation.destroy! | ||
# end | ||
# end | ||
# end | ||
end | ||
end |
141 changes: 141 additions & 0 deletions
141
db/migrate/20240903184807_migrate_tag_added_filter_to_all_providers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# frozen_string_literal: true | ||
class MigrateTagAddedFilterToAllProviders < ActiveRecord::Migration[7.1] | ||
def up | ||
if defined?(DiscourseAutomation) | ||
begin | ||
slack_usage_rows = DB.query <<~SQL | ||
SELECT plugin_store_rows.* FROM plugin_store_rows | ||
WHERE plugin_store_rows.type_name = 'JSON' | ||
AND plugin_store_rows.plugin_name = 'discourse-chat-integration' | ||
AND (key LIKE 'channel:%') | ||
AND (value::json->>'provider'='slack') | ||
SQL | ||
|
||
old_migration_delete = <<~SQL | ||
DELETE FROM discourse_automation_automations | ||
WHERE id IN ( | ||
SELECT a.id | ||
FROM discourse_automation_automations a | ||
JOIN discourse_automation_fields f ON f.automation_id = a.id | ||
WHERE a.script = 'send_slack_message' | ||
AND a.trigger = 'topic_tags_changed' | ||
AND a.name = 'When tags change in topic' | ||
AND f.name = 'message' | ||
AND f.metadata->>'value' = '${ADDED_AND_REMOVED}' | ||
) | ||
SQL | ||
# Trash old migration | ||
DB.exec old_migration_delete if slack_usage_rows.count > 0 | ||
|
||
rules_with_tag_added = <<~SQL | ||
SELECT value | ||
FROM plugin_store_rows | ||
WHERE plugin_name = 'discourse-chat-integration' | ||
AND key LIKE 'rule:%' | ||
AND value::json->>'filter' = 'tag_added' | ||
SQL | ||
|
||
channel_query = <<~SQL | ||
SELECT * | ||
FROM plugin_store_rows | ||
WHERE type_name = 'JSON' | ||
AND plugin_name = 'discourse-chat-integration' | ||
AND key LIKE 'channel:%' | ||
AND id = :channel_id | ||
LIMIT 1 | ||
SQL | ||
|
||
automation_creation = <<~SQL | ||
INSERT INTO discourse_automation_automations (script, trigger, name, enabled, last_updated_by_id, created_at, updated_at) | ||
VALUES ('send_chat_integration_message', 'topic_tags_changed', 'When tags change in topic', true, -1, NOW(), NOW()) | ||
RETURNING id | ||
SQL | ||
|
||
create_automation_field = <<~SQL | ||
INSERT INTO discourse_automation_fields (automation_id, name, component, metadata, target, created_at, updated_at) | ||
VALUES (:automation_id, :name, :component, :metadata, :target, NOW(), NOW()) | ||
SQL | ||
|
||
DB | ||
.query(rules_with_tag_added) | ||
.each do |row| | ||
rule = JSON.parse(row.value).with_indifferent_access | ||
|
||
channel = | ||
JSON.parse( | ||
DB.query(channel_query, channel_id: rule[:channel_id]).first.value, | ||
).with_indifferent_access | ||
|
||
provider_name = channel[:provider] | ||
provider = | ||
DiscourseChatIntegration::Provider | ||
.constants | ||
.select { |constant| constant.to_s =~ /Provider$/ } | ||
.map { |p| DiscourseChatIntegration::Provider.const_get(p) } | ||
.find { |p| p::PROVIDER_NAME == provider_name } | ||
|
||
channel_name = provider.get_channel_name(channel) | ||
category_id = rule[:category_id] | ||
tags = rule[:tags] | ||
|
||
automation_id = DB.query(automation_creation).first.id | ||
|
||
# Triggers: | ||
# Watching categories | ||
metadata = (category_id ? { "value" => [category_id] } : {}).to_json | ||
DB.exec( | ||
create_automation_field, | ||
automation_id: automation_id, | ||
name: "watching_categories", | ||
component: "categories", | ||
metadata: metadata, | ||
target: "trigger", | ||
) | ||
|
||
# Watching tags | ||
metadata = (tags.present? ? { "value" => tags } : {}).to_json | ||
DB.exec( | ||
create_automation_field, | ||
automation_id: automation_id, | ||
name: "watching_tags", | ||
component: "tags", | ||
metadata: metadata, | ||
target: "trigger", | ||
) | ||
|
||
# Script options: | ||
# Provider | ||
DB.exec( | ||
create_automation_field, | ||
automation_id: automation_id, | ||
name: "provider", | ||
component: "choices", | ||
metadata: { "value" => provider_name }.to_json, | ||
target: "script", | ||
) | ||
|
||
# Channel name | ||
DB.exec( | ||
create_automation_field, | ||
automation_id: automation_id, | ||
name: "channel_name", | ||
component: "text", | ||
metadata: { "value" => channel_name }.to_json, | ||
target: "script", | ||
) | ||
end | ||
rescue StandardError | ||
puts "Error migrating tag_added filters to all providers" | ||
end | ||
end | ||
end | ||
|
||
def down | ||
DB.exec <<~SQL if defined?(DiscourseAutomation) | ||
DELETE FROM discourse_automation_automations | ||
WHERE script = 'send_chat_integration_message' | ||
AND trigger = 'topic_tags_changed' | ||
AND name = 'When tags change in topic' | ||
SQL | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't have any rails class in a migration, it should be all sql, if we change these class, migrations are borked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ^. You'll have to inline whatever
DiscourseChatIntegration::Provider.constants
resolves to.