Skip to content

Commit

Permalink
Fix up tempplates
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Jan 2, 2025
1 parent 5a99c76 commit 11d4786
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
],
useMapping: [
(s) => [s.hogFunction, s.template],
(hogFunction, template) => (hogFunction ?? template)?.type === 'site_destination',
// If the function has mappings, or the template has mapping templates, we use mappings
(hogFunction, template) => Array.isArray(hogFunction?.mappings) || template?.mapping_templates?.length,
],
defaultFormState: [
(s) => [s.template, s.hogFunction],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useValues } from 'kea'
import { Group } from 'kea-forms'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { memo, useState } from 'react'
import { memo, useEffect, useState } from 'react'
import { ActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFilter'
import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/ActionFilterRow'

Expand Down Expand Up @@ -62,7 +62,9 @@ const MappingSummary = memo(function MappingSummary({
) : null}
</span>
<IconArrowRight className="text-muted-alt" />
<span>{humanize(firstInputValue)}</span>
<span>
{typeof firstInputValue === 'object' ? JSON.stringify(firstInputValue) : humanize(firstInputValue)}
</span>
<span className="flex-1" />
{mapping.disabled ? <LemonTag type="danger">Disabled</LemonTag> : null}
</span>
Expand Down Expand Up @@ -163,9 +165,16 @@ export function HogFunctionMapping({
}

export function HogFunctionMappings(): JSX.Element | null {
const { useMapping, mappingTemplates } = useValues(hogFunctionConfigurationLogic)
const { useMapping, mappingTemplates, configuration } = useValues(hogFunctionConfigurationLogic)
const [activeKeys, setActiveKeys] = useState<number[]>([])

// If there is only one mapping template, then we start it expanded
useEffect(() => {
if (configuration.mappings?.length === 1) {
setActiveKeys([0])
}
}, [configuration.mappings?.length])

if (!useMapping) {
return null
}
Expand Down
53 changes: 43 additions & 10 deletions posthog/cdp/templates/webhook/template_webhook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from posthog.cdp.templates.hog_function_template import SUB_TEMPLATE_COMMON, HogFunctionSubTemplate, HogFunctionTemplate
from posthog.cdp.templates.hog_function_template import (
SUB_TEMPLATE_COMMON,
HogFunctionMappingTemplate,
HogFunctionSubTemplate,
HogFunctionTemplate,
)


template: HogFunctionTemplate = HogFunctionTemplate(
Expand All @@ -10,8 +15,19 @@
icon_url="/static/posthog-icon.svg",
category=["Custom"],
hog="""
let headers := {}
for (let key, value in inputs.headers) {
headers[key] := value
}
if (inputs.additional_headers) {
for (let key, value in inputs.additional_headers) {
headers[key] := value
}
}
let payload := {
'headers': inputs.headers,
'headers': headers,
'body': inputs.body,
'method': inputs.method
}
Expand Down Expand Up @@ -64,14 +80,6 @@
"default": "POST",
"required": False,
},
{
"key": "body",
"type": "json",
"label": "JSON Body",
"default": {"event": "{event}", "person": "{person}"},
"secret": False,
"required": False,
},
{
"key": "headers",
"type": "dictionary",
Expand Down Expand Up @@ -108,4 +116,29 @@
type="internal_destination",
),
],
mapping_templates=[
HogFunctionMappingTemplate(
name="Webhook",
include_by_default=True,
filters={"events": [{"id": "$pageview", "name": "Pageview", "type": "events"}]},
inputs_schema=[
{
"key": "body",
"type": "json",
"label": "JSON Body",
"default": {"event": "{event}", "person": "{person}"},
"secret": False,
"required": False,
},
{
"key": "additional_headers",
"type": "dictionary",
"label": "Additional headers",
"secret": False,
"required": False,
"default": {},
},
],
),
],
)
23 changes: 23 additions & 0 deletions posthog/cdp/templates/webhook/test_template_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ def test_function_works(self):
)
assert self.get_mock_print_calls() == snapshot([])

def test_function_merges_headers(self):
self.run_function(
inputs={
"url": "https://posthog.com",
"method": "GET",
"headers": {"Content-Type": "application/json"},
"additional_headers": {"X-Custom-Header": "test"},
"body": {"hello": "world"},
"debug": False,
}
)

assert self.get_mock_fetch_calls()[0] == snapshot(
(
"https://posthog.com",
{
"headers": {"Content-Type": "application/json", "X-Custom-Header": "test"},
"body": {"hello": "world"},
"method": "GET",
},
)
)

def test_prints_when_debugging(self):
self.run_function(
inputs={
Expand Down

0 comments on commit 11d4786

Please sign in to comment.