Compare commits
2 Commits
49431def83
...
0047671458
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0047671458 | ||
|
|
a8169da11c |
@ -1,13 +1,14 @@
|
|||||||
// Onboarding scan — real filesystem scan, Git analysis, and profile compilation.
|
// Onboarding scan — real filesystem scan, Git analysis, and profile compilation.
|
||||||
// Runs synchronously on the host machine (no sandbox needed).
|
// Runs synchronously on the host machine (no sandbox needed).
|
||||||
import { spawn } from "node:child_process";
|
import { spawn } from "node:child_process";
|
||||||
import { access, readdir, realpath, stat } from "node:fs/promises";
|
import { access, readdir, stat } from "node:fs/promises";
|
||||||
import { basename, dirname, join } from "node:path";
|
import { basename, dirname, join } from "node:path";
|
||||||
import type { Actor } from "@rakazo/contracts";
|
import type { Actor } from "@rakazo/contracts";
|
||||||
import type { PrismaClient } from "@rakazo/db";
|
import type { PrismaClient } from "@rakazo/db";
|
||||||
import {
|
import {
|
||||||
createSpaceForMember,
|
createSpaceForMember,
|
||||||
InvalidSpaceNameError,
|
InvalidSpaceNameError,
|
||||||
|
resolveProjectWorkspaceDirectory,
|
||||||
SpaceLimitError,
|
SpaceLimitError,
|
||||||
wireProjectWorkspace,
|
wireProjectWorkspace,
|
||||||
} from "@rakazo/db";
|
} from "@rakazo/db";
|
||||||
@ -656,7 +657,8 @@ async function createProjects(
|
|||||||
|
|
||||||
for (const repo of repos) {
|
for (const repo of repos) {
|
||||||
// Compare and link the real folder, so the next scan recognizes the projects of this one.
|
// Compare and link the real folder, so the next scan recognizes the projects of this one.
|
||||||
const folder = await realpath(repo.path).catch(() => null);
|
// Checking it before the project exists means a folder that cannot be linked leaves none.
|
||||||
|
const folder = await resolveProjectWorkspaceDirectory(repo.path).catch(() => null);
|
||||||
if (!folder || linkedFolders.has(folder)) continue;
|
if (!folder || linkedFolders.has(folder)) continue;
|
||||||
let space: { id: string; name: string };
|
let space: { id: string; name: string };
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -137,6 +137,7 @@ import {
|
|||||||
releaseSpaceDeletionClaim,
|
releaseSpaceDeletionClaim,
|
||||||
renewSpaceDeletionClaim,
|
renewSpaceDeletionClaim,
|
||||||
requireMembership,
|
requireMembership,
|
||||||
|
resolveProjectWorkspaceDirectory,
|
||||||
SPACE_DELETION_CLAIM_TIMEOUT_MS,
|
SPACE_DELETION_CLAIM_TIMEOUT_MS,
|
||||||
SpaceDeletionInProgressError,
|
SpaceDeletionInProgressError,
|
||||||
SpaceLimitError,
|
SpaceLimitError,
|
||||||
@ -573,6 +574,18 @@ export function createRouter(deps: RouterDeps) {
|
|||||||
if (input.workspacePath && !context.actor.isDeploymentOwner) {
|
if (input.workspacePath && !context.actor.isDeploymentOwner) {
|
||||||
throw new ORPCError("FORBIDDEN");
|
throw new ORPCError("FORBIDDEN");
|
||||||
}
|
}
|
||||||
|
// Check the folder before the space exists, so a bad path leaves no space behind.
|
||||||
|
let workspacePath: string | null = null;
|
||||||
|
if (input.workspacePath) {
|
||||||
|
try {
|
||||||
|
workspacePath = await resolveProjectWorkspaceDirectory(input.workspacePath);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof InvalidProjectWorkspacePathError) {
|
||||||
|
throw new ORPCError("BAD_REQUEST", { message: error.message });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
let space: { id: string; name: string };
|
let space: { id: string; name: string };
|
||||||
try {
|
try {
|
||||||
space = await createSpaceForMember(deps.prisma, {
|
space = await createSpaceForMember(deps.prisma, {
|
||||||
@ -586,20 +599,9 @@ export function createRouter(deps: RouterDeps) {
|
|||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
let workspace = null;
|
const workspace = workspacePath
|
||||||
if (input.workspacePath) {
|
? await wireProjectWorkspace(deps.prisma, { spaceId: space.id, workspacePath })
|
||||||
try {
|
: null;
|
||||||
workspace = await wireProjectWorkspace(deps.prisma, {
|
|
||||||
spaceId: space.id,
|
|
||||||
workspacePath: input.workspacePath,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof InvalidProjectWorkspacePathError) {
|
|
||||||
throw new ORPCError("BAD_REQUEST", { message: error.message });
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
id: space.id,
|
id: space.id,
|
||||||
name: space.name,
|
name: space.name,
|
||||||
|
|||||||
@ -72,7 +72,7 @@ describe("project workspace wiring", () => {
|
|||||||
|
|
||||||
const wired = await wireProjectWorkspace(prisma as unknown as Pick<PrismaClient, "space">, {
|
const wired = await wireProjectWorkspace(prisma as unknown as Pick<PrismaClient, "space">, {
|
||||||
spaceId: "space-1",
|
spaceId: "space-1",
|
||||||
workspacePath: root,
|
workspacePath: await resolveProjectWorkspaceDirectory(root),
|
||||||
});
|
});
|
||||||
expect(wired.path).toBe(realpathSync(root));
|
expect(wired.path).toBe(realpathSync(root));
|
||||||
expect(wired.git).toEqual({
|
expect(wired.git).toEqual({
|
||||||
|
|||||||
@ -50,18 +50,17 @@ export interface WiredProjectWorkspace {
|
|||||||
} | null;
|
} | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Persist a host folder and optional git metadata on a newly created space. */
|
/** Persist a folder from `resolveProjectWorkspaceDirectory`, and its git metadata, on a new space. */
|
||||||
export async function wireProjectWorkspace(
|
export async function wireProjectWorkspace(
|
||||||
prisma: Pick<PrismaClient, "space">,
|
prisma: Pick<PrismaClient, "space">,
|
||||||
input: { spaceId: string; workspacePath: string },
|
input: { spaceId: string; workspacePath: string },
|
||||||
): Promise<WiredProjectWorkspace> {
|
): Promise<WiredProjectWorkspace> {
|
||||||
const resolved = await resolveProjectWorkspaceDirectory(input.workspacePath);
|
const git = await readGitCheckout(input.workspacePath);
|
||||||
const git = await readGitCheckout(resolved);
|
|
||||||
|
|
||||||
await prisma.space.update({
|
await prisma.space.update({
|
||||||
where: { id: input.spaceId },
|
where: { id: input.spaceId },
|
||||||
data: {
|
data: {
|
||||||
workspacePath: resolved,
|
workspacePath: input.workspacePath,
|
||||||
gitRemoteUrl: git.present && git.remoteUrl ? git.remoteUrl : null,
|
gitRemoteUrl: git.present && git.remoteUrl ? git.remoteUrl : null,
|
||||||
gitDefaultBranch: git.present && git.defaultBranch ? git.defaultBranch : null,
|
gitDefaultBranch: git.present && git.defaultBranch ? git.defaultBranch : null,
|
||||||
gitHeadCommit: git.present ? git.headCommit : null,
|
gitHeadCommit: git.present ? git.headCommit : null,
|
||||||
@ -69,7 +68,7 @@ export async function wireProjectWorkspace(
|
|||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
path: resolved,
|
path: input.workspacePath,
|
||||||
git:
|
git:
|
||||||
git.present && git.remoteUrl && git.defaultBranch
|
git.present && git.remoteUrl && git.defaultBranch
|
||||||
? {
|
? {
|
||||||
|
|||||||
@ -2239,15 +2239,25 @@ describeWithDatabase("API authorization and resource isolation", () => {
|
|||||||
const legacyNavigation = await rpc<SpaceNavigation>(app, member, "spaces/list");
|
const legacyNavigation = await rpc<SpaceNavigation>(app, member, "spaces/list");
|
||||||
expect(legacyNavigation.spaces.find((space) => space.id === legacy.id)?.workspace).toBeNull();
|
expect(legacyNavigation.spaces.find((space) => space.id === legacy.id)?.workspace).toBeNull();
|
||||||
|
|
||||||
|
// A bad folder is rejected before the project exists, so fixing it and retrying leaves one.
|
||||||
|
for (const badPath of [path.join(hostFolder, "missing"), path.join(hostFolder, ".env")]) {
|
||||||
|
const rejected = await raw(app, owner, "spaces/create", {
|
||||||
|
name: "Owner folder",
|
||||||
|
workspacePath: badPath,
|
||||||
|
});
|
||||||
|
expect(rejected.status, badPath).toBe(400);
|
||||||
|
}
|
||||||
const linked = await rpc<Space>(app, owner, "spaces/create", {
|
const linked = await rpc<Space>(app, owner, "spaces/create", {
|
||||||
name: "Owner folder",
|
name: "Owner folder",
|
||||||
workspacePath: hostFolder,
|
workspacePath: hostFolder,
|
||||||
});
|
});
|
||||||
expect(linked.workspace?.path).toBe(hostFolder);
|
expect(linked.workspace?.path).toBe(hostFolder);
|
||||||
const ownerNavigation = await rpc<SpaceNavigation>(app, owner, "spaces/list");
|
const ownerNavigation = await rpc<SpaceNavigation>(app, owner, "spaces/list");
|
||||||
expect(ownerNavigation.spaces.find((space) => space.id === linked.id)?.workspace?.path).toBe(
|
expect(
|
||||||
hostFolder,
|
ownerNavigation.spaces
|
||||||
);
|
.filter((space) => space.name === "Owner folder")
|
||||||
|
.map((space) => space.workspace?.path),
|
||||||
|
).toEqual([hostFolder]);
|
||||||
const ownerBot = await rpc<Bot>(
|
const ownerBot = await rpc<Bot>(
|
||||||
app,
|
app,
|
||||||
owner,
|
owner,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user