Skip to content
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 26 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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 Aug 29, 2024
c5d7568
DEV: change how `excerpt` method works
Grubba27 Aug 29, 2024
3c237ec
feature: Add `send_chat_integration_message` scriptable
Grubba27 Aug 30, 2024
b316558
DEV: Add `get_channel_by_name` to every provider
Grubba27 Sep 3, 2024
846996d
DEV: Add `get_channel_name` to all providers
Grubba27 Sep 3, 2024
8c1513d
DEV: Add removal of old migration data
Grubba27 Sep 4, 2024
3d54fd1
DEV: solve review comments
Grubba27 Sep 4, 2024
6987ca3
DEV: update test locale strings
Grubba27 Sep 4, 2024
96b91bf
DEV: remove empty line to trigger lint
Grubba27 Sep 4, 2024
42e6830
DEV: lint applied
Grubba27 Sep 4, 2024
1df0946
DEV: Add tests for automation integration
Grubba27 Sep 5, 2024
f3eda71
DEV: add rails logger for when automatio error occurs
Grubba27 Sep 5, 2024
96f8da9
DEV: move migration to be SQL only
Grubba27 Sep 5, 2024
5144318
DEV: update migration with correct table names
Grubba27 Sep 5, 2024
b865868
DEV: Update migrate_tag_added_filter_to_all_providers to use smaller …
Grubba27 Sep 6, 2024
d5c0774
DEV: update comments in migration file
Grubba27 Sep 6, 2024
620a5a9
DEV: update indentation in client.en.yml
Grubba27 Sep 10, 2024
2e07334
DEV: update with review comments
Grubba27 Sep 10, 2024
e4d464e
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 Sep 10, 2024
86979fb
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 Sep 10, 2024
3d46798
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 Sep 10, 2024
94bdec9
Update spec/integration/automation_spec.rb
Grubba27 Sep 10, 2024
3ea5b6c
Update lib/discourse_chat_integration/chat_integration_reference_post.rb
Grubba27 Sep 10, 2024
4f681a8
DEV: update specs with review comments
Grubba27 Sep 10, 2024
fa2a8eb
DEV: update typos in tests
Grubba27 Sep 10, 2024
9a60a45
DEV: inlined functions for getting channel name for provider in migra…
Grubba27 Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,12 @@ en:
label: URL
channel:
label: Channel

send_chat_integration_message:
title: Send Chat-Integration message
fields:
channel_name:
label: Channel name
description: "You can find the channel name in the Chat Integration settings"
provider:
label: Provider
8 changes: 8 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ en:
scriptables:
send_slack_message:
title: Send Slack message
send_chat_integration_message:
title: Send Chat-Integration message
chat_integration:

all_categories: "(all categories)"
Expand All @@ -134,6 +136,12 @@ en:
group_mention_template: "mentions of: @%{name}"
group_message_template: "messages to: @%{name}"


topic_tag_changed:
added_and_removed: "Added %{added} and removed %{removed}"
added: "Added %{added}"
removed: "Removed %{removed}"

provider:

#######################################
Expand Down
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 db/migrate/20240903184807_migrate_tag_added_filter_to_all_providers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true
Copy link
Contributor

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

Copy link
Contributor

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.

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
Loading