Skip to content
TypeScript

fix(shared): saving one source control token no longer drops the other hosts' tokens (#29554)

Yordis Prietobff68fb6b6f8
diff --git a/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx b/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx
index df73fa3b24b..54b0018a249 100644
--- a/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx
+++ b/apps/web/src/components/settings/SourceControlAppSignInSettings.tsx
@@ -171,7 +171,7 @@ export function SourceControlAppSignInSettings({
/>
</span>
) : tokenOverrides ? (
- "Signed in, but the token saved below is used until you remove it."
+ "Signed in, but the token saved below is used while it can be read."
) : (
"Signed in"
);
diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts
index 4bb3119561b..cf8e6effe05 100644
--- a/packages/contracts/src/settings.ts
+++ b/packages/contracts/src/settings.ts
@@ -1696,7 +1696,7 @@ export const ServerSettingsPatch = Schema.Struct({
otlpLogsUrl: Schema.optionalKey(TrimmedString),
}),
),
- /** Merges per host, replacing patched fields whole; redaction markers retain secrets, empty or omitted secrets remove them. */
+ /** Merges per host, replacing patched fields whole; redaction markers retain secrets, empty secrets remove them. A non-empty token map keeps the saved tokens of hosts it omits; an empty map clears them all. */
sourceControlHosts: Schema.optionalKey(Schema.Record(Schema.String, SourceControlHostSettings)),
// Replace whole maps to avoid half-merged driver configs.
providerInstances: Schema.optionalKey(Schema.Record(ProviderInstanceId, ProviderInstanceConfig)),
diff --git a/packages/shared/src/serverSettings.test.ts b/packages/shared/src/serverSettings.test.ts
index a897f3d427a..cfbc9339e4e 100644
--- a/packages/shared/src/serverSettings.test.ts
+++ b/packages/shared/src/serverSettings.test.ts
@@ -127,6 +127,24 @@ describe("serverSettings helpers", () => {
});
});
+ it("keeps a source control host's saved tokens when a patch sets another host's token", () => {
+ const reference = { provider: "vault", reference: "github/token" };
+ const saved = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
+ sourceControlHosts: { github: { tokens: { "github.com": reference } } },
+ });
+ const patched = applyServerSettingsPatch(saved, {
+ sourceControlHosts: { github: { tokens: { "ghe.example": "x" } } },
+ });
+ expect(patched.sourceControlHosts.github?.tokens).toEqual({
+ "github.com": reference,
+ "ghe.example": "x",
+ });
+ const recased = applyServerSettingsPatch(saved, {
+ sourceControlHosts: { github: { tokens: { " GitHub.com ": "ghp_new" } } },
+ });
+ expect(recased.sourceControlHosts.github?.tokens).toEqual({ "github.com": "ghp_new" });
+ });
+
it("inherits actions, preserves existing actions, and supports empty overrides and reset", () => {
const project = { id: ProjectId.make("project-actions"), scripts: [] };
const action = {
diff --git a/packages/shared/src/serverSettings.ts b/packages/shared/src/serverSettings.ts
index e936c9f83ac..0db213a90f7 100644
--- a/packages/shared/src/serverSettings.ts
+++ b/packages/shared/src/serverSettings.ts
@@ -23,6 +23,7 @@ import {
withIdleCompaction,
} from "@t3tools/contracts";
import * as Option from "effect/Option";
+import * as Predicate from "effect/Predicate";
import * as Schema from "effect/Schema";
import { deepMerge } from "./Struct.ts";
import { fromLenientJson } from "./schemaJson.ts";
@@ -226,6 +227,15 @@ function mergeSettingsEntries<Value>(
return Object.fromEntries(next);
}
+// Secret persistence keys tokens by trimmed, lowercased host, so merge on the same key.
+const byHost = (tokens: unknown) =>
+ Object.fromEntries(
+ Object.entries(Predicate.isObject(tokens) ? tokens : {}).map(([host, token]) => [
+ host.trim().toLowerCase(),
+ token,
+ ]),
+ );
+
const knownProjectOverrideKeys = new Set(Object.keys(ProjectSettingsOverrides.fields));
function pickOverrideKeys(entry: ProjectSettingsOverrides | undefined, known: boolean) {
@@ -530,10 +540,26 @@ export function applyServerSettingsPatch(
sourceControlHosts: {
...current.sourceControlHosts,
...Object.fromEntries(
- Object.entries(sourceControlHostsPatch).map(([kind, fields]) => [
- kind,
- { ...current.sourceControlHosts[kind], ...fields },
- ]),
+ Object.entries(sourceControlHostsPatch).map(([kind, fields]) => {
+ const saved = current.sourceControlHosts[kind];
+ return [
+ kind,
+ {
+ ...saved,
+ ...fields,
+ // Older clients send the whole token map; a host missing from it keeps its saved
+ // token. An empty map still clears them all.
+ ...(Predicate.isObject(fields.tokens) && Object.keys(fields.tokens).length > 0
+ ? {
+ tokens: {
+ ...byHost(saved?.tokens),
+ ...byHost(fields.tokens),
+ },
+ }
+ : {}),
+ },
+ ];
+ }),
),
},
}