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(api): sync team name on project rename (#69368)

Alex Lider91624599ab97
diff --git a/frontend/src/scenes/teamLogic.test.ts b/frontend/src/scenes/teamLogic.test.ts
index 6a0133c8b388..999d94c21bd4 100644
--- a/frontend/src/scenes/teamLogic.test.ts
+++ b/frontend/src/scenes/teamLogic.test.ts
@@ -1,10 +1,12 @@
-import { MOCK_DEFAULT_TEAM, MOCK_TEAM_ID } from 'lib/api.mock'
+import { MOCK_DEFAULT_PROJECT, MOCK_DEFAULT_TEAM, MOCK_TEAM_ID } from 'lib/api.mock'
import { expectLogic } from 'kea-test-utils'
+import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { AppContext } from '~/types'
+import { projectLogic } from './projectLogic'
import { teamLogic } from './teamLogic'
describe('teamLogic', () => {
@@ -28,6 +30,44 @@ describe('teamLogic', () => {
})
})
+ describe('updateCurrentTeam with a name-only payload', () => {
+ beforeEach(() => {
+ initKeaTests(false)
+ // Simulate projectLogic not having loaded yet, as the rename must not depend on it
+ window.POSTHOG_APP_CONTEXT = {
+ ...window.POSTHOG_APP_CONTEXT,
+ current_project: undefined,
+ } as unknown as AppContext
+ useMocks({
+ get: {
+ '/api/projects/@current': () => [500, {}],
+ },
+ patch: {
+ // Only /api/projects is mocked: a name-only update must not hit the
+ // deprecated /api/environments endpoint
+ '/api/projects/:id': async ({ request }) => [
+ 200,
+ { ...MOCK_DEFAULT_PROJECT, ...((await request.json()) as Record<string, any>) },
+ ],
+ },
+ })
+ logic = teamLogic()
+ logic.mount()
+ })
+
+ it('renames the parent project and syncs it into projectLogic', async () => {
+ await expectLogic(logic).toDispatchActions(['loadCurrentTeamSuccess'])
+ expect(projectLogic.values.currentProject).toBeNull()
+
+ await expectLogic(logic, () => {
+ logic.actions.updateCurrentTeam({ name: 'Renamed project' })
+ }).toDispatchActions([projectLogic.actionTypes.loadCurrentProjectSuccess, 'updateCurrentTeamSuccess'])
+
+ expect(logic.values.currentTeam?.name).toBe('Renamed project')
+ expect(projectLogic.values.currentProject?.name).toBe('Renamed project')
+ })
+ })
+
describe('before team is loaded', () => {
beforeEach(() => {
initKeaTests(false)
diff --git a/frontend/src/scenes/teamLogic.tsx b/frontend/src/scenes/teamLogic.tsx
index 1fefb4b5d2bc..e9120e9949e9 100644
--- a/frontend/src/scenes/teamLogic.tsx
+++ b/frontend/src/scenes/teamLogic.tsx
@@ -67,6 +67,8 @@ export const teamLogic = kea<teamLogicType>([
['loadCurrentOrganization'],
customProductsLogic,
['loadCustomProducts'],
+ projectLogic,
+ ['loadCurrentProjectSuccess'],
],
})),
actions({
@@ -113,16 +115,22 @@ export const teamLogic = kea<teamLogicType>([
}
}
- const promises: [Promise<TeamType>, Promise<ProjectType> | undefined] = [
- api.update(`api/environments/${values.currentTeam.id}`, payload),
- undefined,
- ]
- if (Object.keys(payload).length === 1 && payload.name && values.currentProject) {
- // If we're only updating the name, update the project name as well for equivalence
- promises[1] = api.update(`api/projects/${values.currentProject.id}`, { name: payload.name })
+ let patchedTeam: TeamType | TeamPublicType
+ if (Object.keys(payload).length === 1 && payload.name && values.currentTeam.project_id) {
+ // Renames go through /api/projects, which mirrors the name onto the passthrough
+ // team server-side. /api/environments is deprecated, don't add calls to it
+ const patchedProject = await api.update<ProjectType>(
+ `api/projects/${values.currentTeam.project_id}`,
+ { name: payload.name }
+ )
+ breakpoint()
+ // Sync projectLogic too, as surfaces like the project switcher read currentProject.name
+ actions.loadCurrentProjectSuccess(patchedProject)
+ patchedTeam = { ...values.currentTeam, name: patchedProject.name }
+ } else {
+ patchedTeam = await api.update(`api/environments/${values.currentTeam.id}`, payload)
+ breakpoint()
}
- const [patchedTeam] = await Promise.all(promises)
- breakpoint()
// We need to reload current org (which lists its teams) in organizationLogic
actions.loadCurrentOrganization()
diff --git a/posthog/api/project.py b/posthog/api/project.py
index 4b480f706d62..42741a6fbe9f 100644
--- a/posthog/api/project.py
+++ b/posthog/api/project.py
@@ -1209,6 +1209,12 @@ class ProjectBackwardCompatSerializer(
should_team_be_saved_too = True
setattr(team, attr, value)
+ if "name" in validated_data:
+ # Keep Team.name mirroring Project.name: surfaces like the organization's teams
+ # list and the app context still read the name off the Team row
+ should_team_be_saved_too = True
+ team.name = validated_data["name"]
+
instance.save()
if should_team_be_saved_too:
team.save()
diff --git a/posthog/api/test/test_project.py b/posthog/api/test/test_project.py
index a6145cba7498..a0e08c3dd352 100644
--- a/posthog/api/test/test_project.py
+++ b/posthog/api/test/test_project.py
@@ -702,6 +702,18 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore
self.assertEqual(self.team.base_currency, "EUR")
self.assertEqual(self.team.capture_dead_clicks, True)
+ def test_rename_project_syncs_passthrough_team_name(self):
+ response = self.client.patch(
+ f"/api/projects/{self.project.id}/",
+ {"name": "Renamed project"},
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
+
+ self.project.refresh_from_db()
+ self.team.refresh_from_db()
+ self.assertEqual(self.project.name, "Renamed project")
+ self.assertEqual(self.team.name, "Renamed project")
+
def test_customer_analytics_config_writes_through_to_team(self):
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()