Prepare datamodel for multi-environment (#1765)
Co-authored-by: justsomelegs <[email protected]> Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
This commit is contained in:
121 files changed
+10195
-4958
No files matched your search
@@ -0,0 +1,193 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import { Duration, Effect, FileSystem, Layer } from "effect";
|
||||
import { TestClock } from "effect/testing";
|
||||
|
||||
import { runProcess } from "../../processRunner.ts";
|
||||
import { RepositoryIdentityResolver } from "../Services/RepositoryIdentityResolver.ts";
|
||||
import {
|
||||
makeRepositoryIdentityResolver,
|
||||
RepositoryIdentityResolverLive,
|
||||
} from "./RepositoryIdentityResolver.ts";
|
||||
|
||||
const git = (cwd: string, args: ReadonlyArray<string>) =>
|
||||
Effect.promise(() => runProcess("git", ["-C", cwd, ...args]));
|
||||
|
||||
const makeRepositoryIdentityResolverTestLayer = (options: {
|
||||
readonly positiveCacheTtl?: Duration.Input;
|
||||
readonly negativeCacheTtl?: Duration.Input;
|
||||
}) =>
|
||||
Layer.effect(
|
||||
RepositoryIdentityResolver,
|
||||
makeRepositoryIdentityResolver({
|
||||
cacheCapacity: 16,
|
||||
...options,
|
||||
}),
|
||||
);
|
||||
|
||||
it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
|
||||
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-test-",
|
||||
});
|
||||
|
||||
yield* git(cwd, ["init"]);
|
||||
yield* git(cwd, ["remote", "add", "origin", "[email protected]:T3Tools/t3code.git"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const identity = yield* resolver.resolve(cwd);
|
||||
|
||||
expect(identity).not.toBeNull();
|
||||
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
|
||||
expect(identity?.displayName).toBe("t3tools/t3code");
|
||||
expect(identity?.provider).toBe("github");
|
||||
expect(identity?.owner).toBe("t3tools");
|
||||
expect(identity?.name).toBe("t3code");
|
||||
}).pipe(Effect.provide(RepositoryIdentityResolverLive)),
|
||||
);
|
||||
|
||||
it.effect("returns null for non-git folders and repos without remotes", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const nonGitDir = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-non-git-",
|
||||
});
|
||||
const gitDir = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-no-remote-",
|
||||
});
|
||||
|
||||
yield* git(gitDir, ["init"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const nonGitIdentity = yield* resolver.resolve(nonGitDir);
|
||||
const noRemoteIdentity = yield* resolver.resolve(gitDir);
|
||||
|
||||
expect(nonGitIdentity).toBeNull();
|
||||
expect(noRemoteIdentity).toBeNull();
|
||||
}).pipe(Effect.provide(RepositoryIdentityResolverLive)),
|
||||
);
|
||||
|
||||
it.effect("prefers upstream over origin when both remotes are configured", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-upstream-test-",
|
||||
});
|
||||
|
||||
yield* git(cwd, ["init"]);
|
||||
yield* git(cwd, ["remote", "add", "origin", "[email protected]:julius/t3code.git"]);
|
||||
yield* git(cwd, ["remote", "add", "upstream", "[email protected]:T3Tools/t3code.git"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const identity = yield* resolver.resolve(cwd);
|
||||
|
||||
expect(identity).not.toBeNull();
|
||||
expect(identity?.locator.remoteName).toBe("upstream");
|
||||
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
|
||||
expect(identity?.displayName).toBe("t3tools/t3code");
|
||||
}).pipe(Effect.provide(RepositoryIdentityResolverLive)),
|
||||
);
|
||||
|
||||
it.effect("uses the last remote path segment as the repository name for nested groups", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-nested-group-test-",
|
||||
});
|
||||
|
||||
yield* git(cwd, ["init"]);
|
||||
yield* git(cwd, ["remote", "add", "origin", "[email protected]:T3Tools/platform/t3code.git"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const identity = yield* resolver.resolve(cwd);
|
||||
|
||||
expect(identity).not.toBeNull();
|
||||
expect(identity?.canonicalKey).toBe("gitlab.com/t3tools/platform/t3code");
|
||||
expect(identity?.displayName).toBe("t3tools/platform/t3code");
|
||||
expect(identity?.owner).toBe("t3tools");
|
||||
expect(identity?.name).toBe("t3code");
|
||||
}).pipe(Effect.provide(RepositoryIdentityResolverLive)),
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"refreshes cached null identities after the negative TTL when a remote is configured later",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-late-remote-test-",
|
||||
});
|
||||
|
||||
yield* git(cwd, ["init"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const initialIdentity = yield* resolver.resolve(cwd);
|
||||
expect(initialIdentity).toBeNull();
|
||||
|
||||
yield* git(cwd, ["remote", "add", "origin", "[email protected]:T3Tools/t3code.git"]);
|
||||
|
||||
const cachedIdentity = yield* resolver.resolve(cwd);
|
||||
expect(cachedIdentity).toBeNull();
|
||||
|
||||
yield* TestClock.adjust(Duration.millis(120));
|
||||
|
||||
const refreshedIdentity = yield* resolver.resolve(cwd);
|
||||
expect(refreshedIdentity).not.toBeNull();
|
||||
expect(refreshedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
|
||||
expect(refreshedIdentity?.name).toBe("t3code");
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.merge(
|
||||
TestClock.layer(),
|
||||
makeRepositoryIdentityResolverTestLayer({
|
||||
negativeCacheTtl: Duration.millis(50),
|
||||
positiveCacheTtl: Duration.seconds(1),
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("refreshes cached identities after the positive TTL when a remote changes", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const cwd = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-repository-identity-remote-change-test-",
|
||||
});
|
||||
|
||||
yield* git(cwd, ["init"]);
|
||||
yield* git(cwd, ["remote", "add", "origin", "[email protected]:T3Tools/t3code.git"]);
|
||||
|
||||
const resolver = yield* RepositoryIdentityResolver;
|
||||
const initialIdentity = yield* resolver.resolve(cwd);
|
||||
expect(initialIdentity).not.toBeNull();
|
||||
expect(initialIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
|
||||
|
||||
yield* git(cwd, ["remote", "set-url", "origin", "[email protected]:T3Tools/t3code-next.git"]);
|
||||
|
||||
const cachedIdentity = yield* resolver.resolve(cwd);
|
||||
expect(cachedIdentity).not.toBeNull();
|
||||
expect(cachedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code");
|
||||
|
||||
yield* TestClock.adjust(Duration.millis(180));
|
||||
|
||||
const refreshedIdentity = yield* resolver.resolve(cwd);
|
||||
expect(refreshedIdentity).not.toBeNull();
|
||||
expect(refreshedIdentity?.canonicalKey).toBe("github.com/t3tools/t3code-next");
|
||||
expect(refreshedIdentity?.displayName).toBe("t3tools/t3code-next");
|
||||
expect(refreshedIdentity?.name).toBe("t3code-next");
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.merge(
|
||||
TestClock.layer(),
|
||||
makeRepositoryIdentityResolverTestLayer({
|
||||
negativeCacheTtl: Duration.millis(50),
|
||||
positiveCacheTtl: Duration.millis(100),
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,147 @@
|
||||
import type { RepositoryIdentity } from "@t3tools/contracts";
|
||||
import { Cache, Duration, Effect, Exit, Layer } from "effect";
|
||||
import { detectGitHostingProviderFromRemoteUrl, normalizeGitRemoteUrl } from "@t3tools/shared/git";
|
||||
|
||||
import { runProcess } from "../../processRunner.ts";
|
||||
import {
|
||||
RepositoryIdentityResolver,
|
||||
type RepositoryIdentityResolverShape,
|
||||
} from "../Services/RepositoryIdentityResolver.ts";
|
||||
|
||||
function parseRemoteFetchUrls(stdout: string): Map<string, string> {
|
||||
const remotes = new Map<string, string>();
|
||||
for (const line of stdout.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.length === 0) continue;
|
||||
const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)$/.exec(trimmed);
|
||||
if (!match) continue;
|
||||
const [, remoteName = "", remoteUrl = "", direction = ""] = match;
|
||||
if (direction !== "fetch" || remoteName.length === 0 || remoteUrl.length === 0) {
|
||||
continue;
|
||||
}
|
||||
remotes.set(remoteName, remoteUrl);
|
||||
}
|
||||
return remotes;
|
||||
}
|
||||
|
||||
function pickPrimaryRemote(
|
||||
remotes: ReadonlyMap<string, string>,
|
||||
): { readonly remoteName: string; readonly remoteUrl: string } | null {
|
||||
for (const preferredRemoteName of ["upstream", "origin"] as const) {
|
||||
const remoteUrl = remotes.get(preferredRemoteName);
|
||||
if (remoteUrl) {
|
||||
return { remoteName: preferredRemoteName, remoteUrl };
|
||||
}
|
||||
}
|
||||
|
||||
const [remoteName, remoteUrl] =
|
||||
[...remotes.entries()].toSorted(([left], [right]) => left.localeCompare(right))[0] ?? [];
|
||||
return remoteName && remoteUrl ? { remoteName, remoteUrl } : null;
|
||||
}
|
||||
|
||||
function buildRepositoryIdentity(input: {
|
||||
readonly remoteName: string;
|
||||
readonly remoteUrl: string;
|
||||
}): RepositoryIdentity {
|
||||
const canonicalKey = normalizeGitRemoteUrl(input.remoteUrl);
|
||||
const hostingProvider = detectGitHostingProviderFromRemoteUrl(input.remoteUrl);
|
||||
const repositoryPath = canonicalKey.split("/").slice(1).join("/");
|
||||
const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0);
|
||||
const [owner] = repositoryPathSegments;
|
||||
const repositoryName = repositoryPathSegments.at(-1);
|
||||
|
||||
return {
|
||||
canonicalKey,
|
||||
locator: {
|
||||
source: "git-remote",
|
||||
remoteName: input.remoteName,
|
||||
remoteUrl: input.remoteUrl,
|
||||
},
|
||||
...(repositoryPath ? { displayName: repositoryPath } : {}),
|
||||
...(hostingProvider ? { provider: hostingProvider.kind } : {}),
|
||||
...(owner ? { owner } : {}),
|
||||
...(repositoryName ? { name: repositoryName } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
|
||||
const DEFAULT_POSITIVE_CACHE_TTL = Duration.minutes(1);
|
||||
const DEFAULT_NEGATIVE_CACHE_TTL = Duration.seconds(10);
|
||||
|
||||
interface RepositoryIdentityResolverOptions {
|
||||
readonly cacheCapacity?: number;
|
||||
readonly positiveCacheTtl?: Duration.Input;
|
||||
readonly negativeCacheTtl?: Duration.Input;
|
||||
}
|
||||
|
||||
async function resolveRepositoryIdentityCacheKey(cwd: string): Promise<string> {
|
||||
let cacheKey = cwd;
|
||||
|
||||
try {
|
||||
const topLevelResult = await runProcess("git", ["-C", cwd, "rev-parse", "--show-toplevel"], {
|
||||
allowNonZeroExit: true,
|
||||
});
|
||||
if (topLevelResult.code !== 0) {
|
||||
return cacheKey;
|
||||
}
|
||||
|
||||
const candidate = topLevelResult.stdout.trim();
|
||||
if (candidate.length > 0) {
|
||||
cacheKey = candidate;
|
||||
}
|
||||
} catch {
|
||||
return cacheKey;
|
||||
}
|
||||
|
||||
return cacheKey;
|
||||
}
|
||||
|
||||
async function resolveRepositoryIdentityFromCacheKey(
|
||||
cacheKey: string,
|
||||
): Promise<RepositoryIdentity | null> {
|
||||
try {
|
||||
const remoteResult = await runProcess("git", ["-C", cacheKey, "remote", "-v"], {
|
||||
allowNonZeroExit: true,
|
||||
});
|
||||
if (remoteResult.code !== 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.stdout));
|
||||
return remote ? buildRepositoryIdentity(remote) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityResolver")(
|
||||
function* (options: RepositoryIdentityResolverOptions = {}) {
|
||||
const repositoryIdentityCache = yield* Cache.makeWith<string, RepositoryIdentity | null>({
|
||||
capacity: options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY,
|
||||
lookup: (cacheKey) => Effect.promise(() => resolveRepositoryIdentityFromCacheKey(cacheKey)),
|
||||
timeToLive: Exit.match({
|
||||
onSuccess: (value) =>
|
||||
value === null
|
||||
? (options.negativeCacheTtl ?? DEFAULT_NEGATIVE_CACHE_TTL)
|
||||
: (options.positiveCacheTtl ?? DEFAULT_POSITIVE_CACHE_TTL),
|
||||
onFailure: () => Duration.zero,
|
||||
}),
|
||||
});
|
||||
|
||||
const resolve: RepositoryIdentityResolverShape["resolve"] = Effect.fn(
|
||||
"RepositoryIdentityResolver.resolve",
|
||||
)(function* (cwd) {
|
||||
const cacheKey = yield* Effect.promise(() => resolveRepositoryIdentityCacheKey(cwd));
|
||||
return yield* Cache.get(repositoryIdentityCache, cacheKey);
|
||||
});
|
||||
|
||||
return {
|
||||
resolve,
|
||||
} satisfies RepositoryIdentityResolverShape;
|
||||
},
|
||||
);
|
||||
|
||||
export const RepositoryIdentityResolverLive = Layer.effect(
|
||||
RepositoryIdentityResolver,
|
||||
makeRepositoryIdentityResolver(),
|
||||
);
|
||||
Reference in new issue
Block a user