Skip to content

Commit 9257787

Browse files
committed
fix(integrations): apply only a real posthog rollout percentage and keep blank grafana panels empty
1 parent 9e4109a commit 9257787

5 files changed

Lines changed: 56 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,9 @@ Return ONLY the timestamp string - no explanations, no quotes, no extra text.`,
15641564
const result: Record<string, unknown> = {}
15651565
if (params.limit) result.limit = Number(params.limit)
15661566
if (params.offset) result.offset = Number(params.offset)
1567-
if (params.rolloutPercentage) result.rolloutPercentage = Number(params.rolloutPercentage)
1567+
if (params.rolloutPercentage != null && params.rolloutPercentage !== '') {
1568+
result.rolloutPercentage = Number(params.rolloutPercentage)
1569+
}
15681570
if (params.responsesLimit) result.responsesLimit = Number(params.responsesLimit)
15691571

15701572
return result

‎apps/sim/tools/grafana/create_dashboard.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe('grafana create dashboard panels', () => {
2121
)
2222
})
2323

24+
it('creates an empty dashboard for blank panels', () => {
25+
expect(buildBody({ ...baseParams, panels: ' ' }).dashboard.panels).toEqual([])
26+
})
27+
2428
it('refuses panels JSON that is not an array', () => {
2529
expect(() => buildBody({ ...baseParams, panels: '{"type": "timeseries"}' })).toThrow(
2630
'panels must be a JSON array'

‎apps/sim/tools/grafana/create_dashboard.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const createDashboardTool: ToolConfig<
113113
}
114114

115115
let panels: unknown = params.panels || []
116+
if (typeof panels === 'string' && !panels.trim()) panels = []
116117
if (typeof panels === 'string') {
117118
try {
118119
panels = JSON.parse(panels)

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ interface CreateFeatureFlagResponse {
3333
flag: FeatureFlag
3434
}
3535

36+
/**
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.
40+
*/
41+
function parseRolloutPercentage(value: unknown): number | undefined {
42+
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
49+
}
50+
3651
export const createFeatureFlagTool: ToolConfig<
3752
CreateFeatureFlagParams,
3853
ToolResponse & { output: CreateFeatureFlagResponse }
@@ -142,15 +157,16 @@ export const createFeatureFlagTool: ToolConfig<
142157
body.ensure_experience_continuity = params.ensureExperienceContinuity
143158
}
144159

145-
if (params.rolloutPercentage !== undefined) {
160+
const rolloutPercentage = parseRolloutPercentage(params.rolloutPercentage)
161+
if (rolloutPercentage !== undefined) {
146162
if (body.filters === undefined) {
147163
body.filters = {
148-
groups: [{ properties: [], rollout_percentage: params.rolloutPercentage }],
164+
groups: [{ properties: [], rollout_percentage: rolloutPercentage }],
149165
}
150166
} else if (Array.isArray(body.filters?.groups)) {
151167
for (const group of body.filters.groups) {
152168
if (group && group.rollout_percentage === undefined) {
153-
group.rollout_percentage = params.rolloutPercentage
169+
group.rollout_percentage = rolloutPercentage
154170
}
155171
}
156172
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,32 @@ describe('posthog delete person', () => {
4343
expect(result?.success).toBe(true)
4444
})
4545
})
46+
47+
describe('posthog create feature flag rollout', () => {
48+
const buildBody = (rolloutPercentage: unknown) => {
49+
const body = posthogTools.posthogCreateFeatureFlagTool.request.body
50+
if (!body) throw new Error('create feature flag has no request body')
51+
return body({
52+
projectId: '1',
53+
region: 'us',
54+
apiKey: 'test-key',
55+
name: 'Beta',
56+
key: 'beta',
57+
rolloutPercentage: rolloutPercentage as number,
58+
}) as { filters?: { groups: { rollout_percentage: number }[] } }
59+
}
60+
61+
it.each([null, '', ' ', 'abc', 150])('sends no release condition for %j', (value) => {
62+
expect(buildBody(value).filters).toBeUndefined()
63+
})
64+
65+
it.each([
66+
['30', 30],
67+
[30, 30],
68+
[0, 0],
69+
])('releases the flag to %j percent', (value, expected) => {
70+
expect(buildBody(value).filters?.groups).toEqual([
71+
{ properties: [], rollout_percentage: expected },
72+
])
73+
})
74+
})

0 commit comments

Comments
 (0)