Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/worker/plugins/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function runProcessEvent(server: Hub, event: PluginEvent): Promise<

const pluginsSucceeded = []
const pluginsFailed = []
const pluginsDeferred = []
for (const pluginConfig of pluginsToRun) {
const processEvent = await pluginConfig.vm?.getProcessEvent()

Expand Down Expand Up @@ -85,13 +86,20 @@ export async function runProcessEvent(server: Hub, event: PluginEvent): Promise<
return null
}
}

const onEvent = await pluginConfig.vm?.getOnEvent()
const onSnapshot = await pluginConfig.vm?.getOnSnapshot()
if (onEvent || onSnapshot) {
pluginsDeferred.push(`${pluginConfig.plugin?.name} (${pluginConfig.id})`)
}
}

if (pluginsSucceeded.length > 0 || pluginsFailed.length > 0) {
if (pluginsSucceeded.length > 0 || pluginsFailed.length > 0 || pluginsDeferred.length > 0) {
event.properties = {
...event.properties,
$plugins_succeeded: pluginsSucceeded,
$plugins_failed: pluginsFailed,
$plugins_deferred: pluginsDeferred,
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ test('plugin meta has what it should have', async () => {
const returnedEvent = await runProcessEvent(hub, event)

expect(Object.keys(returnedEvent!.properties!).sort()).toEqual([
'$plugins_deferred',
'$plugins_failed',
'$plugins_succeeded',
'attachments',
Expand Down Expand Up @@ -234,6 +235,7 @@ test('plugin changing event.team_id throws error', async () => {
properties: {
$plugins_failed: ['test-maxmind-plugin (39)'],
$plugins_succeeded: [],
$plugins_deferred: [],
},
team_id: 2,
}
Expand Down Expand Up @@ -275,6 +277,7 @@ test('plugin throwing error does not prevent ingestion and failure is noted in e
properties: {
$plugins_failed: ['test-maxmind-plugin (39)'],
$plugins_succeeded: [],
$plugins_deferred: [],
},
}
expect(returnedEvent).toEqual(expectedReturnEvent)
Expand Down Expand Up @@ -308,6 +311,41 @@ test('events have property $plugins_succeeded set to the plugins that succeeded'
properties: {
$plugins_failed: [],
$plugins_succeeded: ['test-maxmind-plugin (39)'],
$plugins_deferred: [],
},
}
expect(returnedEvent).toEqual(expectedReturnEvent)
})

test('events have property $plugins_deferred set to the plugins that run after processEvent', async () => {
// silence some spam
console.log = jest.fn()
console.error = jest.fn()

getPluginRows.mockReturnValueOnce([
mockPluginWithArchive(`
function onEvent (event) {
return event
}
`),
])
getPluginConfigRows.mockReturnValueOnce([pluginConfig39])
getPluginAttachmentRows.mockReturnValueOnce([pluginAttachment1])

await setupPlugins(hub)
const { pluginConfigs } = hub

expect(await pluginConfigs.get(39)!.vm!.getTasks(PluginTaskType.Schedule)).toEqual({})

const event = { event: '$test', properties: {}, team_id: 2 } as PluginEvent
const returnedEvent = await runProcessEvent(hub, { ...event })

const expectedReturnEvent = {
...event,
properties: {
$plugins_failed: [],
$plugins_succeeded: [],
$plugins_deferred: ['test-maxmind-plugin (39)'],
},
}
expect(returnedEvent).toEqual(expectedReturnEvent)
Expand Down