Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format legacy plugins to new format #28017

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open

Conversation

MarconLP
Copy link
Member

@MarconLP MarconLP commented Jan 29, 2025

Changes

  • export LegacyPlugin type at the bottom
  • replace console with logger.log
  • await meta.fetch instead of normal
  • make sure all tests work

status for all plugins: https://docs.google.com/spreadsheets/d/1K7Y5ZaefxP_mGl_q2ubjbFj6NybQT1DB0Q53CsEAMek/edit?usp=sharing

Does this work well for both Cloud and self-hosted?

How did you test this code?

benjackwhite and others added 15 commits January 28, 2025 09:44
# Conflicts:
#	plugin-server/src/cdp/consumers/__snapshots__/cdp-cyclotron-plugins-worker.test.ts.snap
#	plugin-server/src/cdp/consumers/cdp-cyclotron-plugins-worker.consumer.ts
#	plugin-server/src/cdp/consumers/cdp-cyclotron-plugins-worker.test.ts
#	plugin-server/src/cdp/consumers/cdp-cyclotron-worker.consumer.ts
#	plugin-server/src/cdp/legacy-plugins/customerio/index.ts
#	plugin-server/src/cdp/legacy-plugins/intercom/index.ts
#	plugin-server/src/cdp/legacy-plugins/types.ts
#	plugin-server/src/main/pluginsServer.ts
#	posthog/cdp/migrations.py
#	posthog/models/plugin.py
@MarconLP
Copy link
Member Author

tests are succeeding locally after creating a test_cyclotron database
2025-01-30 at 13 32 35

@MarconLP MarconLP marked this pull request as ready for review January 30, 2025 12:33
@MarconLP MarconLP requested review from benjackwhite and removed request for benjackwhite January 30, 2025 12:33
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR significantly restructures legacy plugins in the PostHog plugin server, focusing on standardizing plugin formats and improving type safety.

  • Removes multiple legacy plugins (currency-normalization, downsampling, taxonomy, timestamp-parser, etc.) while converting remaining plugins to TypeScript with proper type definitions
  • Updates remaining plugins (Hubspot, Braze, GCS, etc.) to use LegacyPlugin interface instead of Plugin type, with standardized logging via meta.logger
  • Replaces global fetch with meta.fetch across plugins and updates test files to use jest-fetch-mock
  • Consolidates plugin exports in src/cdp/legacy-plugins/index.ts with consistent plugin registration pattern
  • Adds comprehensive test coverage for converted plugins while removing tests for deprecated ones

116 file(s) reviewed, 43 comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Removing currency normalization functionality without a clear replacement could break existing integrations that depend on currency conversion. Ensure this functionality is either migrated elsewhere or that this is an intentional deprecation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This file is being completely removed. Ensure there is a replacement implementation in the new plugin format that maintains the same sampling functionality and backwards compatibility.

