-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add script to post report results in a topic regularly (#328)
This script is similar to the existing one that schedules report results to to be sent to a PM on a regular basis, but instead takes a topic ID and posts to that. This way people can have report results sent to a public topic regularly too and not have to deal with PM recipients and so on.
- Loading branch information
1 parent
d306466
commit da1c99e
Showing
8 changed files
with
410 additions
and
54 deletions.
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
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
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
92 changes: 92 additions & 0 deletions
92
spec/automation/recurring_data_explorer_result_topic_spec.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,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe "RecurringDataExplorerResultTopic" do | ||
fab!(:admin) | ||
|
||
fab!(:user) | ||
fab!(:another_user) { Fabricate(:user) } | ||
fab!(:group_user) { Fabricate(:user) } | ||
fab!(:not_allowed_user) { Fabricate(:user) } | ||
fab!(:topic) | ||
|
||
fab!(:group) { Fabricate(:group, users: [user, another_user]) } | ||
fab!(:another_group) { Fabricate(:group, users: [group_user]) } | ||
|
||
fab!(:automation) do | ||
Fabricate(:automation, script: "recurring_data_explorer_result_topic", trigger: "recurring") | ||
end | ||
fab!(:query) { Fabricate(:query, sql: "SELECT 'testabcd' AS data") } | ||
fab!(:query_group) { Fabricate(:query_group, query: query, group: group) } | ||
fab!(:query_group) { Fabricate(:query_group, query: query, group: another_group) } | ||
|
||
before do | ||
SiteSetting.data_explorer_enabled = true | ||
SiteSetting.discourse_automation_enabled = true | ||
|
||
automation.upsert_field!("query_id", "choices", { value: query.id }) | ||
automation.upsert_field!("topic_id", "text", { value: topic.id }) | ||
automation.upsert_field!( | ||
"query_params", | ||
"key-value", | ||
{ value: [%w[from_days_ago 0], %w[duration_days 15]] }, | ||
) | ||
automation.upsert_field!( | ||
"recurrence", | ||
"period", | ||
{ value: { interval: 1, frequency: "day" } }, | ||
target: "trigger", | ||
) | ||
automation.upsert_field!("start_date", "date_time", { value: 2.minutes.ago }, target: "trigger") | ||
end | ||
|
||
context "when using recurring trigger" do | ||
it "sends the post at recurring date_date" do | ||
freeze_time 1.day.from_now do | ||
expect { Jobs::DiscourseAutomation::Tracker.new.execute }.to change { | ||
topic.reload.posts.count | ||
}.by(1) | ||
|
||
expect(topic.posts.last.raw).to eq( | ||
I18n.t( | ||
"data_explorer.report_generator.post.body", | ||
query_name: query.name, | ||
table: "| data |\n| :----- |\n| testabcd |\n", | ||
base_url: Discourse.base_url, | ||
query_id: query.id, | ||
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"), | ||
timezone: Time.zone.name, | ||
).chomp, | ||
) | ||
end | ||
end | ||
|
||
it "has appropriate content from the report generator" do | ||
freeze_time | ||
automation.update(last_updated_by_id: admin.id) | ||
automation.trigger! | ||
|
||
expect(topic.posts.last.raw).to eq( | ||
I18n.t( | ||
"data_explorer.report_generator.post.body", | ||
query_name: query.name, | ||
table: "| data |\n| :----- |\n| testabcd |\n", | ||
base_url: Discourse.base_url, | ||
query_id: query.id, | ||
created_at: Time.zone.now.strftime("%Y-%m-%d at %H:%M:%S"), | ||
timezone: Time.zone.name, | ||
).chomp, | ||
) | ||
end | ||
|
||
it "does not create the post if skip_empty" do | ||
query.update!(sql: "SELECT NULL LIMIT 0") | ||
automation.upsert_field!("skip_empty", "boolean", { value: true }) | ||
|
||
automation.update(last_updated_by_id: admin.id) | ||
|
||
expect { automation.trigger! }.to_not change { Post.count } | ||
end | ||
end | ||
end |
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
Oops, something went wrong.