From a53d72701b01b6678e023cb4bef66a5b777af5ca Mon Sep 17 00:00:00 2001 From: Will Chen Date: Tue, 29 Apr 2025 12:12:47 -0700 Subject: [PATCH] provide a way to disconnect github repo (#47) --- src/components/GitHubConnector.tsx | 33 ++++++++++++++++++++++++- src/ipc/handlers/github_handlers.ts | 38 +++++++++++++++++++++++++++++ src/ipc/ipc_client.ts | 21 +++++++++++++--- src/preload.ts | 1 + 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/components/GitHubConnector.tsx b/src/components/GitHubConnector.tsx index a30d3ed..6d24c46 100644 --- a/src/components/GitHubConnector.tsx +++ b/src/components/GitHubConnector.tsx @@ -172,6 +172,27 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) { } }; + const [isDisconnecting, setIsDisconnecting] = useState(false); + const [disconnectError, setDisconnectError] = useState(null); + + const handleDisconnectRepo = async () => { + if (!appId) return; + setIsDisconnecting(true); + setDisconnectError(null); + try { + const result = await IpcClient.getInstance().disconnectGithubRepo(appId); + if (result.success) { + refreshApp(); + } else { + setDisconnectError(result.error || "Failed to disconnect repository."); + } + } catch (err: any) { + setDisconnectError(err.message || "Failed to disconnect repository."); + } finally { + setIsDisconnecting(false); + } + }; + if (!settings?.githubAccessToken) { return (
@@ -313,7 +334,7 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) { > {app.githubOrg}/{app.githubRepo} -
+
+
{syncError &&

{syncError}

} {syncSuccess && (

Successfully pushed to GitHub!

)} + {disconnectError && ( +

{disconnectError}

+ )}
); } else { diff --git a/src/ipc/handlers/github_handlers.ts b/src/ipc/handlers/github_handlers.ts index eef5a46..271b64f 100644 --- a/src/ipc/handlers/github_handlers.ts +++ b/src/ipc/handlers/github_handlers.ts @@ -422,10 +422,48 @@ async function handlePushToGithub( } } +async function handleDisconnectGithubRepo( + event: IpcMainInvokeEvent, + { appId }: { appId: number } +) { + try { + logger.log(`Disconnecting GitHub repo for appId: ${appId}`); + + // Get the app from the database + const app = await db.query.apps.findFirst({ + where: eq(apps.id, appId), + }); + + if (!app) { + return { success: false, error: "App not found" }; + } + + // Update app in database to remove GitHub repo and org + await db + .update(apps) + .set({ + githubRepo: null, + githubOrg: null, + }) + .where(eq(apps.id, appId)); + + return { success: true }; + } catch (error) { + logger.error(`Error disconnecting GitHub repo: ${error}`); + return { + success: false, + error: error instanceof Error ? error.message : String(error), + }; + } +} + // --- Registration --- export function registerGithubHandlers() { ipcMain.handle("github:start-flow", handleStartGithubFlow); ipcMain.handle("github:is-repo-available", handleIsRepoAvailable); ipcMain.handle("github:create-repo", handleCreateRepo); ipcMain.handle("github:push", handlePushToGithub); + ipcMain.handle("github:disconnect", (event, args: { appId: number }) => + handleDisconnectGithubRepo(event, args) + ); } diff --git a/src/ipc/ipc_client.ts b/src/ipc/ipc_client.ts index a8cff00..369c27d 100644 --- a/src/ipc/ipc_client.ts +++ b/src/ipc/ipc_client.ts @@ -611,9 +611,24 @@ export class IpcClient { ): Promise<{ success: boolean; error?: string }> { try { const result = await this.ipcRenderer.invoke("github:push", { appId }); - return result; - } catch (error: any) { - return { success: false, error: error.message || "Unknown error" }; + return result as { success: boolean; error?: string }; + } catch (error) { + showError(error); + throw error; + } + } + + public async disconnectGithubRepo( + appId: number + ): Promise<{ success: boolean; error?: string }> { + try { + const result = await this.ipcRenderer.invoke("github:disconnect", { + appId, + }); + return result as { success: boolean; error?: string }; + } catch (error) { + showError(error); + throw error; } } // --- End GitHub Repo Management --- diff --git a/src/preload.ts b/src/preload.ts index ad11fa3..008f514 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -38,6 +38,7 @@ const validInvokeChannels = [ "github:is-repo-available", "github:create-repo", "github:push", + "github:disconnect", "get-app-version", "reload-env-path", "get-proposal",