From a7579fb2adccc95305fdacaeef0d7c5c982e20e9 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 5 Nov 2026 09:21:22 +0100 Subject: [PATCH] fix: resolve gitsync-settings wmill.yaml entry by workspace, not branch Co-Authored-By: Claude Opus 5.5 --- cli/src/commands/gitsync-settings/pull.ts | 9 +++-- cli/src/commands/gitsync-settings/push.ts | 7 ++-- cli/src/commands/gitsync-settings/utils.ts | 27 ++++++++++++- ...settings_workspace_resolution_unit.test.ts | 38 +++++++++++++++++++ 4 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 cli/test/gitsync_settings_workspace_resolution_unit.test.ts diff --git a/cli/src/commands/gitsync-settings/pull.ts b/cli/src/commands/gitsync-settings/pull.ts index 910ba17942..9777a27ce0 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, getEffectiveSettings, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig } from "../../core/conf.ts"; +import { SyncOptions, readConfigFile, DEFAULT_SYNC_OPTIONS, getWmillYamlPath, findWorkspaceByGitBranch, WorkspacesConfig } from "../../core/conf.ts"; import { yamlOptions } from "../sync/sync.ts"; import { deepEqual } from "../../utils/utils.ts"; import { getCurrentGitBranch, isGitRepository } from "../../utils/git.ts"; @@ -21,7 +21,8 @@ import { displayChanges, applyBackendSettingsToBranch, normalizeRepoPath, - outputResult + outputResult, + getEffectiveSettingsForWorkspace } from "./utils.ts"; export async function pullGitSyncSettings( @@ -199,7 +200,7 @@ export async function pullGitSyncSettings( // For existing wmill.yaml files, continue with the normal flow // Calculate current settings once and reuse throughout - const currentSettings = await getEffectiveSettings(localConfig, opts.promotion, true, opts.jsonOutput); + const currentSettings = await getEffectiveSettingsForWorkspace(localConfig, opts, workspace); // Determine write mode for branch-based configuration let writeMode: WriteMode = "replace"; @@ -280,7 +281,7 @@ export async function pullGitSyncSettings( !opts.override ) { // Recalculate current settings with promotion if needed - const effectiveCurrentSettings = await getEffectiveSettings(localConfig, opts.promotion, true, opts.jsonOutput); + const effectiveCurrentSettings = await getEffectiveSettingsForWorkspace(localConfig, opts, workspace); // Use the same logic as diff to determine current settings const gitSyncBackend = GitSyncSettingsConverter.extractGitSyncFields( GitSyncSettingsConverter.normalize(backendSyncOptions), diff --git a/cli/src/commands/gitsync-settings/push.ts b/cli/src/commands/gitsync-settings/push.ts index 0392aa8722..a89f7f6c77 100644 --- a/cli/src/commands/gitsync-settings/push.ts +++ b/cli/src/commands/gitsync-settings/push.ts @@ -7,7 +7,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, validateBranchConfiguration, getEffectiveSettings, getWmillYamlPath } from "../../core/conf.ts"; +import { SyncOptions, readConfigFile, validateBranchConfiguration, getWmillYamlPath } from "../../core/conf.ts"; import { deepEqual } from "../../utils/utils.ts"; import { GitSyncRepository } from "./types.ts"; @@ -19,7 +19,8 @@ import { generateChanges, displayChanges, normalizeRepoPath, - outputResult + outputResult, + getEffectiveSettingsForWorkspace } from "./utils.ts"; export async function pushGitSyncSettings( @@ -178,7 +179,7 @@ export async function pushGitSyncSettings( // Get effective settings for this workspace/repo const repoPath = normalizeRepoPath(selectedRepo.git_repo_resource_path); - const effectiveSettings = await getEffectiveSettings(localConfig, opts.promotion, true, opts.jsonOutput); + const effectiveSettings = await getEffectiveSettingsForWorkspace(localConfig, opts, workspace); // Convert to backend format const backendFormat = GitSyncSettingsConverter.toBackendFormat(effectiveSettings); diff --git a/cli/src/commands/gitsync-settings/utils.ts b/cli/src/commands/gitsync-settings/utils.ts index 3119f2bcda..8bc5e1be8d 100644 --- a/cli/src/commands/gitsync-settings/utils.ts +++ b/cli/src/commands/gitsync-settings/utils.ts @@ -1,7 +1,14 @@ import { colors } from "@cliffy/ansi/colors"; import * as log from "../../core/log.ts"; import { deepEqual, selectRepository } from "../../utils/utils.ts"; -import { SyncOptions, getEffectiveSettings, DEFAULT_SYNC_OPTIONS } from "../../core/conf.ts"; +import { + SyncOptions, + getEffectiveSettings, + DEFAULT_SYNC_OPTIONS, + resolveWsNameForConfigFromFlags, + inferWsNameFromProfile, + inferWsNameFromForkBranch, +} from "../../core/conf.ts"; import { GitSyncRepository, GIT_SYNC_FIELDS } from "./types.ts"; import { GitSyncSettingsConverter } from "./converter.ts"; @@ -21,6 +28,24 @@ export function outputResult(opts: { jsonOutput?: boolean }, result: { } } +/** + * The wmill.yaml settings that apply to the workspace being synced. Resolved by + * workspace first, as `sync pull/push` do: several entries can share one + * `gitBranch`, so matching on the checked-out branch alone picks whichever of + * them comes first in the file. + */ +export async function getEffectiveSettingsForWorkspace( + config: SyncOptions, + opts: { workspace?: string; promotion?: string; jsonOutput?: boolean }, + workspace: { remote: string; workspaceId: string }, +): Promise { + const wsName = + resolveWsNameForConfigFromFlags({ workspaces: config.workspaces, workspace: opts.workspace }) ?? + inferWsNameFromProfile(config, workspace) ?? + inferWsNameFromForkBranch(config, workspace.workspaceId); + return await getEffectiveSettings(config, opts.promotion, true, opts.jsonOutput, wsName); +} + // Helper to normalize repository path by removing $res: prefix export function normalizeRepoPath(path: string): string { return path.replace(/^\$res:/, ""); diff --git a/cli/test/gitsync_settings_workspace_resolution_unit.test.ts b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts new file mode 100644 index 0000000000..243a6a1006 --- /dev/null +++ b/cli/test/gitsync_settings_workspace_resolution_unit.test.ts @@ -0,0 +1,38 @@ +import { expect, test } from "bun:test"; +import type { SyncOptions } from "../src/core/conf.ts"; +import { getEffectiveSettingsForWorkspace } from "../src/commands/gitsync-settings/utils.ts"; + +// Entries sharing a gitBranch must resolve by workspace, not by the first one +// bound to the branch. +test("gitsync-settings: entries sharing a gitBranch resolve by workspace", async () => { + const config: SyncOptions = { + includes: ["f/**"], + workspaces: { + "flh-nonprod": { + baseUrl: "https://windmill.example.com/", + gitBranch: "nonprod", + overrides: { includes: ["f/ops_flh/**"] }, + }, + provinzial: { + baseUrl: "https://windmill.example.com/", + workspaceId: "provinzial-nonprod", + gitBranch: "nonprod", + overrides: { includes: ["f/ops_provinzial/**"] }, + }, + }, + }; + + const byFlag = await getEffectiveSettingsForWorkspace( + config, + { workspace: "flh-nonprod", jsonOutput: true }, + { remote: "http://windmill_server:8000/", workspaceId: "flh-nonprod" }, + ); + expect(byFlag.includes).toEqual(["f/ops_flh/**"]); + + const byProfile = await getEffectiveSettingsForWorkspace( + config, + { workspace: "provinzial-nonprod", jsonOutput: true }, + { remote: "https://windmill.example.com/", workspaceId: "provinzial-nonprod" }, + ); + expect(byProfile.includes).toEqual(["f/ops_provinzial/**"]); +});