Skip to content

Commit 05fb8a7

Browse files
committed
fix(dashboard): integrate PR #861 using persisted domain route bindings
2 parents 0a1a91e + e07018d commit 05fb8a7

14 files changed

Lines changed: 499 additions & 52 deletions

File tree

‎apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/Sidebar.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,21 @@ function hasConnectedDomain(service: {
592592
customDomain?: string;
593593
domain?: string;
594594
name?: string;
595+
publicEndpoints?: Array<{
596+
domainType?: "free" | "custom";
597+
customDomain?: string;
598+
domain?: string;
599+
}>;
595600
}) {
596601
if (!service.exposed) return false;
602+
if (service.publicEndpoints && service.publicEndpoints.length > 0) {
603+
const hasEndpointDomain = service.publicEndpoints.some((ep) =>
604+
ep.domainType === "custom"
605+
? Boolean(ep.customDomain?.trim())
606+
: Boolean(ep.domain?.trim()),
607+
);
608+
if (hasEndpointDomain) return true;
609+
}
597610
if (service.domainType === "custom") return Boolean(service.customDomain?.trim());
598611
return Boolean(service.domain?.trim() || service.name?.trim());
599612
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// @vitest-environment happy-dom
2+
3+
import { act, type ReactNode } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
6+
import { baseDictionary } from "@/i18n";
7+
import type { Service } from "@/lib/api/services";
8+
import { Deployments } from "./Deployments";
9+
10+
const mocks = vi.hoisted(() => ({
11+
context: vi.fn(),
12+
trigger: vi.fn(),
13+
showModal: vi.fn(),
14+
hideModal: vi.fn(),
15+
setActiveTab: vi.fn(),
16+
openBuild: vi.fn(),
17+
}));
18+
vi.mock("@/context/ProjectSettingsContext", () => ({ useProjectSettings: mocks.context }));
19+
vi.mock("@/context/ToastContext", () => ({ useToast: () => ({ showToast: vi.fn() }) }));
20+
vi.mock("@/context/ModalContext", () => ({
21+
useModal: () => ({ showModal: mocks.showModal, hideModal: mocks.hideModal }),
22+
}));
23+
vi.mock("@/lib/api", () => ({
24+
deployApi: { trigger: mocks.trigger },
25+
projectsApi: { getCommitStatus: async () => ({ data: { supported: false } }) },
26+
isAbortError: () => false,
27+
}));
28+
vi.mock("@/lib/deploy-nav", () => ({ openTriggeredBuild: mocks.openBuild }));
29+
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn() }) }));
30+
vi.mock("@/components/i18n-provider", () => ({
31+
useI18n: () => ({ t: baseDictionary }),
32+
interpolate: (text: string, values: Record<string, string>) =>
33+
text.replace(/\{(\w+)\}/g, (_, key: string) => values[key] ?? key),
34+
}));
35+
vi.mock("@/app/(dashboard)/deployments/components", () => ({ DeploymentsContent: () => null }));
36+
37+
const service: Service = {
38+
id: "web",
39+
name: "web",
40+
kind: "compose",
41+
image: "web:1",
42+
build: null,
43+
dockerfile: null,
44+
buildArgs: null,
45+
ports: ["20020:9000"],
46+
dependsOn: [],
47+
environment: {},
48+
volumes: [],
49+
command: null,
50+
restart: "unless-stopped",
51+
exposed: false,
52+
exposedPort: "9000",
53+
domain: null,
54+
customDomain: null,
55+
domainType: "custom",
56+
publicEndpoints: [],
57+
enabled: true,
58+
sortOrder: 0,
59+
};
60+
let root: Root;
61+
let container: HTMLDivElement;
62+
let context: {
63+
id: string;
64+
projectData: { id: string; name: string; port: number };
65+
hasMultipleServices: boolean;
66+
servicesData: { services: Service[] };
67+
domainsData: { domains: Array<{ hostname: string; serviceId?: string; targetPort?: number }> };
68+
refreshServices: ReturnType<typeof vi.fn>;
69+
setActiveTab: typeof mocks.setActiveTab;
70+
};
71+
72+
beforeEach(() => {
73+
vi.clearAllMocks();
74+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
75+
container = document.createElement("div");
76+
document.body.append(container);
77+
root = createRoot(container);
78+
context = {
79+
id: "project",
80+
projectData: { id: "project", name: "demo", port: 9000 },
81+
hasMultipleServices: true,
82+
servicesData: { services: [service] },
83+
domainsData: { domains: [] },
84+
refreshServices: vi.fn().mockResolvedValue([service]),
85+
setActiveTab: mocks.setActiveTab,
86+
};
87+
mocks.context.mockImplementation(() => context);
88+
mocks.trigger.mockResolvedValue({ data: { deploymentId: "deployment" } });
89+
mocks.showModal.mockReturnValue("warning");
90+
});
91+
92+
afterEach(async () => {
93+
await act(async () => root.unmount());
94+
container.remove();
95+
vi.unstubAllGlobals();
96+
});
97+
98+
function button(text: string) {
99+
const result = [...container.querySelectorAll("button")].find((el) => el.textContent === text);
100+
expect(result, text).toBeDefined();
101+
return result!;
102+
}
103+
104+
async function redeploy() {
105+
await act(async () => root.render(<Deployments />));
106+
await act(async () => button(baseDictionary.projects.redeploy.redeployProject).click());
107+
}
108+
109+
it.each([
110+
{ hostname: "app.example.test", serviceId: "web", targetPort: 9000 },
111+
{ hostname: "app.example.test", targetPort: 20020 },
112+
{ hostname: "app.example.test" },
113+
])("redeploys using the loaded domain route without a false warning: %j", async (domain) => {
114+
context.domainsData.domains = [domain];
115+
await redeploy();
116+
expect(mocks.showModal).not.toHaveBeenCalled();
117+
expect(mocks.trigger).toHaveBeenCalledWith({ projectId: "project", smartRoute: true });
118+
expect(mocks.openBuild).toHaveBeenCalled();
119+
});
120+
121+
it("checks freshly loaded services against the project's domain routes", async () => {
122+
context.servicesData.services = [];
123+
context.domainsData.domains = [{ hostname: "app.example.test", serviceId: "web" }];
124+
await redeploy();
125+
expect(context.refreshServices).toHaveBeenCalledOnce();
126+
expect(mocks.trigger).toHaveBeenCalledOnce();
127+
expect(mocks.showModal).not.toHaveBeenCalled();
128+
});
129+
130+
it("keeps the warning for another service's route and sends the operator to Domains", async () => {
131+
context.domainsData.domains = [
132+
{ hostname: "other.example.test", serviceId: "other", targetPort: 9000 },
133+
];
134+
await redeploy();
135+
expect(mocks.trigger).not.toHaveBeenCalled();
136+
expect(mocks.showModal).toHaveBeenCalledOnce();
137+
const modal = mocks.showModal.mock.calls[0][0] as { customContent: ReactNode };
138+
await act(async () => root.render(modal.customContent));
139+
await act(async () => button(baseDictionary.projects.redeploy.openDomains).click());
140+
expect(mocks.setActiveTab).toHaveBeenCalledWith("domains");
141+
expect(mocks.hideModal).toHaveBeenCalledWith("warning");
142+
});

