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): show pending-approval warning for single flag copy (#70938)

Phil Haack614a2b6126ad
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
index f17d767c3966..0bf8ec583a29 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
@@ -1,10 +1,17 @@
-import { MOCK_DEFAULT_BASIC_USER, MOCK_DEFAULT_PROJECT, MOCK_TEAM_ID } from 'lib/api.mock'
+import {
+ MOCK_DEFAULT_BASIC_USER,
+ MOCK_DEFAULT_PROJECT,
+ MOCK_DEFAULT_TEAM,
+ MOCK_ORGANIZATION_ID,
+ MOCK_TEAM_ID,
+} from 'lib/api.mock'
import { router } from 'kea-router'
import { expectLogic, partial } from 'kea-test-utils'
import { dayjs } from 'lib/dayjs'
import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { urls } from 'scenes/urls'
import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
@@ -45,6 +52,15 @@ import {
} from './featureFlagLogic'
import { featureFlagsLogic } from './featureFlagsLogic'
+// jest.config.ts sets clearMocks: true, so these mock.fn() call histories reset before every test.
+jest.mock('lib/lemon-ui/LemonToast/LemonToast', () => ({
+ lemonToast: {
+ success: jest.fn(),
+ error: jest.fn(),
+ warning: jest.fn(),
+ },
+}))
+
const MOCK_FEATURE_FLAG = {
...NEW_FLAG,
id: 1,
@@ -1335,6 +1351,72 @@ describe('featureFlagLogic', () => {
dialogOpenSpy.mockRestore()
})
})
+
+ describe('copyFlagSuccess', () => {
+ const copyFlagsUrl = `/api/organizations/${MOCK_ORGANIZATION_ID}/feature_flags/copy_flags/`
+
+ beforeEach(() => {
+ useMocks({
+ get: {
+ // Hit by the loadProjectsWithCurrentFlag() the listener always triggers afterward.
+ [`/api/organizations/${MOCK_ORGANIZATION_ID}/feature_flags/${MOCK_FEATURE_FLAG.key}/`]: () => [
+ 200,
+ [],
+ ],
+ },
+ })
+ logic.actions.setCopyDestinationProject(MOCK_TEAM_ID)
+ })
+
+ it('shows a pending-approval warning, not a raw error, when the target project gates the write', async () => {
+ useMocks({
+ post: {
+ [copyFlagsUrl]: () => [
+ 200,
+ {
+ success: [],
+ failed: [
+ {
+ project_id: MOCK_TEAM_ID,
+ error_message: 'Approval required',
+ approval_pending: true,
+ change_request_id: 'cr-1',
+ },
+ ],
+ },
+ ],
+ },
+ })
+
+ logic.actions.copyFlag()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(lemonToast.warning).toHaveBeenCalledWith(
+ `A change request was created for ${MOCK_DEFAULT_TEAM.name}; the copy will apply once approved.`
+ )
+ expect(lemonToast.error).not.toHaveBeenCalled()
+ })
+
+ it('shows a plain error toast for a hard failure', async () => {
+ useMocks({
+ post: {
+ [copyFlagsUrl]: () => [
+ 200,
+ {
+ success: [],
+ failed: [{ project_id: MOCK_TEAM_ID, error_message: 'Project not found.' }],
+ },
+ ],
+ },
+ })
+
+ logic.actions.copyFlag()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(lemonToast.error).toHaveBeenCalledWith('Error while copying feature flag: Project not found.')
+ expect(lemonToast.warning).not.toHaveBeenCalled()
+ })
+ })
})
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 2e9b2d707af3..1f338d50298e 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
@@ -2189,9 +2189,24 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
])
warnings.forEach((warning) => lemonToast.warning(warning))
} else {
- const errorMessage = JSON.stringify(featureFlagCopy?.failed) || featureFlagCopy
- lemonToast.error(`Error while saving feature flag: ${errorMessage}`)
- eventUsageLogic.actions.reportFeatureFlagCopyFailure(errorMessage)
+ // This flow copies to a single target project, so at most one failure entry comes back.
+ const failure = featureFlagCopy?.failed[0]
+ if (failure?.approval_pending) {
+ const projectName = values.currentOrganization?.teams.find(
+ (team) => team.id === (failure.project_id ?? values.copyDestinationProject)
+ )?.name
+ lemonToast.warning(
+ `A change request was created for ${
+ projectName ?? 'the destination project'
+ }; the copy will apply once approved.`
+ )
+ eventUsageLogic.actions.reportFeatureFlagCopyFailure(failure.error_message ?? 'Approval pending')
+ } else {
+ const errorMessage =
+ failure?.error_message ?? JSON.stringify(featureFlagCopy?.failed ?? featureFlagCopy)
+ lemonToast.error(`Error while copying feature flag: ${errorMessage}`)
+ eventUsageLogic.actions.reportFeatureFlagCopyFailure(errorMessage)
+ }
}
actions.loadProjectsWithCurrentFlag()