From 84802f5c4eb443758d212b43867da04026cde8ff Mon Sep 17 00:00:00 2001 From: bennykok Date: Thu, 1 Feb 2024 19:42:46 +0800 Subject: [PATCH] feat: update custom nodes to latest hash --- .../custom-form/SnapshotPickerView.tsx | 84 ++++++------------- .../components/custom-form/getBranchInfo.tsx | 52 ++++++++++++ web/src/server/addMachineSchema.ts | 2 +- 3 files changed, 79 insertions(+), 59 deletions(-) create mode 100644 web/src/components/custom-form/getBranchInfo.tsx diff --git a/web/src/components/custom-form/SnapshotPickerView.tsx b/web/src/components/custom-form/SnapshotPickerView.tsx index 4c75ac9..459b9e2 100644 --- a/web/src/components/custom-form/SnapshotPickerView.tsx +++ b/web/src/components/custom-form/SnapshotPickerView.tsx @@ -53,9 +53,8 @@ import { Plus, } from "lucide-react"; import * as React from "react"; -import { toast } from "sonner"; import useSWR from "swr"; -import { z } from "zod"; +import { getBranchInfo } from "./getBranchInfo"; export function SnapshotPickerView({ field, @@ -131,7 +130,7 @@ export function SnapshotPickerView({ { const newNodeList = { @@ -148,6 +147,29 @@ export function SnapshotPickerView({ > Delete + { + const newNodeList = { + ...field.value.git_custom_nodes, + }; + + const branchInfo = await getBranchInfo(key); + + if (!branchInfo) return; + + newNodeList[key].hash = branchInfo?.commit.sha; + + const nodeList = newNodeList; + const newValue = { + ...field.value, + git_custom_nodes: nodeList, + }; + field.onChange(newValue); + }} + > + Update + @@ -268,24 +290,6 @@ 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) { @@ -362,43 +366,7 @@ function CustomNodesSelector({ delete newNodeList[currentValue]; nodeList = newNodeList; } else { - const repoName = extractRepoName(currentValue); - const id = toast.loading(`Fetching repo info...`); - 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); + const branchInfo = await getBranchInfo(currentValue); if (!branchInfo) return; diff --git a/web/src/components/custom-form/getBranchInfo.tsx b/web/src/components/custom-form/getBranchInfo.tsx new file mode 100644 index 0000000..83bda0a --- /dev/null +++ b/web/src/components/custom-form/getBranchInfo.tsx @@ -0,0 +1,52 @@ +import { toast } from "sonner"; +import { z } from "zod"; + +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}`; +} +export async function getBranchInfo(gitUrl: string) { + const repoName = extractRepoName(gitUrl); + const id = toast.loading(`Fetching repo info...`); + 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); + return branchInfo; +} diff --git a/web/src/server/addMachineSchema.ts b/web/src/server/addMachineSchema.ts index 645f14f..6079523 100644 --- a/web/src/server/addMachineSchema.ts +++ b/web/src/server/addMachineSchema.ts @@ -16,7 +16,7 @@ export const insertCustomMachineSchema = createInsertSchema(machinesTable, { schema.snapshot.default({ comfyui: "d0165d819afe76bd4e6bdd710eb5f3e571b6a804", git_custom_nodes: { - "https://github.com/BennyKok/comfyui-deploy.git": { + "https://github.com/bennykok/comfyui-deploy": { hash: "a838cb7ad425e5652c3931fbafdc886b53c48a22", disabled: false, },