DEV: Add modifier for webhook event header generation (#27054)

This commit is contained in:
Mark VanLandingham 2024-05-17 09:33:39 -05:00 committed by GitHub
parent a388f41dd4
commit 9264479c27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -45,7 +45,15 @@ module Jobs
web_hook_body = build_webhook_body
web_hook_event = create_webhook_event(web_hook_body)
uri = URI(@web_hook.payload_url.strip)
web_hook_headers = build_webhook_headers(uri, web_hook_body, web_hook_event)
web_hook_headers =
DiscoursePluginRegistry.apply_modifier(
:web_hook_event_headers,
web_hook_headers,
web_hook_body,
web_hook_event,
)
emitter = WebHookEmitter.new(@web_hook, web_hook_event)
web_hook_response = emitter.emit!(headers: web_hook_headers, body: web_hook_body)

View File

@ -345,5 +345,37 @@ RSpec.describe Jobs::EmitWebHookEvent do
)
expect(event.payload).to eq(MultiJson.dump(ping: "OK"))
end
context "with `webhook_event_headers` modifier" do
let(:modifier_block) do
Proc.new do |headers, _, _|
headers["D-Test-Woo"] = "xyz"
headers
end
end
it "Allows for header modifications" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:web_hook_event_headers, &modifier_block)
stub_request(:post, post_hook.payload_url).to_return(body: "OK", status: 200)
topic_event_type = WebHookEventType.all.first
web_hook_id = Fabricate("#{topic_event_type.name.gsub("_created", "")}_web_hook").id
job.execute(
web_hook_id: web_hook_id,
event_type: topic_event_type.name,
payload: { test: "some payload" }.to_json,
)
webhook_event = WebHookEvent.last
expect(JSON.parse(webhook_event.headers)).to include("D-Test-Woo" => "xyz")
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:web_hook_event_headers,
&modifier_block
)
end
end
end
end