From a07106d649991ea09892220873ea3243766c3e08 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 22 May 2026 17:21:12 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Release=20v0.29.5=20(#4475)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(migrate-auth-secret): exit cleanly when there are no 2FA records The empty-records branch of `main()` returned without calling `process.exit(0)`, leaving the Drizzle Postgres connection pool holding the event loop open. The `migrate-auth-secret` process then hangs indefinitely after printing "No 2FA records found, nothing to migrate." causing the upstream `0.29.3.sh` security migration script (which calls this via `docker exec`) to never reach its final `docker service update` step that mounts the new Docker Secret. Operators end up with the new secret created but the dokploy service still configured with the hardcoded `BETTER_AUTH_SECRET`, while believing the migration completed. Match the success branch a few lines below which already does `process.exit(0)`, and the pattern used in sibling scripts `reset-password.ts` and `reset-2fa.ts`. Closes #4392 * feat(compose): add import from base64 in create service dropdown Adds an "Import" option to the Create Service dropdown that lets users paste a base64-encoded compose export, preview the template (compose YAML, domains, envs, mounts) before confirming, and create the service only on confirm. Adds a `previewTemplate` tRPC procedure that processes the base64 without touching the DB, with server access validation via session. * [autofix.ci] apply automated fixes * Enhance version synchronization workflow to include SDK repository - Updated the GitHub Actions workflow to sync versioning across MCP, CLI, and SDK repositories. - Added steps to bump the version in the SDK repository and regenerate tools from the latest OpenAPI spec. - Improved commit message formatting to include source and release information for all repositories. - Ensured successful synchronization messages for each repository after the version update. * feat(deployment): add readLogs procedure to fetch deployment logs - Introduced a new `readLogs` procedure that allows users to retrieve logs for a specific deployment by providing the deployment ID and an optional tail parameter. - Implemented permission checks to ensure users have access to the requested logs. - Enhanced log retrieval for both cloud and non-cloud environments, utilizing appropriate commands based on the server context. Resolve https://github.com/Dokploy/mcp/issues/14 * feat(deployment): add server access validation for deployment actions - Implemented server access validation in deployment procedures to ensure users can only access deployments associated with their active organization. - Added checks to throw an UNAUTHORIZED error if a user attempts to access a deployment linked to a server outside their organization. This enhancement improves security and access control within the deployment management system. * feat(organization): prevent inviting users with owner role - Added validation to prevent users from being invited with the owner role in the organization and user routers. - Implemented TRPCError responses to ensure proper error handling when attempting to assign the owner role. This change enhances role management and security within the organization structure. https://github.com/Dokploy/dokploy/security/advisories/GHSA-fm9p-wmpw-gxjh * feat(user): implement session cleanup on user update - Added functionality to delete old sessions when a user updates their password, ensuring that only the current session remains active. - This change enhances security by preventing unauthorized access from previous sessions after a password change. Close here https://github.com/Dokploy/dokploy/security/advisories/GHSA-rr9m-w87g-46f3 * feat(settings): add copy button to server IP in web server settings (#4397) * fix: copy Dokploy server IP when clicking server badge (#4390) * fix: copy Dokploy server IP when clicking server badge When a service runs on the local Dokploy server (no remote server), clicking the server badge did nothing because `data.server` is null. Now falls back to the server IP from settings so the badge always copies an IP address. Co-Authored-By: Claude Opus 4.6 (1M context) * feat(copy-ip): implement IP address copying functionality across database service components - Added the ability to copy the server IP address to the clipboard when clicking the server badge in various database service components (Libsql, MariaDB, MongoDB, MySQL, PostgreSQL, Redis). - Integrated the `copy-to-clipboard` library and `sonner` for user feedback upon successful copy action. - Ensured fallback to the server IP from settings when the service data is not available, enhancing user experience and functionality. --------- Co-authored-by: Claude Opus 4.6 (1M context) Co-authored-by: Mauricio Siu * fix: responsive layout (#4391) Signed-off-by: Nahidujjaman Hridoy * fix: automatically converting username to lowercase both in creation of register, and build for extra. (#4382) * fix: allow square brackets in zip path validation for Next.js dynamic routes (#4468) * fix: allow square brackets in zip drop path validation for Next.js dynamic routes ZIP uploads containing Next.js dynamic route files (e.g. app/api/[id]/route.ts, pages/[slug].tsx) were rejected by readValidDirectory because the path regex did not include square bracket characters. * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * fix: prevent webhook deploy crash when commit data lacks modified files (#4470) shouldDeploy passed undefined/null entries from commit.modified straight into micromatch, which throws "Expected input to be a string" and fails every webhook deployment when watch paths are configured. Filter out non-string values before matching. * fix: add type="button" to TooltipTrigger in form components to prevent accidental submission (#4422) Co-authored-by: Maks Pikov * fix: enable comment toggle shortcut in env variable editor (#4402) (#4473) * fix: add tls=true label for domains when certificateType is none (#4018) (#4474) * fix: add tls=true label for compose domains when certificateType is none (#4018) * test: cover tls=true label for certificateType none, require https * fix: scope tls fix to compose labels, leave traefik file config unchanged (#4018) * chore: update version to v0.29.5 in package.json --------- Signed-off-by: Nahidujjaman Hridoy Co-authored-by: ngenohkevin Co-authored-by: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Co-authored-by: Mauricio Siu Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Volodymyr Kravchuk Co-authored-by: Claude Opus 4.6 (1M context) Co-authored-by: Nahidujjaman Hridoy <75487507+nhridoy@users.noreply.github.com> Co-authored-by: Francis <9560564+Baker@users.noreply.github.com> Co-authored-by: mixelburg <52622705+mixelburg@users.noreply.github.com> Co-authored-by: Maks Pikov --- .github/workflows/dokploy.yml | 79 +++ .github/workflows/sync-version.yml | 83 --- .../__test__/compose/domain/labels.test.ts | 45 ++ .../__test__/deploy/should-deploy.test.ts | 41 ++ .../__test__/wss/readValidDirectory.test.ts | 16 + .../application/advanced/show-resources.tsx | 10 +- .../application/domains/handle-domain.tsx | 2 +- .../save-bitbucket-provider-compose.tsx | 2 +- .../generic/save-github-provider-compose.tsx | 2 +- .../generic/save-gitlab-provider-compose.tsx | 2 +- .../dashboard/docker/logs/analyze-logs.tsx | 2 +- .../dashboard/docker/logs/docker-logs-id.tsx | 25 +- .../dashboard/project/add-import.tsx | 494 ++++++++++++++++++ .../dashboard/settings/web-server.tsx | 15 +- .../dokploy/components/shared/code-editor.tsx | 8 +- apps/dokploy/package.json | 2 +- .../environment/[environmentId].tsx | 5 + .../services/application/[applicationId].tsx | 6 +- .../services/compose/[composeId].tsx | 6 +- .../services/libsql/[libsqlId].tsx | 11 + .../services/mariadb/[mariadbId].tsx | 11 + .../services/mongo/[mongoId].tsx | 11 + .../services/mysql/[mysqlId].tsx | 11 + .../services/postgres/[postgresId].tsx | 11 + .../services/redis/[redisId].tsx | 11 + apps/dokploy/scripts/migrate-auth-secret.ts | 2 +- apps/dokploy/server/api/routers/compose.ts | 70 +++ apps/dokploy/server/api/routers/deployment.ts | 59 +++ .../server/api/routers/organization.ts | 8 + apps/dokploy/server/api/routers/user.ts | 19 +- packages/server/src/db/schema/registry.ts | 13 +- packages/server/src/utils/cluster/upload.ts | 4 +- packages/server/src/utils/docker/domain.ts | 4 + .../src/utils/watch-paths/should-deploy.ts | 7 +- packages/server/src/wss/utils.ts | 2 +- 35 files changed, 978 insertions(+), 121 deletions(-) delete mode 100644 .github/workflows/sync-version.yml create mode 100644 apps/dokploy/__test__/deploy/should-deploy.test.ts create mode 100644 apps/dokploy/components/dashboard/project/add-import.tsx diff --git a/.github/workflows/dokploy.yml b/.github/workflows/dokploy.yml index 529cd8f7f..542944611 100644 --- a/.github/workflows/dokploy.yml +++ b/.github/workflows/dokploy.yml @@ -138,6 +138,8 @@ jobs: needs: [combine-manifests] if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest + outputs: + version: ${{ steps.get_version.outputs.version }} steps: - name: Checkout uses: actions/checkout@v4 @@ -160,3 +162,80 @@ jobs: prerelease: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + sync-version: + needs: [generate-release] + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Sync version to MCP repository + run: | + git clone https://x-access-token:${{ secrets.DOCS_SYNC_TOKEN }}@github.com/dokploy/mcp.git /tmp/mcp-repo + cd /tmp/mcp-repo + + jq --arg v "${{ needs.generate-release.outputs.version }}" '.version = $v' package.json > package.json.tmp + mv package.json.tmp package.json + + npm install -g pnpm + pnpm install + pnpm run fetch-openapi + pnpm run generate + + git config user.name "Dokploy Bot" + git config user.email "bot@dokploy.com" + git add -A + git commit -m "chore: bump version to ${{ needs.generate-release.outputs.version }}" \ + -m "Source: ${{ github.repository }}@${{ github.sha }}" \ + --allow-empty + git push + + echo "✅ MCP repo synced to version ${{ needs.generate-release.outputs.version }}" + + - name: Sync version to CLI repository + run: | + git clone https://x-access-token:${{ secrets.DOCS_SYNC_TOKEN }}@github.com/dokploy/cli.git /tmp/cli-repo + cd /tmp/cli-repo + + jq --arg v "${{ needs.generate-release.outputs.version }}" '.version = $v' package.json > package.json.tmp + mv package.json.tmp package.json + + cp ${{ github.workspace }}/openapi.json ./openapi.json + npm install -g pnpm + pnpm install + pnpm run generate + + git config user.name "Dokploy Bot" + git config user.email "bot@dokploy.com" + git add -A + git commit -m "chore: bump version to ${{ needs.generate-release.outputs.version }}" \ + -m "Source: ${{ github.repository }}@${{ github.sha }}" \ + --allow-empty + git push + + echo "✅ CLI repo synced to version ${{ needs.generate-release.outputs.version }}" + + - name: Sync version to SDK repository + run: | + git clone https://x-access-token:${{ secrets.DOCS_SYNC_TOKEN }}@github.com/dokploy/sdk.git /tmp/sdk-repo + cd /tmp/sdk-repo + + jq --arg v "${{ needs.generate-release.outputs.version }}" '.version = $v' package.json > package.json.tmp + mv package.json.tmp package.json + + cp ${{ github.workspace }}/openapi.json ./openapi.json + npm install -g pnpm + pnpm install + pnpm run generate + + git config user.name "Dokploy Bot" + git config user.email "bot@dokploy.com" + git add -A + git commit -m "chore: bump version to ${{ needs.generate-release.outputs.version }}" \ + -m "Source: ${{ github.repository }}@${{ github.sha }}" \ + --allow-empty + git push + + echo "✅ SDK repo synced to version ${{ needs.generate-release.outputs.version }}" diff --git a/.github/workflows/sync-version.yml b/.github/workflows/sync-version.yml deleted file mode 100644 index 5e8ccb706..000000000 --- a/.github/workflows/sync-version.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Sync version to MCP and CLI repos - -on: - release: - types: [published] - push: - tags: - - 'v*' - workflow_dispatch: - -jobs: - sync-version: - name: Sync version to external repos - runs-on: ubuntu-latest - steps: - - name: Checkout Dokploy repository - uses: actions/checkout@v4 - - - name: Get version - id: get_version - run: | - VERSION=$(jq -r .version apps/dokploy/package.json | sed 's/^v//') - echo "version=$VERSION" >> $GITHUB_OUTPUT - echo "Version: $VERSION" - - - name: Sync version to MCP repository - run: | - git clone https://x-access-token:${{ secrets.DOCS_SYNC_TOKEN }}@github.com/dokploy/mcp.git /tmp/mcp-repo - cd /tmp/mcp-repo - - # Regenerate tools from latest OpenAPI spec - npm install -g pnpm - pnpm install - pnpm run fetch-openapi - pnpm run generate - - # Bump version after install so pnpm install doesn't overwrite it - jq --arg v "${{ steps.get_version.outputs.version }}" '.version = $v' package.json > package.json.tmp - mv package.json.tmp package.json - - git config user.name "Dokploy Bot" - git config user.email "bot@dokploy.com" - - git add -A - git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}" \ - -m "Source: ${{ github.repository }}@${{ github.sha }}" \ - -m "Release: ${{ github.event.release.html_url }}" \ - --allow-empty - - git push - - - - name: Sync version to CLI repository - run: | - git clone https://x-access-token:${{ secrets.DOCS_SYNC_TOKEN }}@github.com/dokploy/cli.git /tmp/cli-repo - - cd /tmp/cli-repo - - # Copy latest openapi spec and regenerate commands - cp ${{ github.workspace }}/openapi.json ./openapi.json - npm install -g pnpm - pnpm install - pnpm run generate - - # Bump version after install so pnpm install doesn't overwrite it - if [ -f package.json ]; then - jq --arg v "${{ steps.get_version.outputs.version }}" '.version = $v' package.json > package.json.tmp - mv package.json.tmp package.json - fi - - git config user.name "Dokploy Bot" - git config user.email "bot@dokploy.com" - - git add -A - git commit -m "chore: bump version to ${{ steps.get_version.outputs.version }}" \ - -m "Source: ${{ github.repository }}@${{ github.sha }}" \ - -m "Release: ${{ github.event.release.html_url }}" \ - --allow-empty - - git push - - echo "CLI repo synced to version ${{ steps.get_version.outputs.version }}" - diff --git a/apps/dokploy/__test__/compose/domain/labels.test.ts b/apps/dokploy/__test__/compose/domain/labels.test.ts index ec8e9edc7..dce69cfe4 100644 --- a/apps/dokploy/__test__/compose/domain/labels.test.ts +++ b/apps/dokploy/__test__/compose/domain/labels.test.ts @@ -103,6 +103,51 @@ describe("createDomainLabels", () => { ); }); + it("should add tls=true for certificateType none on websecure entrypoint", async () => { + const noneDomain = { + ...baseDomain, + https: true, + certificateType: "none" as const, + }; + const labels = await createDomainLabels(appName, noneDomain, "websecure"); + expect(labels).toContain( + "traefik.http.routers.test-app-1-websecure.tls=true", + ); + // no cert resolver should be set when relying on a default/custom cert + expect(labels).not.toContain( + "traefik.http.routers.test-app-1-websecure.tls.certresolver=letsencrypt", + ); + }); + + it("should not add tls=true for certificateType none on web entrypoint", async () => { + const noneDomain = { + ...baseDomain, + https: true, + certificateType: "none" as const, + }; + const labels = await createDomainLabels(appName, noneDomain, "web"); + expect(labels).not.toContain( + "traefik.http.routers.test-app-1-web.tls=true", + ); + }); + + it("should add tls=true for certificateType none on a custom https entrypoint", async () => { + const noneDomain = { + ...baseDomain, + https: true, + customEntrypoint: "websecure-custom", + certificateType: "none" as const, + }; + const labels = await createDomainLabels( + appName, + noneDomain, + "websecure-custom", + ); + expect(labels).toContain( + "traefik.http.routers.test-app-1-websecure-custom.tls=true", + ); + }); + it("should handle different ports correctly", async () => { const customPortDomain = { ...baseDomain, port: 3000 }; const labels = await createDomainLabels(appName, customPortDomain, "web"); diff --git a/apps/dokploy/__test__/deploy/should-deploy.test.ts b/apps/dokploy/__test__/deploy/should-deploy.test.ts new file mode 100644 index 000000000..d9a1c0244 --- /dev/null +++ b/apps/dokploy/__test__/deploy/should-deploy.test.ts @@ -0,0 +1,41 @@ +import { shouldDeploy } from "@dokploy/server"; +import { describe, expect, it } from "vitest"; + +describe("shouldDeploy", () => { + it("should deploy when no watch paths are configured", () => { + expect(shouldDeploy(null, ["src/index.ts"])).toBe(true); + expect(shouldDeploy([], ["src/index.ts"])).toBe(true); + }); + + it("should deploy when watch paths match modified files", () => { + expect(shouldDeploy(["src/**"], ["src/index.ts"])).toBe(true); + expect(shouldDeploy(["apps/web/**"], ["apps/web/page.tsx"])).toBe(true); + }); + + it("should not deploy when watch paths do not match", () => { + expect(shouldDeploy(["src/**"], ["docs/readme.md"])).toBe(false); + }); + + it("should not throw when modified files contain non-string values", () => { + expect(() => + shouldDeploy(["src/**"], ["src/index.ts", undefined, null] as any), + ).not.toThrow(); + expect( + shouldDeploy(["src/**"], ["src/index.ts", undefined, null] as any), + ).toBe(true); + }); + + it("should not throw when modified files are undefined or null", () => { + expect(() => shouldDeploy(["src/**"], undefined)).not.toThrow(); + expect(() => shouldDeploy(["src/**"], null)).not.toThrow(); + expect(shouldDeploy(["src/**"], undefined)).toBe(false); + expect(shouldDeploy(["src/**"], null)).toBe(false); + }); + + it("should not throw when every modified file is non-string", () => { + expect(() => + shouldDeploy(["src/**"], [undefined, undefined] as any), + ).not.toThrow(); + expect(shouldDeploy(["src/**"], [undefined, undefined] as any)).toBe(false); + }); +}); diff --git a/apps/dokploy/__test__/wss/readValidDirectory.test.ts b/apps/dokploy/__test__/wss/readValidDirectory.test.ts index 8107bb591..29d3152eb 100644 --- a/apps/dokploy/__test__/wss/readValidDirectory.test.ts +++ b/apps/dokploy/__test__/wss/readValidDirectory.test.ts @@ -78,4 +78,20 @@ describe("readValidDirectory (path traversal)", () => { it("returns false for empty string (resolves to cwd)", () => { expect(readValidDirectory("")).toBe(false); }); + + it("returns true for Next.js dynamic route paths with square brackets", () => { + expect( + readValidDirectory( + `${BASE}/applications/myapp/code/app/api/[id]/route.ts`, + ), + ).toBe(true); + expect( + readValidDirectory(`${BASE}/applications/myapp/code/pages/[slug].tsx`), + ).toBe(true); + expect( + readValidDirectory( + `${BASE}/applications/myapp/code/app/[...catch]/page.tsx`, + ), + ).toBe(true); + }); }); diff --git a/apps/dokploy/components/dashboard/application/advanced/show-resources.tsx b/apps/dokploy/components/dashboard/application/advanced/show-resources.tsx index fa2bda629..f2a48bae7 100644 --- a/apps/dokploy/components/dashboard/application/advanced/show-resources.tsx +++ b/apps/dokploy/components/dashboard/application/advanced/show-resources.tsx @@ -224,7 +224,7 @@ export const ShowResources = ({ id, type }: Props) => { Memory Limit - + @@ -263,7 +263,7 @@ export const ShowResources = ({ id, type }: Props) => { Memory Reservation - + @@ -303,7 +303,7 @@ export const ShowResources = ({ id, type }: Props) => { CPU Limit - + @@ -343,7 +343,7 @@ export const ShowResources = ({ id, type }: Props) => { CPU Reservation - + @@ -379,7 +379,7 @@ export const ShowResources = ({ id, type }: Props) => { Ulimits - + diff --git a/apps/dokploy/components/dashboard/application/domains/handle-domain.tsx b/apps/dokploy/components/dashboard/application/domains/handle-domain.tsx index 4edd6597f..b232591e4 100644 --- a/apps/dokploy/components/dashboard/application/domains/handle-domain.tsx +++ b/apps/dokploy/components/dashboard/application/domains/handle-domain.tsx @@ -806,7 +806,7 @@ export const AddDomain = ({ id, type, domainId = "", children }: Props) => { Middlewares - +
?
diff --git a/apps/dokploy/components/dashboard/compose/general/generic/save-bitbucket-provider-compose.tsx b/apps/dokploy/components/dashboard/compose/general/generic/save-bitbucket-provider-compose.tsx index 745f72d3b..330243ae2 100644 --- a/apps/dokploy/components/dashboard/compose/general/generic/save-bitbucket-provider-compose.tsx +++ b/apps/dokploy/components/dashboard/compose/general/generic/save-bitbucket-provider-compose.tsx @@ -422,7 +422,7 @@ export const SaveBitbucketProviderCompose = ({ composeId }: Props) => { Watch Paths - +
?
diff --git a/apps/dokploy/components/dashboard/compose/general/generic/save-github-provider-compose.tsx b/apps/dokploy/components/dashboard/compose/general/generic/save-github-provider-compose.tsx index 10075fb5c..0c07e688a 100644 --- a/apps/dokploy/components/dashboard/compose/general/generic/save-github-provider-compose.tsx +++ b/apps/dokploy/components/dashboard/compose/general/generic/save-github-provider-compose.tsx @@ -449,7 +449,7 @@ export const SaveGithubProviderCompose = ({ composeId }: Props) => { Watch Paths - +
?
diff --git a/apps/dokploy/components/dashboard/compose/general/generic/save-gitlab-provider-compose.tsx b/apps/dokploy/components/dashboard/compose/general/generic/save-gitlab-provider-compose.tsx index a81774fec..cad08f6bf 100644 --- a/apps/dokploy/components/dashboard/compose/general/generic/save-gitlab-provider-compose.tsx +++ b/apps/dokploy/components/dashboard/compose/general/generic/save-gitlab-provider-compose.tsx @@ -440,7 +440,7 @@ export const SaveGitlabProviderCompose = ({ composeId }: Props) => { Watch Paths - +
?
diff --git a/apps/dokploy/components/dashboard/docker/logs/analyze-logs.tsx b/apps/dokploy/components/dashboard/docker/logs/analyze-logs.tsx index d40697437..1b8ec736a 100644 --- a/apps/dokploy/components/dashboard/docker/logs/analyze-logs.tsx +++ b/apps/dokploy/components/dashboard/docker/logs/analyze-logs.tsx @@ -90,7 +90,7 @@ export function AnalyzeLogs({ logs, context }: Props) { disabled={logs.length === 0} title="Analyze logs with AI" > - + AI diff --git a/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx b/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx index 8d8842ac0..3a5f460e9 100644 --- a/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx +++ b/apps/dokploy/components/dashboard/docker/logs/docker-logs-id.tsx @@ -347,11 +347,13 @@ export const DockerLogsId: React.FC = ({ title={isPaused ? "Resume logs" : "Pause logs"} > {isPaused ? ( - + ) : ( - + )} - {isPaused ? "Resume" : "Pause"} + + {isPaused ? "Resume" : "Pause"} + {isPaused && ( - +
- + Logs paused {messageBuffer.length > 0 && ( diff --git a/apps/dokploy/components/dashboard/project/add-import.tsx b/apps/dokploy/components/dashboard/project/add-import.tsx new file mode 100644 index 000000000..034710e9c --- /dev/null +++ b/apps/dokploy/components/dashboard/project/add-import.tsx @@ -0,0 +1,494 @@ +import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema"; +import { Code2, FileInput, Globe2, HardDrive, HelpCircle } from "lucide-react"; +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import { toast } from "sonner"; +import { z } from "zod"; +import { AlertBlock } from "@/components/shared/alert-block"; +import { CodeEditor } from "@/components/shared/code-editor"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { ScrollArea } from "@/components/ui/scroll-area"; +import { + Select, + SelectContent, + SelectGroup, + SelectItem, + SelectLabel, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Separator } from "@/components/ui/separator"; +import { Textarea } from "@/components/ui/textarea"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; +import { slugify } from "@/lib/slug"; +import { api } from "@/utils/api"; +import { APP_NAME_MESSAGE, APP_NAME_REGEX } from "@/utils/schema"; + +const AddImportSchema = z.object({ + name: z.string().min(1, { message: "Name is required" }), + appName: z + .string() + .min(1, { message: "App name is required" }) + .regex(APP_NAME_REGEX, { message: APP_NAME_MESSAGE }), + base64: z.string().min(1, { message: "Base64 content is required" }), + serverId: z.string().optional(), +}); + +type AddImport = z.infer; + +type TemplateInfo = { + compose: string; + template: { + domains: Array<{ + serviceName: string; + port: number; + path?: string; + host?: string; + }>; + envs: string[]; + mounts: Array<{ filePath: string; content: string }>; + }; +}; + +interface Props { + environmentId: string; + projectName?: string; +} + +export const AddImport = ({ environmentId, projectName }: Props) => { + const utils = api.useUtils(); + const [visible, setVisible] = useState(false); + const [previewOpen, setPreviewOpen] = useState(false); + const [mountOpen, setMountOpen] = useState(false); + const [selectedMount, setSelectedMount] = useState<{ + filePath: string; + content: string; + } | null>(null); + const [templateInfo, setTemplateInfo] = useState(null); + + const slug = slugify(projectName); + const { data: isCloud } = api.settings.isCloud.useQuery(); + const { data: servers } = api.server.withSSHKey.useQuery(); + const shouldShowServerDropdown = !!(servers && servers.length > 0); + + const { mutateAsync: previewTemplate, isPending: isProcessing } = + api.compose.previewTemplate.useMutation(); + const { mutateAsync: createCompose, isPending: isCreating } = + api.compose.create.useMutation(); + const { mutateAsync: importCompose, isPending: isImporting } = + api.compose.import.useMutation(); + + const form = useForm({ + defaultValues: { name: "", appName: `${slug}-`, base64: "" }, + resolver: zodResolver(AddImportSchema), + }); + + const resetAll = () => { + form.reset({ name: "", appName: `${slug}-`, base64: "" }); + setTemplateInfo(null); + setPreviewOpen(false); + setMountOpen(false); + setSelectedMount(null); + }; + + const handleOpenChange = (open: boolean) => { + if (!open) resetAll(); + setVisible(open); + }; + + const handleLoad = async (data: AddImport) => { + try { + const result = await previewTemplate({ + appName: data.appName, + base64: data.base64.trim(), + serverId: data.serverId === "dokploy" ? undefined : data.serverId, + }); + setTemplateInfo(result); + setPreviewOpen(true); + } catch (error) { + toast.error( + error instanceof Error ? error.message : "Error processing template", + ); + } + }; + + const handleImport = async () => { + const data = form.getValues(); + try { + const compose = await createCompose({ + name: data.name, + appName: data.appName, + environmentId, + composeType: "docker-compose", + serverId: data.serverId === "dokploy" ? undefined : data.serverId, + }); + await importCompose({ + composeId: compose.composeId, + base64: data.base64.trim(), + }); + toast.success("Compose imported successfully"); + await utils.environment.one.invalidate({ environmentId }); + resetAll(); + setVisible(false); + } catch (error) { + toast.error( + error instanceof Error ? error.message : "Error importing compose", + ); + } + }; + + const handleCancelPreview = () => { + setPreviewOpen(false); + setTemplateInfo(null); + }; + + return ( + <> + + + e.preventDefault()} + > + + Import + + + + + Import Compose + + Paste a base64-encoded compose export to preview and import it + + + +
+ + ( + + Name + + { + const val = e.target.value || ""; + form.setValue( + "appName", + `${slug}-${slugify(val.trim())}`, + ); + field.onChange(val); + }} + /> + + + + )} + /> + + {shouldShowServerDropdown && ( + ( + + + + + + Select a Server {!isCloud ? "(Optional)" : ""} + + + + + + If no server is selected, the compose will be + deployed on the server where the user is logged + in. + + + + + + + + )} + /> + )} + + ( + + App Name + + + + + + )} + /> + + ( + + Configuration (Base64) + +