Skip to content

Commit 1a57f02

Browse files
committed
fix(posthog): reject an invalid rollout percentage and leave create rollouts for the tool to read
1 parent 4f4aa0c commit 1a57f02

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

‎apps/sim/blocks/blocks/posthog.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PosthogIcon } from '@/components/icons'
22
import type { BlockConfig, BlockMeta } from '@/blocks/types'
33
import { AuthMode, IntegrationType } from '@/blocks/types'
4+
import { mapperOperation } from '@/blocks/utils'
45

56
/** Cohort membership definition, whichever of the three forms the user filled. */
67
const COHORT_DEFINITION_FIELD = ['filters', 'query', 'groups'] as const
@@ -1560,11 +1561,14 @@ Return ONLY the timestamp string - no explanations, no quotes, no extra text.`,
15601561

15611562
return params.operation as string
15621563
},
1563-
params: (params) => {
1564+
params: (params, context) => {
15641565
const result: Record<string, unknown> = {}
15651566
if (params.limit) result.limit = Number(params.limit)
15661567
if (params.offset) result.offset = Number(params.offset)
1567-
if (params.rolloutPercentage != null && params.rolloutPercentage !== '') {
1568+
if (
1569+
params.rolloutPercentage &&
1570+
mapperOperation(params, context) !== 'posthog_create_feature_flag'
1571+
) {
15681572
result.rolloutPercentage = Number(params.rolloutPercentage)
15691573
}
15701574
if (params.responsesLimit) result.responsesLimit = Number(params.responsesLimit)

‎apps/sim/tools/posthog/create_feature_flag.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ interface CreateFeatureFlagResponse {
3434
}
3535

3636
/**
37-
* A rollout percentage the caller actually set: a number, or numeric text from a tool call,
38-
* within 0-100. An empty field arrives as `null` or `''`, and PostHog reads a null rollout as
39-
* 100%, so anything else yields `undefined` and no release condition is sent.
37+
* The rollout percentage the caller set, or `undefined` when the field is empty. An empty
38+
* field arrives as `null` or blank text, and PostHog reads a null rollout as 100%, so it must
39+
* send no release condition. A canvas value arrives as text, so numeric text is accepted.
4040
*/
4141
function parseRolloutPercentage(value: unknown): number | undefined {
42+
if (value == null || (typeof value === 'string' && !value.trim())) return undefined
4243
const parsed =
43-
typeof value === 'number'
44-
? value
45-
: typeof value === 'string' && value.trim()
46-
? Number(value)
47-
: Number.NaN
48-
return Number.isFinite(parsed) && parsed >= 0 && parsed <= 100 ? parsed : undefined
44+
typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : Number.NaN
45+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 100) {
46+
throw new Error('Rollout percentage must be a number from 0 to 100')
47+
}
48+
return parsed
4949
}
5050

5151
export const createFeatureFlagTool: ToolConfig<

‎apps/sim/tools/posthog/posthog.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2+
import { PostHogBlock } from '@/blocks/blocks/posthog'
23
import * as posthogTools from '@/tools/posthog'
34
import type { ToolConfig } from '@/tools/types'
45

@@ -58,17 +59,42 @@ describe('posthog create feature flag rollout', () => {
5859
}) as { filters?: { groups: { rollout_percentage: number }[] } }
5960
}
6061

61-
it.each([null, '', ' ', 'abc', 150])('sends no release condition for %j', (value) => {
62+
it.each([null, undefined, '', ' '])('sends no release condition for %j', (value) => {
6263
expect(buildBody(value).filters).toBeUndefined()
6364
})
6465

66+
it.each(['abc', '50%', 150, -5])('refuses the rollout %j', (value) => {
67+
expect(() => buildBody(value)).toThrow('Rollout percentage must be a number from 0 to 100')
68+
})
69+
6570
it.each([
6671
['30', 30],
72+
[' 30 ', 30],
6773
[30, 30],
6874
[0, 0],
75+
['0', 0],
6976
])('releases the flag to %j percent', (value, expected) => {
7077
expect(buildBody(value).filters?.groups).toEqual([
7178
{ properties: [], rollout_percentage: expected },
7279
])
7380
})
7481
})
82+
83+
describe('posthog block rollout mapping', () => {
84+
const mapParams = (operation: string, rolloutPercentage: unknown) =>
85+
PostHogBlock.tools.config?.params?.({ operation, rolloutPercentage }) ?? {}
86+
87+
it.each(['', ' ', '0', 0, null])(
88+
'leaves a create rollout of %j for the tool to read',
89+
(value) => {
90+
expect(mapParams('posthog_create_feature_flag', value)).not.toHaveProperty(
91+
'rolloutPercentage'
92+
)
93+
}
94+
)
95+
96+
it('keeps sending update rollouts as before', () => {
97+
expect(mapParams('posthog_update_feature_flag', '30')).toEqual({ rolloutPercentage: 30 })
98+
expect(mapParams('posthog_update_feature_flag', 0)).toEqual({})
99+
})
100+
})

0 commit comments

Comments
 (0)