‎apps/dashboard/src/app/(dashboard)/projects/[id]/components/Deployments.tsx‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { DeploymentsContent } from "@/app/(dashboard)/deployments/components";
66
import { deployApi, projectsApi, isAbortError } from "@/lib/api";
77
import type { PendingAction } from "@/lib/api/projects";
88
import { openTriggeredBuild } from "@/lib/deploy-nav";
9-
import { type Service } from "@/lib/api/services";
109
import { useModal } from "@/context/ModalContext";
1110
import { useToast } from "@/context/ToastContext";
1211
import { useI18n, interpolate } from "@/components/i18n-provider";
1312
import { useRouter } from "next/navigation";
1413
import { Rocket, ChevronDown, RefreshCw, Layers } from "lucide-react";
1514
import DropdownMenu from "@/components/ui/DropdownMenu";
1615
import WarningCallout from "@/components/shared/WarningCallout";
17-
16+
import {
17+
hasConnectedDomain,
18+
isPotentiallyPublicService,
19+
shouldWarnAboutUnreachableServices,
20+
} from "./redeploy-unreachable-warning";
1821
export const Deployments = () => {
1922
const {
2023
id,
@@ -23,6 +26,7 @@ export const Deployments = () => {
2326
servicesData,
2427
refreshServices,
2528
hasMultipleServices,
29+
domainsData,
2630
} = useProjectSettings();
2731
const { t } = useI18n();
2832
const { showToast } = useToast();
@@ -109,9 +113,7 @@ export const Deployments = () => {
109113
.getPendingActions(projectData.id)
110114
.then((res) => {
111115
if (cancelled) return;
112-
setBlockedAction(
113-
res?.data?.actions?.find((a) => a.kind === "deploy_blocked") ?? null,
114-
);
116+
setBlockedAction(res?.data?.actions?.find((a) => a.kind === "deploy_blocked") ?? null);
115117
})
116118
.catch(() => {
117119
/* best-effort — the status badge already says Action Required */
@@ -179,8 +181,12 @@ export const Deployments = () => {
179181
if (hasMultipleServices) {
180182
const services =
181183
servicesData.services.length > 0 ? servicesData.services : await refreshServices();
182-
if (shouldWarnAboutUnreachableServices(services)) {
183-
const candidateServices = services.filter(isPotentiallyPublicService);
184+
if (shouldWarnAboutUnreachableServices(services, domainsData.domains, projectData.port)) {
185+
const candidateServices = services.filter(
186+
(s) =>
187+
isPotentiallyPublicService(s) &&
188+
!hasConnectedDomain(s, domainsData.domains, projectData.port),
189+
);
184190
let modalId = "";
185191
modalId = showModal({
186192
customContent: (
@@ -200,10 +206,10 @@ export const Deployments = () => {
200206
className="rounded-lg bg-foreground/[0.06] px-3 py-1.5 text-[12px] font-medium text-foreground transition-colors hover:bg-foreground/[0.1]"
201207
onClick={() => {
202208
hideModal(modalId);
203-
setActiveTab("services");
209+
setActiveTab("domains");
204210
}}
205211
>
206-
{t.projects.redeploy.openServices}
212+
{t.projects.redeploy.openDomains}
207213
</button>
208214
<button
209215
type="button"
@@ -442,19 +448,3 @@ export const Deployments = () => {
442448
</div>
443449
);
444450
};
445-
446-
function hasConnectedDomain(service: Service) {
447-
if (!service.exposed) return false;
448-
if (service.domainType === "custom") return Boolean(service.customDomain?.trim());
449-
return Boolean(service.domain?.trim());
450-
}
451-
452-
function isPotentiallyPublicService(service: Service) {
453-
return service.enabled && (service.ports?.length ?? 0) > 0;
454-
}
455-
456-
function shouldWarnAboutUnreachableServices(services: Service[]) {
457-
const candidateServices = services.filter(isPotentiallyPublicService);
458-
if (candidateServices.length === 0) return false;
459-
return candidateServices.every((service) => !hasConnectedDomain(service));
460-
}

0 commit comments

Comments
 (0)