Add webhook templating (#23289)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>th-downstream
parent
a80efb449e
commit
4eda233e09
@ -0,0 +1,67 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Webhooks::PayloadRenderer
|
||||
class DocumentTraverser
|
||||
INT_REGEX = /[0-9]+/
|
||||
|
||||
def initialize(document)
|
||||
@document = document.with_indifferent_access
|
||||
end
|
||||
|
||||
def get(path)
|
||||
value = @document.dig(*parse_path(path))
|
||||
string = Oj.dump(value)
|
||||
|
||||
# We want to make sure people can use the variable inside
|
||||
# other strings, so it can't be wrapped in quotes.
|
||||
if value.is_a?(String)
|
||||
string[1...-1]
|
||||
else
|
||||
string
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_path(path)
|
||||
path.split('.').filter_map do |segment|
|
||||
if segment.match(INT_REGEX)
|
||||
segment.to_i
|
||||
else
|
||||
segment.presence
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class TemplateParser < Parslet::Parser
|
||||
rule(:dot) { str('.') }
|
||||
rule(:digit) { match('[0-9]') }
|
||||
rule(:property_name) { match('[a-z_]').repeat(1) }
|
||||
rule(:array_index) { digit.repeat(1) }
|
||||
rule(:segment) { (property_name | array_index) }
|
||||
rule(:path) { property_name >> (dot >> segment).repeat }
|
||||
rule(:variable) { (str('}}').absent? >> path).repeat.as(:variable) }
|
||||
rule(:expression) { str('{{') >> variable >> str('}}') }
|
||||
rule(:text) { (str('{{').absent? >> any).repeat(1) }
|
||||
rule(:text_with_expressions) { (text.as(:text) | expression).repeat.as(:text) }
|
||||
root(:text_with_expressions)
|
||||
end
|
||||
|
||||
EXPRESSION_REGEXP = /
|
||||
\{\{
|
||||
[a-z_]+
|
||||
(\.
|
||||
([a-z_]+|[0-9]+)
|
||||
)*
|
||||
\}\}
|
||||
/iox
|
||||
|
||||
def initialize(json)
|
||||
@document = DocumentTraverser.new(Oj.load(json))
|
||||
end
|
||||
|
||||
def render(template)
|
||||
template.gsub(EXPRESSION_REGEXP) { |match| @document.get(match[2...-2]) }
|
||||
end
|
||||
end
|
@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddTemplateToWebhooks < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :webhooks, :template, :text
|
||||
end
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe Webhooks::PayloadRenderer do
|
||||
subject(:renderer) { described_class.new(json) }
|
||||
|
||||
let(:event) { Webhooks::EventPresenter.new(type, object) }
|
||||
let(:payload) { ActiveModelSerializers::SerializableResource.new(event, serializer: REST::Admin::WebhookEventSerializer, scope: nil, scope_name: :current_user).as_json }
|
||||
let(:json) { Oj.dump(payload) }
|
||||
|
||||
describe '#render' do
|
||||
context 'when event is account.approved' do
|
||||
let(:type) { 'account.approved' }
|
||||
let(:object) { Fabricate(:account, display_name: 'Foo"') }
|
||||
|
||||
it 'renders event-related variables into template' do
|
||||
expect(renderer.render('foo={{event}}')).to eq 'foo=account.approved'
|
||||
end
|
||||
|
||||
it 'renders event-specific variables into template' do
|
||||
expect(renderer.render('foo={{object.username}}')).to eq "foo=#{object.username}"
|
||||
end
|
||||
|
||||
it 'escapes values for use in JSON' do
|
||||
expect(renderer.render('foo={{object.account.display_name}}')).to eq 'foo=Foo\\"'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue