From 1ae295b8064a9e654e7624fc662d2ded6b1cec6f Mon Sep 17 00:00:00 2001 From: BennyKok Date: Thu, 18 Jan 2024 01:00:13 +0800 Subject: [PATCH] feat: Now it will fetches the latest git commit hash when you pick a custom nodes --- web/src/components/MachineList.tsx | 2 +- .../custom-form/SnapshotPickerView.tsx | 69 ++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/web/src/components/MachineList.tsx b/web/src/components/MachineList.tsx index ec8e72b..490a9e0 100644 --- a/web/src/components/MachineList.tsx +++ b/web/src/components/MachineList.tsx @@ -343,7 +343,7 @@ export function MachineList({ : undefined } title="New Machine" - description="Add custom Comfyui machines to your account." + description="Add custom ComfyUI machines to your account." serverAction={addCustomMachine} formSchema={addCustomMachineSchema} fieldConfig={{ diff --git a/web/src/components/custom-form/SnapshotPickerView.tsx b/web/src/components/custom-form/SnapshotPickerView.tsx index 1f10b48..49e9b6e 100644 --- a/web/src/components/custom-form/SnapshotPickerView.tsx +++ b/web/src/components/custom-form/SnapshotPickerView.tsx @@ -27,7 +27,9 @@ import { cn } from "@/lib/utils"; import { findAllDeployments } from "@/server/curdDeploments"; import { Check, ChevronsUpDown } from "lucide-react"; import * as React from "react"; +import { toast } from "sonner"; import useSWR from "swr"; +import { z } from "zod"; export function SnapshotPickerView({ field, @@ -161,6 +163,24 @@ type CustomNodeList = { }[]; }; +const RepoSchema = z.object({ + default_branch: z.string(), +}); + +const BranchInfoSchema = z.object({ + commit: z.object({ + sha: z.string(), + }), +}); + +function extractRepoName(repoUrl: string) { + const url = new URL(repoUrl); + const pathParts = url.pathname.split("/"); + const repoName = pathParts[2].replace(".git", ""); + const author = pathParts[1]; + return `${author}/${repoName}`; +} + function CustomNodesSelector({ field, }: Pick) { @@ -216,7 +236,52 @@ function CustomNodesSelector({ { + onSelect={async (currentValue) => { + const repoName = extractRepoName(currentValue); + + console.log(repoName); + + const id = toast.loading(`Fetching repo info...`); + // toast.info("hi"); + + const repo = await fetch( + `https://api.github.com/repos/${repoName}` + ) + .then((x) => x.json()) + .then((x) => { + console.log(x); + return x; + }) + .then((x) => RepoSchema.parse(x)) + .catch((e) => { + console.error(e); + toast.dismiss(id); + toast.error(`Failed to fetch repo info ${e.message}`); + return null; + }); + + if (!repo) return; + + const branch = repo.default_branch; + + const branchInfo = await fetch( + `https://api.github.com/repos/${repoName}/branches/${branch}` + ) + .then((x) => x.json()) + .then((x) => BranchInfoSchema.parse(x)) + .catch((e) => { + console.error(e); + toast.dismiss(id); + toast.error( + `Failed to fetch branch info ${e.message}` + ); + return null; + }); + + toast.dismiss(id); + + if (!branchInfo) return; + let nodeList: Record< string, { @@ -233,7 +298,7 @@ function CustomNodesSelector({ } else { nodeList = { [currentValue]: { - hash: "latest", + hash: branchInfo?.commit.sha, disabled: false, }, ...x,