@@ -178,6 +179,7 @@ function getEmailFromEvent(event) {

export const hubspotPlugin: LegacyPlugin = {
id: 'hubspot',
onEvent,
metadata: metadata as any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: casting metadata as any bypasses type checking - consider defining proper type

Comment on lines 15 to 29
export const PLUGINS_BY_ID = {
[customerioPlugin.id]: customerioPlugin,
[intercomPlugin.id]: intercomPlugin,
[rudderstackPlugin.id]: rudderstackPlugin,
[hubspotPlugin.id]: hubspotPlugin,
[engagePlugin.id]: engagePlugin,
[avoPlugin.id]: avoPlugin,
[patternsPlugin.id]: patternsPlugin,
[brazePlugin.id]: brazePlugin,
[pubsubPlugin.id]: pubsubPlugin,
[sendgridPlugin.id]: sendgridPlugin,
[gcsPlugin.id]: gcsPlugin,
[salesforcePlugin.id]: salesforcePlugin,
[laudspeakerPlugin.id]: laudspeakerPlugin,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider alphabetizing the plugin entries in PLUGINS_BY_ID for better maintainability and easier lookup

Comment on lines 1 to 13
import { customerioPlugin } from './customerio'
import { hubspotPlugin } from './hubspot'
import { intercomPlugin } from './intercom'
import { avoPlugin } from './posthog-avo'
import { brazePlugin } from './posthog-braze-app'
import { engagePlugin } from './posthog-engage-so'
import { gcsPlugin } from './posthog-gcs'
import { laudspeakerPlugin } from './posthog-laudspeaker-app'
import { patternsPlugin } from './posthog-patterns-app'
import { pubsubPlugin } from './pubsub'
import { rudderstackPlugin } from './rudderstack-posthog'
import { salesforcePlugin } from './salesforce/src'
import { sendgridPlugin } from './sendgrid'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider grouping imports by internal/external dependencies and alphabetizing within groups

Comment on lines 305 to 307
metadata: metadata as any,
setupPlugin: setupPlugin as any,
onEvent,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: unsafe type assertions with 'as any' could be replaced with proper types

throw new Error(`Got bad response getting the token ${response.status}`)
}
const body = await response.json()
cache.set(CACHE_TOKEN, body.access_token, CACHE_TTL)
void cache.set(CACHE_TOKEN, body.access_token, CACHE_TTL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: void operator is unnecessary for Promise returned by cache.set

config,
global,
logger: { error: jest.fn(), debug: jest.fn() },
fetch: mockFetch as unknown,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: mockFetch is already typed as jest.Mock, casting to unknown here is unnecessary and could hide type errors

Comment on lines 153 to 154
metadata: metadata as any,
setupPlugin: setupPlugin as any,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Type assertions to 'any' bypass TypeScript's type checking. Consider defining proper types for metadata and setupPlugin

Comment on lines 100 to 104
function isEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const re =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(email).toLowerCase())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add type annotation for email parameter to maintain TypeScript consistency

@MarconLP MarconLP requested a review from benjackwhite January 31, 2025 10:11
@benjackwhite benjackwhite changed the base branch from feat/cyclotron-plugins-all to master January 31, 2025 12:08
const { getMeta, resetMeta } = require('@posthog/plugin-scaffold/test/utils')
const { engagePlugin } = require('../index')
const config = {
publicKey: 'ENGAGE_PUBLIC_KEY',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "ENGAGE_PUBLIC_KEY" is used as
authorization header
.
The hard-coded value "ENGAGE_PUBLIC_KEY" is used as
authorization header
.
The hard-coded value "ENGAGE_PUBLIC_KEY" is used as
authorization header
.
The hard-coded value "ENGAGE_PUBLIC_KEY" is used as
authorization header
.
const { engagePlugin } = require('../index')
const config = {
publicKey: 'ENGAGE_PUBLIC_KEY',
secret: 'ENGAGE_SEECRET',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "ENGAGE_SEECRET" is used as
authorization header
.
The hard-coded value "ENGAGE_SEECRET" is used as
authorization header
.
The hard-coded value "ENGAGE_SEECRET" is used as
authorization header
.
The hard-coded value "ENGAGE_SEECRET" is used as
authorization header
.
}

function result(target: any, path: any, value: any) {
target[path] = value

Check warning

Code scanning / CodeQL

Prototype-polluting function Medium

The property chain
here
is recursively assigned to
target
without guarding against prototype pollution.

Copilot Autofix AI about 2 hours ago

To fix the problem, we need to ensure that the set function does not allow the assignment of properties that can lead to prototype pollution, such as __proto__ and constructor. We can achieve this by adding a check to skip these properties during the assignment process.

  • Modify the set function to include a check that skips the __proto__ and constructor properties.
  • Ensure that the result function is only called if the property is not one of the dangerous properties.
Suggested changeset 1
plugin-server/src/cdp/legacy-plugins/_destinations/posthog-laudspeaker-app/index.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/plugin-server/src/cdp/legacy-plugins/_destinations/posthog-laudspeaker-app/index.ts b/plugin-server/src/cdp/legacy-plugins/_destinations/posthog-laudspeaker-app/index.ts
--- a/plugin-server/src/cdp/legacy-plugins/_destinations/posthog-laudspeaker-app/index.ts
+++ b/plugin-server/src/cdp/legacy-plugins/_destinations/posthog-laudspeaker-app/index.ts
@@ -91,2 +91,6 @@
 
+        if (prop === '__proto__' || prop === 'constructor') {
+            continue
+        }
+
         if (!isObject(target[prop])) {
EOF
@@ -91,2 +91,6 @@

if (prop === '__proto__' || prop === 'constructor') {
continue
}

if (!isObject(target[prop])) {
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}

function result(target: any, path: any, value: any) {
target[path] = value

Check warning

Code scanning / CodeQL

Prototype-polluting function Medium

The property chain
here
is recursively assigned to
target
without guarding against prototype pollution.

Copilot Autofix AI about 2 hours ago

To fix the problem, we need to ensure that the set function does not allow the assignment of properties like __proto__ or constructor that can lead to prototype pollution. We can achieve this by adding a check to skip these properties during the assignment process.

  • Modify the set function to include a check that skips the assignment if the property name is __proto__ or constructor.
  • This change should be made in the file plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts within the set function.
Suggested changeset 1
plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts b/plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts
--- a/plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts
+++ b/plugin-server/src/cdp/legacy-plugins/_destinations/rudderstack-posthog/index.ts
@@ -90,2 +90,6 @@
 
+        if (prop === '__proto__' || prop === 'constructor') {
+            break
+        }
+
         if (!isObject(target[prop])) {
EOF
@@ -90,2 +90,6 @@

if (prop === '__proto__' || prop === 'constructor') {
break
}

if (!isObject(target[prop])) {
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
global,
logger: { error: jest.fn(), debug: jest.fn() },
} as unknown as SalesforceMeta,
'token'

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "token" is used as
authorization header
.

expect(mockFetch).toHaveBeenCalledWith('https://test.salesforce.com/test', {
body: '{"$current_url":"https://home/io"}',
headers: { Authorization: 'Bearer token', 'Content-Type': 'application/json' },

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "Bearer token" is used as
authorization header
.
properties: { $current_url: 'https://home/io' },
} as unknown as ProcessedPluginEvent,
{ config, global, logger: { error: jest.fn(), debug: jest.fn() } } as unknown as SalesforceMeta,
'token'

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "token" is used as
authorization header
.
expect(fetch).toHaveBeenCalledWith('https://api.sendgrid.com/v3/marketing/field_definitions', {
method: 'GET',
headers: {
Authorization: 'Bearer SENDGRID_API_KEY',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "Bearer SENDGRID_API_KEY" is used as
authorization header
.
expect(fetch).toHaveBeenCalledWith('https://api.sendgrid.com/v3/marketing/contacts', {
method: 'PUT',
headers: {
Authorization: 'Bearer SENDGRID_API_KEY',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "Bearer SENDGRID_API_KEY" is used as
authorization header
.
expect(fetch).toHaveBeenCalledWith('https://api.sendgrid.com/v3/marketing/contacts', {
method: 'PUT',
headers: {
Authorization: 'Bearer SENDGRID_API_KEY',

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "Bearer SENDGRID_API_KEY" is used as
authorization header
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants