diff --git a/cli/src/commands/gitsync-settings/pull.ts b/cli/src/commands/gitsync-settings/pull.ts index 5c99a6264d..576cbf7379 100644 --- a/cli/src/commands/gitsync-settings/pull.ts +++ b/cli/src/commands/gitsync-settings/pull.ts @@ -6,7 +6,7 @@ import { GlobalOptions } from "../../types.ts"; import { requireLogin } from "../../core/auth.ts"; import { resolveWorkspace } from "../../core/context.ts"; import * as wmill from "../../../gen/services.gen.ts"; -import { SyncOptions, readConfigFile, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig } from "../../core/conf.ts"; +import { SyncOptions, readConfigFile, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig, getWorkspaceNames, getEffectiveGitBranch } from "../../core/conf.ts"; import { yamlOptions } from "../sync/sync.ts"; import { deepEqual } from "../../utils/utils.ts"; import { getCurrentGitBranch, isGitRepository } from "../../utils/git.ts"; @@ -450,8 +450,19 @@ export async function pullGitSyncSettings( if (!overrideTarget && isGitRepository()) { const currentBranch = getCurrentGitBranch(); if (currentBranch) { - overrideTarget = - findWorkspaceByGitBranch(localConfig.workspaces, currentBranch)?.[0] ?? currentBranch; + // A branch shared by several entries does not say which workspace this + // is; guessing would overwrite another workspace's overrides + const onBranch = getWorkspaceNames(localConfig.workspaces).filter( + (name) => + getEffectiveGitBranch(name, (localConfig.workspaces as any)[name]) === currentBranch, + ); + if (onBranch.length > 1) { + throw new Error( + `Workspace entries ${onBranch.join(", ")} all use git branch '${currentBranch}' and none matches workspace '${workspace.workspaceId}'. ` + + `Name its entry '${workspace.workspaceId}' so it can be resolved.`, + ); + } + overrideTarget = onBranch[0] ?? currentBranch; } } if (overrideTarget) { diff --git a/cli/test/gitsync_settings_workspace_resolution_unit.test.ts b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts index 243a6a1006..9e0d52262c 100644 --- a/cli/test/gitsync_settings_workspace_resolution_unit.test.ts +++ b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts @@ -8,31 +8,31 @@ test("gitsync-settings: entries sharing a gitBranch resolve by workspace", async const config: SyncOptions = { includes: ["f/**"], workspaces: { - "flh-nonprod": { + "team-a-staging": { baseUrl: "https://windmill.example.com/", - gitBranch: "nonprod", - overrides: { includes: ["f/ops_flh/**"] }, + gitBranch: "staging", + overrides: { includes: ["f/team_a/**"] }, }, - provinzial: { + "team-b": { baseUrl: "https://windmill.example.com/", - workspaceId: "provinzial-nonprod", - gitBranch: "nonprod", - overrides: { includes: ["f/ops_provinzial/**"] }, + workspaceId: "team-b-staging", + gitBranch: "staging", + overrides: { includes: ["f/team_b/**"] }, }, }, }; const byFlag = await getEffectiveSettingsForWorkspace( config, - { workspace: "flh-nonprod", jsonOutput: true }, - { remote: "http://windmill_server:8000/", workspaceId: "flh-nonprod" }, + { workspace: "team-a-staging", jsonOutput: true }, + { remote: "http://windmill_server:8000/", workspaceId: "team-a-staging" }, ); - expect(byFlag.includes).toEqual(["f/ops_flh/**"]); + expect(byFlag.includes).toEqual(["f/team_a/**"]); const byProfile = await getEffectiveSettingsForWorkspace( config, - { workspace: "provinzial-nonprod", jsonOutput: true }, - { remote: "https://windmill.example.com/", workspaceId: "provinzial-nonprod" }, + { workspace: "team-b-staging", jsonOutput: true }, + { remote: "https://windmill.example.com/", workspaceId: "team-b-staging" }, ); - expect(byProfile.includes).toEqual(["f/ops_provinzial/**"]); + expect(byProfile.includes).toEqual(["f/team_b/**"]); });