FEATURE: Trigger webhook when assigning and unassigning topics (#61)

* FEATURE: Trigger webhook when assigning and unassigning topics

This PR creates a custom webhook event that you can now select when
creating a webhook to trigger only when a topic has been assigned or
unassigned.

* removed unused file

* Removed functionality that was added to core

This PR into discourse core:

https://github.com/discourse/discourse/pull/9110

adds what was removed in this commit.

It is better to have this logic in core so that it is discoverable and
future webhooks won't end up accidentally using the same ID.
This commit is contained in:
Blake Erickson 2020-03-06 11:57:46 -07:00 committed by GitHub
parent 2adcd9a832
commit 32653be260
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 0 deletions

View File

@ -45,3 +45,8 @@ en:
messages:
assigned_title: "Assigned (%{count})"
assigned: "Assigned"
admin:
web_hooks:
assign_event:
name: "Assign Event"
details: "When a user assigns or unassigns a topic."

View File

@ -248,7 +248,23 @@ class ::TopicAssigner
)
end
# Create a webhook event
if WebHook.active_web_hooks(:assign).exists?
type = :assigned
payload = {
type: type,
topic_id: @topic.id,
topic_title: @topic.title,
assigned_to_id: assign_to.id,
assigned_to_username: assign_to.username,
assigned_by_id: @assigned_by.id,
assigned_by_username: @assigned_by.username
}.to_json
WebHook.enqueue_assign_hooks(type, payload)
end
{ success: true }
end
def unassign(silent: false)
@ -328,6 +344,21 @@ class ::TopicAssigner
action_code: "unassigned"
)
end
# Create a webhook event
if WebHook.active_web_hooks(:assign).exists?
type = :unassigned
payload = {
type: type,
topic_id: @topic.id,
topic_title: @topic.title,
unassigned_to_id: assigned_user.id,
unassigned_to_username: assigned_user.username,
unassigned_by_id: @assigned_by.id,
unassigned_by_username: @assigned_by.username
}.to_json
WebHook.enqueue_assign_hooks(type, payload)
end
end
end

View File

@ -334,4 +334,15 @@ after_initialize do
assigner.unassign(silent: true)
end
end
class ::WebHook
def self.enqueue_assign_hooks(event, payload)
if active_web_hooks('assign').exists?
WebHook.enqueue_hooks(:assign, event,
payload: payload
)
end
end
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
Fabricator(:assign_web_hook, from: :web_hook) do
transient assign_hook: WebHookEventType.find_by(name: 'assign')
after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:assign_hook]]
end
end

View File

@ -2,6 +2,7 @@
require 'rails_helper'
require_relative '../support/assign_allowed_group'
require_relative '../fabricators/assign_hook_fabricator.rb'
describe 'integration tests' do
before do
@ -92,5 +93,22 @@ describe 'integration tests' do
DiscourseEvent.trigger(:assign_topic, topic, user2, admin, true)
expect(topic.reload.custom_fields[TopicAssigner::ASSIGNED_TO_ID].to_i).to eq(user2.id)
end
it "triggers a webhook for assigned and unassigned" do
Fabricate(:assign_web_hook)
DiscourseEvent.trigger(:assign_topic, topic, user2, admin, true)
job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first
expect(job_args["event_name"]).to eq("assigned")
payload = JSON.parse(job_args["payload"])
expect(payload["topic_id"]).to eq(topic.id)
expect(payload["assigned_to_id"]).to eq(user2.id)
DiscourseEvent.trigger(:unassign_topic, topic, admin)
job_args = Jobs::EmitWebHookEvent.jobs[1]["args"].first
expect(job_args["event_name"]).to eq("unassigned")
payload = JSON.parse(job_args["payload"])
expect(payload["topic_id"]).to eq(topic.id)
expect(payload["unassigned_to_id"]).to eq(user2.id)
end
end
end