From 4314eb8daa8b95ccc206a73ceb897ec23c8dc0d4 Mon Sep 17 00:00:00 2001 From: Hydra Date: Fri, 13 Nov 2026 18:19:38 +0200 Subject: [PATCH] fix(dashboard): read runtime source GitHub metadata from API contract --- .../updates/RuntimeSourceUpdate.test.tsx | 41 +++++++++++++++---- .../updates/RuntimeSourceUpdate.tsx | 35 +++++++--------- .../src/context/ProjectSettingsContext.tsx | 3 +- apps/dashboard/src/lib/api/projects.ts | 5 ++- 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/apps/dashboard/src/components/updates/RuntimeSourceUpdate.test.tsx b/apps/dashboard/src/components/updates/RuntimeSourceUpdate.test.tsx index dba1cef03..614e607b2 100644 --- a/apps/dashboard/src/components/updates/RuntimeSourceUpdate.test.tsx +++ b/apps/dashboard/src/components/updates/RuntimeSourceUpdate.test.tsx @@ -7,7 +7,7 @@ import { CloudResourceContext } from "@/context/CloudResourceContext"; import { ApiError, setApiResourceScope } from "@/lib/api/client"; import { RuntimeSourceUpdate } from "./RuntimeSourceUpdate"; import { SecurityProjectReview } from "./SecurityProjectReview"; -import type { ProjectSecurityResult } from "@repo/contracts"; +import type { ProjectControlOperations, ProjectSecurityResult } from "@repo/contracts"; const api = vi.hoisted(() => ({ git: vi.fn(), read: vi.fn(), preview: vi.fn(), publish: vi.fn() })); vi.mock("@/lib/api/projects", () => ({ projectsApi: { getGit: api.git } })); @@ -23,6 +23,22 @@ vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn() }) })); vi.mock("@/context/ToastContext", () => ({ useToast: () => ({ showToast: vi.fn() }) })); vi.mock("@/hooks/useRuntimeUpdate", () => ({ useRuntimeUpdate: () => ({ updating: false }) })); let root: Root, host: HTMLDivElement; +const gitInfo = { + success: true, + provider: "github", + owner: "acme", + repo: "app", + branch: "main", + commits: [], + auto_deploy: false, + webhook_strategy: "app", + webhook_active: true, + webhook_domain: null, + available_strategies: ["app"], + verified_domains: [], + installation_installed: true, + default_rollback_strategy: "git", +} satisfies Awaited>; const source = { owner: "acme", repo: "app", @@ -38,12 +54,7 @@ beforeEach(() => { vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); setApiResourceScope("source-test"); vi.resetAllMocks(); - api.git.mockResolvedValue({ - gitProvider: "github", - gitOwner: "acme", - gitRepo: "app", - gitBranch: "main", - }); + api.git.mockResolvedValue(gitInfo); api.read.mockResolvedValue({ data: source }); api.preview.mockResolvedValue({ data: { @@ -90,6 +101,8 @@ async function setImage(value: string) { it("requires a reviewed diff, invalidates it on edit, and creates a PR without triggering a deployment", async () => { await render(); await click("Propose image update"); + expect(api.read).toHaveBeenCalledWith("project", { path: "Dockerfile", format: "dockerfile" }); + expect(document.body.textContent).toContain("acme/app ยท main"); await setImage("node:24"); expect(document.body.textContent).not.toContain("Create draft PR"); await click("Review change"); @@ -109,6 +122,20 @@ it("requires a reviewed diff, invalidates it on edit, and creates a PR without t ); expect(document.body.textContent).toContain("Recheck the deployed runtime"); }); +it.each([ + { success: false, error: "No repository connected", code: "NO_REPOSITORY" }, + { ...gitInfo, provider: "local" }, + { ...gitInfo, branch: "" }, +])("does not load or publish source without a supported repository and branch: %j", async (git) => { + api.git.mockResolvedValue(git); + await render(); + await click("Propose image update"); + expect(document.body.textContent).toContain( + "This project needs a connected GitHub repository and deployment branch.", + ); + expect(api.read).not.toHaveBeenCalled(); + expect(api.publish).not.toHaveBeenCalled(); +}); it("discards source and pending responses after an account/workspace switch", async () => { let resolve!: (value: unknown) => void; api.read.mockImplementationOnce( diff --git a/apps/dashboard/src/components/updates/RuntimeSourceUpdate.tsx b/apps/dashboard/src/components/updates/RuntimeSourceUpdate.tsx index 0bdecdb2e..28549358a 100644 --- a/apps/dashboard/src/components/updates/RuntimeSourceUpdate.tsx +++ b/apps/dashboard/src/components/updates/RuntimeSourceUpdate.tsx @@ -122,27 +122,20 @@ function SourceEditor({ setBusy(true); projectsApi .getGit(projectId) - .then( - async (git: { - gitProvider?: string; - gitOwner?: string; - gitRepo?: string; - gitBranch?: string; - }) => { - if (!current(request)) return; - if (git.gitProvider !== "github" || !git.gitOwner || !git.gitRepo || !git.gitBranch) { - setError(copy.noRepository); - return; - } - setRepo({ owner: git.gitOwner, repo: git.gitRepo, branch: git.gitBranch }); - const { data } = await updatesApi.runtimeSource(projectId, { - path: initialPath, - format: /\.ya?ml$/i.test(initialPath) ? "compose" : "dockerfile", - ...(serviceId && { serviceId }), - }); - if (current(request)) acceptSource(data); - }, - ) + .then(async (git) => { + if (!current(request)) return; + if (!git.success || git.provider !== "github" || !git.owner || !git.repo || !git.branch) { + setError(copy.noRepository); + return; + } + setRepo({ owner: git.owner, repo: git.repo, branch: git.branch }); + const { data } = await updatesApi.runtimeSource(projectId, { + path: initialPath, + format: /\.ya?ml$/i.test(initialPath) ? "compose" : "dockerfile", + ...(serviceId && { serviceId }), + }); + if (current(request)) acceptSource(data); + }) .catch((error) => { // diagnostics-ignore: showError reports through getApiErrorMessage and displays recovery. if (current(request)) showError(error); diff --git a/apps/dashboard/src/context/ProjectSettingsContext.tsx b/apps/dashboard/src/context/ProjectSettingsContext.tsx index 0a84110b2..a89f1139d 100644 --- a/apps/dashboard/src/context/ProjectSettingsContext.tsx +++ b/apps/dashboard/src/context/ProjectSettingsContext.tsx @@ -755,7 +755,8 @@ const ProjectSettingsState: React.FC = ({ verifiedDomains: response.verified_domains, installationInstalled: response.installation_installed, installUrl: response.install_url, - defaultRollbackStrategy: response.default_rollback_strategy, + defaultRollbackStrategy: + response.default_rollback_strategy === "snapshot" ? "snapshot" : "git", }); } else { // "No repository connected" is the NORMAL state for upload/local diff --git a/apps/dashboard/src/lib/api/projects.ts b/apps/dashboard/src/lib/api/projects.ts index 2c941cff8..88f1bf4b1 100644 --- a/apps/dashboard/src/lib/api/projects.ts +++ b/apps/dashboard/src/lib/api/projects.ts @@ -613,7 +613,10 @@ export const projectsApi = { }>(endpoints.projects.env(id)), /** Get git settings */ - getGit: (id: string | number) => api.get(endpoints.projects.git(id)), + getGit: (id: string | number) => + api.get>>( + endpoints.projects.git(id), + ), /** Link a GitHub repo to an existing project + register webhook */ linkRepo: (