Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Jan 28, 2025
1 parent be04729 commit a2d4450
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,49 @@ exports[`CdpCyclotronWorkerPlugins onEvent should handle and collect errors 3`]
},
]
`;

exports[`CdpCyclotronWorkerPlugins smoke tests should run the plugin: { name: 'customer-io', plugin: [Object] } 1`] = `
[
{
"level": "debug",
"message": "Executing plugin customer-io",
},
{
"level": "info",
"message": "Successfully authenticated with Customer.io. Completing setupPlugin.",
},
{
"level": "debug",
"message": "[email protected]",
},
{
"level": "debug",
"message": "{"status":{},"email":"test@posthog.com"}",
},
{
"level": "debug",
"message": "true",
},
{
"level": "debug",
"message": "Execution successful",
},
]
`;

exports[`CdpCyclotronWorkerPlugins smoke tests should run the plugin: { name: 'intercom', plugin: [Object] } 1`] = `
[
{
"level": "debug",
"message": "Executing plugin intercom",
},
{
"level": "info",
"message": "Contact [email protected] in Intercom not found",
},
{
"level": "debug",
"message": "Execution successful",
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { status } from '~/src/utils/status'

import { PLUGINS_BY_ID } from '../legacy-plugins'
import { LegacyPluginLogger, LegacyPluginMeta } from '../legacy-plugins/types'
import { sanitizeLogMessage } from '../services/hog-executor.service'
import { HogFunctionInvocation, HogFunctionInvocationResult, HogFunctionTypeType } from '../types'
import { CdpCyclotronWorker } from './cdp-cyclotron-worker.consumer'

Expand Down Expand Up @@ -74,7 +75,7 @@ export class CdpCyclotronWorkerPlugins extends CdpCyclotronWorker {
result.logs.push({
level,
timestamp: DateTime.now(),
message: args.join(' '),
message: sanitizeLogMessage(args),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ describe('CdpCyclotronWorkerPlugins', () => {

await processor.start()

processor.fetch = mockFetch = jest.fn(() => Promise.resolve({} as any))
processor.fetch = mockFetch = jest.fn(() =>
Promise.resolve({
status: 200,
json: () =>
Promise.resolve({
status: 200,
}),
} as any)
)

jest.spyOn(processor['cyclotronWorker']!, 'updateJob').mockImplementation(() => {})
jest.spyOn(processor['cyclotronWorker']!, 'releaseJob').mockImplementation(() => Promise.resolve())
Expand Down Expand Up @@ -267,4 +275,49 @@ describe('CdpCyclotronWorkerPlugins', () => {
expect(forSnapshot(getProducedKafkaMessages())).toMatchSnapshot()
})
})

describe('smoke tests', () => {
const testCases = Object.entries(PLUGINS_BY_ID).map(([pluginId, plugin]) => ({
name: pluginId,
plugin,
}))

it.each(testCases)('should run the plugin: %s', async ({ name, plugin }) => {
globals.event.event = '$identify' // Many plugins filter for this
const invocation = createInvocation(fn, globals)

invocation.hogFunction.template_id = `plugin-${plugin.id}`

const inputs: Record<string, any> = {}

for (const input of plugin.metadata.config) {
if (!input.key) {
continue
}

if (input.default) {
inputs[input.key] = input.default
continue
}

if (input.type === 'choice') {
inputs[input.key] = input.choices[0]
} else if (input.type === 'string') {
inputs[input.key] = 'test'
}
}

invocation.hogFunction.name = name
await processor.processInvocations([invocation])

expect(
forSnapshot(
getProducedKafkaMessagesForTopic('log_entries_test').map((m) => ({
message: m.value.message,
level: m.value.level,
}))
)
).toMatchSnapshot()
})
})
})
5 changes: 3 additions & 2 deletions plugin-server/src/cdp/legacy-plugins/customerio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const onEvent = async (event: ProcessedPluginEvent, meta: CustomerIoMeta)

const customer: Customer = syncCustomerMetadata(meta, event)
logger.debug(customer)
logger.debug(shouldCustomerBeTracked(customer, global.eventsConfig))
logger.debug('Should customer be tracked:', shouldCustomerBeTracked(customer, global.eventsConfig))
if (!shouldCustomerBeTracked(customer, global.eventsConfig)) {
return
}
Expand All @@ -147,7 +147,7 @@ function syncCustomerMetadata(meta: CustomerIoMeta, event: ProcessedPluginEvent)
const { logger } = meta
const email = getEmailFromEvent(event)
const customerStatus = new Set() as Customer['status']
logger.debug(email)
logger.debug('Detected email:', email)

// Update customer status
customerStatus.add('seen')
Expand Down Expand Up @@ -256,6 +256,7 @@ function getEmailFromEvent(event: ProcessedPluginEvent): string | null {

export const customerioPlugin: LegacyPlugin = {
id: 'customer-io',
metadata: require('./plugin.json'),
setupPlugin: setupPlugin as any,
onEvent,
}
1 change: 1 addition & 0 deletions plugin-server/src/cdp/legacy-plugins/intercom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function getTimestamp(meta: IntercomMeta, event: ProcessedPluginEvent): number {

export const intercomPlugin: LegacyPlugin = {
id: 'intercom',
metadata: require('./plugin.json'),
onEvent,
setupPlugin: () => Promise.resolve(),
}
6 changes: 5 additions & 1 deletion plugin-server/src/cdp/legacy-plugins/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProcessedPluginEvent } from '@posthog/plugin-scaffold'
import { PluginConfigSchema, ProcessedPluginEvent } from '@posthog/plugin-scaffold'

import { Response, trackedFetch } from '~/src/utils/fetch'

Expand All @@ -19,6 +19,10 @@ export type LegacyPluginMeta = {

export type LegacyPlugin = {
id: string
metadata: {
name: string
config: PluginConfigSchema[]
}
onEvent(event: ProcessedPluginEvent, meta: LegacyPluginMeta): Promise<void>
setupPlugin?: (meta: LegacyPluginMeta) => Promise<void>
}
2 changes: 1 addition & 1 deletion plugin-server/src/cdp/services/hog-executor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const formatInput = (bytecode: any, globals: HogFunctionInvocation['globa
}
}

const sanitizeLogMessage = (args: any[], sensitiveValues?: string[]): string => {
export const sanitizeLogMessage = (args: any[], sensitiveValues?: string[]): string => {
let message = args.map((arg) => (typeof arg !== 'string' ? JSON.stringify(arg) : arg)).join(', ')

// Find and replace any sensitive values
Expand Down

0 comments on commit a2d4450

Please sign in to comment.