Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(flags): apply default release conditions on new flag creation (#65356)

okxint14ebddfe10ba
diff --git a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts
index 15c59b494c04..1ac9e1a19764 100644
--- a/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts
+++ b/frontend/src/scenes/feature-flags/defaultReleaseConditionsLogic.ts
@@ -14,6 +14,33 @@ export interface DefaultReleaseConditionsResponse {
default_groups: FeatureFlagGroupType[]
}
+export async function fetchDefaultReleaseConditions(teamId: number): Promise<DefaultReleaseConditionsResponse> {
+ return await api.get(`/api/environments/${teamId}/default_release_conditions/`)
+}
+
+/**
+ * Returns the cached value if already loaded, otherwise fetches it directly.
+ * Used in loaders that need default release conditions on mount before the
+ * async load from defaultReleaseConditionsLogic has resolved.
+ */
+export async function resolveDefaultReleaseConditions(
+ cached: DefaultReleaseConditionsResponse | null,
+ teamId: number | undefined
+): Promise<DefaultReleaseConditionsResponse | null> {
+ if (cached) {
+ return cached
+ }
+ if (!teamId) {
+ return null
+ }
+ try {
+ return await fetchDefaultReleaseConditions(teamId)
+ } catch (e) {
+ console.warn('Failed to fetch default release conditions:', e)
+ return null
+ }
+}
+
export const defaultReleaseConditionsLogic = kea<defaultReleaseConditionsLogicType>([
path(['scenes', 'feature-flags', 'defaultReleaseConditionsLogic']),
@@ -55,9 +82,7 @@ export const defaultReleaseConditionsLogic = kea<defaultReleaseConditionsLogicTy
if (!teamId) {
return null
}
- return (await api.get(
- `/api/environments/${teamId}/default_release_conditions/`
- )) as DefaultReleaseConditionsResponse
+ return await fetchDefaultReleaseConditions(teamId)
},
saveDefaultReleaseConditions: async () => {
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
index b11311bf7afe..79068110aa50 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
@@ -20,6 +20,7 @@ import { FeatureFlagFilters } from '~/types'
import { TemplateKey } from 'products/feature_flags/frontend/featureFlagTemplateConstants'
+import { defaultReleaseConditionsLogic, resolveDefaultReleaseConditions } from './defaultReleaseConditionsLogic'
import { detectFeatureFlagChanges } from './featureFlagConfirmationLogic'
import {
NEW_FLAG,
@@ -347,6 +348,24 @@ describe('featureFlagLogic', () => {
}
)
+ it('prepends org default groups ahead of template groups when default conditions are enabled', async () => {
+ const DEFAULT_GROUP = { properties: [], rollout_percentage: 10, variant: null }
+ // The shared singleton is warmed to a disabled value on mount, so seed its cache with
+ // enabled defaults before applyTemplate reads it.
+ defaultReleaseConditionsLogic.actions.loadDefaultReleaseConditionsSuccess({
+ enabled: true,
+ default_groups: [DEFAULT_GROUP],
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.applyTemplate('targeted')
+ }).toDispatchActions(['applyTemplate', 'setFeatureFlag'])
+
+ const groups = logic.values.featureFlag.filters.groups
+ expect(groups[0]).toMatchObject(DEFAULT_GROUP)
+ expect(groups.length).toBeGreaterThan(1)
+ })
+
it('preserves variant payloads when applying a template to a non-encrypted flag', async () => {
const MOCK_NON_ENCRYPTED_FLAG: FeatureFlagType = {
...logic.values.featureFlag,
@@ -1008,6 +1027,59 @@ describe('featureFlagLogic', () => {
})
})
})
+
+ describe('default release conditions', () => {
+ const conditionsUrl = `/api/environments/${MOCK_DEFAULT_PROJECT.id}/default_release_conditions/`
+
+ it('applies org default groups to a new flag when default conditions are enabled', async () => {
+ const DEFAULT_GROUP = { properties: [], rollout_percentage: 30, variant: null }
+ // Seed the shared singleton's cache with enabled defaults before the new-flag loader reads it
+ // (it's warmed to a disabled value on mount).
+ defaultReleaseConditionsLogic.actions.loadDefaultReleaseConditionsSuccess({
+ enabled: true,
+ default_groups: [DEFAULT_GROUP],
+ })
+
+ const newLogic = featureFlagLogic({ id: 'new' })
+ newLogic.mount()
+ await expectLogic(newLogic).toDispatchActions(['loadFeatureFlagSuccess'])
+
+ const groups = newLogic.values.featureFlag.filters.groups
+ expect(groups[0]).toMatchObject(DEFAULT_GROUP)
+ expect(groups.length).toBe(1)
+
+ newLogic.unmount()
+ })
+
+ describe('resolveDefaultReleaseConditions', () => {
+ it('returns the cached value without fetching when one is already loaded', async () => {
+ const cached = {
+ enabled: true,
+ default_groups: [{ properties: [], rollout_percentage: 25, variant: null }],
+ }
+ // toBe asserts the same object reference, so a fetched copy would fail the assertion.
+ await expect(resolveDefaultReleaseConditions(cached, MOCK_DEFAULT_PROJECT.id)).resolves.toBe(cached)
+ })
+
+ it('fetches directly when the cache is empty', async () => {
+ const fetched = {
+ enabled: true,
+ default_groups: [{ properties: [], rollout_percentage: 10, variant: null }],
+ }
+ useMocks({ get: { [conditionsUrl]: () => [200, fetched] } })
+ await expect(resolveDefaultReleaseConditions(null, MOCK_DEFAULT_PROJECT.id)).resolves.toEqual(fetched)
+ })
+
+ it('returns null without fetching when no team is available', async () => {
+ await expect(resolveDefaultReleaseConditions(null, undefined)).resolves.toBeNull()
+ })
+
+ it('returns null when the fetch fails so new flag creation is not blocked', async () => {
+ useMocks({ get: { [conditionsUrl]: () => [500, {}] } })
+ await expect(resolveDefaultReleaseConditions(null, MOCK_DEFAULT_PROJECT.id)).resolves.toBeNull()
+ })
+ })
+ })
})
const createFilters = (overrides: Partial<FeatureFlagFilters> = {}): FeatureFlagFilters => ({
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
index 9d080112ac3a..37585da30344 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
@@ -84,7 +84,7 @@ import { TEMPLATE_NAMES } from 'products/feature_flags/frontend/featureFlagTempl
import { organizationLogic } from '../organizationLogic'
import { teamLogic } from '../teamLogic'
import { defaultEvaluationContextsLogic } from './defaultEvaluationContextsLogic'
-import { defaultReleaseConditionsLogic } from './defaultReleaseConditionsLogic'
+import { defaultReleaseConditionsLogic, resolveDefaultReleaseConditions } from './defaultReleaseConditionsLogic'
import { checkFeatureFlagConfirmation } from './featureFlagConfirmationLogic'
import type { FlagIntent } from './featureFlagIntentWarningLogic'
import type { featureFlagLogicType } from './featureFlagLogicType'
@@ -1300,7 +1300,13 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
}
if (flagType !== 'remote_config') {
- const conditionsConfig = values.defaultReleaseConditions
+ // Use cached value if already loaded; fetch directly if not yet available
+ // to avoid a race where defaultReleaseConditionsLogic's async load hasn't
+ // completed before loadFeatureFlag reads values.defaultReleaseConditions.
+ const conditionsConfig = await resolveDefaultReleaseConditions(
+ values.defaultReleaseConditions,
+ values.currentTeam?.id
+ )
if (conditionsConfig?.enabled && conditionsConfig.default_groups?.length > 0) {
baseFlagConfig = {
...baseFlagConfig,
@@ -2022,14 +2028,17 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
maybeApplyUrlIntent(values, actions)
}
},
- applyTemplate: ({ templateId }) => {
+ applyTemplate: async ({ templateId }) => {
const template = values.templates.find((t) => t.id === templateId)
if (!template || !values.featureFlag) {
return
}
const templateValues = template.getValues(values.featureFlag)
- const defaultConfig = values.defaultReleaseConditions
+ const defaultConfig = await resolveDefaultReleaseConditions(
+ values.defaultReleaseConditions,
+ values.currentTeam?.id
+ )
const defaultGroups =
defaultConfig?.enabled && defaultConfig.default_groups?.length > 0 ? defaultConfig.default_groups : []
const templateGroups = templateValues.filters?.groups ?? []