Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cdp): keep configured filters when resetting a function to its template (#85681)

Harley Alexander5ceb04cf23fd
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
index a50e5892fc01..f4be03f83b3a 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
@@ -6,7 +6,7 @@ import api from 'lib/api'
import { ApiError } from 'lib/api-error'
import { initKeaTests } from '~/test/init'
-import { HogFunctionTemplateType, HogFunctionType } from '~/types'
+import { CyclotronJobFiltersType, HogFunctionTemplateType, HogFunctionType } from '~/types'
import { hogFunctionConfigurationLogic } from './hogFunctionConfigurationLogic'
@@ -237,6 +237,49 @@ describe('hogFunctionConfigurationLogic', () => {
})
})
+ describe('resetting to template', () => {
+ const USER_FILTERS: CyclotronJobFiltersType = {
+ events: [{ id: '$pageview', name: '$pageview', type: 'events', order: 0 }],
+ filter_test_accounts: false,
+ }
+ const TEMPLATE_DEFAULT_FILTERS: CyclotronJobFiltersType = {
+ events: [],
+ actions: [],
+ filter_test_accounts: true,
+ }
+ const TEMPLATE_WITH_DEFAULT_FILTERS: HogFunctionTemplateType = {
+ ...HOG_TEMPLATE,
+ code: `${HOG_TEMPLATE.code}\n// updated`,
+ filters: TEMPLATE_DEFAULT_FILTERS,
+ }
+
+ beforeEach(() => {
+ initKeaTests()
+ mockApi.getTemplate.mockResolvedValue(TEMPLATE_WITH_DEFAULT_FILTERS)
+ })
+
+ it.each([
+ ['keeps the configured filters over the template defaults', USER_FILTERS, USER_FILTERS],
+ ['falls back to the template defaults when none are configured', null, TEMPLATE_DEFAULT_FILTERS],
+ ])('%s', async (_name, functionFilters, expectedFilters) => {
+ mockApi.get.mockResolvedValue({
+ ...HOG_FUNCTION,
+ filters: functionFilters,
+ template: TEMPLATE_WITH_DEFAULT_FILTERS,
+ })
+ logic = hogFunctionConfigurationLogic({ id: HOG_FUNCTION.id })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadHogFunctionSuccess'])
+
+ await expectLogic(logic, () => {
+ logic.actions.resetToTemplate()
+ }).toDispatchActions(['setConfigurationValues'])
+
+ expect(logic.values.configuration.filters).toEqual(expectedFilters)
+ expect(logic.values.configuration.hog).toEqual(TEMPLATE_WITH_DEFAULT_FILTERS.code)
+ })
+ })
+
describe('loading a missing function', () => {
beforeEach(() => {
initKeaTests()
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
index ab66f66098b4..9df56a2d8af9 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
@@ -2069,7 +2069,7 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
actions.setConfigurationValues({
...config,
enabled: values.configuration.enabled,
- filters: config.filters ?? values.configuration.filters,
+ filters: values.configuration.filters ?? config.filters,
// NOTE: Technically mapping should also be sanitized against the template mappings but this is a bit of a pain
mappings: values.configuration.mappings?.length ? values.configuration.mappings : config.mappings,
// Keep some existing things when manually resetting the template